blob: e628cb11b5606306e11f456471022fa65488976b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Peter Zijlstra391e43d2011-11-15 17:14:39 +01002 * kernel/sched/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/mmu_context.h>
36#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/completion.h>
39#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070040#include <linux/debug_locks.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040057#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020065#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020066#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010067#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070068#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020069#include <linux/debugfs.h>
70#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020071#include <linux/ftrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090072#include <linux/slab.h>
Carsten Emdef1c6f1a2011-10-26 23:14:16 +020073#include <linux/init_task.h>
Al Viro40401532012-02-13 03:58:52 +000074#include <linux/binfmts.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010075#include <linux/context_tracking.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070076#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David Howells96f951e2012-03-28 18:30:03 +010078#include <asm/switch_to.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070079#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020080#include <asm/irq_regs.h>
Christian Borntraegerdb7e5272012-01-11 08:58:16 +010081#include <asm/mutex.h>
Glauber Costae6e66852011-07-11 15:28:17 -040082#ifdef CONFIG_PARAVIRT
83#include <asm/paravirt.h>
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Peter Zijlstra029632f2011-10-25 10:00:11 +020086#include "sched.h"
Tejun Heoea138442013-01-18 14:05:55 -080087#include "../workqueue_internal.h"
Thomas Gleixner29d5e042012-04-20 13:05:45 +000088#include "../smpboot.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020089
Steven Rostedta8d154b2009-04-10 09:36:00 -040090#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040091#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040092
Peter Zijlstra029632f2011-10-25 10:00:11 +020093void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period)
Paul Turner58088ad2011-07-21 09:43:31 -070094{
95 unsigned long delta;
96 ktime_t soft, hard, now;
97
98 for (;;) {
99 if (hrtimer_active(period_timer))
100 break;
101
102 now = hrtimer_cb_get_time(period_timer);
103 hrtimer_forward(period_timer, now, period);
104
105 soft = hrtimer_get_softexpires(period_timer);
106 hard = hrtimer_get_expires(period_timer);
107 delta = ktime_to_ns(ktime_sub(hard, soft));
108 __hrtimer_start_range_ns(period_timer, soft, delta,
109 HRTIMER_MODE_ABS_PINNED, 0);
110 }
111}
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113DEFINE_MUTEX(sched_domains_mutex);
114DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200115
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100116static void update_rq_clock_task(struct rq *rq, s64 delta);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700117
Peter Zijlstra029632f2011-10-25 10:00:11 +0200118void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200119{
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100120 s64 delta;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700121
Mike Galbraith61eadef2011-04-29 08:36:50 +0200122 if (rq->skip_clock_update > 0)
Mike Galbraithf26f9af2010-12-08 11:05:42 +0100123 return;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700124
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100125 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
Mike Galbraith4036ac12014-06-24 07:49:40 +0200126 if (delta < 0)
127 return;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100128 rq->clock += delta;
129 update_rq_clock_task(rq, delta);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200130}
131
Ingo Molnare436d802007-07-19 21:28:35 +0200132/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200133 * Debugging: various feature bits
134 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200135
136#define SCHED_FEAT(name, enabled) \
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200137 (1UL << __SCHED_FEAT_##name) * enabled |
138
139const_debug unsigned int sysctl_sched_features =
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100140#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200141 0;
142
143#undef SCHED_FEAT
144
145#ifdef CONFIG_SCHED_DEBUG
146#define SCHED_FEAT(name, enabled) \
147 #name ,
148
Hiroshi Shimamoto12925312012-05-25 15:41:54 +0900149static const char * const sched_feat_names[] = {
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100150#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200151};
152
153#undef SCHED_FEAT
154
Li Zefan34f3a812008-10-30 15:23:32 +0800155static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200156{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200157 int i;
158
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200159 for (i = 0; i < __SCHED_FEAT_NR; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800160 if (!(sysctl_sched_features & (1UL << i)))
161 seq_puts(m, "NO_");
162 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200163 }
Li Zefan34f3a812008-10-30 15:23:32 +0800164 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200165
Li Zefan34f3a812008-10-30 15:23:32 +0800166 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200167}
168
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200169#ifdef HAVE_JUMP_LABEL
170
Ingo Molnarc5905af2012-02-24 08:31:31 +0100171#define jump_label_key__true STATIC_KEY_INIT_TRUE
172#define jump_label_key__false STATIC_KEY_INIT_FALSE
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200173
174#define SCHED_FEAT(name, enabled) \
175 jump_label_key__##enabled ,
176
Ingo Molnarc5905af2012-02-24 08:31:31 +0100177struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200178#include "features.h"
179};
180
181#undef SCHED_FEAT
182
183static void sched_feat_disable(int i)
184{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100185 if (static_key_enabled(&sched_feat_keys[i]))
186 static_key_slow_dec(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200187}
188
189static void sched_feat_enable(int i)
190{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100191 if (!static_key_enabled(&sched_feat_keys[i]))
192 static_key_slow_inc(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200193}
194#else
195static void sched_feat_disable(int i) { };
196static void sched_feat_enable(int i) { };
197#endif /* HAVE_JUMP_LABEL */
198
Mel Gorman1a687c22012-11-22 11:16:36 +0000199static int sched_feat_set(char *cmp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200200{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200201 int i;
Mel Gorman1a687c22012-11-22 11:16:36 +0000202 int neg = 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200203
Hillf Danton524429c2011-01-06 20:58:12 +0800204 if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200205 neg = 1;
206 cmp += 3;
207 }
208
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200209 for (i = 0; i < __SCHED_FEAT_NR; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400210 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200211 if (neg) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200212 sysctl_sched_features &= ~(1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200213 sched_feat_disable(i);
214 } else {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200215 sysctl_sched_features |= (1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200216 sched_feat_enable(i);
217 }
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200218 break;
219 }
220 }
221
Mel Gorman1a687c22012-11-22 11:16:36 +0000222 return i;
223}
224
225static ssize_t
226sched_feat_write(struct file *filp, const char __user *ubuf,
227 size_t cnt, loff_t *ppos)
228{
229 char buf[64];
230 char *cmp;
231 int i;
Jason Baron5cd08fb2014-07-02 15:52:44 +0000232 struct inode *inode;
Mel Gorman1a687c22012-11-22 11:16:36 +0000233
234 if (cnt > 63)
235 cnt = 63;
236
237 if (copy_from_user(&buf, ubuf, cnt))
238 return -EFAULT;
239
240 buf[cnt] = 0;
241 cmp = strstrip(buf);
242
Jason Baron5cd08fb2014-07-02 15:52:44 +0000243 /* Ensure the static_key remains in a consistent state */
244 inode = file_inode(filp);
245 mutex_lock(&inode->i_mutex);
Mel Gorman1a687c22012-11-22 11:16:36 +0000246 i = sched_feat_set(cmp);
Jason Baron5cd08fb2014-07-02 15:52:44 +0000247 mutex_unlock(&inode->i_mutex);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200248 if (i == __SCHED_FEAT_NR)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200249 return -EINVAL;
250
Jan Blunck42994722009-11-20 17:40:37 +0100251 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200252
253 return cnt;
254}
255
Li Zefan34f3a812008-10-30 15:23:32 +0800256static int sched_feat_open(struct inode *inode, struct file *filp)
257{
258 return single_open(filp, sched_feat_show, NULL);
259}
260
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700261static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800262 .open = sched_feat_open,
263 .write = sched_feat_write,
264 .read = seq_read,
265 .llseek = seq_lseek,
266 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200267};
268
269static __init int sched_init_debug(void)
270{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200271 debugfs_create_file("sched_features", 0644, NULL, NULL,
272 &sched_feat_fops);
273
274 return 0;
275}
276late_initcall(sched_init_debug);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200277#endif /* CONFIG_SCHED_DEBUG */
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200278
279/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100280 * Number of tasks to iterate in a single balance run.
281 * Limited because this is done with IRQs disabled.
282 */
283const_debug unsigned int sysctl_sched_nr_migrate = 32;
284
285/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200286 * period over which we average the RT time consumption, measured
287 * in ms.
288 *
289 * default: 1s
290 */
291const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
292
293/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100294 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100295 * default: 1s
296 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100297unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100298
Peter Zijlstra029632f2011-10-25 10:00:11 +0200299__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +0100300
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100301/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100302 * part of the period that we allow rt tasks to run in us.
303 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100304 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100305int sysctl_sched_rt_runtime = 950000;
306
Dario Faggioli332ac172013-11-07 14:43:45 +0100307/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200308 * __task_rq_lock - lock the rq @p resides on.
Ingo Molnarb29739f2006-06-27 02:54:51 -0700309 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700310static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700311 __acquires(rq->lock)
312{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100313 struct rq *rq;
314
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200315 lockdep_assert_held(&p->pi_lock);
316
Andi Kleen3a5c3592007-10-15 17:00:14 +0200317 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +0100318 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100319 raw_spin_lock(&rq->lock);
Kirill Tkhaicca26e82014-08-20 13:47:42 +0400320 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200321 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100322 raw_spin_unlock(&rq->lock);
Kirill Tkhaicca26e82014-08-20 13:47:42 +0400323
324 while (unlikely(task_on_rq_migrating(p)))
325 cpu_relax();
Ingo Molnarb29739f2006-06-27 02:54:51 -0700326 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700327}
328
329/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200330 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700332static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200333 __acquires(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 __acquires(rq->lock)
335{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700336 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Andi Kleen3a5c3592007-10-15 17:00:14 +0200338 for (;;) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200339 raw_spin_lock_irqsave(&p->pi_lock, *flags);
Andi Kleen3a5c3592007-10-15 17:00:14 +0200340 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100341 raw_spin_lock(&rq->lock);
Kirill Tkhaicca26e82014-08-20 13:47:42 +0400342 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200343 return rq;
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200344 raw_spin_unlock(&rq->lock);
345 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Kirill Tkhaicca26e82014-08-20 13:47:42 +0400346
347 while (unlikely(task_on_rq_migrating(p)))
348 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Alexey Dobriyana9957442007-10-15 17:00:13 +0200352static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700353 __releases(rq->lock)
354{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100355 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700356}
357
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200358static inline void
359task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 __releases(rq->lock)
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200361 __releases(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200363 raw_spin_unlock(&rq->lock);
364 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800368 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200370static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 __acquires(rq->lock)
372{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700373 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 local_irq_disable();
376 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100377 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 return rq;
380}
381
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100382#ifdef CONFIG_SCHED_HRTICK
383/*
384 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100385 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100386
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100387static void hrtick_clear(struct rq *rq)
388{
389 if (hrtimer_active(&rq->hrtick_timer))
390 hrtimer_cancel(&rq->hrtick_timer);
391}
392
393/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100394 * High-resolution timer tick.
395 * Runs from hardirq context with interrupts disabled.
396 */
397static enum hrtimer_restart hrtick(struct hrtimer *timer)
398{
399 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
400
401 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
402
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100403 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200404 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100405 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100406 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100407
408 return HRTIMER_NORESTART;
409}
410
Rabin Vincent95e904c2008-05-11 05:55:33 +0530411#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200412
413static int __hrtick_restart(struct rq *rq)
414{
415 struct hrtimer *timer = &rq->hrtick_timer;
416 ktime_t time = hrtimer_get_softexpires(timer);
417
418 return __hrtimer_start_range_ns(timer, time, 0, HRTIMER_MODE_ABS_PINNED, 0);
419}
420
Peter Zijlstra31656512008-07-18 18:01:23 +0200421/*
422 * called from hardirq (IPI) context
423 */
424static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200425{
Peter Zijlstra31656512008-07-18 18:01:23 +0200426 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200427
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100428 raw_spin_lock(&rq->lock);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200429 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200430 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100431 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200432}
433
Peter Zijlstra31656512008-07-18 18:01:23 +0200434/*
435 * Called to set the hrtick timer state.
436 *
437 * called with rq->lock held and irqs disabled
438 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200439void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200440{
Peter Zijlstra31656512008-07-18 18:01:23 +0200441 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000442 ktime_t time;
443 s64 delta;
444
445 /*
446 * Don't schedule slices shorter than 10000ns, that just
447 * doesn't make sense and can cause timer DoS.
448 */
449 delta = max_t(s64, delay, 10000LL);
450 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200451
Arjan van de Vencc584b22008-09-01 15:02:30 -0700452 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200453
454 if (rq == this_rq()) {
Peter Zijlstra971ee282013-06-28 11:18:53 +0200455 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200456 } else if (!rq->hrtick_csd_pending) {
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100457 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstra31656512008-07-18 18:01:23 +0200458 rq->hrtick_csd_pending = 1;
459 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200460}
461
462static int
463hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
464{
465 int cpu = (int)(long)hcpu;
466
467 switch (action) {
468 case CPU_UP_CANCELED:
469 case CPU_UP_CANCELED_FROZEN:
470 case CPU_DOWN_PREPARE:
471 case CPU_DOWN_PREPARE_FROZEN:
472 case CPU_DEAD:
473 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +0200474 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200475 return NOTIFY_OK;
476 }
477
478 return NOTIFY_DONE;
479}
480
Rakib Mullickfa748202008-09-22 14:55:45 -0700481static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200482{
483 hotcpu_notifier(hotplug_hrtick, 0);
484}
Peter Zijlstra31656512008-07-18 18:01:23 +0200485#else
486/*
487 * Called to set the hrtick timer state.
488 *
489 * called with rq->lock held and irqs disabled
490 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200491void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200492{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100493 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530494 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +0200495}
496
Andrew Morton006c75f2008-09-22 14:55:46 -0700497static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +0200498{
499}
Rabin Vincent95e904c2008-05-11 05:55:33 +0530500#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200501
502static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100503{
Peter Zijlstra31656512008-07-18 18:01:23 +0200504#ifdef CONFIG_SMP
505 rq->hrtick_csd_pending = 0;
506
507 rq->hrtick_csd.flags = 0;
508 rq->hrtick_csd.func = __hrtick_start;
509 rq->hrtick_csd.info = rq;
510#endif
511
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100512 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
513 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100514}
Andrew Morton006c75f2008-09-22 14:55:46 -0700515#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100516static inline void hrtick_clear(struct rq *rq)
517{
518}
519
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100520static inline void init_rq_hrtick(struct rq *rq)
521{
522}
523
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200524static inline void init_hrtick(void)
525{
526}
Andrew Morton006c75f2008-09-22 14:55:46 -0700527#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100528
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200529/*
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200530 * cmpxchg based fetch_or, macro so it works for different integer types
531 */
532#define fetch_or(ptr, val) \
533({ typeof(*(ptr)) __old, __val = *(ptr); \
534 for (;;) { \
535 __old = cmpxchg((ptr), __val, __val | (val)); \
536 if (__old == __val) \
537 break; \
538 __val = __old; \
539 } \
540 __old; \
541})
542
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700543#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200544/*
545 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
546 * this avoids any races wrt polling state changes and thereby avoids
547 * spurious IPIs.
548 */
549static bool set_nr_and_not_polling(struct task_struct *p)
550{
551 struct thread_info *ti = task_thread_info(p);
552 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
553}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700554
555/*
556 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
557 *
558 * If this returns true, then the idle task promises to call
559 * sched_ttwu_pending() and reschedule soon.
560 */
561static bool set_nr_if_polling(struct task_struct *p)
562{
563 struct thread_info *ti = task_thread_info(p);
564 typeof(ti->flags) old, val = ACCESS_ONCE(ti->flags);
565
566 for (;;) {
567 if (!(val & _TIF_POLLING_NRFLAG))
568 return false;
569 if (val & _TIF_NEED_RESCHED)
570 return true;
571 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
572 if (old == val)
573 break;
574 val = old;
575 }
576 return true;
577}
578
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200579#else
580static bool set_nr_and_not_polling(struct task_struct *p)
581{
582 set_tsk_need_resched(p);
583 return true;
584}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700585
586#ifdef CONFIG_SMP
587static bool set_nr_if_polling(struct task_struct *p)
588{
589 return false;
590}
591#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200592#endif
593
594/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400595 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200596 *
597 * On UP this means the setting of the need_resched flag, on SMP it
598 * might also involve a cross-CPU call to trigger the scheduler on
599 * the target CPU.
600 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400601void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200602{
Kirill Tkhai88751252014-06-29 00:03:57 +0400603 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200604 int cpu;
605
Kirill Tkhai88751252014-06-29 00:03:57 +0400606 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200607
Kirill Tkhai88751252014-06-29 00:03:57 +0400608 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200609 return;
610
Kirill Tkhai88751252014-06-29 00:03:57 +0400611 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200612
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200613 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400614 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200615 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200616 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200617 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200618
Kirill Tkhai88751252014-06-29 00:03:57 +0400619 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200620 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700621 else
622 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200623}
624
Peter Zijlstra029632f2011-10-25 10:00:11 +0200625void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200626{
627 struct rq *rq = cpu_rq(cpu);
628 unsigned long flags;
629
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100630 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200631 return;
Kirill Tkhai88751252014-06-29 00:03:57 +0400632 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100633 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200634}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100635
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200636#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200637#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100638/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700639 * In the semi idle case, use the nearest busy cpu for migrating timers
640 * from an idle cpu. This is good for power-savings.
641 *
642 * We don't do similar optimization for completely idle system, as
643 * selecting an idle cpu will add more delays to the timers than intended
644 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
645 */
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530646int get_nohz_timer_target(int pinned)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700647{
648 int cpu = smp_processor_id();
649 int i;
650 struct sched_domain *sd;
651
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530652 if (pinned || !get_sysctl_timer_migration() || !idle_cpu(cpu))
653 return cpu;
654
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200655 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700656 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200657 for_each_cpu(i, sched_domain_span(sd)) {
658 if (!idle_cpu(i)) {
659 cpu = i;
660 goto unlock;
661 }
662 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700663 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200664unlock:
665 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700666 return cpu;
667}
668/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100669 * When add_timer_on() enqueues a timer into the timer wheel of an
670 * idle CPU then this timer might expire before the next timer event
671 * which is scheduled to wake up that CPU. In case of a completely
672 * idle system the next event might even be infinite time into the
673 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
674 * leaves the inner idle loop so the newly added timer is taken into
675 * account when the CPU goes back to idle and evaluates the timer
676 * wheel for the next timer event.
677 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200678static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100679{
680 struct rq *rq = cpu_rq(cpu);
681
682 if (cpu == smp_processor_id())
683 return;
684
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700685 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100686 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700687 else
688 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100689}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100690
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200691static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200692{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200693 /*
694 * We just need the target to call irq_exit() and re-evaluate
695 * the next tick. The nohz full kick at least implies that.
696 * If needed we can still optimize that later with an
697 * empty IRQ.
698 */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200699 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200700 if (cpu != smp_processor_id() ||
701 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200702 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200703 return true;
704 }
705
706 return false;
707}
708
709void wake_up_nohz_cpu(int cpu)
710{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200711 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200712 wake_up_idle_cpu(cpu);
713}
714
Suresh Siddhaca380622011-10-03 15:09:00 -0700715static inline bool got_nohz_idle_kick(void)
716{
Suresh Siddha1c792db2011-12-01 17:07:32 -0800717 int cpu = smp_processor_id();
Vincent Guittot873b4c62013-06-05 10:13:11 +0200718
719 if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
720 return false;
721
722 if (idle_cpu(cpu) && !need_resched())
723 return true;
724
725 /*
726 * We can't run Idle Load Balance on this CPU for this time so we
727 * cancel it and clear NOHZ_BALANCE_KICK
728 */
729 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
730 return false;
Suresh Siddhaca380622011-10-03 15:09:00 -0700731}
732
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200733#else /* CONFIG_NO_HZ_COMMON */
Suresh Siddhaca380622011-10-03 15:09:00 -0700734
735static inline bool got_nohz_idle_kick(void)
736{
737 return false;
738}
739
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200740#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100741
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200742#ifdef CONFIG_NO_HZ_FULL
743bool sched_can_stop_tick(void)
744{
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100745 /*
746 * More than one running task need preemption.
747 * nr_running update is assumed to be visible
748 * after IPI is sent from wakers.
749 */
Viresh Kumar541b8262014-06-24 14:04:12 +0530750 if (this_rq()->nr_running > 1)
751 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200752
Viresh Kumar541b8262014-06-24 14:04:12 +0530753 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200754}
755#endif /* CONFIG_NO_HZ_FULL */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200756
Peter Zijlstra029632f2011-10-25 10:00:11 +0200757void sched_avg_update(struct rq *rq)
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200758{
759 s64 period = sched_avg_period();
760
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200761 while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -0700762 /*
763 * Inline assembly required to prevent the compiler
764 * optimising this loop into a divmod call.
765 * See __iter_div_u64_rem() for another example of this.
766 */
767 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200768 rq->age_stamp += period;
769 rq->rt_avg /= 2;
770 }
771}
772
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200773#endif /* CONFIG_SMP */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200774
Paul Turnera790de92011-07-21 09:43:29 -0700775#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
776 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200777/*
Paul Turner82774342011-07-21 09:43:35 -0700778 * Iterate task_group tree rooted at *from, calling @down when first entering a
779 * node and @up when leaving it for the final time.
780 *
781 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200782 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200783int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700784 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200785{
786 struct task_group *parent, *child;
787 int ret;
788
Paul Turner82774342011-07-21 09:43:35 -0700789 parent = from;
790
Peter Zijlstraeb755802008-08-19 12:33:05 +0200791down:
792 ret = (*down)(parent, data);
793 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700794 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200795 list_for_each_entry_rcu(child, &parent->children, siblings) {
796 parent = child;
797 goto down;
798
799up:
800 continue;
801 }
802 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700803 if (ret || parent == from)
804 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200805
806 child = parent;
807 parent = parent->parent;
808 if (parent)
809 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700810out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200811 return ret;
812}
813
Peter Zijlstra029632f2011-10-25 10:00:11 +0200814int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200815{
816 return 0;
817}
818#endif
819
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200820static void set_load_weight(struct task_struct *p)
821{
Nikhil Raof05998d2011-05-18 10:09:38 -0700822 int prio = p->static_prio - MAX_RT_PRIO;
823 struct load_weight *load = &p->se.load;
824
Ingo Molnardd41f592007-07-09 18:51:59 +0200825 /*
826 * SCHED_IDLE tasks get minimal weight:
827 */
828 if (p->policy == SCHED_IDLE) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700829 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700830 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200831 return;
832 }
833
Nikhil Raoc8b28112011-05-18 14:37:48 -0700834 load->weight = scale_load(prio_to_weight[prio]);
Nikhil Raof05998d2011-05-18 10:09:38 -0700835 load->inv_weight = prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200836}
837
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100838static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -0600839{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100840 update_rq_clock(rq);
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300841 sched_info_queued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100842 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200843}
844
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100845static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200846{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100847 update_rq_clock(rq);
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300848 sched_info_dequeued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100849 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200850}
851
Peter Zijlstra029632f2011-10-25 10:00:11 +0200852void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100853{
854 if (task_contributes_to_load(p))
855 rq->nr_uninterruptible--;
856
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100857 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100858}
859
Peter Zijlstra029632f2011-10-25 10:00:11 +0200860void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100861{
862 if (task_contributes_to_load(p))
863 rq->nr_uninterruptible++;
864
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100865 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100866}
867
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100868static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700869{
Glauber Costa095c0aa2011-07-11 15:28:18 -0400870/*
871 * In theory, the compile should just see 0 here, and optimize out the call
872 * to sched_rt_avg_update. But I don't trust it...
873 */
874#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
875 s64 steal = 0, irq_delta = 0;
876#endif
877#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +0100878 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100879
880 /*
881 * Since irq_time is only updated on {soft,}irq_exit, we might run into
882 * this case when a previous update_rq_clock() happened inside a
883 * {soft,}irq region.
884 *
885 * When this happens, we stop ->clock_task and only update the
886 * prev_irq_time stamp to account for the part that fit, so that a next
887 * update will consume the rest. This ensures ->clock_task is
888 * monotonic.
889 *
890 * It does however cause some slight miss-attribution of {soft,}irq
891 * time, a more accurate solution would be to update the irq_time using
892 * the current rq->clock timestamp, except that would require using
893 * atomic ops.
894 */
895 if (irq_delta > delta)
896 irq_delta = delta;
897
898 rq->prev_irq_time += irq_delta;
899 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400900#endif
901#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
Ingo Molnarc5905af2012-02-24 08:31:31 +0100902 if (static_key_false((&paravirt_steal_rq_enabled))) {
Glauber Costa095c0aa2011-07-11 15:28:18 -0400903 steal = paravirt_steal_clock(cpu_of(rq));
904 steal -= rq->prev_steal_time_rq;
905
906 if (unlikely(steal > delta))
907 steal = delta;
908
Glauber Costa095c0aa2011-07-11 15:28:18 -0400909 rq->prev_steal_time_rq += steal;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400910 delta -= steal;
911 }
912#endif
913
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100914 rq->clock_task += delta;
915
Glauber Costa095c0aa2011-07-11 15:28:18 -0400916#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400917 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Glauber Costa095c0aa2011-07-11 15:28:18 -0400918 sched_rt_avg_update(rq, irq_delta + steal);
919#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700920}
921
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200922void sched_set_stop_task(int cpu, struct task_struct *stop)
923{
924 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
925 struct task_struct *old_stop = cpu_rq(cpu)->stop;
926
927 if (stop) {
928 /*
929 * Make it appear like a SCHED_FIFO task, its something
930 * userspace knows about and won't get confused about.
931 *
932 * Also, it will make PI more or less work without too
933 * much confusion -- but then, stop work should not
934 * rely on PI working anyway.
935 */
936 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
937
938 stop->sched_class = &stop_sched_class;
939 }
940
941 cpu_rq(cpu)->stop = stop;
942
943 if (old_stop) {
944 /*
945 * Reset it back to a normal scheduling class so that
946 * it can die in pieces.
947 */
948 old_stop->sched_class = &rt_sched_class;
949 }
950}
951
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100952/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200953 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200954 */
Ingo Molnar14531182007-07-09 18:51:59 +0200955static inline int __normal_prio(struct task_struct *p)
956{
Ingo Molnardd41f592007-07-09 18:51:59 +0200957 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200958}
959
960/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700961 * Calculate the expected normal priority: i.e. priority
962 * without taking RT-inheritance into account. Might be
963 * boosted by interactivity modifiers. Changes upon fork,
964 * setprio syscalls, and whenever the interactivity
965 * estimator recalculates.
966 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700967static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700968{
969 int prio;
970
Dario Faggioliaab03e02013-11-28 11:14:43 +0100971 if (task_has_dl_policy(p))
972 prio = MAX_DL_PRIO-1;
973 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700974 prio = MAX_RT_PRIO-1 - p->rt_priority;
975 else
976 prio = __normal_prio(p);
977 return prio;
978}
979
980/*
981 * Calculate the current priority, i.e. the priority
982 * taken into account by the scheduler. This value might
983 * be boosted by RT tasks, or might be boosted by
984 * interactivity modifiers. Will be RT if the task got
985 * RT-boosted. If not then it returns p->normal_prio.
986 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700987static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700988{
989 p->normal_prio = normal_prio(p);
990 /*
991 * If we are RT tasks or we were boosted to RT priority,
992 * keep the priority unchanged. Otherwise, update priority
993 * to the normal priority:
994 */
995 if (!rt_prio(p->prio))
996 return p->normal_prio;
997 return p->prio;
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/**
1001 * task_curr - is this task currently executing on a CPU?
1002 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02001003 *
1004 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001006inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 return cpu_curr(task_cpu(p)) == p;
1009}
1010
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001011/*
1012 * Can drop rq->lock because from sched_class::switched_from() methods drop it.
1013 */
Steven Rostedtcb469842008-01-25 21:08:22 +01001014static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1015 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001016 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001017{
1018 if (prev_class != p->sched_class) {
1019 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001020 prev_class->switched_from(rq, p);
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001021 /* Possble rq->lock 'hole'. */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001022 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001023 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001024 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01001025}
1026
Peter Zijlstra029632f2011-10-25 10:00:11 +02001027void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001028{
1029 const struct sched_class *class;
1030
1031 if (p->sched_class == rq->curr->sched_class) {
1032 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
1033 } else {
1034 for_each_class(class) {
1035 if (class == rq->curr->sched_class)
1036 break;
1037 if (class == p->sched_class) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001038 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001039 break;
1040 }
1041 }
1042 }
1043
1044 /*
1045 * A queue event has occurred, and we're going to schedule. In
1046 * this case, we can save a useless back to back clock update.
1047 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001048 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001049 rq->skip_clock_update = 1;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02001053void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001054{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001055#ifdef CONFIG_SCHED_DEBUG
1056 /*
1057 * We should never call set_task_cpu() on a blocked task,
1058 * ttwu() will sort out the placement.
1059 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001060 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001061 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001062
1063#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001064 /*
1065 * The caller should hold either p->pi_lock or rq->lock, when changing
1066 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1067 *
1068 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001069 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001070 *
1071 * Furthermore, all task_rq users should acquire both locks, see
1072 * task_rq_lock().
1073 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001074 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1075 lockdep_is_held(&task_rq(p)->lock)));
1076#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001077#endif
1078
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001079 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001080
Peter Zijlstra0c697742009-12-22 15:43:19 +01001081 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001082 if (p->sched_class->migrate_task_rq)
1083 p->sched_class->migrate_task_rq(p, new_cpu);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001084 p->se.nr_migrations++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001085 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001086 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001087
1088 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001089}
1090
Peter Zijlstraac66f542013-10-07 11:29:16 +01001091static void __migrate_swap_task(struct task_struct *p, int cpu)
1092{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001093 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001094 struct rq *src_rq, *dst_rq;
1095
1096 src_rq = task_rq(p);
1097 dst_rq = cpu_rq(cpu);
1098
1099 deactivate_task(src_rq, p, 0);
1100 set_task_cpu(p, cpu);
1101 activate_task(dst_rq, p, 0);
1102 check_preempt_curr(dst_rq, p, 0);
1103 } else {
1104 /*
1105 * Task isn't running anymore; make it appear like we migrated
1106 * it before it went to sleep. This means on wakeup we make the
1107 * previous cpu our targer instead of where it really is.
1108 */
1109 p->wake_cpu = cpu;
1110 }
1111}
1112
1113struct migration_swap_arg {
1114 struct task_struct *src_task, *dst_task;
1115 int src_cpu, dst_cpu;
1116};
1117
1118static int migrate_swap_stop(void *data)
1119{
1120 struct migration_swap_arg *arg = data;
1121 struct rq *src_rq, *dst_rq;
1122 int ret = -EAGAIN;
1123
1124 src_rq = cpu_rq(arg->src_cpu);
1125 dst_rq = cpu_rq(arg->dst_cpu);
1126
Peter Zijlstra74602312013-10-10 20:17:22 +02001127 double_raw_lock(&arg->src_task->pi_lock,
1128 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001129 double_rq_lock(src_rq, dst_rq);
1130 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1131 goto unlock;
1132
1133 if (task_cpu(arg->src_task) != arg->src_cpu)
1134 goto unlock;
1135
1136 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1137 goto unlock;
1138
1139 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1140 goto unlock;
1141
1142 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1143 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1144
1145 ret = 0;
1146
1147unlock:
1148 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001149 raw_spin_unlock(&arg->dst_task->pi_lock);
1150 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001151
1152 return ret;
1153}
1154
1155/*
1156 * Cross migrate two tasks
1157 */
1158int migrate_swap(struct task_struct *cur, struct task_struct *p)
1159{
1160 struct migration_swap_arg arg;
1161 int ret = -EINVAL;
1162
Peter Zijlstraac66f542013-10-07 11:29:16 +01001163 arg = (struct migration_swap_arg){
1164 .src_task = cur,
1165 .src_cpu = task_cpu(cur),
1166 .dst_task = p,
1167 .dst_cpu = task_cpu(p),
1168 };
1169
1170 if (arg.src_cpu == arg.dst_cpu)
1171 goto out;
1172
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001173 /*
1174 * These three tests are all lockless; this is OK since all of them
1175 * will be re-checked with proper locks held further down the line.
1176 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001177 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1178 goto out;
1179
1180 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1181 goto out;
1182
1183 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1184 goto out;
1185
Mel Gorman286549d2014-01-21 15:51:03 -08001186 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001187 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1188
1189out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001190 return ret;
1191}
1192
Tejun Heo969c7922010-05-06 18:49:21 +02001193struct migration_arg {
Ingo Molnar36c8b582006-07-03 00:25:41 -07001194 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int dest_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001196};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Tejun Heo969c7922010-05-06 18:49:21 +02001198static int migration_cpu_stop(void *data);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * wait_task_inactive - wait for a thread to unschedule.
1202 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001203 * If @match_state is nonzero, it's the @p->state value just checked and
1204 * not expected to change. If it changes, i.e. @p might have woken up,
1205 * then return zero. When we succeed in waiting for @p to be off its CPU,
1206 * we return a positive number (its total switch count). If a second call
1207 * a short while later returns the same number, the caller can be sure that
1208 * @p has remained unscheduled the whole time.
1209 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * The caller must ensure that the task *will* unschedule sometime soon,
1211 * else this function might spin for a *long* time. This function can't
1212 * be called with interrupts off, or it may introduce deadlock with
1213 * smp_call_function() if an IPI is sent by the same process we are
1214 * waiting to become inactive.
1215 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001216unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001219 int running, queued;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001220 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001221 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Andi Kleen3a5c3592007-10-15 17:00:14 +02001223 for (;;) {
1224 /*
1225 * We do the initial early heuristics without holding
1226 * any task-queue locks at all. We'll only try to get
1227 * the runqueue lock when things look like they will
1228 * work out!
1229 */
1230 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001231
Andi Kleen3a5c3592007-10-15 17:00:14 +02001232 /*
1233 * If the task is actively running on another CPU
1234 * still, just relax and busy-wait without holding
1235 * any locks.
1236 *
1237 * NOTE! Since we don't hold any locks, it's not
1238 * even sure that "rq" stays as the right runqueue!
1239 * But we don't care, since "task_running()" will
1240 * return false if the runqueue has changed and p
1241 * is actually now running somewhere else!
1242 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001243 while (task_running(rq, p)) {
1244 if (match_state && unlikely(p->state != match_state))
1245 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001246 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001247 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001248
Andi Kleen3a5c3592007-10-15 17:00:14 +02001249 /*
1250 * Ok, time to look more closely! We need the rq
1251 * lock now, to be *sure*. If we're wrong, we'll
1252 * just go back and repeat.
1253 */
1254 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001255 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001256 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001257 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001258 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001259 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001260 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001261 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001262
Andi Kleen3a5c3592007-10-15 17:00:14 +02001263 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001264 * If it changed from the expected state, bail out now.
1265 */
1266 if (unlikely(!ncsw))
1267 break;
1268
1269 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001270 * Was it really running after all now that we
1271 * checked with the proper locks actually held?
1272 *
1273 * Oops. Go back and try again..
1274 */
1275 if (unlikely(running)) {
1276 cpu_relax();
1277 continue;
1278 }
1279
1280 /*
1281 * It's not enough that it's not actively running,
1282 * it must be off the runqueue _entirely_, and not
1283 * preempted!
1284 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001285 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001286 * running right now), it's preempted, and we should
1287 * yield - it could be a while.
1288 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001289 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001290 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1291
1292 set_current_state(TASK_UNINTERRUPTIBLE);
1293 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001294 continue;
1295 }
1296
1297 /*
1298 * Ahh, all good. It wasn't running, and it wasn't
1299 * runnable, which means that it will never become
1300 * running in the future either. We're all done!
1301 */
1302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001304
1305 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/***
1309 * kick_process - kick a running thread to enter/exit the kernel
1310 * @p: the to-be-kicked thread
1311 *
1312 * Cause a process which is running on another CPU to enter
1313 * kernel-mode, without any delay. (to get signals handled.)
1314 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001315 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * because all it wants to ensure is that the remote task enters
1317 * the kernel. If the IPI races and the task has been migrated
1318 * to another CPU then no harm is done and the purpose has been
1319 * achieved as well.
1320 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001321void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int cpu;
1324
1325 preempt_disable();
1326 cpu = task_cpu(p);
1327 if ((cpu != smp_processor_id()) && task_curr(p))
1328 smp_send_reschedule(cpu);
1329 preempt_enable();
1330}
Rusty Russellb43e3522009-06-12 22:27:00 -06001331EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07001332#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001334#ifdef CONFIG_SMP
Oleg Nesterov30da6882010-03-15 10:10:19 +01001335/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001336 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01001337 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001338static int select_fallback_rq(int cpu, struct task_struct *p)
1339{
Tang Chenaa00d892013-02-22 16:33:33 -08001340 int nid = cpu_to_node(cpu);
1341 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001342 enum { cpuset, possible, fail } state = cpuset;
1343 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001344
Tang Chenaa00d892013-02-22 16:33:33 -08001345 /*
1346 * If the node that the cpu is on has been offlined, cpu_to_node()
1347 * will return -1. There is no cpu on the node, and we should
1348 * select the cpu on the other node.
1349 */
1350 if (nid != -1) {
1351 nodemask = cpumask_of_node(nid);
1352
1353 /* Look for allowed, online CPU in same node. */
1354 for_each_cpu(dest_cpu, nodemask) {
1355 if (!cpu_online(dest_cpu))
1356 continue;
1357 if (!cpu_active(dest_cpu))
1358 continue;
1359 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1360 return dest_cpu;
1361 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001362 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001363
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001364 for (;;) {
1365 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301366 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001367 if (!cpu_online(dest_cpu))
1368 continue;
1369 if (!cpu_active(dest_cpu))
1370 continue;
1371 goto out;
1372 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001373
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001374 switch (state) {
1375 case cpuset:
1376 /* No more Mr. Nice Guy. */
1377 cpuset_cpus_allowed_fallback(p);
1378 state = possible;
1379 break;
1380
1381 case possible:
1382 do_set_cpus_allowed(p, cpu_possible_mask);
1383 state = fail;
1384 break;
1385
1386 case fail:
1387 BUG();
1388 break;
1389 }
1390 }
1391
1392out:
1393 if (state != cpuset) {
1394 /*
1395 * Don't tell them about moving exiting tasks or
1396 * kernel threads (both mm NULL), since they never
1397 * leave kernel.
1398 */
1399 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001400 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001401 task_pid_nr(p), p->comm, cpu);
1402 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001403 }
1404
1405 return dest_cpu;
1406}
1407
Peter Zijlstrae2912002009-12-16 18:04:36 +01001408/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001409 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001410 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001411static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001412int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001413{
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001414 if (p->nr_cpus_allowed > 1)
1415 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001416
1417 /*
1418 * In order not to call set_task_cpu() on a blocking task we need
1419 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1420 * cpu.
1421 *
1422 * Since this is common to all placement strategies, this lives here.
1423 *
1424 * [ this allows ->select_task() to simply return task_cpu(p) and
1425 * not worry about this generic constraint ]
1426 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001427 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001428 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001429 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001430
1431 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001432}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001433
1434static void update_avg(u64 *avg, u64 sample)
1435{
1436 s64 diff = sample - *avg;
1437 *avg += diff >> 3;
1438}
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001439#endif
1440
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001441static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001442ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001443{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001444#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001445 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001446
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001447#ifdef CONFIG_SMP
1448 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09001449
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001450 if (cpu == this_cpu) {
1451 schedstat_inc(rq, ttwu_local);
1452 schedstat_inc(p, se.statistics.nr_wakeups_local);
1453 } else {
1454 struct sched_domain *sd;
1455
1456 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001457 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001458 for_each_domain(this_cpu, sd) {
1459 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
1460 schedstat_inc(sd, ttwu_wake_remote);
1461 break;
1462 }
1463 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001464 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001465 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001466
1467 if (wake_flags & WF_MIGRATED)
1468 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
1469
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001470#endif /* CONFIG_SMP */
1471
1472 schedstat_inc(rq, ttwu_count);
1473 schedstat_inc(p, se.statistics.nr_wakeups);
1474
1475 if (wake_flags & WF_SYNC)
1476 schedstat_inc(p, se.statistics.nr_wakeups_sync);
1477
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001478#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09001479}
1480
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001481static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001482{
Tejun Heo9ed38112009-12-03 15:08:03 +09001483 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001484 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001485
1486 /* if a worker is waking up, notify workqueue */
1487 if (p->flags & PF_WQ_WORKER)
1488 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001489}
1490
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001491/*
1492 * Mark the task runnable and perform wakeup-preemption.
1493 */
Peter Zijlstra89363382011-04-05 17:23:42 +02001494static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001495ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001496{
Tejun Heo9ed38112009-12-03 15:08:03 +09001497 check_preempt_curr(rq, p, wake_flags);
Peter Zijlstraa8d7ad52013-03-14 10:48:39 +01001498 trace_sched_wakeup(p, true);
Tejun Heo9ed38112009-12-03 15:08:03 +09001499
1500 p->state = TASK_RUNNING;
1501#ifdef CONFIG_SMP
1502 if (p->sched_class->task_woken)
1503 p->sched_class->task_woken(rq, p);
1504
Steven Rostedte69c6342010-12-06 17:10:31 -05001505 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001506 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001507 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001508
Jason Lowabfafa52013-09-13 11:26:51 -07001509 update_avg(&rq->avg_idle, delta);
1510
1511 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001512 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001513
Tejun Heo9ed38112009-12-03 15:08:03 +09001514 rq->idle_stamp = 0;
1515 }
1516#endif
1517}
1518
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001519static void
1520ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
1521{
1522#ifdef CONFIG_SMP
1523 if (p->sched_contributes_to_load)
1524 rq->nr_uninterruptible--;
1525#endif
1526
1527 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
1528 ttwu_do_wakeup(rq, p, wake_flags);
1529}
1530
1531/*
1532 * Called in case the task @p isn't fully descheduled from its runqueue,
1533 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1534 * since all we need to do is flip p->state to TASK_RUNNING, since
1535 * the task is still ->on_rq.
1536 */
1537static int ttwu_remote(struct task_struct *p, int wake_flags)
1538{
1539 struct rq *rq;
1540 int ret = 0;
1541
1542 rq = __task_rq_lock(p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001543 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001544 /* check_preempt_curr() may use rq clock */
1545 update_rq_clock(rq);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001546 ttwu_do_wakeup(rq, p, wake_flags);
1547 ret = 1;
1548 }
1549 __task_rq_unlock(rq);
1550
1551 return ret;
1552}
1553
Peter Zijlstra317f3942011-04-05 17:23:58 +02001554#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001555void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001556{
1557 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001558 struct llist_node *llist = llist_del_all(&rq->wake_list);
1559 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001560 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001561
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001562 if (!llist)
1563 return;
1564
1565 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001566
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001567 while (llist) {
1568 p = llist_entry(llist, struct task_struct, wake_entry);
1569 llist = llist_next(llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001570 ttwu_do_activate(rq, p, 0);
1571 }
1572
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001573 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001574}
1575
1576void scheduler_ipi(void)
1577{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001578 /*
1579 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1580 * TIF_NEED_RESCHED remotely (for the first time) will also send
1581 * this IPI.
1582 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001583 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001584
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001585 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001586 return;
1587
1588 /*
1589 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1590 * traditionally all their work was done from the interrupt return
1591 * path. Now that we actually do some work, we need to make sure
1592 * we do call them.
1593 *
1594 * Some archs already do call them, luckily irq_enter/exit nest
1595 * properly.
1596 *
1597 * Arguably we should visit all archs and update all handlers,
1598 * however a fair share of IPIs are still resched only so this would
1599 * somewhat pessimize the simple resched case.
1600 */
1601 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001602 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001603
1604 /*
1605 * Check if someone kicked us for doing the nohz idle load balance.
1606 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001607 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001608 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001609 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001610 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001611 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001612}
1613
1614static void ttwu_queue_remote(struct task_struct *p, int cpu)
1615{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001616 struct rq *rq = cpu_rq(cpu);
1617
1618 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1619 if (!set_nr_if_polling(rq->idle))
1620 smp_send_reschedule(cpu);
1621 else
1622 trace_sched_wake_idle_without_ipi(cpu);
1623 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001624}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001625
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001626void wake_up_if_idle(int cpu)
1627{
1628 struct rq *rq = cpu_rq(cpu);
1629 unsigned long flags;
1630
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001631 rcu_read_lock();
1632
1633 if (!is_idle_task(rcu_dereference(rq->curr)))
1634 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001635
1636 if (set_nr_if_polling(rq->idle)) {
1637 trace_sched_wake_idle_without_ipi(cpu);
1638 } else {
1639 raw_spin_lock_irqsave(&rq->lock, flags);
1640 if (is_idle_task(rq->curr))
1641 smp_send_reschedule(cpu);
1642 /* Else cpu is not in idle, do nothing here */
1643 raw_spin_unlock_irqrestore(&rq->lock, flags);
1644 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001645
1646out:
1647 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001648}
1649
Peter Zijlstra39be3502012-01-26 12:44:34 +01001650bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001651{
1652 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1653}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001654#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001655
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001656static void ttwu_queue(struct task_struct *p, int cpu)
1657{
1658 struct rq *rq = cpu_rq(cpu);
1659
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001660#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001661 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001662 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001663 ttwu_queue_remote(p, cpu);
1664 return;
1665 }
1666#endif
1667
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001668 raw_spin_lock(&rq->lock);
1669 ttwu_do_activate(rq, p, 0);
1670 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001671}
1672
1673/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001675 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001677 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
1679 * Put it on the run-queue if it's not already there. The "current"
1680 * thread is always on the run-queue (except when the actual
1681 * re-schedule is in progress), and as such you're allowed to do
1682 * the simpler "current->state = TASK_RUNNING" to mark yourself
1683 * runnable without the overhead of this.
1684 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001685 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001686 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001688static int
1689try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001692 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001693
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02001694 /*
1695 * If we are going to wake up a thread waiting for CONDITION we
1696 * need to ensure that CONDITION=1 done by the caller can not be
1697 * reordered with p->state check below. This pairs with mb() in
1698 * set_current_state() the waiting thread does.
1699 */
1700 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001701 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001702 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto out;
1704
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001705 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001707
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001708 if (p->on_rq && ttwu_remote(p, wake_flags))
1709 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001712 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001713 * If the owning (remote) cpu is still in the middle of schedule() with
1714 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001715 */
Peter Zijlstraf3e94782012-09-12 11:22:00 +02001716 while (p->on_cpu)
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001717 cpu_relax();
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001718 /*
1719 * Pairs with the smp_wmb() in finish_lock_switch().
1720 */
1721 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001723 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001724 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001725
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001726 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001727 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01001728
Peter Zijlstraac66f542013-10-07 11:29:16 +01001729 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001730 if (task_cpu(p) != cpu) {
1731 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02001732 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001736 ttwu_queue(p, cpu);
1737stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001738 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001740 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 return success;
1743}
1744
David Howells50fa6102009-04-28 15:01:38 +01001745/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02001746 * try_to_wake_up_local - try to wake up a local task with rq lock held
1747 * @p: the thread to be awakened
1748 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02001749 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02001750 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02001751 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02001752 */
1753static void try_to_wake_up_local(struct task_struct *p)
1754{
1755 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001756
Tejun Heo383efcd2013-03-18 12:22:34 -07001757 if (WARN_ON_ONCE(rq != this_rq()) ||
1758 WARN_ON_ONCE(p == current))
1759 return;
1760
Tejun Heo21aa9af2010-06-08 21:40:37 +02001761 lockdep_assert_held(&rq->lock);
1762
Peter Zijlstra2acca552011-04-05 17:23:50 +02001763 if (!raw_spin_trylock(&p->pi_lock)) {
1764 raw_spin_unlock(&rq->lock);
1765 raw_spin_lock(&p->pi_lock);
1766 raw_spin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001767 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02001768
Tejun Heo21aa9af2010-06-08 21:40:37 +02001769 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02001770 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02001771
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001772 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001773 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
1774
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001775 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001776 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02001777out:
1778 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001779}
1780
1781/**
David Howells50fa6102009-04-28 15:01:38 +01001782 * wake_up_process - Wake up a specific process
1783 * @p: The process to be woken up.
1784 *
1785 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02001786 * processes.
1787 *
1788 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01001789 *
1790 * It may be assumed that this function implies a write memory barrier before
1791 * changing the task state if and only if any tasks are woken up.
1792 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001793int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01001795 WARN_ON(task_is_stopped_or_traced(p));
1796 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798EXPORT_SYMBOL(wake_up_process);
1799
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001800int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 return try_to_wake_up(p, state, 0);
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
Juri Lellia5e7be32014-09-19 10:22:39 +01001806 * This function clears the sched_dl_entity static params.
1807 */
1808void __dl_clear_params(struct task_struct *p)
1809{
1810 struct sched_dl_entity *dl_se = &p->dl;
1811
1812 dl_se->dl_runtime = 0;
1813 dl_se->dl_deadline = 0;
1814 dl_se->dl_period = 0;
1815 dl_se->flags = 0;
1816 dl_se->dl_bw = 0;
1817}
1818
1819/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * Perform scheduler related setup for a newly forked process p.
1821 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001822 *
1823 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01001825static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001827 p->on_rq = 0;
1828
1829 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001830 p->se.exec_start = 0;
1831 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001832 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001833 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001834 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001835 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001836
1837#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03001838 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001839#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001840
Dario Faggioliaab03e02013-11-28 11:14:43 +01001841 RB_CLEAR_NODE(&p->dl.rb_node);
1842 hrtimer_init(&p->dl.dl_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Juri Lellia5e7be32014-09-19 10:22:39 +01001843 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001844
Peter Zijlstrafa717062008-01-25 21:08:27 +01001845 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07001846
Avi Kivitye107be32007-07-26 13:40:43 +02001847#ifdef CONFIG_PREEMPT_NOTIFIERS
1848 INIT_HLIST_HEAD(&p->preempt_notifiers);
1849#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001850
1851#ifdef CONFIG_NUMA_BALANCING
1852 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01001853 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001854 p->mm->numa_scan_seq = 0;
1855 }
1856
Rik van Riel5e1576e2013-10-07 11:29:26 +01001857 if (clone_flags & CLONE_VM)
1858 p->numa_preferred_nid = current->numa_preferred_nid;
1859 else
1860 p->numa_preferred_nid = -1;
1861
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001862 p->node_stamp = 0ULL;
1863 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001864 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001865 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02001866 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001867 p->last_task_numa_placement = 0;
1868 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001869
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001870 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001871#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02001872}
1873
Mel Gorman1a687c22012-11-22 11:16:36 +00001874#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00001875#ifdef CONFIG_SCHED_DEBUG
Mel Gorman1a687c22012-11-22 11:16:36 +00001876void set_numabalancing_state(bool enabled)
1877{
1878 if (enabled)
1879 sched_feat_set("NUMA");
1880 else
1881 sched_feat_set("NO_NUMA");
1882}
Mel Gorman3105b862012-11-23 11:23:49 +00001883#else
1884__read_mostly bool numabalancing_enabled;
1885
1886void set_numabalancing_state(bool enabled)
1887{
1888 numabalancing_enabled = enabled;
1889}
1890#endif /* CONFIG_SCHED_DEBUG */
Andi Kleen54a43d52014-01-23 15:53:13 -08001891
1892#ifdef CONFIG_PROC_SYSCTL
1893int sysctl_numa_balancing(struct ctl_table *table, int write,
1894 void __user *buffer, size_t *lenp, loff_t *ppos)
1895{
1896 struct ctl_table t;
1897 int err;
1898 int state = numabalancing_enabled;
1899
1900 if (write && !capable(CAP_SYS_ADMIN))
1901 return -EPERM;
1902
1903 t = *table;
1904 t.data = &state;
1905 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
1906 if (err < 0)
1907 return err;
1908 if (write)
1909 set_numabalancing_state(state);
1910 return err;
1911}
1912#endif
1913#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00001914
Ingo Molnardd41f592007-07-09 18:51:59 +02001915/*
1916 * fork()/clone()-time setup:
1917 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001918int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02001919{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001920 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001921 int cpu = get_cpu();
1922
Rik van Riel5e1576e2013-10-07 11:29:26 +01001923 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01001924 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01001925 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01001926 * nobody will actually run it, and a signal or other external
1927 * event cannot wake it up and insert it on the runqueue either.
1928 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01001929 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001930
Ingo Molnarb29739f2006-06-27 02:54:51 -07001931 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02001932 * Make sure we do not leak PI boosting priority to the child.
1933 */
1934 p->prio = current->normal_prio;
1935
1936 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001937 * Revert to default priority/policy on fork if requested.
1938 */
1939 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01001940 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001941 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02001942 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02001943 p->rt_priority = 0;
1944 } else if (PRIO_TO_NICE(p->static_prio) < 0)
1945 p->static_prio = NICE_TO_PRIO(0);
1946
1947 p->prio = p->normal_prio = __normal_prio(p);
1948 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02001949
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001950 /*
1951 * We don't need the reset flag anymore after the fork. It has
1952 * fulfilled its duty:
1953 */
1954 p->sched_reset_on_fork = 0;
1955 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02001956
Dario Faggioliaab03e02013-11-28 11:14:43 +01001957 if (dl_prio(p->prio)) {
1958 put_cpu();
1959 return -EAGAIN;
1960 } else if (rt_prio(p->prio)) {
1961 p->sched_class = &rt_sched_class;
1962 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02001963 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001964 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001965
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001966 if (p->sched_class->task_fork)
1967 p->sched_class->task_fork(p);
1968
Peter Zijlstra86951592010-06-22 11:44:53 +02001969 /*
1970 * The child is not yet in the pid-hash so no cgroup attach races,
1971 * and the cgroup is pinned to this child due to cgroup_fork()
1972 * is ran before sched_fork().
1973 *
1974 * Silence PROVE_RCU.
1975 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001976 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02001977 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001978 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02001979
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001980#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001981 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001982 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02001984#if defined(CONFIG_SMP)
1985 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07001986#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02001987 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01001988#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05001989 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001990 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01001991#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05001992
Nick Piggin476d1392005-06-25 14:57:29 -07001993 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01001994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Dario Faggioli332ac172013-11-07 14:43:45 +01001997unsigned long to_ratio(u64 period, u64 runtime)
1998{
1999 if (runtime == RUNTIME_INF)
2000 return 1ULL << 20;
2001
2002 /*
2003 * Doing this here saves a lot of checks in all
2004 * the calling paths, and returning zero seems
2005 * safe for them anyway.
2006 */
2007 if (period == 0)
2008 return 0;
2009
2010 return div64_u64(runtime << 20, period);
2011}
2012
2013#ifdef CONFIG_SMP
2014inline struct dl_bw *dl_bw_of(int i)
2015{
Kirill Tkhai66339c32014-09-22 22:36:24 +04002016 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2017 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002018 return &cpu_rq(i)->rd->dl_bw;
2019}
2020
Peter Zijlstrade212f12013-12-19 11:54:45 +01002021static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002022{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002023 struct root_domain *rd = cpu_rq(i)->rd;
2024 int cpus = 0;
2025
Kirill Tkhai66339c32014-09-22 22:36:24 +04002026 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2027 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002028 for_each_cpu_and(i, rd->span, cpu_active_mask)
2029 cpus++;
2030
2031 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002032}
2033#else
2034inline struct dl_bw *dl_bw_of(int i)
2035{
2036 return &cpu_rq(i)->dl.dl_bw;
2037}
2038
Peter Zijlstrade212f12013-12-19 11:54:45 +01002039static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002040{
2041 return 1;
2042}
2043#endif
2044
Dario Faggioli332ac172013-11-07 14:43:45 +01002045/*
2046 * We must be sure that accepting a new task (or allowing changing the
2047 * parameters of an existing one) is consistent with the bandwidth
2048 * constraints. If yes, this function also accordingly updates the currently
2049 * allocated bandwidth to reflect the new situation.
2050 *
2051 * This function is called while holding p's rq->lock.
2052 */
2053static int dl_overflow(struct task_struct *p, int policy,
2054 const struct sched_attr *attr)
2055{
2056
2057 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002058 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002059 u64 runtime = attr->sched_runtime;
2060 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002061 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002062
2063 if (new_bw == p->dl.dl_bw)
2064 return 0;
2065
2066 /*
2067 * Either if a task, enters, leave, or stays -deadline but changes
2068 * its parameters, we may need to update accordingly the total
2069 * allocated bandwidth of the container.
2070 */
2071 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002072 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002073 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2074 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2075 __dl_add(dl_b, new_bw);
2076 err = 0;
2077 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2078 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2079 __dl_clear(dl_b, p->dl.dl_bw);
2080 __dl_add(dl_b, new_bw);
2081 err = 0;
2082 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2083 __dl_clear(dl_b, p->dl.dl_bw);
2084 err = 0;
2085 }
2086 raw_spin_unlock(&dl_b->lock);
2087
2088 return err;
2089}
2090
2091extern void init_dl_bw(struct dl_bw *dl_b);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
2094 * wake_up_new_task - wake up a newly created task for the first time.
2095 *
2096 * This function will do some initial scheduler statistics housekeeping
2097 * that must be done for every newly created context, then puts the task
2098 * on the runqueue and wakes it.
2099 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002100void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002103 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002104
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002105 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002106#ifdef CONFIG_SMP
2107 /*
2108 * Fork balancing, do it here and not earlier because:
2109 * - cpus_allowed can change in the fork path
2110 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002111 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002112 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Alex Shia75cdaa2013-06-20 10:18:47 +08002115 /* Initialize new task's runnable average */
2116 init_task_runnable_average(p);
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002117 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002118 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002119 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra89363382011-04-05 17:23:42 +02002120 trace_sched_wakeup_new(p, true);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002121 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002122#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002123 if (p->sched_class->task_woken)
2124 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002125#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002126 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Avi Kivitye107be32007-07-26 13:40:43 +02002129#ifdef CONFIG_PREEMPT_NOTIFIERS
2130
2131/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002132 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002133 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002134 */
2135void preempt_notifier_register(struct preempt_notifier *notifier)
2136{
2137 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2138}
2139EXPORT_SYMBOL_GPL(preempt_notifier_register);
2140
2141/**
2142 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002143 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002144 *
2145 * This is safe to call from within a preemption notifier.
2146 */
2147void preempt_notifier_unregister(struct preempt_notifier *notifier)
2148{
2149 hlist_del(&notifier->link);
2150}
2151EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2152
2153static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2154{
2155 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002156
Sasha Levinb67bfe02013-02-27 17:06:00 -08002157 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002158 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2159}
2160
2161static void
2162fire_sched_out_preempt_notifiers(struct task_struct *curr,
2163 struct task_struct *next)
2164{
2165 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002166
Sasha Levinb67bfe02013-02-27 17:06:00 -08002167 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002168 notifier->ops->sched_out(notifier, next);
2169}
2170
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002171#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002172
2173static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2174{
2175}
2176
2177static void
2178fire_sched_out_preempt_notifiers(struct task_struct *curr,
2179 struct task_struct *next)
2180{
2181}
2182
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002183#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002186 * prepare_task_switch - prepare to switch tasks
2187 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002188 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002189 * @next: the task we are going to switch to.
2190 *
2191 * This is called with the rq lock held and interrupts off. It must
2192 * be paired with a subsequent finish_task_switch after the context
2193 * switch.
2194 *
2195 * prepare_task_switch sets up locking and calls architecture specific
2196 * hooks.
2197 */
Avi Kivitye107be32007-07-26 13:40:43 +02002198static inline void
2199prepare_task_switch(struct rq *rq, struct task_struct *prev,
2200 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002201{
Andrew Vagin895dd922012-07-12 14:14:29 +04002202 trace_sched_switch(prev, next);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002203 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002204 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002205 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002206 prepare_lock_switch(rq, next);
2207 prepare_arch_switch(next);
2208}
2209
2210/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 * finish_task_switch - clean up after a task-switch
2212 * @prev: the thread we just switched away from.
2213 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002214 * finish_task_switch must be called after the context switch, paired
2215 * with a prepare_task_switch call before the context switch.
2216 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2217 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 *
2219 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002220 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * with the lock held can cause deadlocks; see schedule() for
2222 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002223 *
2224 * The context switch have flipped the stack from under us and restored the
2225 * local variables which were saved when this task called schedule() in the
2226 * past. prev == current is still correct but we need to recalculate this_rq
2227 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002229static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 __releases(rq->lock)
2231{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002232 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002234 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 rq->prev_mm = NULL;
2237
2238 /*
2239 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002240 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002241 * schedule one last time. The schedule call will never return, and
2242 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002243 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * still held, otherwise prev could be scheduled on another cpu, die
2245 * there before we look at prev->state, and then the reference would
2246 * be dropped twice.
2247 * Manfred Spraul <manfred@colorfullife.com>
2248 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002249 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002250 vtime_task_switch(prev);
Nick Piggin4866cde2005-06-25 14:57:23 -07002251 finish_arch_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002252 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002253 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002254 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002255
Avi Kivitye107be32007-07-26 13:40:43 +02002256 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (mm)
2258 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002259 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002260 if (prev->sched_class->task_dead)
2261 prev->sched_class->task_dead(prev);
2262
bibo maoc6fd91f2006-03-26 01:38:20 -08002263 /*
2264 * Remove function-return probe instances associated with this
2265 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002266 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002267 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002269 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002270
2271 tick_nohz_task_switch(current);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002272 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Gregory Haskins3f029d32009-07-29 11:08:47 -04002275#ifdef CONFIG_SMP
2276
Gregory Haskins3f029d32009-07-29 11:08:47 -04002277/* rq->lock is NOT held, but preemption is disabled */
2278static inline void post_schedule(struct rq *rq)
2279{
2280 if (rq->post_schedule) {
2281 unsigned long flags;
2282
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002283 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002284 if (rq->curr->sched_class->post_schedule)
2285 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002286 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002287
2288 rq->post_schedule = 0;
2289 }
2290}
2291
2292#else
2293
Gregory Haskins3f029d32009-07-29 11:08:47 -04002294static inline void post_schedule(struct rq *rq)
2295{
2296}
2297
2298#endif
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300/**
2301 * schedule_tail - first thing a freshly forked thread must call.
2302 * @prev: the thread we just switched away from.
2303 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002304asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 __releases(rq->lock)
2306{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002307 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002308
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002309 /* finish_task_switch() drops rq->lock and enables preemtion */
2310 preempt_disable();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002311 rq = finish_task_switch(prev);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002312 post_schedule(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002313 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002316 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
2319/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002320 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002322static inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002323context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002324 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Ingo Molnardd41f592007-07-09 18:51:59 +02002326 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Avi Kivitye107be32007-07-26 13:40:43 +02002328 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002329
Ingo Molnardd41f592007-07-09 18:51:59 +02002330 mm = next->mm;
2331 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002332 /*
2333 * For paravirt, this is coupled with an exit in switch_to to
2334 * combine the page table reload and the switch backend into
2335 * one hypercall.
2336 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002337 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002338
Heiko Carstens31915ab2010-09-16 14:42:25 +02002339 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 next->active_mm = oldmm;
2341 atomic_inc(&oldmm->mm_count);
2342 enter_lazy_tlb(oldmm, next);
2343 } else
2344 switch_mm(oldmm, mm, next);
2345
Heiko Carstens31915ab2010-09-16 14:42:25 +02002346 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 rq->prev_mm = oldmm;
2349 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002350 /*
2351 * Since the runqueue lock will be released by the next
2352 * task (which is an invalid locking op but in the case
2353 * of the scheduler it's an obvious special-case), so we
2354 * do an early lockdep release here:
2355 */
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002356 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01002358 context_tracking_task_switch(prev, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* Here we just switch the register state and the stack. */
2360 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002361 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002362
2363 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002367 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 *
2369 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002370 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 */
2372unsigned long nr_running(void)
2373{
2374 unsigned long i, sum = 0;
2375
2376 for_each_online_cpu(i)
2377 sum += cpu_rq(i)->nr_running;
2378
2379 return sum;
2380}
2381
Tim Chen2ee507c2014-07-31 10:29:48 -07002382/*
2383 * Check if only the current task is running on the cpu.
2384 */
2385bool single_task_running(void)
2386{
2387 if (cpu_rq(smp_processor_id())->nr_running == 1)
2388 return true;
2389 else
2390 return false;
2391}
2392EXPORT_SYMBOL(single_task_running);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394unsigned long long nr_context_switches(void)
2395{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002396 int i;
2397 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002399 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 sum += cpu_rq(i)->nr_switches;
2401
2402 return sum;
2403}
2404
2405unsigned long nr_iowait(void)
2406{
2407 unsigned long i, sum = 0;
2408
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002409 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2411
2412 return sum;
2413}
2414
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002415unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002416{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002417 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002418 return atomic_read(&this->nr_iowait);
2419}
2420
Mel Gorman372ba8c2014-08-06 14:19:21 +01002421void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2422{
2423 struct rq *this = this_rq();
2424 *nr_waiters = atomic_read(&this->nr_iowait);
2425 *load = this->cpu_load[0];
2426}
2427
Ingo Molnardd41f592007-07-09 18:51:59 +02002428#ifdef CONFIG_SMP
2429
Ingo Molnar48f24c42006-07-03 00:25:40 -07002430/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002431 * sched_exec - execve() is a valuable balancing opportunity, because at
2432 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002434void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Peter Zijlstra38022902009-12-16 18:04:37 +01002436 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002438 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002439
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002440 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002441 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002442 if (dest_cpu == smp_processor_id())
2443 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002444
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002445 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002446 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002447
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002448 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2449 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return;
2451 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002452unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002453 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456#endif
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002459DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002462EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002465 * Return accounted runtime for the task.
2466 * In case the task is currently running, return the runtime plus current's
2467 * pending runtime that have not been accounted yet.
2468 */
2469unsigned long long task_sched_runtime(struct task_struct *p)
2470{
2471 unsigned long flags;
2472 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002473 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002474
Peter Zijlstra911b2892013-11-11 18:21:56 +01002475#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2476 /*
2477 * 64-bit doesn't need locks to atomically read a 64bit value.
2478 * So we have a optimization chance when the task's delta_exec is 0.
2479 * Reading ->on_cpu is racy, but this is ok.
2480 *
2481 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2482 * If we race with it entering cpu, unaccounted time is 0. This is
2483 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002484 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2485 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002486 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002487 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002488 return p->se.sum_exec_runtime;
2489#endif
2490
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002491 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002492 /*
2493 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2494 * project cycles that may never be accounted to this
2495 * thread, breaking clock_gettime().
2496 */
2497 if (task_current(rq, p) && task_on_rq_queued(p)) {
2498 update_rq_clock(rq);
2499 p->sched_class->update_curr(rq);
2500 }
2501 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002502 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002503
2504 return ns;
2505}
2506
Balbir Singh49048622008-09-05 18:12:23 +02002507/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002508 * This function gets called by the timer code, with HZ frequency.
2509 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002510 */
2511void scheduler_tick(void)
2512{
Christoph Lameter7835b982006-12-10 02:20:22 -08002513 int cpu = smp_processor_id();
2514 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002515 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002516
2517 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002518
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002519 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002520 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002521 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002522 update_cpu_load_active(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002523 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002524
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002525 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002526
Christoph Lametere418e1c2006-12-10 02:20:23 -08002527#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002528 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002529 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002530#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002531 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002534#ifdef CONFIG_NO_HZ_FULL
2535/**
2536 * scheduler_tick_max_deferment
2537 *
2538 * Keep at least one tick per second when a single
2539 * active task is running because the scheduler doesn't
2540 * yet completely support full dynticks environment.
2541 *
2542 * This makes sure that uptime, CFS vruntime, load
2543 * balancing, etc... continue to move forward, even
2544 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002545 *
2546 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002547 */
2548u64 scheduler_tick_max_deferment(void)
2549{
2550 struct rq *rq = this_rq();
2551 unsigned long next, now = ACCESS_ONCE(jiffies);
2552
2553 next = rq->last_sched_tick + HZ;
2554
2555 if (time_before_eq(next, now))
2556 return 0;
2557
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002558 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002559}
2560#endif
2561
Lai Jiangshan132380a2009-04-02 14:18:25 +08002562notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002563{
2564 if (in_lock_functions(addr)) {
2565 addr = CALLER_ADDR2;
2566 if (in_lock_functions(addr))
2567 addr = CALLER_ADDR3;
2568 }
2569 return addr;
2570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002572#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2573 defined(CONFIG_PREEMPT_TRACER))
2574
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002575void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002577#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * Underflow?
2580 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002581 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2582 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002583#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002584 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002585#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /*
2587 * Spinlock count overflowing soon?
2588 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002589 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2590 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002591#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002592 if (preempt_count() == val) {
2593 unsigned long ip = get_parent_ip(CALLER_ADDR1);
2594#ifdef CONFIG_DEBUG_PREEMPT
2595 current->preempt_disable_ip = ip;
2596#endif
2597 trace_preempt_off(CALLER_ADDR0, ip);
2598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002600EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002601NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002603void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002605#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /*
2607 * Underflow?
2608 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01002609 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
2612 * Is the spinlock portion underflowing?
2613 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002614 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2615 !(preempt_count() & PREEMPT_MASK)))
2616 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002617#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002618
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002619 if (preempt_count() == val)
2620 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002621 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002623EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002624NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626#endif
2627
2628/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002629 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002631static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Dave Jones664dfa62011-12-22 16:39:30 -05002633 if (oops_in_progress)
2634 return;
2635
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01002636 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
2637 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02002638
Ingo Molnardd41f592007-07-09 18:51:59 +02002639 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07002640 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02002641 if (irqs_disabled())
2642 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002643#ifdef CONFIG_DEBUG_PREEMPT
2644 if (in_atomic_preempt_off()) {
2645 pr_err("Preemption disabled at:");
2646 print_ip_sym(current->preempt_disable_ip);
2647 pr_cont("\n");
2648 }
2649#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07002650 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10302651 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02002652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Ingo Molnardd41f592007-07-09 18:51:59 +02002654/*
2655 * Various schedule()-time debugging checks and statistics:
2656 */
2657static inline void schedule_debug(struct task_struct *prev)
2658{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01002659#ifdef CONFIG_SCHED_STACK_END_CHECK
2660 BUG_ON(unlikely(task_stack_end_corrupted(prev)));
2661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002663 * Test if we are atomic. Since do_exit() needs to call into
Oleg Nesterov192301e2013-11-13 16:45:38 +01002664 * schedule() atomically, we ignore that path. Otherwise whine
2665 * if we are scheduling when we should not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 */
Oleg Nesterov192301e2013-11-13 16:45:38 +01002667 if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
Ingo Molnardd41f592007-07-09 18:51:59 +02002668 __schedule_bug(prev);
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002669 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2672
Ingo Molnar2d723762007-10-15 17:00:12 +02002673 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02002674}
2675
2676/*
2677 * Pick up the highest-prio task:
2678 */
2679static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01002680pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02002681{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002682 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02002683 struct task_struct *p;
2684
2685 /*
2686 * Optimization: we know that if all tasks are in
2687 * the fair class we can call that function directly:
2688 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002689 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01002690 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002691 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02002692 if (unlikely(p == RETRY_TASK))
2693 goto again;
2694
2695 /* assumes fair_sched_class->next == idle_sched_class */
2696 if (unlikely(!p))
2697 p = idle_sched_class.pick_next_task(rq, prev);
2698
2699 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02002700 }
2701
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002702again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002703 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002704 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002705 if (p) {
2706 if (unlikely(p == RETRY_TASK))
2707 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02002708 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002709 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002710 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002711
2712 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02002713}
2714
2715/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002716 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03002717 *
2718 * The main means of driving the scheduler and thus entering this function are:
2719 *
2720 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
2721 *
2722 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
2723 * paths. For example, see arch/x86/entry_64.S.
2724 *
2725 * To drive preemption between tasks, the scheduler sets the flag in timer
2726 * interrupt handler scheduler_tick().
2727 *
2728 * 3. Wakeups don't really cause entry into schedule(). They add a
2729 * task to the run-queue and that's it.
2730 *
2731 * Now, if the new task added to the run-queue preempts the current
2732 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
2733 * called on the nearest possible occasion:
2734 *
2735 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
2736 *
2737 * - in syscall or exception context, at the next outmost
2738 * preempt_enable(). (this might be as soon as the wake_up()'s
2739 * spin_unlock()!)
2740 *
2741 * - in IRQ context, return from interrupt-handler to
2742 * preemptible context
2743 *
2744 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
2745 * then at the next:
2746 *
2747 * - cond_resched() call
2748 * - explicit schedule() call
2749 * - return from syscall or exception to user-space
2750 * - return from interrupt-handler to user-space
Ingo Molnardd41f592007-07-09 18:51:59 +02002751 */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002752static void __sched __schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02002753{
2754 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08002755 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02002756 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02002757 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002758
Peter Zijlstraff743342009-03-13 12:21:26 +01002759need_resched:
2760 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02002761 cpu = smp_processor_id();
2762 rq = cpu_rq(cpu);
Paul E. McKenney38200cf2014-10-21 12:50:04 -07002763 rcu_note_context_switch();
Ingo Molnardd41f592007-07-09 18:51:59 +02002764 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02002765
Ingo Molnardd41f592007-07-09 18:51:59 +02002766 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Peter Zijlstra31656512008-07-18 18:01:23 +02002768 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02002769 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002770
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002771 /*
2772 * Make sure that signal_pending_state()->signal_pending() below
2773 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
2774 * done by the caller to avoid the race with signal_wake_up().
2775 */
2776 smp_mb__before_spinlock();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002777 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Oleg Nesterov246d86b2010-05-19 14:57:11 +02002779 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02002780 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02002781 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002782 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002783 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02002784 deactivate_task(rq, prev, DEQUEUE_SLEEP);
2785 prev->on_rq = 0;
2786
Tejun Heo21aa9af2010-06-08 21:40:37 +02002787 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02002788 * If a worker went to sleep, notify and ask workqueue
2789 * whether it wants to wake up a task to maintain
2790 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002791 */
2792 if (prev->flags & PF_WQ_WORKER) {
2793 struct task_struct *to_wakeup;
2794
2795 to_wakeup = wq_worker_sleeping(prev, cpu);
2796 if (to_wakeup)
2797 try_to_wake_up_local(to_wakeup);
2798 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02002799 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002800 switch_count = &prev->nvcsw;
2801 }
2802
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002803 if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)
Peter Zijlstra606dba22012-02-11 06:05:00 +01002804 update_rq_clock(rq);
2805
2806 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01002807 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002808 clear_preempt_need_resched();
Mike Galbraithf26f9af2010-12-08 11:05:42 +01002809 rq->skip_clock_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 rq->nr_switches++;
2813 rq->curr = next;
2814 ++*switch_count;
2815
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002816 rq = context_switch(rq, prev, next); /* unlocks the rq */
2817 cpu = cpu_of(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002819 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Gregory Haskins3f029d32009-07-29 11:08:47 -04002821 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Thomas Gleixnerba74c142011-03-21 13:32:17 +01002823 sched_preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01002824 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 goto need_resched;
2826}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002827
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002828static inline void sched_submit_work(struct task_struct *tsk)
2829{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02002830 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002831 return;
2832 /*
2833 * If we are going to sleep and we have plugged IO queued,
2834 * make sure to submit it to avoid deadlocks.
2835 */
2836 if (blk_needs_flush_plug(tsk))
2837 blk_schedule_flush_plug(tsk);
2838}
2839
Andi Kleen722a9f92014-05-02 00:44:38 +02002840asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002841{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002842 struct task_struct *tsk = current;
2843
2844 sched_submit_work(tsk);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002845 __schedule();
2846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847EXPORT_SYMBOL(schedule);
2848
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01002849#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02002850asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002851{
2852 /*
2853 * If we come here after a random call to set_need_resched(),
2854 * or we have been woken up remotely but the IPI has not yet arrived,
2855 * we haven't yet exited the RCU idle mode. Do it here manually until
2856 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002857 *
2858 * NB: There are buggy callers of this function. Ideally we
2859 * should warn if prev_state != IN_USER, but that will trigger
2860 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002861 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002862 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002863 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002864 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002865}
2866#endif
2867
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01002868/**
2869 * schedule_preempt_disabled - called with preemption disabled
2870 *
2871 * Returns with preemption disabled. Note: preempt_count must be 1
2872 */
2873void __sched schedule_preempt_disabled(void)
2874{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01002875 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01002876 schedule();
2877 preempt_disable();
2878}
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880#ifdef CONFIG_PREEMPT
2881/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002882 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002883 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * occur there and call schedule directly.
2885 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002886asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 /*
2889 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002890 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02002892 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return;
2894
Andi Kleen3a5c3592007-10-15 17:00:14 +02002895 do {
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002896 __preempt_count_add(PREEMPT_ACTIVE);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002897 __schedule();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002898 __preempt_count_sub(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002899
2900 /*
2901 * Check again in case we missed a preemption opportunity
2902 * between schedule and now.
2903 */
2904 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08002905 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002907NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02002909
2910#ifdef CONFIG_CONTEXT_TRACKING
2911/**
2912 * preempt_schedule_context - preempt_schedule called by tracing
2913 *
2914 * The tracing infrastructure uses preempt_enable_notrace to prevent
2915 * recursion and tracing preempt enabling caused by the tracing
2916 * infrastructure itself. But as tracing can happen in areas coming
2917 * from userspace or just about to enter userspace, a preempt enable
2918 * can occur before user_exit() is called. This will cause the scheduler
2919 * to be called when the system is still in usermode.
2920 *
2921 * To prevent this, the preempt_enable_notrace will use this function
2922 * instead of preempt_schedule() to exit user context if needed before
2923 * calling the scheduler.
2924 */
2925asmlinkage __visible void __sched notrace preempt_schedule_context(void)
2926{
2927 enum ctx_state prev_ctx;
2928
2929 if (likely(!preemptible()))
2930 return;
2931
2932 do {
2933 __preempt_count_add(PREEMPT_ACTIVE);
2934 /*
2935 * Needs preempt disabled in case user_exit() is traced
2936 * and the tracer calls preempt_enable_notrace() causing
2937 * an infinite recursion.
2938 */
2939 prev_ctx = exception_enter();
2940 __schedule();
2941 exception_exit(prev_ctx);
2942
2943 __preempt_count_sub(PREEMPT_ACTIVE);
2944 barrier();
2945 } while (need_resched());
2946}
2947EXPORT_SYMBOL_GPL(preempt_schedule_context);
2948#endif /* CONFIG_CONTEXT_TRACKING */
2949
Thomas Gleixner32e475d2013-11-21 12:41:44 +01002950#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002953 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 * off of irq context.
2955 * Note, that this is called and return with irqs disabled. This will
2956 * protect us against recursive calling from irq.
2957 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002958asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002960 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01002961
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002962 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002963 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002965 prev_state = exception_enter();
2966
Andi Kleen3a5c3592007-10-15 17:00:14 +02002967 do {
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002968 __preempt_count_add(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002969 local_irq_enable();
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002970 __schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02002971 local_irq_disable();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002972 __preempt_count_sub(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002973
2974 /*
2975 * Check again in case we missed a preemption opportunity
2976 * between schedule and now.
2977 */
2978 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08002979 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002980
2981 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
Peter Zijlstra63859d42009-09-15 19:14:42 +02002984int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002985 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Peter Zijlstra63859d42009-09-15 19:14:42 +02002987 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989EXPORT_SYMBOL(default_wake_function);
2990
Ingo Molnarb29739f2006-06-27 02:54:51 -07002991#ifdef CONFIG_RT_MUTEXES
2992
2993/*
2994 * rt_mutex_setprio - set the current priority of a task
2995 * @p: task
2996 * @prio: prio value (kernel-internal form)
2997 *
2998 * This function changes the 'effective' priority of a task. It does
2999 * not touch ->normal_prio like __setscheduler().
3000 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003001 * Used by the rt_mutex code to implement priority inheritance
3002 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003003 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003004void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003005{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003006 int oldprio, queued, running, enqueue_flag = 0;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003007 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003008 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003009
Dario Faggioliaab03e02013-11-28 11:14:43 +01003010 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003011
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003012 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003013
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003014 /*
3015 * Idle task boosting is a nono in general. There is one
3016 * exception, when PREEMPT_RT and NOHZ is active:
3017 *
3018 * The idle task calls get_next_timer_interrupt() and holds
3019 * the timer wheel base->lock on the CPU and another CPU wants
3020 * to access the timer (probably to cancel it). We can safely
3021 * ignore the boosting request, as the idle CPU runs this code
3022 * with interrupts disabled and will complete the lock
3023 * protected section without being interrupted. So there is no
3024 * real need to boost.
3025 */
3026 if (unlikely(p == rq->idle)) {
3027 WARN_ON(p != rq->curr);
3028 WARN_ON(p->pi_blocked_on);
3029 goto out_unlock;
3030 }
3031
Steven Rostedta8027072010-09-20 15:13:34 -04003032 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003033 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003034 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003035 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003036 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003037 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003038 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003039 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003040 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003041
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003042 /*
3043 * Boosting condition are:
3044 * 1. -rt task is running and holds mutex A
3045 * --> -dl task blocks on mutex A
3046 *
3047 * 2. -dl task is running and holds mutex A
3048 * --> -dl task blocks on mutex A and could preempt the
3049 * running task
3050 */
3051 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003052 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3053 if (!dl_prio(p->normal_prio) ||
3054 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003055 p->dl.dl_boosted = 1;
3056 p->dl.dl_throttled = 0;
3057 enqueue_flag = ENQUEUE_REPLENISH;
3058 } else
3059 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003060 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003061 } else if (rt_prio(prio)) {
3062 if (dl_prio(oldprio))
3063 p->dl.dl_boosted = 0;
3064 if (oldprio < prio)
3065 enqueue_flag = ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003066 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003067 } else {
3068 if (dl_prio(oldprio))
3069 p->dl.dl_boosted = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003070 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003071 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003072
Ingo Molnarb29739f2006-06-27 02:54:51 -07003073 p->prio = prio;
3074
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003075 if (running)
3076 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003077 if (queued)
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003078 enqueue_task(rq, p, enqueue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003079
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003080 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003081out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003082 __task_rq_unlock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003083}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003084#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003085
Ingo Molnar36c8b582006-07-03 00:25:41 -07003086void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003088 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003090 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003092 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return;
3094 /*
3095 * We have to be careful, if called from sys_setpriority(),
3096 * the task might be in the middle of scheduling on another CPU.
3097 */
3098 rq = task_rq_lock(p, &flags);
3099 /*
3100 * The RT priorities are set via sched_setscheduler(), but we still
3101 * allow the 'normal' nice value to be set - but as expected
3102 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003103 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003105 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 p->static_prio = NICE_TO_PRIO(nice);
3107 goto out_unlock;
3108 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003109 queued = task_on_rq_queued(p);
3110 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003111 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003114 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003115 old_prio = p->prio;
3116 p->prio = effective_prio(p);
3117 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003119 if (queued) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003120 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003122 * If the task increased its priority or is running and
3123 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003125 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003126 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003129 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131EXPORT_SYMBOL(set_user_nice);
3132
Matt Mackalle43379f2005-05-01 08:59:00 -07003133/*
3134 * can_nice - check if a task can reduce its nice value
3135 * @p: task
3136 * @nice: nice value
3137 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003138int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003139{
Matt Mackall024f4742005-08-18 11:24:19 -07003140 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003141 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003142
Jiri Slaby78d7d402010-03-05 13:42:54 -08003143 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003144 capable(CAP_SYS_NICE));
3145}
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147#ifdef __ARCH_WANT_SYS_NICE
3148
3149/*
3150 * sys_nice - change the priority of the current process.
3151 * @increment: priority increment
3152 *
3153 * sys_setpriority is a more generic, but much slower function that
3154 * does similar things.
3155 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003156SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003158 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 /*
3161 * Setpriority might change our priority at the same moment.
3162 * We don't have to worry. Conceptually one call occurs first
3163 * and we have a single winner.
3164 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003165 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003166 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003168 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003169 if (increment < 0 && !can_nice(current, nice))
3170 return -EPERM;
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 retval = security_task_setnice(current, nice);
3173 if (retval)
3174 return retval;
3175
3176 set_user_nice(current, nice);
3177 return 0;
3178}
3179
3180#endif
3181
3182/**
3183 * task_prio - return the priority value of a given task.
3184 * @p: the task in question.
3185 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003186 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 * RT tasks are offset by -200. Normal tasks are centered
3188 * around 0, value goes from -16 to +15.
3189 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003190int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 return p->prio - MAX_RT_PRIO;
3193}
3194
3195/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 * idle_cpu - is a given cpu idle currently?
3197 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003198 *
3199 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 */
3201int idle_cpu(int cpu)
3202{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003203 struct rq *rq = cpu_rq(cpu);
3204
3205 if (rq->curr != rq->idle)
3206 return 0;
3207
3208 if (rq->nr_running)
3209 return 0;
3210
3211#ifdef CONFIG_SMP
3212 if (!llist_empty(&rq->wake_list))
3213 return 0;
3214#endif
3215
3216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219/**
3220 * idle_task - return the idle task for a given cpu.
3221 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003222 *
3223 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003225struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 return cpu_rq(cpu)->idle;
3228}
3229
3230/**
3231 * find_process_by_pid - find a process with a matching PID value.
3232 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003233 *
3234 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003236static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003238 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Dario Faggioliaab03e02013-11-28 11:14:43 +01003241/*
3242 * This function initializes the sched_dl_entity of a newly becoming
3243 * SCHED_DEADLINE task.
3244 *
3245 * Only the static values are considered here, the actual runtime and the
3246 * absolute deadline will be properly calculated when the task is enqueued
3247 * for the first time with its new policy.
3248 */
3249static void
3250__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3251{
3252 struct sched_dl_entity *dl_se = &p->dl;
3253
3254 init_dl_task_timer(dl_se);
3255 dl_se->dl_runtime = attr->sched_runtime;
3256 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003257 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003258 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003259 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01003260 dl_se->dl_throttled = 0;
3261 dl_se->dl_new = 1;
Juri Lelli5bfd1262014-04-15 13:49:04 +02003262 dl_se->dl_yielded = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003263}
3264
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003265/*
3266 * sched_setparam() passes in -1 for its policy, to let the functions
3267 * it calls know not to change it.
3268 */
3269#define SETPARAM_POLICY -1
3270
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003271static void __setscheduler_params(struct task_struct *p,
3272 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003274 int policy = attr->sched_policy;
3275
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003276 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003277 policy = p->policy;
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003280
Dario Faggioliaab03e02013-11-28 11:14:43 +01003281 if (dl_policy(policy))
3282 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003283 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003284 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3285
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003286 /*
3287 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3288 * !rt_policy. Always setting this ensures that things like
3289 * getparam()/getattr() don't report silly values for !rt tasks.
3290 */
3291 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003292 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003293 set_load_weight(p);
3294}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003295
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003296/* Actually do priority change: must hold pi & rq lock. */
3297static void __setscheduler(struct rq *rq, struct task_struct *p,
3298 const struct sched_attr *attr)
3299{
3300 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003301
Steven Rostedt383afd02014-03-11 19:24:20 -04003302 /*
3303 * If we get here, there was no pi waiters boosting the
3304 * task. It is safe to use the normal prio.
3305 */
3306 p->prio = normal_prio(p);
3307
Dario Faggioliaab03e02013-11-28 11:14:43 +01003308 if (dl_prio(p->prio))
3309 p->sched_class = &dl_sched_class;
3310 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003311 p->sched_class = &rt_sched_class;
3312 else
3313 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003315
3316static void
3317__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3318{
3319 struct sched_dl_entity *dl_se = &p->dl;
3320
3321 attr->sched_priority = p->rt_priority;
3322 attr->sched_runtime = dl_se->dl_runtime;
3323 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003324 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003325 attr->sched_flags = dl_se->flags;
3326}
3327
3328/*
3329 * This function validates the new parameters of a -deadline task.
3330 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003331 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003332 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003333 * user parameters are above the internal resolution of 1us (we
3334 * check sched_runtime only since it is always the smaller one) and
3335 * below 2^63 ns (we have to check both sched_deadline and
3336 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003337 */
3338static bool
3339__checkparam_dl(const struct sched_attr *attr)
3340{
Juri Lellib08278192014-05-13 14:11:31 +02003341 /* deadline != 0 */
3342 if (attr->sched_deadline == 0)
3343 return false;
3344
3345 /*
3346 * Since we truncate DL_SCALE bits, make sure we're at least
3347 * that big.
3348 */
3349 if (attr->sched_runtime < (1ULL << DL_SCALE))
3350 return false;
3351
3352 /*
3353 * Since we use the MSB for wrap-around and sign issues, make
3354 * sure it's not set (mind that period can be equal to zero).
3355 */
3356 if (attr->sched_deadline & (1ULL << 63) ||
3357 attr->sched_period & (1ULL << 63))
3358 return false;
3359
3360 /* runtime <= deadline <= period (if period != 0) */
3361 if ((attr->sched_period != 0 &&
3362 attr->sched_period < attr->sched_deadline) ||
3363 attr->sched_deadline < attr->sched_runtime)
3364 return false;
3365
3366 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003367}
3368
David Howellsc69e8d92008-11-14 10:39:19 +11003369/*
3370 * check the target process has a UID that matches the current process's
3371 */
3372static bool check_same_owner(struct task_struct *p)
3373{
3374 const struct cred *cred = current_cred(), *pcred;
3375 bool match;
3376
3377 rcu_read_lock();
3378 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003379 match = (uid_eq(cred->euid, pcred->euid) ||
3380 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003381 rcu_read_unlock();
3382 return match;
3383}
3384
Dario Faggiolid50dde52013-11-07 14:43:36 +01003385static int __sched_setscheduler(struct task_struct *p,
3386 const struct sched_attr *attr,
3387 bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Steven Rostedt383afd02014-03-11 19:24:20 -04003389 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3390 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003391 int retval, oldprio, oldpolicy = -1, queued, running;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003392 int policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003394 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003395 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003396 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Steven Rostedt66e53932006-06-27 02:54:44 -07003398 /* may grab non-irq protected spin_locks */
3399 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400recheck:
3401 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003402 if (policy < 0) {
3403 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003405 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003406 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003407
Dario Faggioliaab03e02013-11-28 11:14:43 +01003408 if (policy != SCHED_DEADLINE &&
3409 policy != SCHED_FIFO && policy != SCHED_RR &&
Lennart Poetteringca94c442009-06-15 17:17:47 +02003410 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
3411 policy != SCHED_IDLE)
3412 return -EINVAL;
3413 }
3414
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003415 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3416 return -EINVAL;
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 /*
3419 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003420 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3421 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003423 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003424 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003426 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3427 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return -EINVAL;
3429
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003430 /*
3431 * Allow unprivileged RT tasks to decrease priority:
3432 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003433 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003434 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003435 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003436 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003437 return -EPERM;
3438 }
3439
Ingo Molnare05606d2007-07-09 18:51:59 +02003440 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003441 unsigned long rlim_rtprio =
3442 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003443
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003444 /* can't set/change the rt policy */
3445 if (policy != p->policy && !rlim_rtprio)
3446 return -EPERM;
3447
3448 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003449 if (attr->sched_priority > p->rt_priority &&
3450 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003451 return -EPERM;
3452 }
Darren Hartc02aa732011-02-17 15:37:07 -08003453
Juri Lellid44753b2014-03-03 12:09:21 +01003454 /*
3455 * Can't set/change SCHED_DEADLINE policy at all for now
3456 * (safest behavior); in the future we would like to allow
3457 * unprivileged DL tasks to increase their relative deadline
3458 * or reduce their runtime (both ways reducing utilization)
3459 */
3460 if (dl_policy(policy))
3461 return -EPERM;
3462
Ingo Molnardd41f592007-07-09 18:51:59 +02003463 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003464 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3465 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003466 */
Darren Hartc02aa732011-02-17 15:37:07 -08003467 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003468 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003469 return -EPERM;
3470 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003471
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003472 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003473 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003474 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003475
3476 /* Normal users shall not reset the sched_reset_on_fork flag */
3477 if (p->sched_reset_on_fork && !reset_on_fork)
3478 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003481 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003482 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003483 if (retval)
3484 return retval;
3485 }
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003488 * make sure no PI-waiters arrive (or leave) while we are
3489 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003490 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003491 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 * runqueue lock must be held.
3493 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003494 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003495
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003496 /*
3497 * Changing the policy of the stop threads its a very bad idea
3498 */
3499 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003500 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003501 return -EINVAL;
3502 }
3503
Dario Faggiolia51e9192011-03-24 14:00:18 +01003504 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003505 * If not changing anything there's no need to proceed further,
3506 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003507 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003508 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003509 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003510 goto change;
3511 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3512 goto change;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003513 if (dl_policy(policy))
3514 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003515
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003516 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003517 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003518 return 0;
3519 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003520change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003521
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003522 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003523#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003524 /*
3525 * Do not allow realtime tasks into groups that have no runtime
3526 * assigned.
3527 */
3528 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01003529 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
3530 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003531 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003532 return -EPERM;
3533 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003534#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01003535#ifdef CONFIG_SMP
3536 if (dl_bandwidth_enabled() && dl_policy(policy)) {
3537 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01003538
3539 /*
3540 * Don't allow tasks with an affinity mask smaller than
3541 * the entire root_domain to become SCHED_DEADLINE. We
3542 * will also fail if there's no bandwidth available.
3543 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003544 if (!cpumask_subset(span, &p->cpus_allowed) ||
3545 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003546 task_rq_unlock(rq, p, &flags);
3547 return -EPERM;
3548 }
3549 }
3550#endif
3551 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 /* recheck policy now with rq lock held */
3554 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3555 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003556 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 goto recheck;
3558 }
Dario Faggioli332ac172013-11-07 14:43:45 +01003559
3560 /*
3561 * If setscheduling to SCHED_DEADLINE (or changing the parameters
3562 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
3563 * is available.
3564 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003565 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003566 task_rq_unlock(rq, p, &flags);
3567 return -EBUSY;
3568 }
3569
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003570 p->sched_reset_on_fork = reset_on_fork;
3571 oldprio = p->prio;
3572
3573 /*
3574 * Special case for priority boosted tasks.
3575 *
3576 * If the new priority is lower or equal (user space view)
3577 * than the current (boosted) priority, we just store the new
3578 * normal parameters and do not touch the scheduler class and
3579 * the runqueue. This will be done when the task deboost
3580 * itself.
3581 */
3582 if (rt_mutex_check_prio(p, newprio)) {
3583 __setscheduler_params(p, attr);
3584 task_rq_unlock(rq, p, &flags);
3585 return 0;
3586 }
3587
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003588 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003589 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003590 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01003591 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003592 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003593 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003594
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003595 prev_class = p->sched_class;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003596 __setscheduler(rq, p, attr);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003597
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003598 if (running)
3599 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003600 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01003601 /*
3602 * We enqueue to tail when the priority of a task is
3603 * increased (user space view).
3604 */
3605 enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
3606 }
Steven Rostedtcb469842008-01-25 21:08:22 +01003607
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003608 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003609 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003610
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07003611 rt_mutex_adjust_pi(p);
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return 0;
3614}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003615
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003616static int _sched_setscheduler(struct task_struct *p, int policy,
3617 const struct sched_param *param, bool check)
3618{
3619 struct sched_attr attr = {
3620 .sched_policy = policy,
3621 .sched_priority = param->sched_priority,
3622 .sched_nice = PRIO_TO_NICE(p->static_prio),
3623 };
3624
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003625 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
3626 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003627 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
3628 policy &= ~SCHED_RESET_ON_FORK;
3629 attr.sched_policy = policy;
3630 }
3631
3632 return __sched_setscheduler(p, &attr, check);
3633}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003634/**
3635 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
3636 * @p: the task in question.
3637 * @policy: new policy.
3638 * @param: structure containing the new RT priority.
3639 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003640 * Return: 0 on success. An error code otherwise.
3641 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10003642 * NOTE that the task may be already dead.
3643 */
3644int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003645 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003646{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003647 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649EXPORT_SYMBOL_GPL(sched_setscheduler);
3650
Dario Faggiolid50dde52013-11-07 14:43:36 +01003651int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
3652{
3653 return __sched_setscheduler(p, attr, true);
3654}
3655EXPORT_SYMBOL_GPL(sched_setattr);
3656
Rusty Russell961ccdd2008-06-23 13:55:38 +10003657/**
3658 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
3659 * @p: the task in question.
3660 * @policy: new policy.
3661 * @param: structure containing the new RT priority.
3662 *
3663 * Just like sched_setscheduler, only don't bother checking if the
3664 * current context has permission. For example, this is needed in
3665 * stop_machine(): we create temporary high priority worker threads,
3666 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003667 *
3668 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10003669 */
3670int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003671 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003672{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003673 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003674}
3675
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003676static int
3677do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 struct sched_param lparam;
3680 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003681 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 if (!param || pid < 0)
3684 return -EINVAL;
3685 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
3686 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003687
3688 rcu_read_lock();
3689 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003691 if (p != NULL)
3692 retval = sched_setscheduler(p, policy, &lparam);
3693 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07003694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return retval;
3696}
3697
Dario Faggiolid50dde52013-11-07 14:43:36 +01003698/*
3699 * Mimics kernel/events/core.c perf_copy_attr().
3700 */
3701static int sched_copy_attr(struct sched_attr __user *uattr,
3702 struct sched_attr *attr)
3703{
3704 u32 size;
3705 int ret;
3706
3707 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
3708 return -EFAULT;
3709
3710 /*
3711 * zero the full structure, so that a short copy will be nice.
3712 */
3713 memset(attr, 0, sizeof(*attr));
3714
3715 ret = get_user(size, &uattr->size);
3716 if (ret)
3717 return ret;
3718
3719 if (size > PAGE_SIZE) /* silly large */
3720 goto err_size;
3721
3722 if (!size) /* abi compat */
3723 size = SCHED_ATTR_SIZE_VER0;
3724
3725 if (size < SCHED_ATTR_SIZE_VER0)
3726 goto err_size;
3727
3728 /*
3729 * If we're handed a bigger struct than we know of,
3730 * ensure all the unknown bits are 0 - i.e. new
3731 * user-space does not rely on any kernel feature
3732 * extensions we dont know about yet.
3733 */
3734 if (size > sizeof(*attr)) {
3735 unsigned char __user *addr;
3736 unsigned char __user *end;
3737 unsigned char val;
3738
3739 addr = (void __user *)uattr + sizeof(*attr);
3740 end = (void __user *)uattr + size;
3741
3742 for (; addr < end; addr++) {
3743 ret = get_user(val, addr);
3744 if (ret)
3745 return ret;
3746 if (val)
3747 goto err_size;
3748 }
3749 size = sizeof(*attr);
3750 }
3751
3752 ret = copy_from_user(attr, uattr, size);
3753 if (ret)
3754 return -EFAULT;
3755
3756 /*
3757 * XXX: do we want to be lenient like existing syscalls; or do we want
3758 * to be strict and return an error on out-of-bounds values?
3759 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003760 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003761
Michael Kerriske78c7bc2014-05-09 16:54:28 +02003762 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003763
3764err_size:
3765 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02003766 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003767}
3768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769/**
3770 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
3771 * @pid: the pid in question.
3772 * @policy: new policy.
3773 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003774 *
3775 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003777SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
3778 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Jason Baronc21761f2006-01-18 17:43:03 -08003780 /* negative values for policy are not valid */
3781 if (policy < 0)
3782 return -EINVAL;
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return do_sched_setscheduler(pid, policy, param);
3785}
3786
3787/**
3788 * sys_sched_setparam - set/change the RT priority of a thread
3789 * @pid: the pid in question.
3790 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003791 *
3792 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003794SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003796 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
3799/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01003800 * sys_sched_setattr - same as above, but with extended sched_attr
3801 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01003802 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09003803 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003804 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003805SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
3806 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003807{
3808 struct sched_attr attr;
3809 struct task_struct *p;
3810 int retval;
3811
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003812 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003813 return -EINVAL;
3814
Michael Kerrisk143cf232014-05-09 16:54:15 +02003815 retval = sched_copy_attr(uattr, &attr);
3816 if (retval)
3817 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003818
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02003819 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02003820 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003821
3822 rcu_read_lock();
3823 retval = -ESRCH;
3824 p = find_process_by_pid(pid);
3825 if (p != NULL)
3826 retval = sched_setattr(p, &attr);
3827 rcu_read_unlock();
3828
3829 return retval;
3830}
3831
3832/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
3834 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003835 *
3836 * Return: On success, the policy of the thread. Otherwise, a negative error
3837 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003839SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Ingo Molnar36c8b582006-07-03 00:25:41 -07003841 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003842 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02003845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003848 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 p = find_process_by_pid(pid);
3850 if (p) {
3851 retval = security_task_getscheduler(p);
3852 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02003853 retval = p->policy
3854 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003856 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return retval;
3858}
3859
3860/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02003861 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 * @pid: the pid in question.
3863 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003864 *
3865 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
3866 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003868SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02003870 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003871 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003872 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02003875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003877 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 p = find_process_by_pid(pid);
3879 retval = -ESRCH;
3880 if (!p)
3881 goto out_unlock;
3882
3883 retval = security_task_getscheduler(p);
3884 if (retval)
3885 goto out_unlock;
3886
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02003887 if (task_has_rt_policy(p))
3888 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003889 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 /*
3892 * This one might sleep, we cannot do it with a spinlock held ...
3893 */
3894 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return retval;
3897
3898out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003899 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return retval;
3901}
3902
Dario Faggiolid50dde52013-11-07 14:43:36 +01003903static int sched_read_attr(struct sched_attr __user *uattr,
3904 struct sched_attr *attr,
3905 unsigned int usize)
3906{
3907 int ret;
3908
3909 if (!access_ok(VERIFY_WRITE, uattr, usize))
3910 return -EFAULT;
3911
3912 /*
3913 * If we're handed a smaller struct than we know of,
3914 * ensure all the unknown bits are 0 - i.e. old
3915 * user-space does not get uncomplete information.
3916 */
3917 if (usize < sizeof(*attr)) {
3918 unsigned char *addr;
3919 unsigned char *end;
3920
3921 addr = (void *)attr + usize;
3922 end = (void *)attr + sizeof(*attr);
3923
3924 for (; addr < end; addr++) {
3925 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02003926 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003927 }
3928
3929 attr->size = usize;
3930 }
3931
Vegard Nossum4efbc452014-02-16 22:24:17 +01003932 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003933 if (ret)
3934 return -EFAULT;
3935
Michael Kerrisk22400672014-05-09 16:54:33 +02003936 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003937}
3938
3939/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01003940 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01003941 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01003942 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003943 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09003944 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003945 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003946SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
3947 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003948{
3949 struct sched_attr attr = {
3950 .size = sizeof(struct sched_attr),
3951 };
3952 struct task_struct *p;
3953 int retval;
3954
3955 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003956 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003957 return -EINVAL;
3958
3959 rcu_read_lock();
3960 p = find_process_by_pid(pid);
3961 retval = -ESRCH;
3962 if (!p)
3963 goto out_unlock;
3964
3965 retval = security_task_getscheduler(p);
3966 if (retval)
3967 goto out_unlock;
3968
3969 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003970 if (p->sched_reset_on_fork)
3971 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003972 if (task_has_dl_policy(p))
3973 __getparam_dl(p, &attr);
3974 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003975 attr.sched_priority = p->rt_priority;
3976 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003977 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003978
3979 rcu_read_unlock();
3980
3981 retval = sched_read_attr(uattr, &attr, size);
3982 return retval;
3983
3984out_unlock:
3985 rcu_read_unlock();
3986 return retval;
3987}
3988
Rusty Russell96f874e22008-11-25 02:35:14 +10303989long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10303991 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003992 struct task_struct *p;
3993 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Thomas Gleixner23f5d142009-12-09 10:15:01 +00003995 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 p = find_process_by_pid(pid);
3998 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00003999 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return -ESRCH;
4001 }
4002
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004003 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004005 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Tejun Heo14a40ff2013-03-19 13:45:20 -07004007 if (p->flags & PF_NO_SETAFFINITY) {
4008 retval = -EINVAL;
4009 goto out_put_task;
4010 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304011 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4012 retval = -ENOMEM;
4013 goto out_put_task;
4014 }
4015 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4016 retval = -ENOMEM;
4017 goto out_free_cpus_allowed;
4018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004020 if (!check_same_owner(p)) {
4021 rcu_read_lock();
4022 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4023 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004024 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004025 }
4026 rcu_read_unlock();
4027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004029 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004030 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004031 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004032
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004033
4034 cpuset_cpus_allowed(p, cpus_allowed);
4035 cpumask_and(new_mask, in_mask, cpus_allowed);
4036
Dario Faggioli332ac172013-11-07 14:43:45 +01004037 /*
4038 * Since bandwidth control happens on root_domain basis,
4039 * if admission test is enabled, we only admit -deadline
4040 * tasks allowed to run on all the CPUs in the task's
4041 * root_domain.
4042 */
4043#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004044 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4045 rcu_read_lock();
4046 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004047 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004048 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004049 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004050 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004051 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004052 }
4053#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004054again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304055 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Paul Menage8707d8b2007-10-18 23:40:22 -07004057 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304058 cpuset_cpus_allowed(p, cpus_allowed);
4059 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004060 /*
4061 * We must have raced with a concurrent cpuset
4062 * update. Just reset the cpus_allowed to the
4063 * cpuset's cpus_allowed
4064 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304065 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004066 goto again;
4067 }
4068 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004069out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304070 free_cpumask_var(new_mask);
4071out_free_cpus_allowed:
4072 free_cpumask_var(cpus_allowed);
4073out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return retval;
4076}
4077
4078static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304079 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Rusty Russell96f874e22008-11-25 02:35:14 +10304081 if (len < cpumask_size())
4082 cpumask_clear(new_mask);
4083 else if (len > cpumask_size())
4084 len = cpumask_size();
4085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4087}
4088
4089/**
4090 * sys_sched_setaffinity - set the cpu affinity of a process
4091 * @pid: pid of the process
4092 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4093 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004094 *
4095 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004097SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4098 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304100 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 int retval;
4102
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304103 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304106 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4107 if (retval == 0)
4108 retval = sched_setaffinity(pid, new_mask);
4109 free_cpumask_var(new_mask);
4110 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
Rusty Russell96f874e22008-11-25 02:35:14 +10304113long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004115 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004116 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004119 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 retval = -ESRCH;
4122 p = find_process_by_pid(pid);
4123 if (!p)
4124 goto out_unlock;
4125
David Quigleye7834f82006-06-23 02:03:59 -07004126 retval = security_task_getscheduler(p);
4127 if (retval)
4128 goto out_unlock;
4129
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004130 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004131 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004132 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
4134out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004135 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Ulrich Drepper9531b622007-08-09 11:16:46 +02004137 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
4140/**
4141 * sys_sched_getaffinity - get the cpu affinity of a process
4142 * @pid: pid of the process
4143 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4144 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004145 *
4146 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004148SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4149 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304152 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004154 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004155 return -EINVAL;
4156 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return -EINVAL;
4158
Rusty Russellf17c8602008-11-25 02:35:11 +10304159 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4160 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Rusty Russellf17c8602008-11-25 02:35:11 +10304162 ret = sched_getaffinity(pid, mask);
4163 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004164 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004165
4166 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304167 ret = -EFAULT;
4168 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004169 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304170 }
4171 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Rusty Russellf17c8602008-11-25 02:35:11 +10304173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
4176/**
4177 * sys_sched_yield - yield the current processor to other threads.
4178 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004179 * This function yields the current CPU to other tasks. If there are no
4180 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004181 *
4182 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004184SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004186 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Ingo Molnar2d723762007-10-15 17:00:12 +02004188 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004189 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 /*
4192 * Since we are going to call schedule() anyway, there's
4193 * no need to preempt or enable interrupts:
4194 */
4195 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004196 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004197 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004198 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 schedule();
4201
4202 return 0;
4203}
4204
Andrew Mortone7b38402006-06-30 01:56:00 -07004205static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004207 __preempt_count_add(PREEMPT_ACTIVE);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004208 __schedule();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004209 __preempt_count_sub(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
Herbert Xu02b67cc32008-01-25 21:08:28 +01004212int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004214 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 __cond_resched();
4216 return 1;
4217 }
4218 return 0;
4219}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004220EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004223 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 * call schedule, and on return reacquire the lock.
4225 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004226 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 * operations here to prevent schedule() from being called twice (once via
4228 * spin_unlock(), once by hand).
4229 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004230int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004232 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004233 int ret = 0;
4234
Peter Zijlstraf607c662009-07-20 19:16:29 +02004235 lockdep_assert_held(lock);
4236
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004237 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004239 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01004240 __cond_resched();
4241 else
4242 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004243 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004248EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004250int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
4252 BUG_ON(!in_softirq());
4253
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004254 if (should_resched()) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004255 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 __cond_resched();
4257 local_bh_disable();
4258 return 1;
4259 }
4260 return 0;
4261}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004262EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264/**
4265 * yield - yield the current processor to other threads.
4266 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004267 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4268 *
4269 * The scheduler is at all times free to pick the calling task as the most
4270 * eligible task to run, if removing the yield() call from your code breaks
4271 * it, its already broken.
4272 *
4273 * Typical broken usage is:
4274 *
4275 * while (!event)
4276 * yield();
4277 *
4278 * where one assumes that yield() will let 'the other' process run that will
4279 * make event true. If the current task is a SCHED_FIFO task that will never
4280 * happen. Never use yield() as a progress guarantee!!
4281 *
4282 * If you want to use yield() to wait for something, use wait_event().
4283 * If you want to use yield() to be 'nice' for others, use cond_resched().
4284 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 */
4286void __sched yield(void)
4287{
4288 set_current_state(TASK_RUNNING);
4289 sys_sched_yield();
4290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291EXPORT_SYMBOL(yield);
4292
Mike Galbraithd95f4122011-02-01 09:50:51 -05004293/**
4294 * yield_to - yield the current processor to another thread in
4295 * your thread group, or accelerate that thread toward the
4296 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004297 * @p: target task
4298 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004299 *
4300 * It's the caller's job to ensure that the target task struct
4301 * can't go away on us before we can do any checks.
4302 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004303 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304304 * true (>0) if we indeed boosted the target task.
4305 * false (0) if we failed to boost the target.
4306 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004307 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004308int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004309{
4310 struct task_struct *curr = current;
4311 struct rq *rq, *p_rq;
4312 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004313 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004314
4315 local_irq_save(flags);
4316 rq = this_rq();
4317
4318again:
4319 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304320 /*
4321 * If we're the only runnable task on the rq and target rq also
4322 * has only one task, there's absolutely no point in yielding.
4323 */
4324 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4325 yielded = -ESRCH;
4326 goto out_irq;
4327 }
4328
Mike Galbraithd95f4122011-02-01 09:50:51 -05004329 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004330 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004331 double_rq_unlock(rq, p_rq);
4332 goto again;
4333 }
4334
4335 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304336 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004337
4338 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304339 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004340
4341 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304342 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004343
4344 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004345 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004346 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004347 /*
4348 * Make p's CPU reschedule; pick_next_entity takes care of
4349 * fairness.
4350 */
4351 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004352 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004353 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004354
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304355out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004356 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304357out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004358 local_irq_restore(flags);
4359
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304360 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004361 schedule();
4362
4363 return yielded;
4364}
4365EXPORT_SYMBOL_GPL(yield_to);
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004368 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 */
4371void __sched io_schedule(void)
4372{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004373 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004375 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01004377 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004378 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004380 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004382 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384EXPORT_SYMBOL(io_schedule);
4385
4386long __sched io_schedule_timeout(long timeout)
4387{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004388 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 long ret;
4390
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004391 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01004393 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004394 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004396 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004398 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return ret;
4400}
4401
4402/**
4403 * sys_sched_get_priority_max - return maximum RT priority.
4404 * @policy: scheduling class.
4405 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004406 * Return: On success, this syscall returns the maximum
4407 * rt_priority that can be used by a given scheduling class.
4408 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004410SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 int ret = -EINVAL;
4413
4414 switch (policy) {
4415 case SCHED_FIFO:
4416 case SCHED_RR:
4417 ret = MAX_USER_RT_PRIO-1;
4418 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004419 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004421 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004422 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 ret = 0;
4424 break;
4425 }
4426 return ret;
4427}
4428
4429/**
4430 * sys_sched_get_priority_min - return minimum RT priority.
4431 * @policy: scheduling class.
4432 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004433 * Return: On success, this syscall returns the minimum
4434 * rt_priority that can be used by a given scheduling class.
4435 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004437SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
4439 int ret = -EINVAL;
4440
4441 switch (policy) {
4442 case SCHED_FIFO:
4443 case SCHED_RR:
4444 ret = 1;
4445 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004446 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004448 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004449 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 ret = 0;
4451 }
4452 return ret;
4453}
4454
4455/**
4456 * sys_sched_rr_get_interval - return the default timeslice of a process.
4457 * @pid: pid of the process.
4458 * @interval: userspace pointer to the timeslice value.
4459 *
4460 * this syscall writes the default timeslice value of a given process
4461 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004462 *
4463 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4464 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004466SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004467 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004469 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004470 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004471 unsigned long flags;
4472 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004473 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004477 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004480 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 p = find_process_by_pid(pid);
4482 if (!p)
4483 goto out_unlock;
4484
4485 retval = security_task_getscheduler(p);
4486 if (retval)
4487 goto out_unlock;
4488
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004489 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004490 time_slice = 0;
4491 if (p->sched_class->get_rr_interval)
4492 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004493 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004494
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004495 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004496 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return retval;
4503}
4504
Steven Rostedt7c731e02008-05-12 21:20:41 +02004505static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004506
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004507void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004510 int ppid;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004511 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 state = p->state ? __ffs(p->state) + 1 : 0;
Erik Gilling28d06862010-11-19 18:08:51 -08004514 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004515 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004516#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004518 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004520 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521#else
4522 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004523 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004525 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526#endif
4527#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004528 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004530 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004531 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08004532 if (pid_alive(p))
4533 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08004534 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004535 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08004536 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07004537 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Tejun Heo3d1cb202013-04-30 15:27:22 -07004539 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01004540 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004543void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004545 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Ingo Molnar4bd77322007-07-11 21:21:47 +02004547#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004548 printk(KERN_INFO
4549 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004551 printk(KERN_INFO
4552 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004554 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004555 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /*
4557 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004558 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 */
4560 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004561 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004562 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004565 touch_all_softlockup_watchdogs();
4566
Ingo Molnardd41f592007-07-09 18:51:59 +02004567#ifdef CONFIG_SCHED_DEBUG
4568 sysrq_sched_debug_show();
4569#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004570 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004571 /*
4572 * Only show locks if all tasks are dumped:
4573 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02004574 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004575 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
Paul Gortmaker0db06282013-06-19 14:53:51 -04004578void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02004579{
Ingo Molnardd41f592007-07-09 18:51:59 +02004580 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004581}
4582
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004583/**
4584 * init_idle - set up an idle thread for a given CPU
4585 * @idle: task in question
4586 * @cpu: cpu the idle task belongs to
4587 *
4588 * NOTE: this function does not set the idle thread's NEED_RESCHED
4589 * flag, to make booting more robust.
4590 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004591void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004593 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 unsigned long flags;
4595
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004596 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01004597
Rik van Riel5e1576e2013-10-07 11:29:26 +01004598 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01004599 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02004600 idle->se.exec_start = sched_clock();
4601
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004602 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004603 /*
4604 * We're having a chicken and egg problem, even though we are
4605 * holding rq->lock, the cpu isn't yet set to this cpu so the
4606 * lockdep check in task_group() will fail.
4607 *
4608 * Similar case to sched_fork(). / Alternatively we could
4609 * use task_rq_lock() here and obtain the other rq->lock.
4610 *
4611 * Silence PROVE_RCU
4612 */
4613 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02004614 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004615 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004618 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02004619#if defined(CONFIG_SMP)
4620 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07004621#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004622 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02004625 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06004626
Ingo Molnardd41f592007-07-09 18:51:59 +02004627 /*
4628 * The idle tasks have their own, simple scheduling class:
4629 */
4630 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05004631 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02004632 vtime_init_idle(idle, cpu);
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02004633#if defined(CONFIG_SMP)
4634 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
4635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Juri Lellif82f8042014-10-07 09:52:11 +01004638int cpuset_cpumask_can_shrink(const struct cpumask *cur,
4639 const struct cpumask *trial)
4640{
4641 int ret = 1, trial_cpus;
4642 struct dl_bw *cur_dl_b;
4643 unsigned long flags;
4644
Juri Lelli75e23e492014-10-28 11:54:46 +00004645 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004646 cur_dl_b = dl_bw_of(cpumask_any(cur));
4647 trial_cpus = cpumask_weight(trial);
4648
4649 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
4650 if (cur_dl_b->bw != -1 &&
4651 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
4652 ret = 0;
4653 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004654 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004655
4656 return ret;
4657}
4658
Juri Lelli7f514122014-09-19 10:22:40 +01004659int task_can_attach(struct task_struct *p,
4660 const struct cpumask *cs_cpus_allowed)
4661{
4662 int ret = 0;
4663
4664 /*
4665 * Kthreads which disallow setaffinity shouldn't be moved
4666 * to a new cpuset; we don't want to change their cpu
4667 * affinity and isolating such threads by their set of
4668 * allowed nodes is unnecessary. Thus, cpusets are not
4669 * applicable for such threads. This prevents checking for
4670 * success of set_cpus_allowed_ptr() on all attached tasks
4671 * before cpus_allowed may be changed.
4672 */
4673 if (p->flags & PF_NO_SETAFFINITY) {
4674 ret = -EINVAL;
4675 goto out;
4676 }
4677
4678#ifdef CONFIG_SMP
4679 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
4680 cs_cpus_allowed)) {
4681 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
4682 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00004683 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01004684 bool overflow;
4685 int cpus;
4686 unsigned long flags;
4687
Juri Lelli75e23e492014-10-28 11:54:46 +00004688 rcu_read_lock_sched();
4689 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01004690 raw_spin_lock_irqsave(&dl_b->lock, flags);
4691 cpus = dl_bw_cpus(dest_cpu);
4692 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
4693 if (overflow)
4694 ret = -EBUSY;
4695 else {
4696 /*
4697 * We reserve space for this task in the destination
4698 * root_domain, as we can't fail after this point.
4699 * We will free resources in the source root_domain
4700 * later on (see set_cpus_allowed_dl()).
4701 */
4702 __dl_add(dl_b, p->dl.dl_bw);
4703 }
4704 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004705 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01004706
4707 }
4708#endif
4709out:
4710 return ret;
4711}
4712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713#ifdef CONFIG_SMP
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004714/*
4715 * move_queued_task - move a queued task to new rq.
4716 *
4717 * Returns (locked) new rq. Old rq's lock is released.
4718 */
4719static struct rq *move_queued_task(struct task_struct *p, int new_cpu)
4720{
4721 struct rq *rq = task_rq(p);
4722
4723 lockdep_assert_held(&rq->lock);
4724
4725 dequeue_task(rq, p, 0);
4726 p->on_rq = TASK_ON_RQ_MIGRATING;
4727 set_task_cpu(p, new_cpu);
4728 raw_spin_unlock(&rq->lock);
4729
4730 rq = cpu_rq(new_cpu);
4731
4732 raw_spin_lock(&rq->lock);
4733 BUG_ON(task_cpu(p) != new_cpu);
4734 p->on_rq = TASK_ON_RQ_QUEUED;
4735 enqueue_task(rq, p, 0);
4736 check_preempt_curr(rq, p, 0);
4737
4738 return rq;
4739}
4740
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004741void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
4742{
4743 if (p->sched_class && p->sched_class->set_cpus_allowed)
4744 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra49396022011-06-25 15:45:46 +02004745
4746 cpumask_copy(&p->cpus_allowed, new_mask);
Peter Zijlstra29baa742012-04-23 12:11:21 +02004747 p->nr_cpus_allowed = cpumask_weight(new_mask);
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004748}
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750/*
4751 * This is how migration works:
4752 *
Tejun Heo969c7922010-05-06 18:49:21 +02004753 * 1) we invoke migration_cpu_stop() on the target CPU using
4754 * stop_one_cpu().
4755 * 2) stopper starts to run (implicitly forcing the migrated thread
4756 * off the CPU)
4757 * 3) it checks whether the migrated task is still in the wrong runqueue.
4758 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02004760 * 5) stopper completes and stop_one_cpu() returns and the migration
4761 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 */
4763
4764/*
4765 * Change a given task's CPU affinity. Migrate the thread to a
4766 * proper CPU and schedule it away if the CPU it's executing on
4767 * is removed from the allowed bitmask.
4768 *
4769 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004770 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 * call is not atomic; no spinlocks may be held.
4772 */
Rusty Russell96f874e22008-11-25 02:35:14 +10304773int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
4775 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004776 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02004777 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004778 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 rq = task_rq_lock(p, &flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01004781
Yong Zhangdb44fc02011-05-09 22:07:05 +08004782 if (cpumask_equal(&p->cpus_allowed, new_mask))
4783 goto out;
4784
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01004785 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 ret = -EINVAL;
4787 goto out;
4788 }
4789
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004790 do_set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01004791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e22008-11-25 02:35:14 +10304793 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 goto out;
4795
Tejun Heo969c7922010-05-06 18:49:21 +02004796 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004797 if (task_running(rq, p) || p->state == TASK_WAKING) {
Tejun Heo969c7922010-05-06 18:49:21 +02004798 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004800 task_rq_unlock(rq, p, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004801 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 tlb_migrate_finish(p->mm);
4803 return 0;
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004804 } else if (task_on_rq_queued(p))
4805 rq = move_queued_task(p, dest_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806out:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004807 task_rq_unlock(rq, p, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return ret;
4810}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07004811EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004814 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 * this because either it can't run here any more (set_cpus_allowed()
4816 * away from this CPU, or CPU going down), or because we're
4817 * attempting to rebalance this task on exec (sched_exec).
4818 *
4819 * So we race with normal scheduler movements, but that's OK, as long
4820 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004821 *
4822 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004824static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004826 struct rq *rq;
Peter Zijlstrae2912002009-12-16 18:04:36 +01004827 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Max Krasnyanskye761b772008-07-15 04:43:49 -07004829 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004830 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004832 rq = cpu_rq(src_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004834 raw_spin_lock(&p->pi_lock);
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004835 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 /* Already moved. */
4837 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004838 goto done;
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 /* Affinity changed (again). */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004841 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004842 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Peter Zijlstrae2912002009-12-16 18:04:36 +01004844 /*
4845 * If we're not on a rq, the next wake-up will ensure we're
4846 * placed properly.
4847 */
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004848 if (task_on_rq_queued(p))
4849 rq = move_queued_task(p, dest_cpu);
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004850done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07004851 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004852fail:
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004853 raw_spin_unlock(&rq->lock);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004854 raw_spin_unlock(&p->pi_lock);
Kirill Korotaevefc30812006-06-27 02:54:32 -07004855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
Mel Gormane6628d52013-10-07 11:29:02 +01004858#ifdef CONFIG_NUMA_BALANCING
4859/* Migrate current task p to target_cpu */
4860int migrate_task_to(struct task_struct *p, int target_cpu)
4861{
4862 struct migration_arg arg = { p, target_cpu };
4863 int curr_cpu = task_cpu(p);
4864
4865 if (curr_cpu == target_cpu)
4866 return 0;
4867
4868 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
4869 return -EINVAL;
4870
4871 /* TODO: This is not properly updating schedstats */
4872
Mel Gorman286549d2014-01-21 15:51:03 -08004873 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01004874 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
4875}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004876
4877/*
4878 * Requeue a task on a given node and accurately track the number of NUMA
4879 * tasks on the runqueues
4880 */
4881void sched_setnuma(struct task_struct *p, int nid)
4882{
4883 struct rq *rq;
4884 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004885 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004886
4887 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004888 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004889 running = task_current(rq, p);
4890
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004891 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004892 dequeue_task(rq, p, 0);
4893 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004894 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004895
4896 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004897
4898 if (running)
4899 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004900 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004901 enqueue_task(rq, p, 0);
4902 task_rq_unlock(rq, p, &flags);
4903}
Mel Gormane6628d52013-10-07 11:29:02 +01004904#endif
4905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906/*
Tejun Heo969c7922010-05-06 18:49:21 +02004907 * migration_cpu_stop - this will be executed by a highprio stopper thread
4908 * and performs thread migration by bumping thread off CPU then
4909 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 */
Tejun Heo969c7922010-05-06 18:49:21 +02004911static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Tejun Heo969c7922010-05-06 18:49:21 +02004913 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Tejun Heo969c7922010-05-06 18:49:21 +02004915 /*
4916 * The original target cpu might have gone down and we might
4917 * be on another cpu but it doesn't matter.
4918 */
4919 local_irq_disable();
Lai Jiangshan5cd038f2014-06-04 16:25:15 +08004920 /*
4921 * We need to explicitly wake pending tasks before running
4922 * __migrate_task() such that we will not miss enforcing cpus_allowed
4923 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
4924 */
4925 sched_ttwu_pending();
Tejun Heo969c7922010-05-06 18:49:21 +02004926 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
4927 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return 0;
4929}
4930
4931#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Ingo Molnar48f24c42006-07-03 00:25:40 -07004933/*
4934 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 * offline.
4936 */
4937void idle_task_exit(void)
4938{
4939 struct mm_struct *mm = current->active_mm;
4940
4941 BUG_ON(cpu_online(smp_processor_id()));
4942
Martin Schwidefskya53efe52012-10-26 17:17:44 +02004943 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02004945 finish_arch_post_lock_switch();
4946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 mmdrop(mm);
4948}
4949
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004950/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02004951 * Since this CPU is going 'away' for a while, fold any nr_active delta
4952 * we might have. Assumes we're called after migrate_tasks() so that the
4953 * nr_active count is stable.
4954 *
4955 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004956 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02004957static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Peter Zijlstra5d180232012-08-20 11:26:57 +02004959 long delta = calc_load_fold_active(rq);
4960 if (delta)
4961 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02004962}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004963
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01004964static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
4965{
4966}
4967
4968static const struct sched_class fake_sched_class = {
4969 .put_prev_task = put_prev_task_fake,
4970};
4971
4972static struct task_struct fake_task = {
4973 /*
4974 * Avoid pull_{rt,dl}_task()
4975 */
4976 .prio = MAX_PRIO + 1,
4977 .sched_class = &fake_sched_class,
4978};
4979
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004980/*
4981 * Migrate all tasks from the rq, sleeping tasks will be migrated by
4982 * try_to_wake_up()->select_task_rq().
4983 *
4984 * Called with rq->lock held even though we'er in stop_machine() and
4985 * there's no concurrency possible, we hold the required locks anyway
4986 * because of lock validation efforts.
4987 */
4988static void migrate_tasks(unsigned int dead_cpu)
4989{
4990 struct rq *rq = cpu_rq(dead_cpu);
4991 struct task_struct *next, *stop = rq->stop;
4992 int dest_cpu;
4993
4994 /*
4995 * Fudge the rq selection such that the below task selection loop
4996 * doesn't get stuck on the currently eligible stop task.
4997 *
4998 * We're currently inside stop_machine() and the rq is either stuck
4999 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5000 * either way we should never end up calling schedule() until we're
5001 * done here.
5002 */
5003 rq->stop = NULL;
5004
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005005 /*
5006 * put_prev_task() and pick_next_task() sched
5007 * class method both need to have an up-to-date
5008 * value of rq->clock[_task]
5009 */
5010 update_rq_clock(rq);
5011
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005012 for ( ; ; ) {
5013 /*
5014 * There's this thread running, bail when that's the only
5015 * remaining thread.
5016 */
5017 if (rq->nr_running == 1)
5018 break;
5019
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005020 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005021 BUG_ON(!next);
5022 next->sched_class->put_prev_task(rq, next);
5023
5024 /* Find suitable destination for @next, with force if needed. */
5025 dest_cpu = select_fallback_rq(dead_cpu, next);
5026 raw_spin_unlock(&rq->lock);
5027
5028 __migrate_task(next, dead_cpu, dest_cpu);
5029
5030 raw_spin_lock(&rq->lock);
5031 }
5032
5033 rq->stop = stop;
5034}
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036#endif /* CONFIG_HOTPLUG_CPU */
5037
Nick Piggine692ab52007-07-26 13:40:43 +02005038#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5039
5040static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005041 {
5042 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005043 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005044 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005045 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005046};
5047
5048static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005049 {
5050 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005051 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005052 .child = sd_ctl_dir,
5053 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005054 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005055};
5056
5057static struct ctl_table *sd_alloc_ctl_entry(int n)
5058{
5059 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005060 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005061
Nick Piggine692ab52007-07-26 13:40:43 +02005062 return entry;
5063}
5064
Milton Miller6382bc92007-10-15 17:00:19 +02005065static void sd_free_ctl_entry(struct ctl_table **tablep)
5066{
Milton Millercd7900762007-10-17 16:55:11 +02005067 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005068
Milton Millercd7900762007-10-17 16:55:11 +02005069 /*
5070 * In the intermediate directories, both the child directory and
5071 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005072 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005073 * static strings and all have proc handlers.
5074 */
5075 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005076 if (entry->child)
5077 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005078 if (entry->proc_handler == NULL)
5079 kfree(entry->procname);
5080 }
Milton Miller6382bc92007-10-15 17:00:19 +02005081
5082 kfree(*tablep);
5083 *tablep = NULL;
5084}
5085
Namhyung Kim201c3732012-08-16 17:03:24 +09005086static int min_load_idx = 0;
libinfd9b86d2013-04-08 14:39:12 +08005087static int max_load_idx = CPU_LOAD_IDX_MAX-1;
Namhyung Kim201c3732012-08-16 17:03:24 +09005088
Nick Piggine692ab52007-07-26 13:40:43 +02005089static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005090set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005091 const char *procname, void *data, int maxlen,
Namhyung Kim201c3732012-08-16 17:03:24 +09005092 umode_t mode, proc_handler *proc_handler,
5093 bool load_idx)
Nick Piggine692ab52007-07-26 13:40:43 +02005094{
Nick Piggine692ab52007-07-26 13:40:43 +02005095 entry->procname = procname;
5096 entry->data = data;
5097 entry->maxlen = maxlen;
5098 entry->mode = mode;
5099 entry->proc_handler = proc_handler;
Namhyung Kim201c3732012-08-16 17:03:24 +09005100
5101 if (load_idx) {
5102 entry->extra1 = &min_load_idx;
5103 entry->extra2 = &max_load_idx;
5104 }
Nick Piggine692ab52007-07-26 13:40:43 +02005105}
5106
5107static struct ctl_table *
5108sd_alloc_ctl_domain_table(struct sched_domain *sd)
5109{
Alex Shi37e6bae2014-01-23 18:39:54 +08005110 struct ctl_table *table = sd_alloc_ctl_entry(14);
Nick Piggine692ab52007-07-26 13:40:43 +02005111
Milton Millerad1cdc12007-10-15 17:00:19 +02005112 if (table == NULL)
5113 return NULL;
5114
Alexey Dobriyane0361852007-08-09 11:16:46 +02005115 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005116 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005117 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005118 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005119 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005120 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005121 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005122 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005123 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005124 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005125 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005126 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005127 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005128 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005129 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Namhyung Kim201c3732012-08-16 17:03:24 +09005130 sizeof(int), 0644, proc_dointvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005131 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Namhyung Kim201c3732012-08-16 17:03:24 +09005132 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005133 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005134 &sd->cache_nice_tries,
Namhyung Kim201c3732012-08-16 17:03:24 +09005135 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005136 set_table_entry(&table[10], "flags", &sd->flags,
Namhyung Kim201c3732012-08-16 17:03:24 +09005137 sizeof(int), 0644, proc_dointvec_minmax, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005138 set_table_entry(&table[11], "max_newidle_lb_cost",
5139 &sd->max_newidle_lb_cost,
5140 sizeof(long), 0644, proc_doulongvec_minmax, false);
5141 set_table_entry(&table[12], "name", sd->name,
Namhyung Kim201c3732012-08-16 17:03:24 +09005142 CORENAME_MAX_SIZE, 0444, proc_dostring, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005143 /* &table[13] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005144
5145 return table;
5146}
5147
Joe Perchesbe7002e2013-06-12 11:55:36 -07005148static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005149{
5150 struct ctl_table *entry, *table;
5151 struct sched_domain *sd;
5152 int domain_num = 0, i;
5153 char buf[32];
5154
5155 for_each_domain(cpu, sd)
5156 domain_num++;
5157 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005158 if (table == NULL)
5159 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005160
5161 i = 0;
5162 for_each_domain(cpu, sd) {
5163 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005164 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005165 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005166 entry->child = sd_alloc_ctl_domain_table(sd);
5167 entry++;
5168 i++;
5169 }
5170 return table;
5171}
5172
5173static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005174static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005175{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005176 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005177 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5178 char buf[32];
5179
Milton Miller73785472007-10-24 18:23:48 +02005180 WARN_ON(sd_ctl_dir[0].child);
5181 sd_ctl_dir[0].child = entry;
5182
Milton Millerad1cdc12007-10-15 17:00:19 +02005183 if (entry == NULL)
5184 return;
5185
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005186 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005187 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005188 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005189 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005190 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005191 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005192 }
Milton Miller73785472007-10-24 18:23:48 +02005193
5194 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005195 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5196}
Milton Miller6382bc92007-10-15 17:00:19 +02005197
Milton Miller73785472007-10-24 18:23:48 +02005198/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005199static void unregister_sched_domain_sysctl(void)
5200{
Milton Miller73785472007-10-24 18:23:48 +02005201 if (sd_sysctl_header)
5202 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005203 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005204 if (sd_ctl_dir[0].child)
5205 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005206}
Nick Piggine692ab52007-07-26 13:40:43 +02005207#else
Milton Miller6382bc92007-10-15 17:00:19 +02005208static void register_sched_domain_sysctl(void)
5209{
5210}
5211static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005212{
5213}
5214#endif
5215
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005216static void set_rq_online(struct rq *rq)
5217{
5218 if (!rq->online) {
5219 const struct sched_class *class;
5220
Rusty Russellc6c49272008-11-25 02:35:05 +10305221 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005222 rq->online = 1;
5223
5224 for_each_class(class) {
5225 if (class->rq_online)
5226 class->rq_online(rq);
5227 }
5228 }
5229}
5230
5231static void set_rq_offline(struct rq *rq)
5232{
5233 if (rq->online) {
5234 const struct sched_class *class;
5235
5236 for_each_class(class) {
5237 if (class->rq_offline)
5238 class->rq_offline(rq);
5239 }
5240
Rusty Russellc6c49272008-11-25 02:35:05 +10305241 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005242 rq->online = 0;
5243 }
5244}
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246/*
5247 * migration_call - callback that gets triggered when a CPU is added.
5248 * Here we can start up the necessary migration thread for the new CPU.
5249 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005250static int
Ingo Molnar48f24c42006-07-03 00:25:40 -07005251migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005253 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005255 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005257 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005258
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005260 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005264 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005265 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005266 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305267 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005268
5269 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005270 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005271 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005275 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02005276 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01005277 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005278 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005279 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305280 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005281 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005282 }
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005283 migrate_tasks(cpu);
5284 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005285 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005286 break;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005287
Peter Zijlstra5d180232012-08-20 11:26:57 +02005288 case CPU_DEAD:
Peter Zijlstraf319da02012-08-20 11:26:57 +02005289 calc_load_migrate(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291#endif
5292 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005293
5294 update_max_interval();
5295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return NOTIFY_OK;
5297}
5298
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005299/*
5300 * Register at high priority so that task migration (migrate_all_tasks)
5301 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005304static struct notifier_block migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005306 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307};
5308
Corey Minyarda803f022014-05-08 13:47:39 -05005309static void __cpuinit set_cpu_rq_start_time(void)
5310{
5311 int cpu = smp_processor_id();
5312 struct rq *rq = cpu_rq(cpu);
5313 rq->age_stamp = sched_clock_cpu(cpu);
5314}
5315
Paul Gortmaker0db06282013-06-19 14:53:51 -04005316static int sched_cpu_active(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005317 unsigned long action, void *hcpu)
5318{
5319 switch (action & ~CPU_TASKS_FROZEN) {
Corey Minyarda803f022014-05-08 13:47:39 -05005320 case CPU_STARTING:
5321 set_cpu_rq_start_time();
5322 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005323 case CPU_DOWN_FAILED:
5324 set_cpu_active((long)hcpu, true);
5325 return NOTIFY_OK;
5326 default:
5327 return NOTIFY_DONE;
5328 }
5329}
5330
Paul Gortmaker0db06282013-06-19 14:53:51 -04005331static int sched_cpu_inactive(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005332 unsigned long action, void *hcpu)
5333{
Peter Zijlstrade212f12013-12-19 11:54:45 +01005334 unsigned long flags;
5335 long cpu = (long)hcpu;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005336 struct dl_bw *dl_b;
Peter Zijlstrade212f12013-12-19 11:54:45 +01005337
Tejun Heo3a101d02010-06-08 21:40:36 +02005338 switch (action & ~CPU_TASKS_FROZEN) {
5339 case CPU_DOWN_PREPARE:
Peter Zijlstrade212f12013-12-19 11:54:45 +01005340 set_cpu_active(cpu, false);
5341
5342 /* explicitly allow suspend */
5343 if (!(action & CPU_TASKS_FROZEN)) {
Peter Zijlstrade212f12013-12-19 11:54:45 +01005344 bool overflow;
5345 int cpus;
5346
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005347 rcu_read_lock_sched();
5348 dl_b = dl_bw_of(cpu);
5349
Peter Zijlstrade212f12013-12-19 11:54:45 +01005350 raw_spin_lock_irqsave(&dl_b->lock, flags);
5351 cpus = dl_bw_cpus(cpu);
5352 overflow = __dl_overflow(dl_b, cpus, 0, 0);
5353 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
5354
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005355 rcu_read_unlock_sched();
5356
Peter Zijlstrade212f12013-12-19 11:54:45 +01005357 if (overflow)
5358 return notifier_from_errno(-EBUSY);
5359 }
Tejun Heo3a101d02010-06-08 21:40:36 +02005360 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005361 }
Peter Zijlstrade212f12013-12-19 11:54:45 +01005362
5363 return NOTIFY_DONE;
Tejun Heo3a101d02010-06-08 21:40:36 +02005364}
5365
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005366static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
5368 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005369 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005370
Tejun Heo3a101d02010-06-08 21:40:36 +02005371 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005372 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5373 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5375 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005376
Tejun Heo3a101d02010-06-08 21:40:36 +02005377 /* Register cpu active notifiers */
5378 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5379 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5380
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005383early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384#endif
5385
5386#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005387
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005388static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5389
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005390#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005391
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005392static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005393
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005394static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005395{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005396 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005397
5398 return 0;
5399}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005400early_param("sched_debug", sched_debug_setup);
5401
5402static inline bool sched_debug(void)
5403{
5404 return sched_debug_enabled;
5405}
Mike Travisf6630112009-11-17 18:22:15 -06005406
Mike Travis7c16ec52008-04-04 18:11:11 -07005407static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305408 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005409{
5410 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07005411 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005412
Rusty Russell968ea6d2008-12-13 21:55:51 +10305413 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e22008-11-25 02:35:14 +10305414 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005415
5416 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5417
5418 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005419 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005420 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005421 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5422 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005423 return -1;
5424 }
5425
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005426 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005427
Rusty Russell758b2cd2008-11-25 02:35:04 +10305428 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005429 printk(KERN_ERR "ERROR: domain->span does not contain "
5430 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005431 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305432 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005433 printk(KERN_ERR "ERROR: domain->groups does not contain"
5434 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005435 }
5436
5437 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5438 do {
5439 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005440 printk("\n");
5441 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005442 break;
5443 }
5444
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005445 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005446 * Even though we initialize ->capacity to something semi-sane,
5447 * we leave capacity_orig unset. This allows us to detect if
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005448 * domain iteration is still funny without causing /0 traps.
5449 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005450 if (!group->sgc->capacity_orig) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005451 printk(KERN_CONT "\n");
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005452 printk(KERN_ERR "ERROR: domain->cpu_capacity not set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005453 break;
5454 }
5455
Rusty Russell758b2cd2008-11-25 02:35:04 +10305456 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005457 printk(KERN_CONT "\n");
5458 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005459 break;
5460 }
5461
Peter Zijlstracb83b622012-04-17 15:49:36 +02005462 if (!(sd->flags & SD_OVERLAP) &&
5463 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005464 printk(KERN_CONT "\n");
5465 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005466 break;
5467 }
5468
Rusty Russell758b2cd2008-11-25 02:35:04 +10305469 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005470
Rusty Russell968ea6d2008-12-13 21:55:51 +10305471 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305472
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005473 printk(KERN_CONT " %s", str);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005474 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005475 printk(KERN_CONT " (cpu_capacity = %d)",
5476 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305477 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005478
5479 group = group->next;
5480 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005481 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005482
Rusty Russell758b2cd2008-11-25 02:35:04 +10305483 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005484 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005485
Rusty Russell758b2cd2008-11-25 02:35:04 +10305486 if (sd->parent &&
5487 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005488 printk(KERN_ERR "ERROR: parent span is not a superset "
5489 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005490 return 0;
5491}
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493static void sched_domain_debug(struct sched_domain *sd, int cpu)
5494{
5495 int level = 0;
5496
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005497 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005498 return;
5499
Nick Piggin41c7ce92005-06-25 14:57:24 -07005500 if (!sd) {
5501 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5502 return;
5503 }
5504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5506
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005507 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005508 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 level++;
5511 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005512 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005513 break;
5514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005516#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005517# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005518static inline bool sched_debug(void)
5519{
5520 return false;
5521}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005522#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005524static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005525{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305526 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005527 return 1;
5528
5529 /* Following flags need at least 2 groups */
5530 if (sd->flags & (SD_LOAD_BALANCE |
5531 SD_BALANCE_NEWIDLE |
5532 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005533 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005534 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005535 SD_SHARE_PKG_RESOURCES |
5536 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005537 if (sd->groups != sd->groups->next)
5538 return 0;
5539 }
5540
5541 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005542 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005543 return 0;
5544
5545 return 1;
5546}
5547
Ingo Molnar48f24c42006-07-03 00:25:40 -07005548static int
5549sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005550{
5551 unsigned long cflags = sd->flags, pflags = parent->flags;
5552
5553 if (sd_degenerate(parent))
5554 return 1;
5555
Rusty Russell758b2cd2008-11-25 02:35:04 +10305556 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005557 return 0;
5558
Suresh Siddha245af2c2005-06-25 14:57:25 -07005559 /* Flags needing groups don't count if only 1 group in parent */
5560 if (parent->groups == parent->groups->next) {
5561 pflags &= ~(SD_LOAD_BALANCE |
5562 SD_BALANCE_NEWIDLE |
5563 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005564 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005565 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005566 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005567 SD_PREFER_SIBLING |
5568 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005569 if (nr_node_ids == 1)
5570 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005571 }
5572 if (~cflags & pflags)
5573 return 0;
5574
5575 return 1;
5576}
5577
Peter Zijlstradce840a2011-04-07 14:09:50 +02005578static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305579{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005580 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005581
Rusty Russell68e74562008-11-25 02:35:13 +10305582 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005583 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005584 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305585 free_cpumask_var(rd->rto_mask);
5586 free_cpumask_var(rd->online);
5587 free_cpumask_var(rd->span);
5588 kfree(rd);
5589}
5590
Gregory Haskins57d885f2008-01-25 21:08:18 +01005591static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5592{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005593 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005594 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005595
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005596 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005597
5598 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005599 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005600
Rusty Russellc6c49272008-11-25 02:35:05 +10305601 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005602 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005603
Rusty Russellc6c49272008-11-25 02:35:05 +10305604 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005605
Ingo Molnara0490fa2009-02-12 11:35:40 +01005606 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005607 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005608 * set old_rd to NULL to skip the freeing later
5609 * in this function:
5610 */
5611 if (!atomic_dec_and_test(&old_rd->refcount))
5612 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005613 }
5614
5615 atomic_inc(&rd->refcount);
5616 rq->rd = rd;
5617
Rusty Russellc6c49272008-11-25 02:35:05 +10305618 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005619 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005620 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005621
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005622 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005623
5624 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005625 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005626}
5627
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005628static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005629{
5630 memset(rd, 0, sizeof(*rd));
5631
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005632 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005633 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005634 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305635 goto free_span;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005636 if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305637 goto free_online;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005638 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
5639 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005640
Dario Faggioli332ac172013-11-07 14:43:45 +01005641 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005642 if (cpudl_init(&rd->cpudl) != 0)
5643 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005644
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005645 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305646 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305647 return 0;
5648
Rusty Russell68e74562008-11-25 02:35:13 +10305649free_rto_mask:
5650 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005651free_dlo_mask:
5652 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305653free_online:
5654 free_cpumask_var(rd->online);
5655free_span:
5656 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005657out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305658 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005659}
5660
Peter Zijlstra029632f2011-10-25 10:00:11 +02005661/*
5662 * By default the system creates a single root-domain with all cpus as
5663 * members (mimicking the global state we have today).
5664 */
5665struct root_domain def_root_domain;
5666
Gregory Haskins57d885f2008-01-25 21:08:18 +01005667static void init_defrootdomain(void)
5668{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005669 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305670
Gregory Haskins57d885f2008-01-25 21:08:18 +01005671 atomic_set(&def_root_domain.refcount, 1);
5672}
5673
Gregory Haskinsdc938522008-01-25 21:08:26 +01005674static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005675{
5676 struct root_domain *rd;
5677
5678 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5679 if (!rd)
5680 return NULL;
5681
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005682 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305683 kfree(rd);
5684 return NULL;
5685 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005686
5687 return rd;
5688}
5689
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005690static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005691{
5692 struct sched_group *tmp, *first;
5693
5694 if (!sg)
5695 return;
5696
5697 first = sg;
5698 do {
5699 tmp = sg->next;
5700
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005701 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5702 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005703
5704 kfree(sg);
5705 sg = tmp;
5706 } while (sg != first);
5707}
5708
Peter Zijlstradce840a2011-04-07 14:09:50 +02005709static void free_sched_domain(struct rcu_head *rcu)
5710{
5711 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005712
5713 /*
5714 * If its an overlapping domain it has private groups, iterate and
5715 * nuke them all.
5716 */
5717 if (sd->flags & SD_OVERLAP) {
5718 free_sched_groups(sd->groups, 1);
5719 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005720 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005721 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005722 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005723 kfree(sd);
5724}
5725
5726static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5727{
5728 call_rcu(&sd->rcu, free_sched_domain);
5729}
5730
5731static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5732{
5733 for (; sd; sd = sd->parent)
5734 destroy_sched_domain(sd, cpu);
5735}
5736
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005738 * Keep a special pointer to the highest sched_domain that has
5739 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5740 * allows us to avoid some pointer chasing select_idle_sibling().
5741 *
5742 * Also keep a unique ID per domain (we use the first cpu number in
5743 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005744 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005745 */
5746DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005747DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005748DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005749DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305750DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5751DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005752
5753static void update_top_cache_domain(int cpu)
5754{
5755 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005756 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005757 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005758 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005759
5760 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005761 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005762 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005763 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005764 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005765 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005766 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005767
5768 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005769 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005770 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005771
5772 sd = lowest_flag_domain(cpu, SD_NUMA);
5773 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305774
5775 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
5776 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005777}
5778
5779/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01005780 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 * hold the hotplug lock.
5782 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01005783static void
5784cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005786 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005787 struct sched_domain *tmp;
5788
5789 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08005790 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005791 struct sched_domain *parent = tmp->parent;
5792 if (!parent)
5793 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08005794
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005795 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005796 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005797 if (parent->parent)
5798 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02005799 /*
5800 * Transfer SD_PREFER_SIBLING down in case of a
5801 * degenerate parent; the spans match for this
5802 * so the property transfers.
5803 */
5804 if (parent->flags & SD_PREFER_SIBLING)
5805 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005806 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08005807 } else
5808 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005809 }
5810
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005811 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005812 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005813 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005814 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005815 if (sd)
5816 sd->child = NULL;
5817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005819 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Gregory Haskins57d885f2008-01-25 21:08:18 +01005821 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005822 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07005823 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005824 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005825
5826 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827}
5828
5829/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10305830static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
5832/* Setup the mask of cpus configured for isolated domains */
5833static int __init isolated_cpu_setup(char *str)
5834{
Rusty Russellbdddd292009-12-02 14:09:16 +10305835 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10305836 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 return 1;
5838}
5839
Ingo Molnar8927f492007-10-15 17:00:13 +02005840__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005842struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005843 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005844 struct root_domain *rd;
5845};
5846
Andreas Herrmann2109b992009-08-18 12:53:00 +02005847enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02005848 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005849 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02005850 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02005851 sa_none,
5852};
5853
Peter Zijlstrac1174872012-05-31 14:47:33 +02005854/*
5855 * Build an iteration mask that can exclude certain CPUs from the upwards
5856 * domain traversal.
5857 *
5858 * Asymmetric node setups can result in situations where the domain tree is of
5859 * unequal depth, make sure to skip domains that already cover the entire
5860 * range.
5861 *
5862 * In that case build_sched_domains() will have terminated the iteration early
5863 * and our sibling sd spans will be empty. Domains should always include the
5864 * cpu they're built on, so check that.
5865 *
5866 */
5867static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
5868{
5869 const struct cpumask *span = sched_domain_span(sd);
5870 struct sd_data *sdd = sd->private;
5871 struct sched_domain *sibling;
5872 int i;
5873
5874 for_each_cpu(i, span) {
5875 sibling = *per_cpu_ptr(sdd->sd, i);
5876 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
5877 continue;
5878
5879 cpumask_set_cpu(i, sched_group_mask(sg));
5880 }
5881}
5882
5883/*
5884 * Return the canonical balance cpu for this group, this is the first cpu
5885 * of this group that's also in the iteration mask.
5886 */
5887int group_balance_cpu(struct sched_group *sg)
5888{
5889 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
5890}
5891
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005892static int
5893build_overlap_sched_groups(struct sched_domain *sd, int cpu)
5894{
5895 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
5896 const struct cpumask *span = sched_domain_span(sd);
5897 struct cpumask *covered = sched_domains_tmpmask;
5898 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005899 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005900 int i;
5901
5902 cpumask_clear(covered);
5903
5904 for_each_cpu(i, span) {
5905 struct cpumask *sg_span;
5906
5907 if (cpumask_test_cpu(i, covered))
5908 continue;
5909
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005910 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02005911
5912 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005913 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02005914 continue;
5915
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005916 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08005917 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005918
5919 if (!sg)
5920 goto fail;
5921
5922 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005923 if (sibling->child)
5924 cpumask_copy(sg_span, sched_domain_span(sibling->child));
5925 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005926 cpumask_set_cpu(i, sg_span);
5927
5928 cpumask_or(covered, covered, sg_span);
5929
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005930 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
5931 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02005932 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005933
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005934 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005935 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005936 * domains and no possible iteration will get us here, we won't
5937 * die on a /0 trap.
5938 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005939 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005940 sg->sgc->capacity_orig = sg->sgc->capacity;
Peter Zijlstrac1174872012-05-31 14:47:33 +02005941
5942 /*
5943 * Make sure the first group of this domain contains the
5944 * canonical balance cpu. Otherwise the sched_domain iteration
5945 * breaks. See update_sg_lb_stats().
5946 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005947 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02005948 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005949 groups = sg;
5950
5951 if (!first)
5952 first = sg;
5953 if (last)
5954 last->next = sg;
5955 last = sg;
5956 last->next = first;
5957 }
5958 sd->groups = groups;
5959
5960 return 0;
5961
5962fail:
5963 free_sched_groups(first, 0);
5964
5965 return -ENOMEM;
5966}
5967
Peter Zijlstradce840a2011-04-07 14:09:50 +02005968static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005970 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
5971 struct sched_domain *child = sd->child;
5972
5973 if (child)
5974 cpu = cpumask_first(sched_domain_span(child));
5975
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005976 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005977 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005978 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
5979 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005980 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 return cpu;
5983}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Ingo Molnar48f24c42006-07-03 00:25:40 -07005985/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02005986 * build_sched_groups will build a circular linked list of the groups
5987 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04005988 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005989 *
5990 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07005991 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005992static int
5993build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005994{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005995 struct sched_group *first = NULL, *last = NULL;
5996 struct sd_data *sdd = sd->private;
5997 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02005998 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005999 int i;
6000
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006001 get_group(cpu, sdd, &sd->groups);
6002 atomic_inc(&sd->groups->ref);
6003
Viresh Kumar09366292013-06-11 16:32:43 +05306004 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006005 return 0;
6006
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006007 lockdep_assert_held(&sched_domains_mutex);
6008 covered = sched_domains_tmpmask;
6009
Peter Zijlstradce840a2011-04-07 14:09:50 +02006010 cpumask_clear(covered);
6011
6012 for_each_cpu(i, span) {
6013 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306014 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006015
6016 if (cpumask_test_cpu(i, covered))
6017 continue;
6018
Viresh Kumarcd08e922013-06-11 16:32:44 +05306019 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006020 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006021
6022 for_each_cpu(j, span) {
6023 if (get_group(j, sdd, NULL) != group)
6024 continue;
6025
6026 cpumask_set_cpu(j, covered);
6027 cpumask_set_cpu(j, sched_group_cpus(sg));
6028 }
6029
6030 if (!first)
6031 first = sg;
6032 if (last)
6033 last->next = sg;
6034 last = sg;
6035 }
6036 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006037
6038 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006039}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006042 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006043 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006044 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006045 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006046 * Typically cpu_capacity for all the groups in a sched domain will be same
6047 * unless there are asymmetries in the topology. If there are asymmetries,
6048 * group having more cpu_capacity will pickup more load compared to the
6049 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006050 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006051static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006052{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006053 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006054
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306055 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006056
6057 do {
6058 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6059 sg = sg->next;
6060 } while (sg != sd->groups);
6061
Peter Zijlstrac1174872012-05-31 14:47:33 +02006062 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006063 return;
6064
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006065 update_group_capacity(sd, cpu);
6066 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006067}
6068
6069/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006070 * Initializers for schedule domains
6071 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6072 */
6073
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006074static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006075int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006076
6077static int __init setup_relax_domain_level(char *str)
6078{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006079 if (kstrtoint(str, 0, &default_relax_domain_level))
6080 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006081
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006082 return 1;
6083}
6084__setup("relax_domain_level=", setup_relax_domain_level);
6085
6086static void set_domain_attribute(struct sched_domain *sd,
6087 struct sched_domain_attr *attr)
6088{
6089 int request;
6090
6091 if (!attr || attr->relax_domain_level < 0) {
6092 if (default_relax_domain_level < 0)
6093 return;
6094 else
6095 request = default_relax_domain_level;
6096 } else
6097 request = attr->relax_domain_level;
6098 if (request < sd->level) {
6099 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006100 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006101 } else {
6102 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006103 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006104 }
6105}
6106
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006107static void __sdt_free(const struct cpumask *cpu_map);
6108static int __sdt_alloc(const struct cpumask *cpu_map);
6109
Andreas Herrmann2109b992009-08-18 12:53:00 +02006110static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6111 const struct cpumask *cpu_map)
6112{
6113 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006114 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006115 if (!atomic_read(&d->rd->refcount))
6116 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006117 case sa_sd:
6118 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006119 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006120 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006121 case sa_none:
6122 break;
6123 }
6124}
6125
6126static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6127 const struct cpumask *cpu_map)
6128{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006129 memset(d, 0, sizeof(*d));
6130
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006131 if (__sdt_alloc(cpu_map))
6132 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006133 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006134 if (!d->sd)
6135 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006136 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006137 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006138 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006139 return sa_rootdomain;
6140}
6141
Peter Zijlstradce840a2011-04-07 14:09:50 +02006142/*
6143 * NULL the sd_data elements we've used to build the sched_domain and
6144 * sched_group structure so that the subsequent __free_domain_allocs()
6145 * will not free the data we're using.
6146 */
6147static void claim_allocations(int cpu, struct sched_domain *sd)
6148{
6149 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006150
6151 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6152 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6153
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006154 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006155 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006156
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006157 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6158 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006159}
6160
Peter Zijlstracb83b622012-04-17 15:49:36 +02006161#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006162static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006163enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006164static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006165int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006166static struct cpumask ***sched_domains_numa_masks;
6167static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006168#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006169
Vincent Guittot143e1e22014-04-11 11:44:37 +02006170/*
6171 * SD_flags allowed in topology descriptions.
6172 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006173 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006174 * SD_SHARE_PKG_RESOURCES - describes shared caches
6175 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006176 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006177 *
6178 * Odd one out:
6179 * SD_ASYM_PACKING - describes SMT quirks
6180 */
6181#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006182 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006183 SD_SHARE_PKG_RESOURCES | \
6184 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006185 SD_ASYM_PACKING | \
6186 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006187
6188static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006189sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006190{
6191 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006192 int sd_weight, sd_flags = 0;
6193
6194#ifdef CONFIG_NUMA
6195 /*
6196 * Ugly hack to pass state to sd_numa_mask()...
6197 */
6198 sched_domains_curr_level = tl->numa_level;
6199#endif
6200
6201 sd_weight = cpumask_weight(tl->mask(cpu));
6202
6203 if (tl->sd_flags)
6204 sd_flags = (*tl->sd_flags)();
6205 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6206 "wrong sd_flags in topology description\n"))
6207 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006208
6209 *sd = (struct sched_domain){
6210 .min_interval = sd_weight,
6211 .max_interval = 2*sd_weight,
6212 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006213 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006214
6215 .cache_nice_tries = 0,
6216 .busy_idx = 0,
6217 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006218 .newidle_idx = 0,
6219 .wake_idx = 0,
6220 .forkexec_idx = 0,
6221
6222 .flags = 1*SD_LOAD_BALANCE
6223 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006224 | 1*SD_BALANCE_EXEC
6225 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006226 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006227 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006228 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006229 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006230 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006231 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006232 | 0*SD_NUMA
6233 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006234 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006235
Peter Zijlstracb83b622012-04-17 15:49:36 +02006236 .last_balance = jiffies,
6237 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006238 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006239 .max_newidle_lb_cost = 0,
6240 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006241#ifdef CONFIG_SCHED_DEBUG
6242 .name = tl->name,
6243#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006244 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006245
6246 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006247 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006248 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006249
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006250 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittot143e1e22014-04-11 11:44:37 +02006251 sd->imbalance_pct = 110;
6252 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006253
6254 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6255 sd->imbalance_pct = 117;
6256 sd->cache_nice_tries = 1;
6257 sd->busy_idx = 2;
6258
6259#ifdef CONFIG_NUMA
6260 } else if (sd->flags & SD_NUMA) {
6261 sd->cache_nice_tries = 2;
6262 sd->busy_idx = 3;
6263 sd->idle_idx = 2;
6264
6265 sd->flags |= SD_SERIALIZE;
6266 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6267 sd->flags &= ~(SD_BALANCE_EXEC |
6268 SD_BALANCE_FORK |
6269 SD_WAKE_AFFINE);
6270 }
6271
6272#endif
6273 } else {
6274 sd->flags |= SD_PREFER_SIBLING;
6275 sd->cache_nice_tries = 1;
6276 sd->busy_idx = 2;
6277 sd->idle_idx = 1;
6278 }
6279
6280 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006281
6282 return sd;
6283}
6284
Vincent Guittot143e1e22014-04-11 11:44:37 +02006285/*
6286 * Topology list, bottom-up.
6287 */
6288static struct sched_domain_topology_level default_topology[] = {
6289#ifdef CONFIG_SCHED_SMT
6290 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6291#endif
6292#ifdef CONFIG_SCHED_MC
6293 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6294#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006295 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6296 { NULL, },
6297};
6298
6299struct sched_domain_topology_level *sched_domain_topology = default_topology;
6300
6301#define for_each_sd_topology(tl) \
6302 for (tl = sched_domain_topology; tl->mask; tl++)
6303
6304void set_sched_topology(struct sched_domain_topology_level *tl)
6305{
6306 sched_domain_topology = tl;
6307}
6308
6309#ifdef CONFIG_NUMA
6310
Peter Zijlstracb83b622012-04-17 15:49:36 +02006311static const struct cpumask *sd_numa_mask(int cpu)
6312{
6313 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6314}
6315
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006316static void sched_numa_warn(const char *str)
6317{
6318 static int done = false;
6319 int i,j;
6320
6321 if (done)
6322 return;
6323
6324 done = true;
6325
6326 printk(KERN_WARNING "ERROR: %s\n\n", str);
6327
6328 for (i = 0; i < nr_node_ids; i++) {
6329 printk(KERN_WARNING " ");
6330 for (j = 0; j < nr_node_ids; j++)
6331 printk(KERN_CONT "%02d ", node_distance(i,j));
6332 printk(KERN_CONT "\n");
6333 }
6334 printk(KERN_WARNING "\n");
6335}
6336
Rik van Riel9942f792014-10-17 03:29:49 -04006337bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006338{
6339 int i;
6340
6341 if (distance == node_distance(0, 0))
6342 return true;
6343
6344 for (i = 0; i < sched_domains_numa_levels; i++) {
6345 if (sched_domains_numa_distance[i] == distance)
6346 return true;
6347 }
6348
6349 return false;
6350}
6351
Rik van Riele3fe70b2014-10-17 03:29:50 -04006352/*
6353 * A system can have three types of NUMA topology:
6354 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6355 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6356 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6357 *
6358 * The difference between a glueless mesh topology and a backplane
6359 * topology lies in whether communication between not directly
6360 * connected nodes goes through intermediary nodes (where programs
6361 * could run), or through backplane controllers. This affects
6362 * placement of programs.
6363 *
6364 * The type of topology can be discerned with the following tests:
6365 * - If the maximum distance between any nodes is 1 hop, the system
6366 * is directly connected.
6367 * - If for two nodes A and B, located N > 1 hops away from each other,
6368 * there is an intermediary node C, which is < N hops away from both
6369 * nodes A and B, the system is a glueless mesh.
6370 */
6371static void init_numa_topology_type(void)
6372{
6373 int a, b, c, n;
6374
6375 n = sched_max_numa_distance;
6376
6377 if (n <= 1)
6378 sched_numa_topology_type = NUMA_DIRECT;
6379
6380 for_each_online_node(a) {
6381 for_each_online_node(b) {
6382 /* Find two nodes furthest removed from each other. */
6383 if (node_distance(a, b) < n)
6384 continue;
6385
6386 /* Is there an intermediary node between a and b? */
6387 for_each_online_node(c) {
6388 if (node_distance(a, c) < n &&
6389 node_distance(b, c) < n) {
6390 sched_numa_topology_type =
6391 NUMA_GLUELESS_MESH;
6392 return;
6393 }
6394 }
6395
6396 sched_numa_topology_type = NUMA_BACKPLANE;
6397 return;
6398 }
6399 }
6400}
6401
Peter Zijlstracb83b622012-04-17 15:49:36 +02006402static void sched_init_numa(void)
6403{
6404 int next_distance, curr_distance = node_distance(0, 0);
6405 struct sched_domain_topology_level *tl;
6406 int level = 0;
6407 int i, j, k;
6408
Peter Zijlstracb83b622012-04-17 15:49:36 +02006409 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6410 if (!sched_domains_numa_distance)
6411 return;
6412
6413 /*
6414 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6415 * unique distances in the node_distance() table.
6416 *
6417 * Assumes node_distance(0,j) includes all distances in
6418 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006419 */
6420 next_distance = curr_distance;
6421 for (i = 0; i < nr_node_ids; i++) {
6422 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006423 for (k = 0; k < nr_node_ids; k++) {
6424 int distance = node_distance(i, k);
6425
6426 if (distance > curr_distance &&
6427 (distance < next_distance ||
6428 next_distance == curr_distance))
6429 next_distance = distance;
6430
6431 /*
6432 * While not a strong assumption it would be nice to know
6433 * about cases where if node A is connected to B, B is not
6434 * equally connected to A.
6435 */
6436 if (sched_debug() && node_distance(k, i) != distance)
6437 sched_numa_warn("Node-distance not symmetric");
6438
6439 if (sched_debug() && i && !find_numa_distance(distance))
6440 sched_numa_warn("Node-0 not representative");
6441 }
6442 if (next_distance != curr_distance) {
6443 sched_domains_numa_distance[level++] = next_distance;
6444 sched_domains_numa_levels = level;
6445 curr_distance = next_distance;
6446 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006447 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006448
6449 /*
6450 * In case of sched_debug() we verify the above assumption.
6451 */
6452 if (!sched_debug())
6453 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006454 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006455
6456 if (!level)
6457 return;
6458
Peter Zijlstracb83b622012-04-17 15:49:36 +02006459 /*
6460 * 'level' contains the number of unique distances, excluding the
6461 * identity distance node_distance(i,i).
6462 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306463 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006464 * numbers.
6465 */
6466
Tang Chen5f7865f2012-09-25 21:12:30 +08006467 /*
6468 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6469 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6470 * the array will contain less then 'level' members. This could be
6471 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6472 * in other functions.
6473 *
6474 * We reset it to 'level' at the end of this function.
6475 */
6476 sched_domains_numa_levels = 0;
6477
Peter Zijlstracb83b622012-04-17 15:49:36 +02006478 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6479 if (!sched_domains_numa_masks)
6480 return;
6481
6482 /*
6483 * Now for each level, construct a mask per node which contains all
6484 * cpus of nodes that are that many hops away from us.
6485 */
6486 for (i = 0; i < level; i++) {
6487 sched_domains_numa_masks[i] =
6488 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6489 if (!sched_domains_numa_masks[i])
6490 return;
6491
6492 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006493 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006494 if (!mask)
6495 return;
6496
6497 sched_domains_numa_masks[i][j] = mask;
6498
6499 for (k = 0; k < nr_node_ids; k++) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006500 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006501 continue;
6502
6503 cpumask_or(mask, mask, cpumask_of_node(k));
6504 }
6505 }
6506 }
6507
Vincent Guittot143e1e22014-04-11 11:44:37 +02006508 /* Compute default topology size */
6509 for (i = 0; sched_domain_topology[i].mask; i++);
6510
Vincent Guittotc515db82014-05-13 11:11:01 +02006511 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006512 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6513 if (!tl)
6514 return;
6515
6516 /*
6517 * Copy the default topology bits..
6518 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006519 for (i = 0; sched_domain_topology[i].mask; i++)
6520 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006521
6522 /*
6523 * .. and append 'j' levels of NUMA goodness.
6524 */
6525 for (j = 0; j < level; i++, j++) {
6526 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006527 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006528 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006529 .flags = SDTL_OVERLAP,
6530 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006531 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006532 };
6533 }
6534
6535 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006536
6537 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006538 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006539
6540 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006541}
Tang Chen301a5cb2012-09-25 21:12:31 +08006542
6543static void sched_domains_numa_masks_set(int cpu)
6544{
6545 int i, j;
6546 int node = cpu_to_node(cpu);
6547
6548 for (i = 0; i < sched_domains_numa_levels; i++) {
6549 for (j = 0; j < nr_node_ids; j++) {
6550 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6551 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6552 }
6553 }
6554}
6555
6556static void sched_domains_numa_masks_clear(int cpu)
6557{
6558 int i, j;
6559 for (i = 0; i < sched_domains_numa_levels; i++) {
6560 for (j = 0; j < nr_node_ids; j++)
6561 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6562 }
6563}
6564
6565/*
6566 * Update sched_domains_numa_masks[level][node] array when new cpus
6567 * are onlined.
6568 */
6569static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6570 unsigned long action,
6571 void *hcpu)
6572{
6573 int cpu = (long)hcpu;
6574
6575 switch (action & ~CPU_TASKS_FROZEN) {
6576 case CPU_ONLINE:
6577 sched_domains_numa_masks_set(cpu);
6578 break;
6579
6580 case CPU_DEAD:
6581 sched_domains_numa_masks_clear(cpu);
6582 break;
6583
6584 default:
6585 return NOTIFY_DONE;
6586 }
6587
6588 return NOTIFY_OK;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006589}
6590#else
6591static inline void sched_init_numa(void)
6592{
6593}
Tang Chen301a5cb2012-09-25 21:12:31 +08006594
6595static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6596 unsigned long action,
6597 void *hcpu)
6598{
6599 return 0;
6600}
Peter Zijlstracb83b622012-04-17 15:49:36 +02006601#endif /* CONFIG_NUMA */
6602
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006603static int __sdt_alloc(const struct cpumask *cpu_map)
6604{
6605 struct sched_domain_topology_level *tl;
6606 int j;
6607
Viresh Kumar27723a62013-06-10 16:27:20 +05306608 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006609 struct sd_data *sdd = &tl->data;
6610
6611 sdd->sd = alloc_percpu(struct sched_domain *);
6612 if (!sdd->sd)
6613 return -ENOMEM;
6614
6615 sdd->sg = alloc_percpu(struct sched_group *);
6616 if (!sdd->sg)
6617 return -ENOMEM;
6618
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006619 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6620 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006621 return -ENOMEM;
6622
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006623 for_each_cpu(j, cpu_map) {
6624 struct sched_domain *sd;
6625 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006626 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006627
6628 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
6629 GFP_KERNEL, cpu_to_node(j));
6630 if (!sd)
6631 return -ENOMEM;
6632
6633 *per_cpu_ptr(sdd->sd, j) = sd;
6634
6635 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6636 GFP_KERNEL, cpu_to_node(j));
6637 if (!sg)
6638 return -ENOMEM;
6639
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006640 sg->next = sg;
6641
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006642 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006643
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006644 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006645 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006646 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006647 return -ENOMEM;
6648
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006649 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006650 }
6651 }
6652
6653 return 0;
6654}
6655
6656static void __sdt_free(const struct cpumask *cpu_map)
6657{
6658 struct sched_domain_topology_level *tl;
6659 int j;
6660
Viresh Kumar27723a62013-06-10 16:27:20 +05306661 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006662 struct sd_data *sdd = &tl->data;
6663
6664 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006665 struct sched_domain *sd;
6666
6667 if (sdd->sd) {
6668 sd = *per_cpu_ptr(sdd->sd, j);
6669 if (sd && (sd->flags & SD_OVERLAP))
6670 free_sched_groups(sd->groups, 0);
6671 kfree(*per_cpu_ptr(sdd->sd, j));
6672 }
6673
6674 if (sdd->sg)
6675 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006676 if (sdd->sgc)
6677 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006678 }
6679 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006680 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006681 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006682 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006683 free_percpu(sdd->sgc);
6684 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006685 }
6686}
6687
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006688struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306689 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6690 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006691{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006692 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006693 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006694 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006695
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006696 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006697 if (child) {
6698 sd->level = child->level + 1;
6699 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006700 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306701 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006702
6703 if (!cpumask_subset(sched_domain_span(child),
6704 sched_domain_span(sd))) {
6705 pr_err("BUG: arch topology borken\n");
6706#ifdef CONFIG_SCHED_DEBUG
6707 pr_err(" the %s domain not a subset of the %s domain\n",
6708 child->name, sd->name);
6709#endif
6710 /* Fixup, ensure @sd has at least @child cpus. */
6711 cpumask_or(sched_domain_span(sd),
6712 sched_domain_span(sd),
6713 sched_domain_span(child));
6714 }
6715
Peter Zijlstra60495e72011-04-07 14:10:04 +02006716 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006717 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006718
6719 return sd;
6720}
6721
Mike Travis7c16ec52008-04-04 18:11:11 -07006722/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006723 * Build sched domains for a given set of cpus and attach the sched domains
6724 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006726static int build_sched_domains(const struct cpumask *cpu_map,
6727 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728{
Viresh Kumar1c632162013-06-10 16:27:18 +05306729 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006730 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006731 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006732 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306733
Andreas Herrmann2109b992009-08-18 12:53:00 +02006734 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6735 if (alloc_state != sa_rootdomain)
6736 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006737
Peter Zijlstradce840a2011-04-07 14:09:50 +02006738 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306739 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006740 struct sched_domain_topology_level *tl;
6741
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006742 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306743 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306744 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306745 if (tl == sched_domain_topology)
6746 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006747 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6748 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006749 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6750 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006751 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006752 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006753
Peter Zijlstradce840a2011-04-07 14:09:50 +02006754 /* Build the groups for the domains */
6755 for_each_cpu(i, cpu_map) {
6756 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6757 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006758 if (sd->flags & SD_OVERLAP) {
6759 if (build_overlap_sched_groups(sd, i))
6760 goto error;
6761 } else {
6762 if (build_sched_groups(sd, i))
6763 goto error;
6764 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006765 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Nicolas Pitreced549f2014-05-26 18:19:38 -04006768 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006769 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6770 if (!cpumask_test_cpu(i, cpu_map))
6771 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Peter Zijlstradce840a2011-04-07 14:09:50 +02006773 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6774 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006775 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006776 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006777 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006778
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006780 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306781 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006782 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006783 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006785 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006786
Peter Zijlstra822ff792011-04-07 14:09:51 +02006787 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006788error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02006789 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02006790 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791}
Paul Jackson029190c2007-10-18 23:40:20 -07006792
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306793static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07006794static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02006795static struct sched_domain_attr *dattr_cur;
6796 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07006797
6798/*
6799 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10306800 * cpumask) fails, then fallback to a single sched domain,
6801 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07006802 */
Rusty Russell42128232008-11-25 02:35:12 +10306803static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07006804
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006805/*
6806 * arch_update_cpu_topology lets virtualized architectures update the
6807 * cpu core maps. It is supposed to return 1 if the topology changed
6808 * or 0 if it stayed the same.
6809 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006810int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01006811{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006812 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01006813}
6814
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306815cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
6816{
6817 int i;
6818 cpumask_var_t *doms;
6819
6820 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
6821 if (!doms)
6822 return NULL;
6823 for (i = 0; i < ndoms; i++) {
6824 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
6825 free_sched_domains(doms, i);
6826 return NULL;
6827 }
6828 }
6829 return doms;
6830}
6831
6832void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
6833{
6834 unsigned int i;
6835 for (i = 0; i < ndoms; i++)
6836 free_cpumask_var(doms[i]);
6837 kfree(doms);
6838}
6839
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006840/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006841 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006842 * For now this just excludes isolated cpus, but could be used to
6843 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006844 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02006845static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006846{
Milton Miller73785472007-10-24 18:23:48 +02006847 int err;
6848
Heiko Carstens22e52b02008-03-12 18:31:59 +01006849 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006850 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306851 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07006852 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306853 doms_cur = &fallback_doms;
6854 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006855 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02006856 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006857
6858 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006859}
6860
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006861/*
6862 * Detach sched domains from a group of cpus specified in cpu_map
6863 * These cpus will now be attached to the NULL domain
6864 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306865static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006866{
6867 int i;
6868
Peter Zijlstradce840a2011-04-07 14:09:50 +02006869 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306870 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006871 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006872 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006873}
6874
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006875/* handle null as "default" */
6876static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
6877 struct sched_domain_attr *new, int idx_new)
6878{
6879 struct sched_domain_attr tmp;
6880
6881 /* fast path */
6882 if (!new && !cur)
6883 return 1;
6884
6885 tmp = SD_ATTR_INIT;
6886 return !memcmp(cur ? (cur + idx_cur) : &tmp,
6887 new ? (new + idx_new) : &tmp,
6888 sizeof(struct sched_domain_attr));
6889}
6890
Paul Jackson029190c2007-10-18 23:40:20 -07006891/*
6892 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006893 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006894 * doms_new[] to the current sched domain partitioning, doms_cur[].
6895 * It destroys each deleted domain and builds each new domain.
6896 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306897 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006898 * The masks don't intersect (don't overlap.) We should setup one
6899 * sched domain for each mask. CPUs not in any of the cpumasks will
6900 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006901 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6902 * it as it is.
6903 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306904 * The passed in 'doms_new' should be allocated using
6905 * alloc_sched_domains. This routine takes ownership of it and will
6906 * free_sched_domains it when done with it. If the caller failed the
6907 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
6908 * and partition_sched_domains() will fallback to the single partition
6909 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07006910 *
Rusty Russell96f874e22008-11-25 02:35:14 +10306911 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08006912 * ndoms_new == 0 is a special case for destroying existing domains,
6913 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006914 *
Paul Jackson029190c2007-10-18 23:40:20 -07006915 * Call with hotplug lock held
6916 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306917void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006918 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07006919{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006920 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006921 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07006922
Heiko Carstens712555e2008-04-28 11:33:07 +02006923 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006924
Milton Miller73785472007-10-24 18:23:48 +02006925 /* always unregister in case we don't destroy any domains */
6926 unregister_sched_domain_sysctl();
6927
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006928 /* Let architecture update cpu core mappings. */
6929 new_topology = arch_update_cpu_topology();
6930
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006931 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07006932
6933 /* Destroy deleted domains */
6934 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006935 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306936 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006937 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006938 goto match1;
6939 }
6940 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306941 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07006942match1:
6943 ;
6944 }
6945
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006946 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07006947 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006948 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306949 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006950 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08006951 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07006952 }
6953
Paul Jackson029190c2007-10-18 23:40:20 -07006954 /* Build new domains */
6955 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006956 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306957 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006958 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006959 goto match2;
6960 }
6961 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006962 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07006963match2:
6964 ;
6965 }
6966
6967 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306968 if (doms_cur != &fallback_doms)
6969 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006970 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07006971 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006972 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07006973 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02006974
6975 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006976
Heiko Carstens712555e2008-04-28 11:33:07 +02006977 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07006978}
6979
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306980static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
6981
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982/*
Tejun Heo3a101d02010-06-08 21:40:36 +02006983 * Update cpusets according to cpu_active mask. If cpusets are
6984 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
6985 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306986 *
6987 * If we come here as part of a suspend/resume, don't touch cpusets because we
6988 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02006990static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
6991 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306993 switch (action) {
6994 case CPU_ONLINE_FROZEN:
6995 case CPU_DOWN_FAILED_FROZEN:
6996
6997 /*
6998 * num_cpus_frozen tracks how many CPUs are involved in suspend
6999 * resume sequence. As long as this is not the last online
7000 * operation in the resume sequence, just build a single sched
7001 * domain, ignoring cpusets.
7002 */
7003 num_cpus_frozen--;
7004 if (likely(num_cpus_frozen)) {
7005 partition_sched_domains(1, NULL, NULL);
7006 break;
7007 }
7008
7009 /*
7010 * This is the last CPU online operation. So fall through and
7011 * restore the original sched domains by considering the
7012 * cpuset configurations.
7013 */
7014
Max Krasnyanskye761b772008-07-15 04:43:49 -07007015 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007016 case CPU_DOWN_FAILED:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307017 cpuset_update_active_cpus(true);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307018 break;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007019 default:
7020 return NOTIFY_DONE;
7021 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307022 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007023}
Tejun Heo3a101d02010-06-08 21:40:36 +02007024
Tejun Heo0b2e9182010-06-21 23:53:31 +02007025static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7026 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007027{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307028 switch (action) {
Tejun Heo3a101d02010-06-08 21:40:36 +02007029 case CPU_DOWN_PREPARE:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307030 cpuset_update_active_cpus(false);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307031 break;
7032 case CPU_DOWN_PREPARE_FROZEN:
7033 num_cpus_frozen++;
7034 partition_sched_domains(1, NULL, NULL);
7035 break;
Tejun Heo3a101d02010-06-08 21:40:36 +02007036 default:
7037 return NOTIFY_DONE;
7038 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307039 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02007040}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007041
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042void __init sched_init_smp(void)
7043{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307044 cpumask_var_t non_isolated_cpus;
7045
7046 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007047 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007048
Peter Zijlstracb83b622012-04-17 15:49:36 +02007049 sched_init_numa();
7050
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007051 /*
7052 * There's no userspace yet to cause hotplug operations; hence all the
7053 * cpu masks are stable and all blatant races in the below code cannot
7054 * happen.
7055 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007056 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007057 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307058 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7059 if (cpumask_empty(non_isolated_cpus))
7060 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007061 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007062
Tang Chen301a5cb2012-09-25 21:12:31 +08007063 hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
Tejun Heo3a101d02010-06-08 21:40:36 +02007064 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7065 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007066
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007067 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007068
7069 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307070 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007071 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007072 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307073 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307074
Rusty Russell0e3900e2008-11-25 02:35:13 +10307075 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007076 init_sched_dl_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077}
7078#else
7079void __init sched_init_smp(void)
7080{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007081 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082}
7083#endif /* CONFIG_SMP */
7084
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307085const_debug unsigned int sysctl_timer_migration = 1;
7086
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087int in_sched_functions(unsigned long addr)
7088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 return in_lock_functions(addr) ||
7090 (addr >= (unsigned long)__sched_text_start
7091 && addr < (unsigned long)__sched_text_end);
7092}
7093
Peter Zijlstra029632f2011-10-25 10:00:11 +02007094#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007095/*
7096 * Default task group.
7097 * Every task in system belongs to this group at bootup.
7098 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007099struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007100LIST_HEAD(task_groups);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007101#endif
7102
Joonsoo Kime6252c32013-04-23 17:27:41 +09007103DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007104
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105void __init sched_init(void)
7106{
Ingo Molnardd41f592007-07-09 18:51:59 +02007107 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007108 unsigned long alloc_size = 0, ptr;
7109
7110#ifdef CONFIG_FAIR_GROUP_SCHED
7111 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7112#endif
7113#ifdef CONFIG_RT_GROUP_SCHED
7114 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7115#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007116 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007117 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007118
7119#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007120 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007121 ptr += nr_cpu_ids * sizeof(void **);
7122
Yong Zhang07e06b02011-01-07 15:17:36 +08007123 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007124 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007125
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007126#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007127#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007128 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007129 ptr += nr_cpu_ids * sizeof(void **);
7130
Yong Zhang07e06b02011-01-07 15:17:36 +08007131 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007132 ptr += nr_cpu_ids * sizeof(void **);
7133
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007134#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007135 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007136#ifdef CONFIG_CPUMASK_OFFSTACK
7137 for_each_possible_cpu(i) {
7138 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7139 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7140 }
7141#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007142
Dario Faggioli332ac172013-11-07 14:43:45 +01007143 init_rt_bandwidth(&def_rt_bandwidth,
7144 global_rt_period(), global_rt_runtime());
7145 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007146 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007147
Gregory Haskins57d885f2008-01-25 21:08:18 +01007148#ifdef CONFIG_SMP
7149 init_defrootdomain();
7150#endif
7151
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007152#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007153 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007154 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007155#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007156
Dhaval Giani7c941432010-01-20 13:26:18 +01007157#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007158 list_add(&root_task_group.list, &task_groups);
7159 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007160 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007161 autogroup_init(&init_task);
Glauber Costa54c707e2011-11-28 14:45:19 -02007162
Dhaval Giani7c941432010-01-20 13:26:18 +01007163#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007164
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007165 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007166 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
7168 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007169 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007170 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007171 rq->calc_load_active = 0;
7172 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007173 init_cfs_rq(&rq->cfs);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007174 init_rt_rq(&rq->rt, rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01007175 init_dl_rq(&rq->dl, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007176#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007177 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007178 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007179 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007180 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007181 *
7182 * In case of task-groups formed thr' the cgroup filesystem, it
7183 * gets 100% of the cpu resources in the system. This overall
7184 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007185 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007186 * based on each entity's (task or task-group's) weight
7187 * (se->load.weight).
7188 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007189 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007190 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7191 * then A0's share of the cpu resource is:
7192 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007193 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007194 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007195 * We achieve this by letting root_task_group's tasks sit
7196 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007197 */
Paul Turnerab84d312011-07-21 09:43:28 -07007198 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007199 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007200#endif /* CONFIG_FAIR_GROUP_SCHED */
7201
7202 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007203#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007204 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
Ingo Molnardd41f592007-07-09 18:51:59 +02007207 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7208 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007209
7210 rq->last_load_update_tick = jiffies;
7211
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007213 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007214 rq->rd = NULL;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007215 rq->cpu_capacity = SCHED_CAPACITY_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04007216 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007218 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007220 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007221 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007222 rq->idle_stamp = 0;
7223 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007224 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007225
7226 INIT_LIST_HEAD(&rq->cfs_tasks);
7227
Gregory Haskinsdc938522008-01-25 21:08:26 +01007228 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007229#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007230 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007231#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007232#ifdef CONFIG_NO_HZ_FULL
7233 rq->last_sched_tick = 0;
7234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007236 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 }
7239
Peter Williams2dd73a42006-06-27 02:54:34 -07007240 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007241
Avi Kivitye107be32007-07-26 13:40:43 +02007242#ifdef CONFIG_PREEMPT_NOTIFIERS
7243 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7244#endif
7245
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 /*
7247 * The boot idle thread does lazy MMU switching as well:
7248 */
7249 atomic_inc(&init_mm.mm_count);
7250 enter_lazy_tlb(&init_mm, current);
7251
7252 /*
7253 * Make us the idle thread. Technically, schedule() should not be
7254 * called from this thread, however somewhere below it might be,
7255 * but because we are the idle thread, we just pick up running again
7256 * when this runqueue becomes "idle".
7257 */
7258 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007259
7260 calc_load_update = jiffies + LOAD_FREQ;
7261
Ingo Molnardd41f592007-07-09 18:51:59 +02007262 /*
7263 * During early bootup we pretend to be a normal task:
7264 */
7265 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007266
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307267#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007268 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307269 /* May be allocated at isolcpus cmdline parse time */
7270 if (cpu_isolated_map == NULL)
7271 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007272 idle_thread_set_boot_cpu();
Corey Minyarda803f022014-05-08 13:47:39 -05007273 set_cpu_rq_start_time();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007274#endif
7275 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307276
Ingo Molnar6892b752008-02-13 14:02:36 +01007277 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278}
7279
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007280#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007281static inline int preempt_count_equals(int preempt_offset)
7282{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007283 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007284
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007285 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007286}
7287
Simon Kagstromd8948372009-12-23 11:08:18 +01007288void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007290 /*
7291 * Blocking primitives will set (and therefore destroy) current->state,
7292 * since we will exit with TASK_RUNNING make sure we enter with it,
7293 * otherwise we will destroy state.
7294 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007295 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007296 "do not call blocking ops when !TASK_RUNNING; "
7297 "state=%lx set at [<%p>] %pS\n",
7298 current->state,
7299 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007300 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007301
Peter Zijlstra34274452014-09-24 10:18:56 +02007302 ___might_sleep(file, line, preempt_offset);
7303}
7304EXPORT_SYMBOL(__might_sleep);
7305
7306void ___might_sleep(const char *file, int line, int preempt_offset)
7307{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 static unsigned long prev_jiffy; /* ratelimiting */
7309
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007310 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007311 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7312 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007313 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007314 return;
7315 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7316 return;
7317 prev_jiffy = jiffies;
7318
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007319 printk(KERN_ERR
7320 "BUG: sleeping function called from invalid context at %s:%d\n",
7321 file, line);
7322 printk(KERN_ERR
7323 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7324 in_atomic(), irqs_disabled(),
7325 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007326
7327 debug_show_held_locks(current);
7328 if (irqs_disabled())
7329 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007330#ifdef CONFIG_DEBUG_PREEMPT
7331 if (!preempt_count_equals(preempt_offset)) {
7332 pr_err("Preemption disabled at:");
7333 print_ip_sym(current->preempt_disable_ip);
7334 pr_cont("\n");
7335 }
7336#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007337 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338}
Peter Zijlstra34274452014-09-24 10:18:56 +02007339EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340#endif
7341
7342#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007343static void normalize_task(struct rq *rq, struct task_struct *p)
7344{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007345 const struct sched_class *prev_class = p->sched_class;
Dario Faggiolid50dde52013-11-07 14:43:36 +01007346 struct sched_attr attr = {
7347 .sched_policy = SCHED_NORMAL,
7348 };
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007349 int old_prio = p->prio;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007350 int queued;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007351
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007352 queued = task_on_rq_queued(p);
7353 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01007354 dequeue_task(rq, p, 0);
Dario Faggiolid50dde52013-11-07 14:43:36 +01007355 __setscheduler(rq, p, &attr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007356 if (queued) {
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01007357 enqueue_task(rq, p, 0);
Kirill Tkhai88751252014-06-29 00:03:57 +04007358 resched_curr(rq);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007359 }
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007360
7361 check_class_changed(rq, p, prev_class, old_prio);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007362}
7363
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364void normalize_rt_tasks(void)
7365{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007366 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007368 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007370 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007371 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007372 /*
7373 * Only normalize user tasks:
7374 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007375 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007376 continue;
7377
Ingo Molnardd41f592007-07-09 18:51:59 +02007378 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007379#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007380 p->se.statistics.wait_start = 0;
7381 p->se.statistics.sleep_start = 0;
7382 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007383#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007384
Dario Faggioliaab03e02013-11-28 11:14:43 +01007385 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007386 /*
7387 * Renice negative nice level userspace
7388 * tasks back to 0:
7389 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007390 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007391 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007395 rq = task_rq_lock(p, &flags);
Ingo Molnar178be792007-10-15 17:00:18 +02007396 normalize_task(rq, p);
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007397 task_rq_unlock(rq, p, &flags);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007398 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007399 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400}
7401
7402#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007403
Jason Wessel67fc4e02010-05-20 21:04:21 -05007404#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007405/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007406 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007407 *
7408 * They can only be called when the whole system has been
7409 * stopped - every CPU needs to be quiescent, and no scheduling
7410 * activity can take place. Using them for anything else would
7411 * be a serious bug, and as a result, they aren't even visible
7412 * under any other configuration.
7413 */
7414
7415/**
7416 * curr_task - return the current task for a given cpu.
7417 * @cpu: the processor in question.
7418 *
7419 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007420 *
7421 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007422 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007423struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007424{
7425 return cpu_curr(cpu);
7426}
7427
Jason Wessel67fc4e02010-05-20 21:04:21 -05007428#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7429
7430#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007431/**
7432 * set_curr_task - set the current task for a given cpu.
7433 * @cpu: the processor in question.
7434 * @p: the task pointer to set.
7435 *
7436 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007437 * are serviced on a separate stack. It allows the architecture to switch the
7438 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007439 * must be called with all CPU's synchronized, and interrupts disabled, the
7440 * and caller must save the original value of the current task (see
7441 * curr_task() above) and restore that value before reenabling interrupts and
7442 * re-starting the system.
7443 *
7444 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7445 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007446void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007447{
7448 cpu_curr(cpu) = p;
7449}
7450
7451#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007452
Dhaval Giani7c941432010-01-20 13:26:18 +01007453#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007454/* task_group_lock serializes the addition/removal of task groups */
7455static DEFINE_SPINLOCK(task_group_lock);
7456
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007457static void free_sched_group(struct task_group *tg)
7458{
7459 free_fair_sched_group(tg);
7460 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007461 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007462 kfree(tg);
7463}
7464
7465/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007466struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007467{
7468 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007469
7470 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7471 if (!tg)
7472 return ERR_PTR(-ENOMEM);
7473
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007474 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007475 goto err;
7476
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007477 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007478 goto err;
7479
Li Zefanace783b2013-01-24 14:30:48 +08007480 return tg;
7481
7482err:
7483 free_sched_group(tg);
7484 return ERR_PTR(-ENOMEM);
7485}
7486
7487void sched_online_group(struct task_group *tg, struct task_group *parent)
7488{
7489 unsigned long flags;
7490
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007491 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007492 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007493
7494 WARN_ON(!parent); /* root should already exist */
7495
7496 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007497 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007498 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007499 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007500}
7501
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007502/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007503static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007504{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007505 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007506 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007507}
7508
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007509/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007510void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007511{
Li Zefanace783b2013-01-24 14:30:48 +08007512 /* wait for possible concurrent references to cfs_rqs complete */
7513 call_rcu(&tg->rcu, free_sched_group_rcu);
7514}
7515
7516void sched_offline_group(struct task_group *tg)
7517{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007518 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007519 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007520
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007521 /* end participation in shares distribution */
7522 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007523 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007524
7525 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007526 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007527 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007528 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007529}
7530
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007531/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007532 * The caller of this function should have put the task in its new group
7533 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7534 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007535 */
7536void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007537{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007538 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007539 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007540 unsigned long flags;
7541 struct rq *rq;
7542
7543 rq = task_rq_lock(tsk, &flags);
7544
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007545 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007546 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007547
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007548 if (queued)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007549 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007550 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007551 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007552
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007553 /*
7554 * All callers are synchronized by task_rq_lock(); we do not use RCU
7555 * which is pointless here. Thus, we pass "true" to task_css_check()
7556 * to prevent lockdep warnings.
7557 */
7558 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007559 struct task_group, css);
7560 tg = autogroup_task_group(tsk, tg);
7561 tsk->sched_task_group = tg;
7562
Peter Zijlstra810b3812008-02-29 15:21:01 -05007563#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007564 if (tsk->sched_class->task_move_group)
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007565 tsk->sched_class->task_move_group(tsk, queued);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007566 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007567#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007568 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007569
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007570 if (unlikely(running))
7571 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007572 if (queued)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01007573 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007574
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007575 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007576}
Dhaval Giani7c941432010-01-20 13:26:18 +01007577#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007578
Paul Turnera790de92011-07-21 09:43:29 -07007579#ifdef CONFIG_RT_GROUP_SCHED
7580/*
7581 * Ensure that the real time constraints are schedulable.
7582 */
7583static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007584
Dhaval Giani521f1a242008-02-28 15:21:56 +05307585/* Must be called with tasklist_lock held */
7586static inline int tg_has_rt_tasks(struct task_group *tg)
7587{
7588 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007589
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007590 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007591 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307592 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007593 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007594
Dhaval Giani521f1a242008-02-28 15:21:56 +05307595 return 0;
7596}
7597
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007598struct rt_schedulable_data {
7599 struct task_group *tg;
7600 u64 rt_period;
7601 u64 rt_runtime;
7602};
7603
Paul Turnera790de92011-07-21 09:43:29 -07007604static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007605{
7606 struct rt_schedulable_data *d = data;
7607 struct task_group *child;
7608 unsigned long total, sum = 0;
7609 u64 period, runtime;
7610
7611 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7612 runtime = tg->rt_bandwidth.rt_runtime;
7613
7614 if (tg == d->tg) {
7615 period = d->rt_period;
7616 runtime = d->rt_runtime;
7617 }
7618
Peter Zijlstra4653f802008-09-23 15:33:44 +02007619 /*
7620 * Cannot have more runtime than the period.
7621 */
7622 if (runtime > period && runtime != RUNTIME_INF)
7623 return -EINVAL;
7624
7625 /*
7626 * Ensure we don't starve existing RT tasks.
7627 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007628 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7629 return -EBUSY;
7630
7631 total = to_ratio(period, runtime);
7632
Peter Zijlstra4653f802008-09-23 15:33:44 +02007633 /*
7634 * Nobody can have more than the global setting allows.
7635 */
7636 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7637 return -EINVAL;
7638
7639 /*
7640 * The sum of our children's runtime should not exceed our own.
7641 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007642 list_for_each_entry_rcu(child, &tg->children, siblings) {
7643 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7644 runtime = child->rt_bandwidth.rt_runtime;
7645
7646 if (child == d->tg) {
7647 period = d->rt_period;
7648 runtime = d->rt_runtime;
7649 }
7650
7651 sum += to_ratio(period, runtime);
7652 }
7653
7654 if (sum > total)
7655 return -EINVAL;
7656
7657 return 0;
7658}
7659
7660static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7661{
Paul Turner82774342011-07-21 09:43:35 -07007662 int ret;
7663
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007664 struct rt_schedulable_data data = {
7665 .tg = tg,
7666 .rt_period = period,
7667 .rt_runtime = runtime,
7668 };
7669
Paul Turner82774342011-07-21 09:43:35 -07007670 rcu_read_lock();
7671 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7672 rcu_read_unlock();
7673
7674 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007675}
7676
Paul Turnerab84d312011-07-21 09:43:28 -07007677static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007678 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007679{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007680 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007681
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007682 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307683 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007684 err = __rt_schedulable(tg, rt_period, rt_runtime);
7685 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307686 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007687
Thomas Gleixner0986b112009-11-17 15:32:06 +01007688 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007689 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7690 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007691
7692 for_each_possible_cpu(i) {
7693 struct rt_rq *rt_rq = tg->rt_rq[i];
7694
Thomas Gleixner0986b112009-11-17 15:32:06 +01007695 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007696 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007697 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007698 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007699 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007700unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307701 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007702 mutex_unlock(&rt_constraints_mutex);
7703
7704 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007705}
7706
Li Zefan25cc7da2013-03-05 16:07:33 +08007707static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007708{
7709 u64 rt_runtime, rt_period;
7710
7711 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7712 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7713 if (rt_runtime_us < 0)
7714 rt_runtime = RUNTIME_INF;
7715
Paul Turnerab84d312011-07-21 09:43:28 -07007716 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007717}
7718
Li Zefan25cc7da2013-03-05 16:07:33 +08007719static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007720{
7721 u64 rt_runtime_us;
7722
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007723 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007724 return -1;
7725
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007726 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007727 do_div(rt_runtime_us, NSEC_PER_USEC);
7728 return rt_runtime_us;
7729}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007730
Li Zefan25cc7da2013-03-05 16:07:33 +08007731static int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007732{
7733 u64 rt_runtime, rt_period;
7734
7735 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
7736 rt_runtime = tg->rt_bandwidth.rt_runtime;
7737
Raistlin619b0482008-06-26 18:54:09 +02007738 if (rt_period == 0)
7739 return -EINVAL;
7740
Paul Turnerab84d312011-07-21 09:43:28 -07007741 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007742}
7743
Li Zefan25cc7da2013-03-05 16:07:33 +08007744static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007745{
7746 u64 rt_period_us;
7747
7748 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
7749 do_div(rt_period_us, NSEC_PER_USEC);
7750 return rt_period_us;
7751}
Dario Faggioli332ac172013-11-07 14:43:45 +01007752#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007753
Dario Faggioli332ac172013-11-07 14:43:45 +01007754#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007755static int sched_rt_global_constraints(void)
7756{
7757 int ret = 0;
7758
7759 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007760 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02007761 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007762 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007763 mutex_unlock(&rt_constraints_mutex);
7764
7765 return ret;
7766}
Dhaval Giani54e99122009-02-27 15:13:54 +05307767
Li Zefan25cc7da2013-03-05 16:07:33 +08007768static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05307769{
7770 /* Don't accept realtime tasks when there is no way for them to run */
7771 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
7772 return 0;
7773
7774 return 1;
7775}
7776
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007777#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007778static int sched_rt_global_constraints(void)
7779{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007780 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007781 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07007782
Thomas Gleixner0986b112009-11-17 15:32:06 +01007783 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007784 for_each_possible_cpu(i) {
7785 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
7786
Thomas Gleixner0986b112009-11-17 15:32:06 +01007787 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007788 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01007789 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007790 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007791 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007792
Dario Faggioli332ac172013-11-07 14:43:45 +01007793 return ret;
7794}
7795#endif /* CONFIG_RT_GROUP_SCHED */
7796
Dario Faggioli332ac172013-11-07 14:43:45 +01007797static int sched_dl_global_constraints(void)
7798{
Peter Zijlstra17248132013-12-17 12:44:49 +01007799 u64 runtime = global_rt_runtime();
7800 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01007801 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007802 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007803 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01007804 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007805
7806 /*
7807 * Here we want to check the bandwidth not being set to some
7808 * value smaller than the currently allocated bandwidth in
7809 * any of the root_domains.
7810 *
7811 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
7812 * cycling on root_domains... Discussion on different/better
7813 * solutions is welcome!
7814 */
Peter Zijlstra17248132013-12-17 12:44:49 +01007815 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007816 rcu_read_lock_sched();
7817 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01007818
Juri Lelli49516342014-02-11 09:24:27 +01007819 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007820 if (new_bw < dl_b->total_bw)
7821 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01007822 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007823
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007824 rcu_read_unlock_sched();
7825
Peter Zijlstra17248132013-12-17 12:44:49 +01007826 if (ret)
7827 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01007828 }
7829
Peter Zijlstra17248132013-12-17 12:44:49 +01007830 return ret;
7831}
7832
7833static void sched_dl_do_global(void)
7834{
7835 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007836 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007837 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01007838 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01007839
7840 def_dl_bandwidth.dl_period = global_rt_period();
7841 def_dl_bandwidth.dl_runtime = global_rt_runtime();
7842
7843 if (global_rt_runtime() != RUNTIME_INF)
7844 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
7845
7846 /*
7847 * FIXME: As above...
7848 */
7849 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007850 rcu_read_lock_sched();
7851 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01007852
Juri Lelli49516342014-02-11 09:24:27 +01007853 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007854 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01007855 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007856
7857 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01007858 }
7859}
7860
7861static int sched_rt_global_validate(void)
7862{
7863 if (sysctl_sched_rt_period <= 0)
7864 return -EINVAL;
7865
Juri Lellie9e7cb32014-02-11 09:24:26 +01007866 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
7867 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01007868 return -EINVAL;
7869
Dario Faggioli332ac172013-11-07 14:43:45 +01007870 return 0;
7871}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007872
Peter Zijlstra17248132013-12-17 12:44:49 +01007873static void sched_rt_do_global(void)
7874{
7875 def_rt_bandwidth.rt_runtime = global_rt_runtime();
7876 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
7877}
7878
7879int sched_rt_handler(struct ctl_table *table, int write,
7880 void __user *buffer, size_t *lenp,
7881 loff_t *ppos)
7882{
7883 int old_period, old_runtime;
7884 static DEFINE_MUTEX(mutex);
7885 int ret;
7886
7887 mutex_lock(&mutex);
7888 old_period = sysctl_sched_rt_period;
7889 old_runtime = sysctl_sched_rt_runtime;
7890
7891 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7892
7893 if (!ret && write) {
7894 ret = sched_rt_global_validate();
7895 if (ret)
7896 goto undo;
7897
7898 ret = sched_rt_global_constraints();
7899 if (ret)
7900 goto undo;
7901
7902 ret = sched_dl_global_constraints();
7903 if (ret)
7904 goto undo;
7905
7906 sched_rt_do_global();
7907 sched_dl_do_global();
7908 }
7909 if (0) {
7910undo:
7911 sysctl_sched_rt_period = old_period;
7912 sysctl_sched_rt_runtime = old_runtime;
7913 }
7914 mutex_unlock(&mutex);
7915
7916 return ret;
7917}
7918
Clark Williamsce0dbbb2013-02-07 09:47:04 -06007919int sched_rr_handler(struct ctl_table *table, int write,
7920 void __user *buffer, size_t *lenp,
7921 loff_t *ppos)
7922{
7923 int ret;
7924 static DEFINE_MUTEX(mutex);
7925
7926 mutex_lock(&mutex);
7927 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7928 /* make sure that internally we keep jiffies */
7929 /* also, writing zero resets timeslice to default */
7930 if (!ret && write) {
7931 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
7932 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
7933 }
7934 mutex_unlock(&mutex);
7935 return ret;
7936}
7937
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007938#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007939
Tejun Heoa7c6d552013-08-08 20:11:23 -04007940static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007941{
Tejun Heoa7c6d552013-08-08 20:11:23 -04007942 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007943}
7944
Tejun Heoeb954192013-08-08 20:11:23 -04007945static struct cgroup_subsys_state *
7946cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007947{
Tejun Heoeb954192013-08-08 20:11:23 -04007948 struct task_group *parent = css_tg(parent_css);
7949 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007950
Tejun Heoeb954192013-08-08 20:11:23 -04007951 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007952 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08007953 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007954 }
7955
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007956 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007957 if (IS_ERR(tg))
7958 return ERR_PTR(-ENOMEM);
7959
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007960 return &tg->css;
7961}
7962
Tejun Heoeb954192013-08-08 20:11:23 -04007963static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007964{
Tejun Heoeb954192013-08-08 20:11:23 -04007965 struct task_group *tg = css_tg(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04007966 struct task_group *parent = css_tg(css->parent);
Li Zefanace783b2013-01-24 14:30:48 +08007967
Tejun Heo63876982013-08-08 20:11:23 -04007968 if (parent)
7969 sched_online_group(tg, parent);
Li Zefanace783b2013-01-24 14:30:48 +08007970 return 0;
7971}
7972
Tejun Heoeb954192013-08-08 20:11:23 -04007973static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007974{
Tejun Heoeb954192013-08-08 20:11:23 -04007975 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007976
7977 sched_destroy_group(tg);
7978}
7979
Tejun Heoeb954192013-08-08 20:11:23 -04007980static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007981{
Tejun Heoeb954192013-08-08 20:11:23 -04007982 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08007983
7984 sched_offline_group(tg);
7985}
7986
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007987static void cpu_cgroup_fork(struct task_struct *task)
7988{
7989 sched_move_task(task);
7990}
7991
Tejun Heoeb954192013-08-08 20:11:23 -04007992static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007993 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007994{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007995 struct task_struct *task;
7996
Tejun Heo924f0d9a2014-02-13 06:58:41 -05007997 cgroup_taskset_for_each(task, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007998#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04007999 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008000 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008001#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008002 /* We don't support RT-tasks being in separate groups */
8003 if (task->sched_class != &fair_sched_class)
8004 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008005#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008006 }
Ben Blumbe367d02009-09-23 15:56:31 -07008007 return 0;
8008}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008009
Tejun Heoeb954192013-08-08 20:11:23 -04008010static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008011 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008012{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008013 struct task_struct *task;
8014
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008015 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008016 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008017}
8018
Tejun Heoeb954192013-08-08 20:11:23 -04008019static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
8020 struct cgroup_subsys_state *old_css,
8021 struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008022{
8023 /*
8024 * cgroup_exit() is called in the copy_process() failure path.
8025 * Ignore this case since the task hasn't ran yet, this avoids
8026 * trying to poke a half freed task state from generic code.
8027 */
8028 if (!(task->flags & PF_EXITING))
8029 return;
8030
8031 sched_move_task(task);
8032}
8033
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008034#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008035static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8036 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008037{
Tejun Heo182446d2013-08-08 20:11:24 -04008038 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008039}
8040
Tejun Heo182446d2013-08-08 20:11:24 -04008041static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8042 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008043{
Tejun Heo182446d2013-08-08 20:11:24 -04008044 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008045
Nikhil Raoc8b28112011-05-18 14:37:48 -07008046 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008047}
Paul Turnerab84d312011-07-21 09:43:28 -07008048
8049#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008050static DEFINE_MUTEX(cfs_constraints_mutex);
8051
Paul Turnerab84d312011-07-21 09:43:28 -07008052const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8053const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8054
Paul Turnera790de92011-07-21 09:43:29 -07008055static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8056
Paul Turnerab84d312011-07-21 09:43:28 -07008057static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8058{
Paul Turner56f570e2011-11-07 20:26:33 -08008059 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008060 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008061
8062 if (tg == &root_task_group)
8063 return -EINVAL;
8064
8065 /*
8066 * Ensure we have at some amount of bandwidth every period. This is
8067 * to prevent reaching a state of large arrears when throttled via
8068 * entity_tick() resulting in prolonged exit starvation.
8069 */
8070 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8071 return -EINVAL;
8072
8073 /*
8074 * Likewise, bound things on the otherside by preventing insane quota
8075 * periods. This also allows us to normalize in computing quota
8076 * feasibility.
8077 */
8078 if (period > max_cfs_quota_period)
8079 return -EINVAL;
8080
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008081 /*
8082 * Prevent race between setting of cfs_rq->runtime_enabled and
8083 * unthrottle_offline_cfs_rqs().
8084 */
8085 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008086 mutex_lock(&cfs_constraints_mutex);
8087 ret = __cfs_schedulable(tg, period, quota);
8088 if (ret)
8089 goto out_unlock;
8090
Paul Turner58088ad2011-07-21 09:43:31 -07008091 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008092 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008093 /*
8094 * If we need to toggle cfs_bandwidth_used, off->on must occur
8095 * before making related changes, and on->off must occur afterwards
8096 */
8097 if (runtime_enabled && !runtime_was_enabled)
8098 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008099 raw_spin_lock_irq(&cfs_b->lock);
8100 cfs_b->period = ns_to_ktime(period);
8101 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008102
Paul Turnera9cf55b2011-07-21 09:43:32 -07008103 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008104 /* restart the period timer (if active) to handle new period expiry */
8105 if (runtime_enabled && cfs_b->timer_active) {
8106 /* force a reprogram */
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04008107 __start_cfs_bandwidth(cfs_b, true);
Paul Turner58088ad2011-07-21 09:43:31 -07008108 }
Paul Turnerab84d312011-07-21 09:43:28 -07008109 raw_spin_unlock_irq(&cfs_b->lock);
8110
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008111 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008112 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008113 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008114
8115 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008116 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008117 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008118
Peter Zijlstra029632f2011-10-25 10:00:11 +02008119 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008120 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008121 raw_spin_unlock_irq(&rq->lock);
8122 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008123 if (runtime_was_enabled && !runtime_enabled)
8124 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008125out_unlock:
8126 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008127 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008128
Paul Turnera790de92011-07-21 09:43:29 -07008129 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008130}
8131
8132int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8133{
8134 u64 quota, period;
8135
Peter Zijlstra029632f2011-10-25 10:00:11 +02008136 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008137 if (cfs_quota_us < 0)
8138 quota = RUNTIME_INF;
8139 else
8140 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8141
8142 return tg_set_cfs_bandwidth(tg, period, quota);
8143}
8144
8145long tg_get_cfs_quota(struct task_group *tg)
8146{
8147 u64 quota_us;
8148
Peter Zijlstra029632f2011-10-25 10:00:11 +02008149 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008150 return -1;
8151
Peter Zijlstra029632f2011-10-25 10:00:11 +02008152 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008153 do_div(quota_us, NSEC_PER_USEC);
8154
8155 return quota_us;
8156}
8157
8158int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8159{
8160 u64 quota, period;
8161
8162 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008163 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008164
Paul Turnerab84d312011-07-21 09:43:28 -07008165 return tg_set_cfs_bandwidth(tg, period, quota);
8166}
8167
8168long tg_get_cfs_period(struct task_group *tg)
8169{
8170 u64 cfs_period_us;
8171
Peter Zijlstra029632f2011-10-25 10:00:11 +02008172 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008173 do_div(cfs_period_us, NSEC_PER_USEC);
8174
8175 return cfs_period_us;
8176}
8177
Tejun Heo182446d2013-08-08 20:11:24 -04008178static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8179 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008180{
Tejun Heo182446d2013-08-08 20:11:24 -04008181 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008182}
8183
Tejun Heo182446d2013-08-08 20:11:24 -04008184static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8185 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008186{
Tejun Heo182446d2013-08-08 20:11:24 -04008187 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008188}
8189
Tejun Heo182446d2013-08-08 20:11:24 -04008190static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8191 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008192{
Tejun Heo182446d2013-08-08 20:11:24 -04008193 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008194}
8195
Tejun Heo182446d2013-08-08 20:11:24 -04008196static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8197 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008198{
Tejun Heo182446d2013-08-08 20:11:24 -04008199 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008200}
8201
Paul Turnera790de92011-07-21 09:43:29 -07008202struct cfs_schedulable_data {
8203 struct task_group *tg;
8204 u64 period, quota;
8205};
8206
8207/*
8208 * normalize group quota/period to be quota/max_period
8209 * note: units are usecs
8210 */
8211static u64 normalize_cfs_quota(struct task_group *tg,
8212 struct cfs_schedulable_data *d)
8213{
8214 u64 quota, period;
8215
8216 if (tg == d->tg) {
8217 period = d->period;
8218 quota = d->quota;
8219 } else {
8220 period = tg_get_cfs_period(tg);
8221 quota = tg_get_cfs_quota(tg);
8222 }
8223
8224 /* note: these should typically be equivalent */
8225 if (quota == RUNTIME_INF || quota == -1)
8226 return RUNTIME_INF;
8227
8228 return to_ratio(period, quota);
8229}
8230
8231static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8232{
8233 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008234 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008235 s64 quota = 0, parent_quota = -1;
8236
8237 if (!tg->parent) {
8238 quota = RUNTIME_INF;
8239 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008240 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008241
8242 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008243 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008244
8245 /*
8246 * ensure max(child_quota) <= parent_quota, inherit when no
8247 * limit is set
8248 */
8249 if (quota == RUNTIME_INF)
8250 quota = parent_quota;
8251 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8252 return -EINVAL;
8253 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008254 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008255
8256 return 0;
8257}
8258
8259static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8260{
Paul Turner82774342011-07-21 09:43:35 -07008261 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008262 struct cfs_schedulable_data data = {
8263 .tg = tg,
8264 .period = period,
8265 .quota = quota,
8266 };
8267
8268 if (quota != RUNTIME_INF) {
8269 do_div(data.period, NSEC_PER_USEC);
8270 do_div(data.quota, NSEC_PER_USEC);
8271 }
8272
Paul Turner82774342011-07-21 09:43:35 -07008273 rcu_read_lock();
8274 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8275 rcu_read_unlock();
8276
8277 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008278}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008279
Tejun Heo2da8ca82013-12-05 12:28:04 -05008280static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008281{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008282 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008283 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008284
Tejun Heo44ffc752013-12-05 12:28:01 -05008285 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8286 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8287 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008288
8289 return 0;
8290}
Paul Turnerab84d312011-07-21 09:43:28 -07008291#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008292#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008293
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008294#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008295static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8296 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008297{
Tejun Heo182446d2013-08-08 20:11:24 -04008298 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008299}
8300
Tejun Heo182446d2013-08-08 20:11:24 -04008301static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8302 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008303{
Tejun Heo182446d2013-08-08 20:11:24 -04008304 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008305}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008306
Tejun Heo182446d2013-08-08 20:11:24 -04008307static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8308 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008309{
Tejun Heo182446d2013-08-08 20:11:24 -04008310 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008311}
8312
Tejun Heo182446d2013-08-08 20:11:24 -04008313static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8314 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008315{
Tejun Heo182446d2013-08-08 20:11:24 -04008316 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008317}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008318#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008319
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008320static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008321#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008322 {
8323 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008324 .read_u64 = cpu_shares_read_u64,
8325 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008326 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008327#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008328#ifdef CONFIG_CFS_BANDWIDTH
8329 {
8330 .name = "cfs_quota_us",
8331 .read_s64 = cpu_cfs_quota_read_s64,
8332 .write_s64 = cpu_cfs_quota_write_s64,
8333 },
8334 {
8335 .name = "cfs_period_us",
8336 .read_u64 = cpu_cfs_period_read_u64,
8337 .write_u64 = cpu_cfs_period_write_u64,
8338 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008339 {
8340 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008341 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008342 },
Paul Turnerab84d312011-07-21 09:43:28 -07008343#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008344#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008345 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008346 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008347 .read_s64 = cpu_rt_runtime_read,
8348 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008349 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008350 {
8351 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008352 .read_u64 = cpu_rt_period_read_uint,
8353 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008354 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008355#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008356 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008357};
8358
Tejun Heo073219e2014-02-08 10:36:58 -05008359struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008360 .css_alloc = cpu_cgroup_css_alloc,
8361 .css_free = cpu_cgroup_css_free,
Li Zefanace783b2013-01-24 14:30:48 +08008362 .css_online = cpu_cgroup_css_online,
8363 .css_offline = cpu_cgroup_css_offline,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008364 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008365 .can_attach = cpu_cgroup_can_attach,
8366 .attach = cpu_cgroup_attach,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008367 .exit = cpu_cgroup_exit,
Tejun Heo55779642014-07-15 11:05:09 -04008368 .legacy_cftypes = cpu_files,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008369 .early_init = 1,
8370};
8371
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008372#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008373
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008374void dump_cpu_task(int cpu)
8375{
8376 pr_info("Task dump for CPU %d:\n", cpu);
8377 sched_show_task(cpu_curr(cpu));
8378}