blob: 46a2345f9f45174744dfaeefaea447313245aa66 [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;
Kirill Tkhaibb041592014-12-15 14:56:58 +03001835#ifdef CONFIG_SMP
1836 p->se.avg.decay_count = 0;
1837#endif
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001838 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001839
1840#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03001841 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001842#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001843
Dario Faggioliaab03e02013-11-28 11:14:43 +01001844 RB_CLEAR_NODE(&p->dl.rb_node);
1845 hrtimer_init(&p->dl.dl_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Juri Lellia5e7be32014-09-19 10:22:39 +01001846 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001847
Peter Zijlstrafa717062008-01-25 21:08:27 +01001848 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07001849
Avi Kivitye107be32007-07-26 13:40:43 +02001850#ifdef CONFIG_PREEMPT_NOTIFIERS
1851 INIT_HLIST_HEAD(&p->preempt_notifiers);
1852#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001853
1854#ifdef CONFIG_NUMA_BALANCING
1855 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01001856 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001857 p->mm->numa_scan_seq = 0;
1858 }
1859
Rik van Riel5e1576e2013-10-07 11:29:26 +01001860 if (clone_flags & CLONE_VM)
1861 p->numa_preferred_nid = current->numa_preferred_nid;
1862 else
1863 p->numa_preferred_nid = -1;
1864
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001865 p->node_stamp = 0ULL;
1866 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001867 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001868 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02001869 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001870 p->last_task_numa_placement = 0;
1871 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001872
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001873 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001874#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02001875}
1876
Mel Gorman1a687c22012-11-22 11:16:36 +00001877#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00001878#ifdef CONFIG_SCHED_DEBUG
Mel Gorman1a687c22012-11-22 11:16:36 +00001879void set_numabalancing_state(bool enabled)
1880{
1881 if (enabled)
1882 sched_feat_set("NUMA");
1883 else
1884 sched_feat_set("NO_NUMA");
1885}
Mel Gorman3105b862012-11-23 11:23:49 +00001886#else
1887__read_mostly bool numabalancing_enabled;
1888
1889void set_numabalancing_state(bool enabled)
1890{
1891 numabalancing_enabled = enabled;
1892}
1893#endif /* CONFIG_SCHED_DEBUG */
Andi Kleen54a43d52014-01-23 15:53:13 -08001894
1895#ifdef CONFIG_PROC_SYSCTL
1896int sysctl_numa_balancing(struct ctl_table *table, int write,
1897 void __user *buffer, size_t *lenp, loff_t *ppos)
1898{
1899 struct ctl_table t;
1900 int err;
1901 int state = numabalancing_enabled;
1902
1903 if (write && !capable(CAP_SYS_ADMIN))
1904 return -EPERM;
1905
1906 t = *table;
1907 t.data = &state;
1908 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
1909 if (err < 0)
1910 return err;
1911 if (write)
1912 set_numabalancing_state(state);
1913 return err;
1914}
1915#endif
1916#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00001917
Ingo Molnardd41f592007-07-09 18:51:59 +02001918/*
1919 * fork()/clone()-time setup:
1920 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001921int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02001922{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001923 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001924 int cpu = get_cpu();
1925
Rik van Riel5e1576e2013-10-07 11:29:26 +01001926 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01001927 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01001928 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01001929 * nobody will actually run it, and a signal or other external
1930 * event cannot wake it up and insert it on the runqueue either.
1931 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01001932 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001933
Ingo Molnarb29739f2006-06-27 02:54:51 -07001934 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02001935 * Make sure we do not leak PI boosting priority to the child.
1936 */
1937 p->prio = current->normal_prio;
1938
1939 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001940 * Revert to default priority/policy on fork if requested.
1941 */
1942 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01001943 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001944 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02001945 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02001946 p->rt_priority = 0;
1947 } else if (PRIO_TO_NICE(p->static_prio) < 0)
1948 p->static_prio = NICE_TO_PRIO(0);
1949
1950 p->prio = p->normal_prio = __normal_prio(p);
1951 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02001952
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02001953 /*
1954 * We don't need the reset flag anymore after the fork. It has
1955 * fulfilled its duty:
1956 */
1957 p->sched_reset_on_fork = 0;
1958 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02001959
Dario Faggioliaab03e02013-11-28 11:14:43 +01001960 if (dl_prio(p->prio)) {
1961 put_cpu();
1962 return -EAGAIN;
1963 } else if (rt_prio(p->prio)) {
1964 p->sched_class = &rt_sched_class;
1965 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02001966 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001967 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001968
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001969 if (p->sched_class->task_fork)
1970 p->sched_class->task_fork(p);
1971
Peter Zijlstra86951592010-06-22 11:44:53 +02001972 /*
1973 * The child is not yet in the pid-hash so no cgroup attach races,
1974 * and the cgroup is pinned to this child due to cgroup_fork()
1975 * is ran before sched_fork().
1976 *
1977 * Silence PROVE_RCU.
1978 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001979 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02001980 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001981 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02001982
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001983#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001984 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001985 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02001987#if defined(CONFIG_SMP)
1988 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07001989#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02001990 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01001991#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05001992 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001993 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01001994#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05001995
Nick Piggin476d1392005-06-25 14:57:29 -07001996 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01001997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Dario Faggioli332ac172013-11-07 14:43:45 +01002000unsigned long to_ratio(u64 period, u64 runtime)
2001{
2002 if (runtime == RUNTIME_INF)
2003 return 1ULL << 20;
2004
2005 /*
2006 * Doing this here saves a lot of checks in all
2007 * the calling paths, and returning zero seems
2008 * safe for them anyway.
2009 */
2010 if (period == 0)
2011 return 0;
2012
2013 return div64_u64(runtime << 20, period);
2014}
2015
2016#ifdef CONFIG_SMP
2017inline struct dl_bw *dl_bw_of(int i)
2018{
Kirill Tkhai66339c32014-09-22 22:36:24 +04002019 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2020 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002021 return &cpu_rq(i)->rd->dl_bw;
2022}
2023
Peter Zijlstrade212f12013-12-19 11:54:45 +01002024static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002025{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002026 struct root_domain *rd = cpu_rq(i)->rd;
2027 int cpus = 0;
2028
Kirill Tkhai66339c32014-09-22 22:36:24 +04002029 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2030 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002031 for_each_cpu_and(i, rd->span, cpu_active_mask)
2032 cpus++;
2033
2034 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002035}
2036#else
2037inline struct dl_bw *dl_bw_of(int i)
2038{
2039 return &cpu_rq(i)->dl.dl_bw;
2040}
2041
Peter Zijlstrade212f12013-12-19 11:54:45 +01002042static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002043{
2044 return 1;
2045}
2046#endif
2047
Dario Faggioli332ac172013-11-07 14:43:45 +01002048/*
2049 * We must be sure that accepting a new task (or allowing changing the
2050 * parameters of an existing one) is consistent with the bandwidth
2051 * constraints. If yes, this function also accordingly updates the currently
2052 * allocated bandwidth to reflect the new situation.
2053 *
2054 * This function is called while holding p's rq->lock.
2055 */
2056static int dl_overflow(struct task_struct *p, int policy,
2057 const struct sched_attr *attr)
2058{
2059
2060 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002061 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002062 u64 runtime = attr->sched_runtime;
2063 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002064 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002065
2066 if (new_bw == p->dl.dl_bw)
2067 return 0;
2068
2069 /*
2070 * Either if a task, enters, leave, or stays -deadline but changes
2071 * its parameters, we may need to update accordingly the total
2072 * allocated bandwidth of the container.
2073 */
2074 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002075 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002076 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2077 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2078 __dl_add(dl_b, new_bw);
2079 err = 0;
2080 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2081 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2082 __dl_clear(dl_b, p->dl.dl_bw);
2083 __dl_add(dl_b, new_bw);
2084 err = 0;
2085 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2086 __dl_clear(dl_b, p->dl.dl_bw);
2087 err = 0;
2088 }
2089 raw_spin_unlock(&dl_b->lock);
2090
2091 return err;
2092}
2093
2094extern void init_dl_bw(struct dl_bw *dl_b);
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096/*
2097 * wake_up_new_task - wake up a newly created task for the first time.
2098 *
2099 * This function will do some initial scheduler statistics housekeeping
2100 * that must be done for every newly created context, then puts the task
2101 * on the runqueue and wakes it.
2102 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002103void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002106 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002107
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002108 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002109#ifdef CONFIG_SMP
2110 /*
2111 * Fork balancing, do it here and not earlier because:
2112 * - cpus_allowed can change in the fork path
2113 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002114 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002115 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Alex Shia75cdaa2013-06-20 10:18:47 +08002118 /* Initialize new task's runnable average */
2119 init_task_runnable_average(p);
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002120 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002121 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002122 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra89363382011-04-05 17:23:42 +02002123 trace_sched_wakeup_new(p, true);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002124 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002125#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002126 if (p->sched_class->task_woken)
2127 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002128#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002129 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Avi Kivitye107be32007-07-26 13:40:43 +02002132#ifdef CONFIG_PREEMPT_NOTIFIERS
2133
2134/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002135 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002136 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002137 */
2138void preempt_notifier_register(struct preempt_notifier *notifier)
2139{
2140 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2141}
2142EXPORT_SYMBOL_GPL(preempt_notifier_register);
2143
2144/**
2145 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002146 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002147 *
2148 * This is safe to call from within a preemption notifier.
2149 */
2150void preempt_notifier_unregister(struct preempt_notifier *notifier)
2151{
2152 hlist_del(&notifier->link);
2153}
2154EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2155
2156static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2157{
2158 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002159
Sasha Levinb67bfe02013-02-27 17:06:00 -08002160 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002161 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2162}
2163
2164static void
2165fire_sched_out_preempt_notifiers(struct task_struct *curr,
2166 struct task_struct *next)
2167{
2168 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002169
Sasha Levinb67bfe02013-02-27 17:06:00 -08002170 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002171 notifier->ops->sched_out(notifier, next);
2172}
2173
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002174#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002175
2176static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2177{
2178}
2179
2180static void
2181fire_sched_out_preempt_notifiers(struct task_struct *curr,
2182 struct task_struct *next)
2183{
2184}
2185
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002186#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002189 * prepare_task_switch - prepare to switch tasks
2190 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002191 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002192 * @next: the task we are going to switch to.
2193 *
2194 * This is called with the rq lock held and interrupts off. It must
2195 * be paired with a subsequent finish_task_switch after the context
2196 * switch.
2197 *
2198 * prepare_task_switch sets up locking and calls architecture specific
2199 * hooks.
2200 */
Avi Kivitye107be32007-07-26 13:40:43 +02002201static inline void
2202prepare_task_switch(struct rq *rq, struct task_struct *prev,
2203 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002204{
Andrew Vagin895dd922012-07-12 14:14:29 +04002205 trace_sched_switch(prev, next);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002206 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002207 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002208 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002209 prepare_lock_switch(rq, next);
2210 prepare_arch_switch(next);
2211}
2212
2213/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * finish_task_switch - clean up after a task-switch
2215 * @prev: the thread we just switched away from.
2216 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002217 * finish_task_switch must be called after the context switch, paired
2218 * with a prepare_task_switch call before the context switch.
2219 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2220 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 *
2222 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002223 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 * with the lock held can cause deadlocks; see schedule() for
2225 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002226 *
2227 * The context switch have flipped the stack from under us and restored the
2228 * local variables which were saved when this task called schedule() in the
2229 * past. prev == current is still correct but we need to recalculate this_rq
2230 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002232static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 __releases(rq->lock)
2234{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002235 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002237 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 rq->prev_mm = NULL;
2240
2241 /*
2242 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002243 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002244 * schedule one last time. The schedule call will never return, and
2245 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002246 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 * still held, otherwise prev could be scheduled on another cpu, die
2248 * there before we look at prev->state, and then the reference would
2249 * be dropped twice.
2250 * Manfred Spraul <manfred@colorfullife.com>
2251 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002252 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002253 vtime_task_switch(prev);
Nick Piggin4866cde2005-06-25 14:57:23 -07002254 finish_arch_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002255 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002256 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002257 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002258
Avi Kivitye107be32007-07-26 13:40:43 +02002259 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (mm)
2261 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002262 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002263 if (prev->sched_class->task_dead)
2264 prev->sched_class->task_dead(prev);
2265
bibo maoc6fd91f2006-03-26 01:38:20 -08002266 /*
2267 * Remove function-return probe instances associated with this
2268 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002269 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002270 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002272 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002273
2274 tick_nohz_task_switch(current);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002275 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Gregory Haskins3f029d32009-07-29 11:08:47 -04002278#ifdef CONFIG_SMP
2279
Gregory Haskins3f029d32009-07-29 11:08:47 -04002280/* rq->lock is NOT held, but preemption is disabled */
2281static inline void post_schedule(struct rq *rq)
2282{
2283 if (rq->post_schedule) {
2284 unsigned long flags;
2285
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002286 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002287 if (rq->curr->sched_class->post_schedule)
2288 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002289 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002290
2291 rq->post_schedule = 0;
2292 }
2293}
2294
2295#else
2296
Gregory Haskins3f029d32009-07-29 11:08:47 -04002297static inline void post_schedule(struct rq *rq)
2298{
2299}
2300
2301#endif
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303/**
2304 * schedule_tail - first thing a freshly forked thread must call.
2305 * @prev: the thread we just switched away from.
2306 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002307asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 __releases(rq->lock)
2309{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002310 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002311
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002312 /* finish_task_switch() drops rq->lock and enables preemtion */
2313 preempt_disable();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002314 rq = finish_task_switch(prev);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002315 post_schedule(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002316 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002319 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
2322/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002323 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002325static inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002326context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002327 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Ingo Molnardd41f592007-07-09 18:51:59 +02002329 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Avi Kivitye107be32007-07-26 13:40:43 +02002331 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002332
Ingo Molnardd41f592007-07-09 18:51:59 +02002333 mm = next->mm;
2334 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002335 /*
2336 * For paravirt, this is coupled with an exit in switch_to to
2337 * combine the page table reload and the switch backend into
2338 * one hypercall.
2339 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002340 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002341
Heiko Carstens31915ab2010-09-16 14:42:25 +02002342 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 next->active_mm = oldmm;
2344 atomic_inc(&oldmm->mm_count);
2345 enter_lazy_tlb(oldmm, next);
2346 } else
2347 switch_mm(oldmm, mm, next);
2348
Heiko Carstens31915ab2010-09-16 14:42:25 +02002349 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 rq->prev_mm = oldmm;
2352 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002353 /*
2354 * Since the runqueue lock will be released by the next
2355 * task (which is an invalid locking op but in the case
2356 * of the scheduler it's an obvious special-case), so we
2357 * do an early lockdep release here:
2358 */
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002359 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01002361 context_tracking_task_switch(prev, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /* Here we just switch the register state and the stack. */
2363 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002364 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002365
2366 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002370 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 *
2372 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002373 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 */
2375unsigned long nr_running(void)
2376{
2377 unsigned long i, sum = 0;
2378
2379 for_each_online_cpu(i)
2380 sum += cpu_rq(i)->nr_running;
2381
2382 return sum;
2383}
2384
Tim Chen2ee507c2014-07-31 10:29:48 -07002385/*
2386 * Check if only the current task is running on the cpu.
2387 */
2388bool single_task_running(void)
2389{
2390 if (cpu_rq(smp_processor_id())->nr_running == 1)
2391 return true;
2392 else
2393 return false;
2394}
2395EXPORT_SYMBOL(single_task_running);
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397unsigned long long nr_context_switches(void)
2398{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002399 int i;
2400 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002402 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 sum += cpu_rq(i)->nr_switches;
2404
2405 return sum;
2406}
2407
2408unsigned long nr_iowait(void)
2409{
2410 unsigned long i, sum = 0;
2411
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002412 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2414
2415 return sum;
2416}
2417
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002418unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002419{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002420 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002421 return atomic_read(&this->nr_iowait);
2422}
2423
Mel Gorman372ba8c2014-08-06 14:19:21 +01002424void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2425{
2426 struct rq *this = this_rq();
2427 *nr_waiters = atomic_read(&this->nr_iowait);
2428 *load = this->cpu_load[0];
2429}
2430
Ingo Molnardd41f592007-07-09 18:51:59 +02002431#ifdef CONFIG_SMP
2432
Ingo Molnar48f24c42006-07-03 00:25:40 -07002433/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002434 * sched_exec - execve() is a valuable balancing opportunity, because at
2435 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002437void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Peter Zijlstra38022902009-12-16 18:04:37 +01002439 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002441 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002442
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002443 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002444 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002445 if (dest_cpu == smp_processor_id())
2446 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002447
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002448 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002449 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002450
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002451 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2452 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return;
2454 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002455unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002456 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459#endif
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002462DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002465EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002468 * Return accounted runtime for the task.
2469 * In case the task is currently running, return the runtime plus current's
2470 * pending runtime that have not been accounted yet.
2471 */
2472unsigned long long task_sched_runtime(struct task_struct *p)
2473{
2474 unsigned long flags;
2475 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002476 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002477
Peter Zijlstra911b2892013-11-11 18:21:56 +01002478#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2479 /*
2480 * 64-bit doesn't need locks to atomically read a 64bit value.
2481 * So we have a optimization chance when the task's delta_exec is 0.
2482 * Reading ->on_cpu is racy, but this is ok.
2483 *
2484 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2485 * If we race with it entering cpu, unaccounted time is 0. This is
2486 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002487 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2488 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002489 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002490 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002491 return p->se.sum_exec_runtime;
2492#endif
2493
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002494 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002495 /*
2496 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2497 * project cycles that may never be accounted to this
2498 * thread, breaking clock_gettime().
2499 */
2500 if (task_current(rq, p) && task_on_rq_queued(p)) {
2501 update_rq_clock(rq);
2502 p->sched_class->update_curr(rq);
2503 }
2504 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002505 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002506
2507 return ns;
2508}
2509
Balbir Singh49048622008-09-05 18:12:23 +02002510/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002511 * This function gets called by the timer code, with HZ frequency.
2512 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002513 */
2514void scheduler_tick(void)
2515{
Christoph Lameter7835b982006-12-10 02:20:22 -08002516 int cpu = smp_processor_id();
2517 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002518 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002519
2520 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002521
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002522 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002523 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002524 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002525 update_cpu_load_active(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002526 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002527
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002528 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002529
Christoph Lametere418e1c2006-12-10 02:20:23 -08002530#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002531 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002532 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002533#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002534 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002537#ifdef CONFIG_NO_HZ_FULL
2538/**
2539 * scheduler_tick_max_deferment
2540 *
2541 * Keep at least one tick per second when a single
2542 * active task is running because the scheduler doesn't
2543 * yet completely support full dynticks environment.
2544 *
2545 * This makes sure that uptime, CFS vruntime, load
2546 * balancing, etc... continue to move forward, even
2547 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002548 *
2549 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002550 */
2551u64 scheduler_tick_max_deferment(void)
2552{
2553 struct rq *rq = this_rq();
2554 unsigned long next, now = ACCESS_ONCE(jiffies);
2555
2556 next = rq->last_sched_tick + HZ;
2557
2558 if (time_before_eq(next, now))
2559 return 0;
2560
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002561 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002562}
2563#endif
2564
Lai Jiangshan132380a2009-04-02 14:18:25 +08002565notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002566{
2567 if (in_lock_functions(addr)) {
2568 addr = CALLER_ADDR2;
2569 if (in_lock_functions(addr))
2570 addr = CALLER_ADDR3;
2571 }
2572 return addr;
2573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002575#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2576 defined(CONFIG_PREEMPT_TRACER))
2577
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002578void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002580#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
2582 * Underflow?
2583 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002584 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2585 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002586#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002587 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002588#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /*
2590 * Spinlock count overflowing soon?
2591 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002592 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2593 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002594#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002595 if (preempt_count() == val) {
2596 unsigned long ip = get_parent_ip(CALLER_ADDR1);
2597#ifdef CONFIG_DEBUG_PREEMPT
2598 current->preempt_disable_ip = ip;
2599#endif
2600 trace_preempt_off(CALLER_ADDR0, ip);
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002603EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002604NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002606void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002608#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /*
2610 * Underflow?
2611 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01002612 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /*
2615 * Is the spinlock portion underflowing?
2616 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002617 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2618 !(preempt_count() & PREEMPT_MASK)))
2619 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002620#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002621
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002622 if (preempt_count() == val)
2623 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002624 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002626EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002627NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629#endif
2630
2631/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002632 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002634static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Dave Jones664dfa62011-12-22 16:39:30 -05002636 if (oops_in_progress)
2637 return;
2638
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01002639 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
2640 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02002641
Ingo Molnardd41f592007-07-09 18:51:59 +02002642 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07002643 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02002644 if (irqs_disabled())
2645 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002646#ifdef CONFIG_DEBUG_PREEMPT
2647 if (in_atomic_preempt_off()) {
2648 pr_err("Preemption disabled at:");
2649 print_ip_sym(current->preempt_disable_ip);
2650 pr_cont("\n");
2651 }
2652#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07002653 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10302654 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02002655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Ingo Molnardd41f592007-07-09 18:51:59 +02002657/*
2658 * Various schedule()-time debugging checks and statistics:
2659 */
2660static inline void schedule_debug(struct task_struct *prev)
2661{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01002662#ifdef CONFIG_SCHED_STACK_END_CHECK
2663 BUG_ON(unlikely(task_stack_end_corrupted(prev)));
2664#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002666 * Test if we are atomic. Since do_exit() needs to call into
Oleg Nesterov192301e2013-11-13 16:45:38 +01002667 * schedule() atomically, we ignore that path. Otherwise whine
2668 * if we are scheduling when we should not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 */
Oleg Nesterov192301e2013-11-13 16:45:38 +01002670 if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
Ingo Molnardd41f592007-07-09 18:51:59 +02002671 __schedule_bug(prev);
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002672 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02002673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2675
Ingo Molnar2d723762007-10-15 17:00:12 +02002676 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02002677}
2678
2679/*
2680 * Pick up the highest-prio task:
2681 */
2682static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01002683pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02002684{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002685 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02002686 struct task_struct *p;
2687
2688 /*
2689 * Optimization: we know that if all tasks are in
2690 * the fair class we can call that function directly:
2691 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002692 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01002693 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002694 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02002695 if (unlikely(p == RETRY_TASK))
2696 goto again;
2697
2698 /* assumes fair_sched_class->next == idle_sched_class */
2699 if (unlikely(!p))
2700 p = idle_sched_class.pick_next_task(rq, prev);
2701
2702 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02002703 }
2704
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002705again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002706 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002707 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002708 if (p) {
2709 if (unlikely(p == RETRY_TASK))
2710 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02002711 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002712 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002713 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002714
2715 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02002716}
2717
2718/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002719 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03002720 *
2721 * The main means of driving the scheduler and thus entering this function are:
2722 *
2723 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
2724 *
2725 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
2726 * paths. For example, see arch/x86/entry_64.S.
2727 *
2728 * To drive preemption between tasks, the scheduler sets the flag in timer
2729 * interrupt handler scheduler_tick().
2730 *
2731 * 3. Wakeups don't really cause entry into schedule(). They add a
2732 * task to the run-queue and that's it.
2733 *
2734 * Now, if the new task added to the run-queue preempts the current
2735 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
2736 * called on the nearest possible occasion:
2737 *
2738 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
2739 *
2740 * - in syscall or exception context, at the next outmost
2741 * preempt_enable(). (this might be as soon as the wake_up()'s
2742 * spin_unlock()!)
2743 *
2744 * - in IRQ context, return from interrupt-handler to
2745 * preemptible context
2746 *
2747 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
2748 * then at the next:
2749 *
2750 * - cond_resched() call
2751 * - explicit schedule() call
2752 * - return from syscall or exception to user-space
2753 * - return from interrupt-handler to user-space
Ingo Molnardd41f592007-07-09 18:51:59 +02002754 */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002755static void __sched __schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02002756{
2757 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08002758 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02002759 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02002760 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002761
Peter Zijlstraff743342009-03-13 12:21:26 +01002762need_resched:
2763 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02002764 cpu = smp_processor_id();
2765 rq = cpu_rq(cpu);
Paul E. McKenney38200cf2014-10-21 12:50:04 -07002766 rcu_note_context_switch();
Ingo Molnardd41f592007-07-09 18:51:59 +02002767 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02002768
Ingo Molnardd41f592007-07-09 18:51:59 +02002769 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Peter Zijlstra31656512008-07-18 18:01:23 +02002771 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02002772 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002773
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002774 /*
2775 * Make sure that signal_pending_state()->signal_pending() below
2776 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
2777 * done by the caller to avoid the race with signal_wake_up().
2778 */
2779 smp_mb__before_spinlock();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002780 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Oleg Nesterov246d86b2010-05-19 14:57:11 +02002782 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02002783 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02002784 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002785 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002786 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02002787 deactivate_task(rq, prev, DEQUEUE_SLEEP);
2788 prev->on_rq = 0;
2789
Tejun Heo21aa9af2010-06-08 21:40:37 +02002790 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02002791 * If a worker went to sleep, notify and ask workqueue
2792 * whether it wants to wake up a task to maintain
2793 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002794 */
2795 if (prev->flags & PF_WQ_WORKER) {
2796 struct task_struct *to_wakeup;
2797
2798 to_wakeup = wq_worker_sleeping(prev, cpu);
2799 if (to_wakeup)
2800 try_to_wake_up_local(to_wakeup);
2801 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02002802 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002803 switch_count = &prev->nvcsw;
2804 }
2805
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002806 if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)
Peter Zijlstra606dba22012-02-11 06:05:00 +01002807 update_rq_clock(rq);
2808
2809 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01002810 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002811 clear_preempt_need_resched();
Mike Galbraithf26f9af2010-12-08 11:05:42 +01002812 rq->skip_clock_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 rq->nr_switches++;
2816 rq->curr = next;
2817 ++*switch_count;
2818
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002819 rq = context_switch(rq, prev, next); /* unlocks the rq */
2820 cpu = cpu_of(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002822 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Gregory Haskins3f029d32009-07-29 11:08:47 -04002824 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Thomas Gleixnerba74c142011-03-21 13:32:17 +01002826 sched_preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01002827 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 goto need_resched;
2829}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002830
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002831static inline void sched_submit_work(struct task_struct *tsk)
2832{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02002833 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002834 return;
2835 /*
2836 * If we are going to sleep and we have plugged IO queued,
2837 * make sure to submit it to avoid deadlocks.
2838 */
2839 if (blk_needs_flush_plug(tsk))
2840 blk_schedule_flush_plug(tsk);
2841}
2842
Andi Kleen722a9f92014-05-02 00:44:38 +02002843asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002844{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02002845 struct task_struct *tsk = current;
2846
2847 sched_submit_work(tsk);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002848 __schedule();
2849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850EXPORT_SYMBOL(schedule);
2851
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01002852#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02002853asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002854{
2855 /*
2856 * If we come here after a random call to set_need_resched(),
2857 * or we have been woken up remotely but the IPI has not yet arrived,
2858 * we haven't yet exited the RCU idle mode. Do it here manually until
2859 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002860 *
2861 * NB: There are buggy callers of this function. Ideally we
2862 * should warn if prev_state != IN_USER, but that will trigger
2863 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002864 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002865 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002866 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08002867 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02002868}
2869#endif
2870
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01002871/**
2872 * schedule_preempt_disabled - called with preemption disabled
2873 *
2874 * Returns with preemption disabled. Note: preempt_count must be 1
2875 */
2876void __sched schedule_preempt_disabled(void)
2877{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01002878 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01002879 schedule();
2880 preempt_disable();
2881}
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883#ifdef CONFIG_PREEMPT
2884/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002885 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002886 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 * occur there and call schedule directly.
2888 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002889asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 /*
2892 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002893 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02002895 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return;
2897
Andi Kleen3a5c3592007-10-15 17:00:14 +02002898 do {
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002899 __preempt_count_add(PREEMPT_ACTIVE);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002900 __schedule();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002901 __preempt_count_sub(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002902
2903 /*
2904 * Check again in case we missed a preemption opportunity
2905 * between schedule and now.
2906 */
2907 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08002908 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002910NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02002912
2913#ifdef CONFIG_CONTEXT_TRACKING
2914/**
2915 * preempt_schedule_context - preempt_schedule called by tracing
2916 *
2917 * The tracing infrastructure uses preempt_enable_notrace to prevent
2918 * recursion and tracing preempt enabling caused by the tracing
2919 * infrastructure itself. But as tracing can happen in areas coming
2920 * from userspace or just about to enter userspace, a preempt enable
2921 * can occur before user_exit() is called. This will cause the scheduler
2922 * to be called when the system is still in usermode.
2923 *
2924 * To prevent this, the preempt_enable_notrace will use this function
2925 * instead of preempt_schedule() to exit user context if needed before
2926 * calling the scheduler.
2927 */
2928asmlinkage __visible void __sched notrace preempt_schedule_context(void)
2929{
2930 enum ctx_state prev_ctx;
2931
2932 if (likely(!preemptible()))
2933 return;
2934
2935 do {
2936 __preempt_count_add(PREEMPT_ACTIVE);
2937 /*
2938 * Needs preempt disabled in case user_exit() is traced
2939 * and the tracer calls preempt_enable_notrace() causing
2940 * an infinite recursion.
2941 */
2942 prev_ctx = exception_enter();
2943 __schedule();
2944 exception_exit(prev_ctx);
2945
2946 __preempt_count_sub(PREEMPT_ACTIVE);
2947 barrier();
2948 } while (need_resched());
2949}
2950EXPORT_SYMBOL_GPL(preempt_schedule_context);
2951#endif /* CONFIG_CONTEXT_TRACKING */
2952
Thomas Gleixner32e475d2013-11-21 12:41:44 +01002953#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002956 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 * off of irq context.
2958 * Note, that this is called and return with irqs disabled. This will
2959 * protect us against recursive calling from irq.
2960 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002961asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002963 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01002964
Andreas Mohr2ed6e342006-07-10 04:43:52 -07002965 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002966 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002968 prev_state = exception_enter();
2969
Andi Kleen3a5c3592007-10-15 17:00:14 +02002970 do {
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002971 __preempt_count_add(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002972 local_irq_enable();
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002973 __schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02002974 local_irq_disable();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002975 __preempt_count_sub(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002976
2977 /*
2978 * Check again in case we missed a preemption opportunity
2979 * between schedule and now.
2980 */
2981 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08002982 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01002983
2984 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
Peter Zijlstra63859d42009-09-15 19:14:42 +02002987int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002988 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Peter Zijlstra63859d42009-09-15 19:14:42 +02002990 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992EXPORT_SYMBOL(default_wake_function);
2993
Ingo Molnarb29739f2006-06-27 02:54:51 -07002994#ifdef CONFIG_RT_MUTEXES
2995
2996/*
2997 * rt_mutex_setprio - set the current priority of a task
2998 * @p: task
2999 * @prio: prio value (kernel-internal form)
3000 *
3001 * This function changes the 'effective' priority of a task. It does
3002 * not touch ->normal_prio like __setscheduler().
3003 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003004 * Used by the rt_mutex code to implement priority inheritance
3005 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003006 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003007void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003008{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003009 int oldprio, queued, running, enqueue_flag = 0;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003010 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003011 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003012
Dario Faggioliaab03e02013-11-28 11:14:43 +01003013 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003014
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003015 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003016
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003017 /*
3018 * Idle task boosting is a nono in general. There is one
3019 * exception, when PREEMPT_RT and NOHZ is active:
3020 *
3021 * The idle task calls get_next_timer_interrupt() and holds
3022 * the timer wheel base->lock on the CPU and another CPU wants
3023 * to access the timer (probably to cancel it). We can safely
3024 * ignore the boosting request, as the idle CPU runs this code
3025 * with interrupts disabled and will complete the lock
3026 * protected section without being interrupted. So there is no
3027 * real need to boost.
3028 */
3029 if (unlikely(p == rq->idle)) {
3030 WARN_ON(p != rq->curr);
3031 WARN_ON(p->pi_blocked_on);
3032 goto out_unlock;
3033 }
3034
Steven Rostedta8027072010-09-20 15:13:34 -04003035 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003036 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003037 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003038 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003039 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003040 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003041 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003042 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003043 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003044
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003045 /*
3046 * Boosting condition are:
3047 * 1. -rt task is running and holds mutex A
3048 * --> -dl task blocks on mutex A
3049 *
3050 * 2. -dl task is running and holds mutex A
3051 * --> -dl task blocks on mutex A and could preempt the
3052 * running task
3053 */
3054 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003055 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3056 if (!dl_prio(p->normal_prio) ||
3057 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003058 p->dl.dl_boosted = 1;
3059 p->dl.dl_throttled = 0;
3060 enqueue_flag = ENQUEUE_REPLENISH;
3061 } else
3062 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003063 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003064 } else if (rt_prio(prio)) {
3065 if (dl_prio(oldprio))
3066 p->dl.dl_boosted = 0;
3067 if (oldprio < prio)
3068 enqueue_flag = ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003069 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003070 } else {
3071 if (dl_prio(oldprio))
3072 p->dl.dl_boosted = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003073 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003074 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003075
Ingo Molnarb29739f2006-06-27 02:54:51 -07003076 p->prio = prio;
3077
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003078 if (running)
3079 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003080 if (queued)
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003081 enqueue_task(rq, p, enqueue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003082
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003083 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003084out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003085 __task_rq_unlock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003086}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003087#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003088
Ingo Molnar36c8b582006-07-03 00:25:41 -07003089void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003091 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003093 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003095 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return;
3097 /*
3098 * We have to be careful, if called from sys_setpriority(),
3099 * the task might be in the middle of scheduling on another CPU.
3100 */
3101 rq = task_rq_lock(p, &flags);
3102 /*
3103 * The RT priorities are set via sched_setscheduler(), but we still
3104 * allow the 'normal' nice value to be set - but as expected
3105 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003106 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003108 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 p->static_prio = NICE_TO_PRIO(nice);
3110 goto out_unlock;
3111 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003112 queued = task_on_rq_queued(p);
3113 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003114 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003117 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003118 old_prio = p->prio;
3119 p->prio = effective_prio(p);
3120 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003122 if (queued) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003123 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003125 * If the task increased its priority or is running and
3126 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003128 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003129 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
3131out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003132 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134EXPORT_SYMBOL(set_user_nice);
3135
Matt Mackalle43379f2005-05-01 08:59:00 -07003136/*
3137 * can_nice - check if a task can reduce its nice value
3138 * @p: task
3139 * @nice: nice value
3140 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003141int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003142{
Matt Mackall024f4742005-08-18 11:24:19 -07003143 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003144 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003145
Jiri Slaby78d7d402010-03-05 13:42:54 -08003146 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003147 capable(CAP_SYS_NICE));
3148}
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150#ifdef __ARCH_WANT_SYS_NICE
3151
3152/*
3153 * sys_nice - change the priority of the current process.
3154 * @increment: priority increment
3155 *
3156 * sys_setpriority is a more generic, but much slower function that
3157 * does similar things.
3158 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003159SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003161 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 /*
3164 * Setpriority might change our priority at the same moment.
3165 * We don't have to worry. Conceptually one call occurs first
3166 * and we have a single winner.
3167 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003168 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003169 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003171 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003172 if (increment < 0 && !can_nice(current, nice))
3173 return -EPERM;
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 retval = security_task_setnice(current, nice);
3176 if (retval)
3177 return retval;
3178
3179 set_user_nice(current, nice);
3180 return 0;
3181}
3182
3183#endif
3184
3185/**
3186 * task_prio - return the priority value of a given task.
3187 * @p: the task in question.
3188 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003189 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 * RT tasks are offset by -200. Normal tasks are centered
3191 * around 0, value goes from -16 to +15.
3192 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003193int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 return p->prio - MAX_RT_PRIO;
3196}
3197
3198/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 * idle_cpu - is a given cpu idle currently?
3200 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003201 *
3202 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 */
3204int idle_cpu(int cpu)
3205{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003206 struct rq *rq = cpu_rq(cpu);
3207
3208 if (rq->curr != rq->idle)
3209 return 0;
3210
3211 if (rq->nr_running)
3212 return 0;
3213
3214#ifdef CONFIG_SMP
3215 if (!llist_empty(&rq->wake_list))
3216 return 0;
3217#endif
3218
3219 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222/**
3223 * idle_task - return the idle task for a given cpu.
3224 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003225 *
3226 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003228struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 return cpu_rq(cpu)->idle;
3231}
3232
3233/**
3234 * find_process_by_pid - find a process with a matching PID value.
3235 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003236 *
3237 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003239static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003241 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Dario Faggioliaab03e02013-11-28 11:14:43 +01003244/*
3245 * This function initializes the sched_dl_entity of a newly becoming
3246 * SCHED_DEADLINE task.
3247 *
3248 * Only the static values are considered here, the actual runtime and the
3249 * absolute deadline will be properly calculated when the task is enqueued
3250 * for the first time with its new policy.
3251 */
3252static void
3253__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3254{
3255 struct sched_dl_entity *dl_se = &p->dl;
3256
3257 init_dl_task_timer(dl_se);
3258 dl_se->dl_runtime = attr->sched_runtime;
3259 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003260 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003261 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003262 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01003263 dl_se->dl_throttled = 0;
3264 dl_se->dl_new = 1;
Juri Lelli5bfd1262014-04-15 13:49:04 +02003265 dl_se->dl_yielded = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003266}
3267
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003268/*
3269 * sched_setparam() passes in -1 for its policy, to let the functions
3270 * it calls know not to change it.
3271 */
3272#define SETPARAM_POLICY -1
3273
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003274static void __setscheduler_params(struct task_struct *p,
3275 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003277 int policy = attr->sched_policy;
3278
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003279 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003280 policy = p->policy;
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003283
Dario Faggioliaab03e02013-11-28 11:14:43 +01003284 if (dl_policy(policy))
3285 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003286 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003287 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3288
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003289 /*
3290 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3291 * !rt_policy. Always setting this ensures that things like
3292 * getparam()/getattr() don't report silly values for !rt tasks.
3293 */
3294 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003295 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003296 set_load_weight(p);
3297}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003298
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003299/* Actually do priority change: must hold pi & rq lock. */
3300static void __setscheduler(struct rq *rq, struct task_struct *p,
3301 const struct sched_attr *attr)
3302{
3303 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003304
Steven Rostedt383afd02014-03-11 19:24:20 -04003305 /*
3306 * If we get here, there was no pi waiters boosting the
3307 * task. It is safe to use the normal prio.
3308 */
3309 p->prio = normal_prio(p);
3310
Dario Faggioliaab03e02013-11-28 11:14:43 +01003311 if (dl_prio(p->prio))
3312 p->sched_class = &dl_sched_class;
3313 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003314 p->sched_class = &rt_sched_class;
3315 else
3316 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003318
3319static void
3320__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3321{
3322 struct sched_dl_entity *dl_se = &p->dl;
3323
3324 attr->sched_priority = p->rt_priority;
3325 attr->sched_runtime = dl_se->dl_runtime;
3326 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003327 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003328 attr->sched_flags = dl_se->flags;
3329}
3330
3331/*
3332 * This function validates the new parameters of a -deadline task.
3333 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003334 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003335 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003336 * user parameters are above the internal resolution of 1us (we
3337 * check sched_runtime only since it is always the smaller one) and
3338 * below 2^63 ns (we have to check both sched_deadline and
3339 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003340 */
3341static bool
3342__checkparam_dl(const struct sched_attr *attr)
3343{
Juri Lellib08278192014-05-13 14:11:31 +02003344 /* deadline != 0 */
3345 if (attr->sched_deadline == 0)
3346 return false;
3347
3348 /*
3349 * Since we truncate DL_SCALE bits, make sure we're at least
3350 * that big.
3351 */
3352 if (attr->sched_runtime < (1ULL << DL_SCALE))
3353 return false;
3354
3355 /*
3356 * Since we use the MSB for wrap-around and sign issues, make
3357 * sure it's not set (mind that period can be equal to zero).
3358 */
3359 if (attr->sched_deadline & (1ULL << 63) ||
3360 attr->sched_period & (1ULL << 63))
3361 return false;
3362
3363 /* runtime <= deadline <= period (if period != 0) */
3364 if ((attr->sched_period != 0 &&
3365 attr->sched_period < attr->sched_deadline) ||
3366 attr->sched_deadline < attr->sched_runtime)
3367 return false;
3368
3369 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003370}
3371
David Howellsc69e8d92008-11-14 10:39:19 +11003372/*
3373 * check the target process has a UID that matches the current process's
3374 */
3375static bool check_same_owner(struct task_struct *p)
3376{
3377 const struct cred *cred = current_cred(), *pcred;
3378 bool match;
3379
3380 rcu_read_lock();
3381 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003382 match = (uid_eq(cred->euid, pcred->euid) ||
3383 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003384 rcu_read_unlock();
3385 return match;
3386}
3387
Dario Faggiolid50dde52013-11-07 14:43:36 +01003388static int __sched_setscheduler(struct task_struct *p,
3389 const struct sched_attr *attr,
3390 bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
Steven Rostedt383afd02014-03-11 19:24:20 -04003392 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3393 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003394 int retval, oldprio, oldpolicy = -1, queued, running;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003395 int policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003397 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003398 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003399 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Steven Rostedt66e53932006-06-27 02:54:44 -07003401 /* may grab non-irq protected spin_locks */
3402 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403recheck:
3404 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003405 if (policy < 0) {
3406 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003408 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003409 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003410
Dario Faggioliaab03e02013-11-28 11:14:43 +01003411 if (policy != SCHED_DEADLINE &&
3412 policy != SCHED_FIFO && policy != SCHED_RR &&
Lennart Poetteringca94c442009-06-15 17:17:47 +02003413 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
3414 policy != SCHED_IDLE)
3415 return -EINVAL;
3416 }
3417
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003418 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3419 return -EINVAL;
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /*
3422 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003423 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3424 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003426 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003427 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003429 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3430 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return -EINVAL;
3432
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003433 /*
3434 * Allow unprivileged RT tasks to decrease priority:
3435 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003436 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003437 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003438 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003439 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003440 return -EPERM;
3441 }
3442
Ingo Molnare05606d2007-07-09 18:51:59 +02003443 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003444 unsigned long rlim_rtprio =
3445 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003446
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003447 /* can't set/change the rt policy */
3448 if (policy != p->policy && !rlim_rtprio)
3449 return -EPERM;
3450
3451 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003452 if (attr->sched_priority > p->rt_priority &&
3453 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003454 return -EPERM;
3455 }
Darren Hartc02aa732011-02-17 15:37:07 -08003456
Juri Lellid44753b2014-03-03 12:09:21 +01003457 /*
3458 * Can't set/change SCHED_DEADLINE policy at all for now
3459 * (safest behavior); in the future we would like to allow
3460 * unprivileged DL tasks to increase their relative deadline
3461 * or reduce their runtime (both ways reducing utilization)
3462 */
3463 if (dl_policy(policy))
3464 return -EPERM;
3465
Ingo Molnardd41f592007-07-09 18:51:59 +02003466 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003467 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3468 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003469 */
Darren Hartc02aa732011-02-17 15:37:07 -08003470 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003471 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003472 return -EPERM;
3473 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003474
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003475 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003476 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003477 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003478
3479 /* Normal users shall not reset the sched_reset_on_fork flag */
3480 if (p->sched_reset_on_fork && !reset_on_fork)
3481 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003484 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003485 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003486 if (retval)
3487 return retval;
3488 }
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003491 * make sure no PI-waiters arrive (or leave) while we are
3492 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003493 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003494 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 * runqueue lock must be held.
3496 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003497 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003498
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003499 /*
3500 * Changing the policy of the stop threads its a very bad idea
3501 */
3502 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003503 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003504 return -EINVAL;
3505 }
3506
Dario Faggiolia51e9192011-03-24 14:00:18 +01003507 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003508 * If not changing anything there's no need to proceed further,
3509 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003510 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003511 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003512 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003513 goto change;
3514 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3515 goto change;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003516 if (dl_policy(policy))
3517 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003518
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003519 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003520 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003521 return 0;
3522 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003523change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003524
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003525 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003526#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003527 /*
3528 * Do not allow realtime tasks into groups that have no runtime
3529 * assigned.
3530 */
3531 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01003532 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
3533 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003534 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003535 return -EPERM;
3536 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003537#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01003538#ifdef CONFIG_SMP
3539 if (dl_bandwidth_enabled() && dl_policy(policy)) {
3540 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01003541
3542 /*
3543 * Don't allow tasks with an affinity mask smaller than
3544 * the entire root_domain to become SCHED_DEADLINE. We
3545 * will also fail if there's no bandwidth available.
3546 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003547 if (!cpumask_subset(span, &p->cpus_allowed) ||
3548 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003549 task_rq_unlock(rq, p, &flags);
3550 return -EPERM;
3551 }
3552 }
3553#endif
3554 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 /* recheck policy now with rq lock held */
3557 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3558 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003559 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 goto recheck;
3561 }
Dario Faggioli332ac172013-11-07 14:43:45 +01003562
3563 /*
3564 * If setscheduling to SCHED_DEADLINE (or changing the parameters
3565 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
3566 * is available.
3567 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003568 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003569 task_rq_unlock(rq, p, &flags);
3570 return -EBUSY;
3571 }
3572
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003573 p->sched_reset_on_fork = reset_on_fork;
3574 oldprio = p->prio;
3575
3576 /*
3577 * Special case for priority boosted tasks.
3578 *
3579 * If the new priority is lower or equal (user space view)
3580 * than the current (boosted) priority, we just store the new
3581 * normal parameters and do not touch the scheduler class and
3582 * the runqueue. This will be done when the task deboost
3583 * itself.
3584 */
3585 if (rt_mutex_check_prio(p, newprio)) {
3586 __setscheduler_params(p, attr);
3587 task_rq_unlock(rq, p, &flags);
3588 return 0;
3589 }
3590
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003591 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003592 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003593 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01003594 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003595 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003596 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003597
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003598 prev_class = p->sched_class;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003599 __setscheduler(rq, p, attr);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003600
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003601 if (running)
3602 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003603 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01003604 /*
3605 * We enqueue to tail when the priority of a task is
3606 * increased (user space view).
3607 */
3608 enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
3609 }
Steven Rostedtcb469842008-01-25 21:08:22 +01003610
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003611 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003612 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003613
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07003614 rt_mutex_adjust_pi(p);
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return 0;
3617}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003618
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003619static int _sched_setscheduler(struct task_struct *p, int policy,
3620 const struct sched_param *param, bool check)
3621{
3622 struct sched_attr attr = {
3623 .sched_policy = policy,
3624 .sched_priority = param->sched_priority,
3625 .sched_nice = PRIO_TO_NICE(p->static_prio),
3626 };
3627
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003628 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
3629 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003630 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
3631 policy &= ~SCHED_RESET_ON_FORK;
3632 attr.sched_policy = policy;
3633 }
3634
3635 return __sched_setscheduler(p, &attr, check);
3636}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003637/**
3638 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
3639 * @p: the task in question.
3640 * @policy: new policy.
3641 * @param: structure containing the new RT priority.
3642 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003643 * Return: 0 on success. An error code otherwise.
3644 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10003645 * NOTE that the task may be already dead.
3646 */
3647int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003648 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003649{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003650 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652EXPORT_SYMBOL_GPL(sched_setscheduler);
3653
Dario Faggiolid50dde52013-11-07 14:43:36 +01003654int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
3655{
3656 return __sched_setscheduler(p, attr, true);
3657}
3658EXPORT_SYMBOL_GPL(sched_setattr);
3659
Rusty Russell961ccdd2008-06-23 13:55:38 +10003660/**
3661 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
3662 * @p: the task in question.
3663 * @policy: new policy.
3664 * @param: structure containing the new RT priority.
3665 *
3666 * Just like sched_setscheduler, only don't bother checking if the
3667 * current context has permission. For example, this is needed in
3668 * stop_machine(): we create temporary high priority worker threads,
3669 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003670 *
3671 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10003672 */
3673int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003674 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003675{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003676 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003677}
3678
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003679static int
3680do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 struct sched_param lparam;
3683 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003684 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 if (!param || pid < 0)
3687 return -EINVAL;
3688 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
3689 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003690
3691 rcu_read_lock();
3692 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003694 if (p != NULL)
3695 retval = sched_setscheduler(p, policy, &lparam);
3696 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return retval;
3699}
3700
Dario Faggiolid50dde52013-11-07 14:43:36 +01003701/*
3702 * Mimics kernel/events/core.c perf_copy_attr().
3703 */
3704static int sched_copy_attr(struct sched_attr __user *uattr,
3705 struct sched_attr *attr)
3706{
3707 u32 size;
3708 int ret;
3709
3710 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
3711 return -EFAULT;
3712
3713 /*
3714 * zero the full structure, so that a short copy will be nice.
3715 */
3716 memset(attr, 0, sizeof(*attr));
3717
3718 ret = get_user(size, &uattr->size);
3719 if (ret)
3720 return ret;
3721
3722 if (size > PAGE_SIZE) /* silly large */
3723 goto err_size;
3724
3725 if (!size) /* abi compat */
3726 size = SCHED_ATTR_SIZE_VER0;
3727
3728 if (size < SCHED_ATTR_SIZE_VER0)
3729 goto err_size;
3730
3731 /*
3732 * If we're handed a bigger struct than we know of,
3733 * ensure all the unknown bits are 0 - i.e. new
3734 * user-space does not rely on any kernel feature
3735 * extensions we dont know about yet.
3736 */
3737 if (size > sizeof(*attr)) {
3738 unsigned char __user *addr;
3739 unsigned char __user *end;
3740 unsigned char val;
3741
3742 addr = (void __user *)uattr + sizeof(*attr);
3743 end = (void __user *)uattr + size;
3744
3745 for (; addr < end; addr++) {
3746 ret = get_user(val, addr);
3747 if (ret)
3748 return ret;
3749 if (val)
3750 goto err_size;
3751 }
3752 size = sizeof(*attr);
3753 }
3754
3755 ret = copy_from_user(attr, uattr, size);
3756 if (ret)
3757 return -EFAULT;
3758
3759 /*
3760 * XXX: do we want to be lenient like existing syscalls; or do we want
3761 * to be strict and return an error on out-of-bounds values?
3762 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003763 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003764
Michael Kerriske78c7bc2014-05-09 16:54:28 +02003765 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003766
3767err_size:
3768 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02003769 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003770}
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772/**
3773 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
3774 * @pid: the pid in question.
3775 * @policy: new policy.
3776 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003777 *
3778 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003780SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
3781 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Jason Baronc21761f2006-01-18 17:43:03 -08003783 /* negative values for policy are not valid */
3784 if (policy < 0)
3785 return -EINVAL;
3786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 return do_sched_setscheduler(pid, policy, param);
3788}
3789
3790/**
3791 * sys_sched_setparam - set/change the RT priority of a thread
3792 * @pid: the pid in question.
3793 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003794 *
3795 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003797SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003799 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800}
3801
3802/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01003803 * sys_sched_setattr - same as above, but with extended sched_attr
3804 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01003805 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09003806 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003807 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003808SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
3809 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003810{
3811 struct sched_attr attr;
3812 struct task_struct *p;
3813 int retval;
3814
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003815 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003816 return -EINVAL;
3817
Michael Kerrisk143cf232014-05-09 16:54:15 +02003818 retval = sched_copy_attr(uattr, &attr);
3819 if (retval)
3820 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003821
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02003822 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02003823 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003824
3825 rcu_read_lock();
3826 retval = -ESRCH;
3827 p = find_process_by_pid(pid);
3828 if (p != NULL)
3829 retval = sched_setattr(p, &attr);
3830 rcu_read_unlock();
3831
3832 return retval;
3833}
3834
3835/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
3837 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003838 *
3839 * Return: On success, the policy of the thread. Otherwise, a negative error
3840 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003842SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
Ingo Molnar36c8b582006-07-03 00:25:41 -07003844 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003845 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02003848 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
3850 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003851 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 p = find_process_by_pid(pid);
3853 if (p) {
3854 retval = security_task_getscheduler(p);
3855 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02003856 retval = p->policy
3857 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003859 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 return retval;
3861}
3862
3863/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02003864 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 * @pid: the pid in question.
3866 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003867 *
3868 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
3869 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003871SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02003873 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003874 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003875 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
3877 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02003878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003880 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 p = find_process_by_pid(pid);
3882 retval = -ESRCH;
3883 if (!p)
3884 goto out_unlock;
3885
3886 retval = security_task_getscheduler(p);
3887 if (retval)
3888 goto out_unlock;
3889
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02003890 if (task_has_rt_policy(p))
3891 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003892 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 /*
3895 * This one might sleep, we cannot do it with a spinlock held ...
3896 */
3897 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return retval;
3900
3901out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00003902 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return retval;
3904}
3905
Dario Faggiolid50dde52013-11-07 14:43:36 +01003906static int sched_read_attr(struct sched_attr __user *uattr,
3907 struct sched_attr *attr,
3908 unsigned int usize)
3909{
3910 int ret;
3911
3912 if (!access_ok(VERIFY_WRITE, uattr, usize))
3913 return -EFAULT;
3914
3915 /*
3916 * If we're handed a smaller struct than we know of,
3917 * ensure all the unknown bits are 0 - i.e. old
3918 * user-space does not get uncomplete information.
3919 */
3920 if (usize < sizeof(*attr)) {
3921 unsigned char *addr;
3922 unsigned char *end;
3923
3924 addr = (void *)attr + usize;
3925 end = (void *)attr + sizeof(*attr);
3926
3927 for (; addr < end; addr++) {
3928 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02003929 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003930 }
3931
3932 attr->size = usize;
3933 }
3934
Vegard Nossum4efbc452014-02-16 22:24:17 +01003935 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003936 if (ret)
3937 return -EFAULT;
3938
Michael Kerrisk22400672014-05-09 16:54:33 +02003939 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003940}
3941
3942/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01003943 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01003944 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01003945 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003946 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09003947 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01003948 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003949SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
3950 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003951{
3952 struct sched_attr attr = {
3953 .size = sizeof(struct sched_attr),
3954 };
3955 struct task_struct *p;
3956 int retval;
3957
3958 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01003959 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01003960 return -EINVAL;
3961
3962 rcu_read_lock();
3963 p = find_process_by_pid(pid);
3964 retval = -ESRCH;
3965 if (!p)
3966 goto out_unlock;
3967
3968 retval = security_task_getscheduler(p);
3969 if (retval)
3970 goto out_unlock;
3971
3972 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003973 if (p->sched_reset_on_fork)
3974 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003975 if (task_has_dl_policy(p))
3976 __getparam_dl(p, &attr);
3977 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003978 attr.sched_priority = p->rt_priority;
3979 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003980 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003981
3982 rcu_read_unlock();
3983
3984 retval = sched_read_attr(uattr, &attr, size);
3985 return retval;
3986
3987out_unlock:
3988 rcu_read_unlock();
3989 return retval;
3990}
3991
Rusty Russell96f874e22008-11-25 02:35:14 +10303992long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10303994 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003995 struct task_struct *p;
3996 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Thomas Gleixner23f5d142009-12-09 10:15:01 +00003998 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
4000 p = find_process_by_pid(pid);
4001 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004002 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return -ESRCH;
4004 }
4005
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004006 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004008 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Tejun Heo14a40ff2013-03-19 13:45:20 -07004010 if (p->flags & PF_NO_SETAFFINITY) {
4011 retval = -EINVAL;
4012 goto out_put_task;
4013 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304014 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4015 retval = -ENOMEM;
4016 goto out_put_task;
4017 }
4018 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4019 retval = -ENOMEM;
4020 goto out_free_cpus_allowed;
4021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004023 if (!check_same_owner(p)) {
4024 rcu_read_lock();
4025 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4026 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004027 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004028 }
4029 rcu_read_unlock();
4030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004032 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004033 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004034 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004035
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004036
4037 cpuset_cpus_allowed(p, cpus_allowed);
4038 cpumask_and(new_mask, in_mask, cpus_allowed);
4039
Dario Faggioli332ac172013-11-07 14:43:45 +01004040 /*
4041 * Since bandwidth control happens on root_domain basis,
4042 * if admission test is enabled, we only admit -deadline
4043 * tasks allowed to run on all the CPUs in the task's
4044 * root_domain.
4045 */
4046#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004047 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4048 rcu_read_lock();
4049 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004050 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004051 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004052 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004053 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004054 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004055 }
4056#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004057again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304058 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Paul Menage8707d8b2007-10-18 23:40:22 -07004060 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304061 cpuset_cpus_allowed(p, cpus_allowed);
4062 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004063 /*
4064 * We must have raced with a concurrent cpuset
4065 * update. Just reset the cpus_allowed to the
4066 * cpuset's cpus_allowed
4067 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304068 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004069 goto again;
4070 }
4071 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004072out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304073 free_cpumask_var(new_mask);
4074out_free_cpus_allowed:
4075 free_cpumask_var(cpus_allowed);
4076out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return retval;
4079}
4080
4081static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304082 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Rusty Russell96f874e22008-11-25 02:35:14 +10304084 if (len < cpumask_size())
4085 cpumask_clear(new_mask);
4086 else if (len > cpumask_size())
4087 len = cpumask_size();
4088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4090}
4091
4092/**
4093 * sys_sched_setaffinity - set the cpu affinity of a process
4094 * @pid: pid of the process
4095 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4096 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004097 *
4098 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004100SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4101 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304103 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 int retval;
4105
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304106 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4107 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304109 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4110 if (retval == 0)
4111 retval = sched_setaffinity(pid, new_mask);
4112 free_cpumask_var(new_mask);
4113 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
Rusty Russell96f874e22008-11-25 02:35:14 +10304116long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004118 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004119 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004122 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 retval = -ESRCH;
4125 p = find_process_by_pid(pid);
4126 if (!p)
4127 goto out_unlock;
4128
David Quigleye7834f82006-06-23 02:03:59 -07004129 retval = security_task_getscheduler(p);
4130 if (retval)
4131 goto out_unlock;
4132
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004133 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004134 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004135 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004138 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Ulrich Drepper9531b622007-08-09 11:16:46 +02004140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
4143/**
4144 * sys_sched_getaffinity - get the cpu affinity of a process
4145 * @pid: pid of the process
4146 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4147 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004148 *
4149 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004151SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4152 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304155 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004157 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004158 return -EINVAL;
4159 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return -EINVAL;
4161
Rusty Russellf17c8602008-11-25 02:35:11 +10304162 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Rusty Russellf17c8602008-11-25 02:35:11 +10304165 ret = sched_getaffinity(pid, mask);
4166 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004167 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004168
4169 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304170 ret = -EFAULT;
4171 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004172 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304173 }
4174 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Rusty Russellf17c8602008-11-25 02:35:11 +10304176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
4179/**
4180 * sys_sched_yield - yield the current processor to other threads.
4181 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004182 * This function yields the current CPU to other tasks. If there are no
4183 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004184 *
4185 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004187SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004189 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Ingo Molnar2d723762007-10-15 17:00:12 +02004191 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004192 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 /*
4195 * Since we are going to call schedule() anyway, there's
4196 * no need to preempt or enable interrupts:
4197 */
4198 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004199 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004200 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004201 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 schedule();
4204
4205 return 0;
4206}
4207
Andrew Mortone7b38402006-06-30 01:56:00 -07004208static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004210 __preempt_count_add(PREEMPT_ACTIVE);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004211 __schedule();
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004212 __preempt_count_sub(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Herbert Xu02b67cc32008-01-25 21:08:28 +01004215int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004217 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 __cond_resched();
4219 return 1;
4220 }
4221 return 0;
4222}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004223EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004226 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 * call schedule, and on return reacquire the lock.
4228 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004229 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 * operations here to prevent schedule() from being called twice (once via
4231 * spin_unlock(), once by hand).
4232 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004233int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004235 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004236 int ret = 0;
4237
Peter Zijlstraf607c662009-07-20 19:16:29 +02004238 lockdep_assert_held(lock);
4239
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004240 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004242 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01004243 __cond_resched();
4244 else
4245 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004246 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004251EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004253int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 BUG_ON(!in_softirq());
4256
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004257 if (should_resched()) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004258 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 __cond_resched();
4260 local_bh_disable();
4261 return 1;
4262 }
4263 return 0;
4264}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004265EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267/**
4268 * yield - yield the current processor to other threads.
4269 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004270 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4271 *
4272 * The scheduler is at all times free to pick the calling task as the most
4273 * eligible task to run, if removing the yield() call from your code breaks
4274 * it, its already broken.
4275 *
4276 * Typical broken usage is:
4277 *
4278 * while (!event)
4279 * yield();
4280 *
4281 * where one assumes that yield() will let 'the other' process run that will
4282 * make event true. If the current task is a SCHED_FIFO task that will never
4283 * happen. Never use yield() as a progress guarantee!!
4284 *
4285 * If you want to use yield() to wait for something, use wait_event().
4286 * If you want to use yield() to be 'nice' for others, use cond_resched().
4287 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 */
4289void __sched yield(void)
4290{
4291 set_current_state(TASK_RUNNING);
4292 sys_sched_yield();
4293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294EXPORT_SYMBOL(yield);
4295
Mike Galbraithd95f4122011-02-01 09:50:51 -05004296/**
4297 * yield_to - yield the current processor to another thread in
4298 * your thread group, or accelerate that thread toward the
4299 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004300 * @p: target task
4301 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004302 *
4303 * It's the caller's job to ensure that the target task struct
4304 * can't go away on us before we can do any checks.
4305 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004306 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304307 * true (>0) if we indeed boosted the target task.
4308 * false (0) if we failed to boost the target.
4309 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004310 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004311int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004312{
4313 struct task_struct *curr = current;
4314 struct rq *rq, *p_rq;
4315 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004316 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004317
4318 local_irq_save(flags);
4319 rq = this_rq();
4320
4321again:
4322 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304323 /*
4324 * If we're the only runnable task on the rq and target rq also
4325 * has only one task, there's absolutely no point in yielding.
4326 */
4327 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4328 yielded = -ESRCH;
4329 goto out_irq;
4330 }
4331
Mike Galbraithd95f4122011-02-01 09:50:51 -05004332 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004333 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004334 double_rq_unlock(rq, p_rq);
4335 goto again;
4336 }
4337
4338 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304339 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004340
4341 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304342 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004343
4344 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304345 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004346
4347 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004348 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004349 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004350 /*
4351 * Make p's CPU reschedule; pick_next_entity takes care of
4352 * fairness.
4353 */
4354 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004355 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004356 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004357
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304358out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004359 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304360out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004361 local_irq_restore(flags);
4362
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304363 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004364 schedule();
4365
4366 return yielded;
4367}
4368EXPORT_SYMBOL_GPL(yield_to);
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004371 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 */
4374void __sched io_schedule(void)
4375{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004376 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004378 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01004380 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004381 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004383 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004385 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387EXPORT_SYMBOL(io_schedule);
4388
4389long __sched io_schedule_timeout(long timeout)
4390{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004391 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 long ret;
4393
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004394 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01004396 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004397 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004399 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004401 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 return ret;
4403}
4404
4405/**
4406 * sys_sched_get_priority_max - return maximum RT priority.
4407 * @policy: scheduling class.
4408 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004409 * Return: On success, this syscall returns the maximum
4410 * rt_priority that can be used by a given scheduling class.
4411 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004413SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
4415 int ret = -EINVAL;
4416
4417 switch (policy) {
4418 case SCHED_FIFO:
4419 case SCHED_RR:
4420 ret = MAX_USER_RT_PRIO-1;
4421 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004422 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004424 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004425 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 ret = 0;
4427 break;
4428 }
4429 return ret;
4430}
4431
4432/**
4433 * sys_sched_get_priority_min - return minimum RT priority.
4434 * @policy: scheduling class.
4435 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004436 * Return: On success, this syscall returns the minimum
4437 * rt_priority that can be used by a given scheduling class.
4438 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004440SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
4442 int ret = -EINVAL;
4443
4444 switch (policy) {
4445 case SCHED_FIFO:
4446 case SCHED_RR:
4447 ret = 1;
4448 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004449 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004451 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004452 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 ret = 0;
4454 }
4455 return ret;
4456}
4457
4458/**
4459 * sys_sched_rr_get_interval - return the default timeslice of a process.
4460 * @pid: pid of the process.
4461 * @interval: userspace pointer to the timeslice value.
4462 *
4463 * this syscall writes the default timeslice value of a given process
4464 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004465 *
4466 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4467 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004469SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004470 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004472 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004473 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004474 unsigned long flags;
4475 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004476 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004483 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 p = find_process_by_pid(pid);
4485 if (!p)
4486 goto out_unlock;
4487
4488 retval = security_task_getscheduler(p);
4489 if (retval)
4490 goto out_unlock;
4491
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004492 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004493 time_slice = 0;
4494 if (p->sched_class->get_rr_interval)
4495 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004496 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004497
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004498 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004499 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004504 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return retval;
4506}
4507
Steven Rostedt7c731e02008-05-12 21:20:41 +02004508static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004509
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004510void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004513 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004514 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004516 if (state)
4517 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08004518 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004519 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004520#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004522 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004524 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525#else
4526 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004527 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004529 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530#endif
4531#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004532 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004534 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004535 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08004536 if (pid_alive(p))
4537 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08004538 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004539 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08004540 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07004541 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Tejun Heo3d1cb202013-04-30 15:27:22 -07004543 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01004544 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004547void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004549 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Ingo Molnar4bd77322007-07-11 21:21:47 +02004551#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004552 printk(KERN_INFO
4553 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004555 printk(KERN_INFO
4556 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004558 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004559 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 /*
4561 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004562 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 */
4564 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004565 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004566 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004569 touch_all_softlockup_watchdogs();
4570
Ingo Molnardd41f592007-07-09 18:51:59 +02004571#ifdef CONFIG_SCHED_DEBUG
4572 sysrq_sched_debug_show();
4573#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004574 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004575 /*
4576 * Only show locks if all tasks are dumped:
4577 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02004578 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004579 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
Paul Gortmaker0db06282013-06-19 14:53:51 -04004582void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02004583{
Ingo Molnardd41f592007-07-09 18:51:59 +02004584 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004585}
4586
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004587/**
4588 * init_idle - set up an idle thread for a given CPU
4589 * @idle: task in question
4590 * @cpu: cpu the idle task belongs to
4591 *
4592 * NOTE: this function does not set the idle thread's NEED_RESCHED
4593 * flag, to make booting more robust.
4594 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004595void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004597 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 unsigned long flags;
4599
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004600 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01004601
Rik van Riel5e1576e2013-10-07 11:29:26 +01004602 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01004603 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02004604 idle->se.exec_start = sched_clock();
4605
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004606 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004607 /*
4608 * We're having a chicken and egg problem, even though we are
4609 * holding rq->lock, the cpu isn't yet set to this cpu so the
4610 * lockdep check in task_group() will fail.
4611 *
4612 * Similar case to sched_fork(). / Alternatively we could
4613 * use task_rq_lock() here and obtain the other rq->lock.
4614 *
4615 * Silence PROVE_RCU
4616 */
4617 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02004618 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004622 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02004623#if defined(CONFIG_SMP)
4624 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07004625#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004626 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02004629 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06004630
Ingo Molnardd41f592007-07-09 18:51:59 +02004631 /*
4632 * The idle tasks have their own, simple scheduling class:
4633 */
4634 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05004635 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02004636 vtime_init_idle(idle, cpu);
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02004637#if defined(CONFIG_SMP)
4638 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
4639#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
Juri Lellif82f8042014-10-07 09:52:11 +01004642int cpuset_cpumask_can_shrink(const struct cpumask *cur,
4643 const struct cpumask *trial)
4644{
4645 int ret = 1, trial_cpus;
4646 struct dl_bw *cur_dl_b;
4647 unsigned long flags;
4648
Juri Lelli75e23e492014-10-28 11:54:46 +00004649 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004650 cur_dl_b = dl_bw_of(cpumask_any(cur));
4651 trial_cpus = cpumask_weight(trial);
4652
4653 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
4654 if (cur_dl_b->bw != -1 &&
4655 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
4656 ret = 0;
4657 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004658 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004659
4660 return ret;
4661}
4662
Juri Lelli7f514122014-09-19 10:22:40 +01004663int task_can_attach(struct task_struct *p,
4664 const struct cpumask *cs_cpus_allowed)
4665{
4666 int ret = 0;
4667
4668 /*
4669 * Kthreads which disallow setaffinity shouldn't be moved
4670 * to a new cpuset; we don't want to change their cpu
4671 * affinity and isolating such threads by their set of
4672 * allowed nodes is unnecessary. Thus, cpusets are not
4673 * applicable for such threads. This prevents checking for
4674 * success of set_cpus_allowed_ptr() on all attached tasks
4675 * before cpus_allowed may be changed.
4676 */
4677 if (p->flags & PF_NO_SETAFFINITY) {
4678 ret = -EINVAL;
4679 goto out;
4680 }
4681
4682#ifdef CONFIG_SMP
4683 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
4684 cs_cpus_allowed)) {
4685 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
4686 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00004687 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01004688 bool overflow;
4689 int cpus;
4690 unsigned long flags;
4691
Juri Lelli75e23e492014-10-28 11:54:46 +00004692 rcu_read_lock_sched();
4693 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01004694 raw_spin_lock_irqsave(&dl_b->lock, flags);
4695 cpus = dl_bw_cpus(dest_cpu);
4696 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
4697 if (overflow)
4698 ret = -EBUSY;
4699 else {
4700 /*
4701 * We reserve space for this task in the destination
4702 * root_domain, as we can't fail after this point.
4703 * We will free resources in the source root_domain
4704 * later on (see set_cpus_allowed_dl()).
4705 */
4706 __dl_add(dl_b, p->dl.dl_bw);
4707 }
4708 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004709 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01004710
4711 }
4712#endif
4713out:
4714 return ret;
4715}
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717#ifdef CONFIG_SMP
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004718/*
4719 * move_queued_task - move a queued task to new rq.
4720 *
4721 * Returns (locked) new rq. Old rq's lock is released.
4722 */
4723static struct rq *move_queued_task(struct task_struct *p, int new_cpu)
4724{
4725 struct rq *rq = task_rq(p);
4726
4727 lockdep_assert_held(&rq->lock);
4728
4729 dequeue_task(rq, p, 0);
4730 p->on_rq = TASK_ON_RQ_MIGRATING;
4731 set_task_cpu(p, new_cpu);
4732 raw_spin_unlock(&rq->lock);
4733
4734 rq = cpu_rq(new_cpu);
4735
4736 raw_spin_lock(&rq->lock);
4737 BUG_ON(task_cpu(p) != new_cpu);
4738 p->on_rq = TASK_ON_RQ_QUEUED;
4739 enqueue_task(rq, p, 0);
4740 check_preempt_curr(rq, p, 0);
4741
4742 return rq;
4743}
4744
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004745void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
4746{
Yao Dongdong1b537c72014-12-29 14:41:43 +08004747 if (p->sched_class->set_cpus_allowed)
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004748 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra49396022011-06-25 15:45:46 +02004749
4750 cpumask_copy(&p->cpus_allowed, new_mask);
Peter Zijlstra29baa742012-04-23 12:11:21 +02004751 p->nr_cpus_allowed = cpumask_weight(new_mask);
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004752}
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754/*
4755 * This is how migration works:
4756 *
Tejun Heo969c7922010-05-06 18:49:21 +02004757 * 1) we invoke migration_cpu_stop() on the target CPU using
4758 * stop_one_cpu().
4759 * 2) stopper starts to run (implicitly forcing the migrated thread
4760 * off the CPU)
4761 * 3) it checks whether the migrated task is still in the wrong runqueue.
4762 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02004764 * 5) stopper completes and stop_one_cpu() returns and the migration
4765 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 */
4767
4768/*
4769 * Change a given task's CPU affinity. Migrate the thread to a
4770 * proper CPU and schedule it away if the CPU it's executing on
4771 * is removed from the allowed bitmask.
4772 *
4773 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004774 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 * call is not atomic; no spinlocks may be held.
4776 */
Rusty Russell96f874e22008-11-25 02:35:14 +10304777int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
4779 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004780 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02004781 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004782 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
4784 rq = task_rq_lock(p, &flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01004785
Yong Zhangdb44fc02011-05-09 22:07:05 +08004786 if (cpumask_equal(&p->cpus_allowed, new_mask))
4787 goto out;
4788
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01004789 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 ret = -EINVAL;
4791 goto out;
4792 }
4793
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004794 do_set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01004795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e22008-11-25 02:35:14 +10304797 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 goto out;
4799
Tejun Heo969c7922010-05-06 18:49:21 +02004800 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004801 if (task_running(rq, p) || p->state == TASK_WAKING) {
Tejun Heo969c7922010-05-06 18:49:21 +02004802 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004804 task_rq_unlock(rq, p, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004805 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 tlb_migrate_finish(p->mm);
4807 return 0;
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004808 } else if (task_on_rq_queued(p))
4809 rq = move_queued_task(p, dest_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810out:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004811 task_rq_unlock(rq, p, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return ret;
4814}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07004815EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
4817/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004818 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 * this because either it can't run here any more (set_cpus_allowed()
4820 * away from this CPU, or CPU going down), or because we're
4821 * attempting to rebalance this task on exec (sched_exec).
4822 *
4823 * So we race with normal scheduler movements, but that's OK, as long
4824 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004825 *
4826 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004828static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004830 struct rq *rq;
Peter Zijlstrae2912002009-12-16 18:04:36 +01004831 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Max Krasnyanskye761b772008-07-15 04:43:49 -07004833 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004836 rq = cpu_rq(src_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004838 raw_spin_lock(&p->pi_lock);
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004839 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 /* Already moved. */
4841 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004842 goto done;
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* Affinity changed (again). */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004845 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004846 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Peter Zijlstrae2912002009-12-16 18:04:36 +01004848 /*
4849 * If we're not on a rq, the next wake-up will ensure we're
4850 * placed properly.
4851 */
Kirill Tkhaia15b12ac2014-09-12 15:03:34 +04004852 if (task_on_rq_queued(p))
4853 rq = move_queued_task(p, dest_cpu);
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004854done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07004855 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07004856fail:
Kirill Tkhaia1e01822014-08-20 13:47:50 +04004857 raw_spin_unlock(&rq->lock);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004858 raw_spin_unlock(&p->pi_lock);
Kirill Korotaevefc30812006-06-27 02:54:32 -07004859 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
Mel Gormane6628d52013-10-07 11:29:02 +01004862#ifdef CONFIG_NUMA_BALANCING
4863/* Migrate current task p to target_cpu */
4864int migrate_task_to(struct task_struct *p, int target_cpu)
4865{
4866 struct migration_arg arg = { p, target_cpu };
4867 int curr_cpu = task_cpu(p);
4868
4869 if (curr_cpu == target_cpu)
4870 return 0;
4871
4872 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
4873 return -EINVAL;
4874
4875 /* TODO: This is not properly updating schedstats */
4876
Mel Gorman286549d2014-01-21 15:51:03 -08004877 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01004878 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
4879}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004880
4881/*
4882 * Requeue a task on a given node and accurately track the number of NUMA
4883 * tasks on the runqueues
4884 */
4885void sched_setnuma(struct task_struct *p, int nid)
4886{
4887 struct rq *rq;
4888 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004889 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004890
4891 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004892 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004893 running = task_current(rq, p);
4894
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004895 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004896 dequeue_task(rq, p, 0);
4897 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004898 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004899
4900 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004901
4902 if (running)
4903 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004904 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004905 enqueue_task(rq, p, 0);
4906 task_rq_unlock(rq, p, &flags);
4907}
Mel Gormane6628d52013-10-07 11:29:02 +01004908#endif
4909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910/*
Tejun Heo969c7922010-05-06 18:49:21 +02004911 * migration_cpu_stop - this will be executed by a highprio stopper thread
4912 * and performs thread migration by bumping thread off CPU then
4913 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 */
Tejun Heo969c7922010-05-06 18:49:21 +02004915static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
Tejun Heo969c7922010-05-06 18:49:21 +02004917 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Tejun Heo969c7922010-05-06 18:49:21 +02004919 /*
4920 * The original target cpu might have gone down and we might
4921 * be on another cpu but it doesn't matter.
4922 */
4923 local_irq_disable();
Lai Jiangshan5cd038f2014-06-04 16:25:15 +08004924 /*
4925 * We need to explicitly wake pending tasks before running
4926 * __migrate_task() such that we will not miss enforcing cpus_allowed
4927 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
4928 */
4929 sched_ttwu_pending();
Tejun Heo969c7922010-05-06 18:49:21 +02004930 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
4931 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return 0;
4933}
4934
4935#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Ingo Molnar48f24c42006-07-03 00:25:40 -07004937/*
4938 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 * offline.
4940 */
4941void idle_task_exit(void)
4942{
4943 struct mm_struct *mm = current->active_mm;
4944
4945 BUG_ON(cpu_online(smp_processor_id()));
4946
Martin Schwidefskya53efe52012-10-26 17:17:44 +02004947 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02004949 finish_arch_post_lock_switch();
4950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 mmdrop(mm);
4952}
4953
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004954/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02004955 * Since this CPU is going 'away' for a while, fold any nr_active delta
4956 * we might have. Assumes we're called after migrate_tasks() so that the
4957 * nr_active count is stable.
4958 *
4959 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004960 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02004961static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
Peter Zijlstra5d180232012-08-20 11:26:57 +02004963 long delta = calc_load_fold_active(rq);
4964 if (delta)
4965 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02004966}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004967
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01004968static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
4969{
4970}
4971
4972static const struct sched_class fake_sched_class = {
4973 .put_prev_task = put_prev_task_fake,
4974};
4975
4976static struct task_struct fake_task = {
4977 /*
4978 * Avoid pull_{rt,dl}_task()
4979 */
4980 .prio = MAX_PRIO + 1,
4981 .sched_class = &fake_sched_class,
4982};
4983
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01004984/*
4985 * Migrate all tasks from the rq, sleeping tasks will be migrated by
4986 * try_to_wake_up()->select_task_rq().
4987 *
4988 * Called with rq->lock held even though we'er in stop_machine() and
4989 * there's no concurrency possible, we hold the required locks anyway
4990 * because of lock validation efforts.
4991 */
4992static void migrate_tasks(unsigned int dead_cpu)
4993{
4994 struct rq *rq = cpu_rq(dead_cpu);
4995 struct task_struct *next, *stop = rq->stop;
4996 int dest_cpu;
4997
4998 /*
4999 * Fudge the rq selection such that the below task selection loop
5000 * doesn't get stuck on the currently eligible stop task.
5001 *
5002 * We're currently inside stop_machine() and the rq is either stuck
5003 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5004 * either way we should never end up calling schedule() until we're
5005 * done here.
5006 */
5007 rq->stop = NULL;
5008
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005009 /*
5010 * put_prev_task() and pick_next_task() sched
5011 * class method both need to have an up-to-date
5012 * value of rq->clock[_task]
5013 */
5014 update_rq_clock(rq);
5015
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005016 for ( ; ; ) {
5017 /*
5018 * There's this thread running, bail when that's the only
5019 * remaining thread.
5020 */
5021 if (rq->nr_running == 1)
5022 break;
5023
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005024 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005025 BUG_ON(!next);
5026 next->sched_class->put_prev_task(rq, next);
5027
5028 /* Find suitable destination for @next, with force if needed. */
5029 dest_cpu = select_fallback_rq(dead_cpu, next);
5030 raw_spin_unlock(&rq->lock);
5031
5032 __migrate_task(next, dead_cpu, dest_cpu);
5033
5034 raw_spin_lock(&rq->lock);
5035 }
5036
5037 rq->stop = stop;
5038}
5039
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040#endif /* CONFIG_HOTPLUG_CPU */
5041
Nick Piggine692ab52007-07-26 13:40:43 +02005042#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5043
5044static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005045 {
5046 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005047 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005048 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005049 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005050};
5051
5052static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005053 {
5054 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005055 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005056 .child = sd_ctl_dir,
5057 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005058 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005059};
5060
5061static struct ctl_table *sd_alloc_ctl_entry(int n)
5062{
5063 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005064 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005065
Nick Piggine692ab52007-07-26 13:40:43 +02005066 return entry;
5067}
5068
Milton Miller6382bc92007-10-15 17:00:19 +02005069static void sd_free_ctl_entry(struct ctl_table **tablep)
5070{
Milton Millercd7900762007-10-17 16:55:11 +02005071 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005072
Milton Millercd7900762007-10-17 16:55:11 +02005073 /*
5074 * In the intermediate directories, both the child directory and
5075 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005076 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005077 * static strings and all have proc handlers.
5078 */
5079 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005080 if (entry->child)
5081 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005082 if (entry->proc_handler == NULL)
5083 kfree(entry->procname);
5084 }
Milton Miller6382bc92007-10-15 17:00:19 +02005085
5086 kfree(*tablep);
5087 *tablep = NULL;
5088}
5089
Namhyung Kim201c3732012-08-16 17:03:24 +09005090static int min_load_idx = 0;
libinfd9b86d2013-04-08 14:39:12 +08005091static int max_load_idx = CPU_LOAD_IDX_MAX-1;
Namhyung Kim201c3732012-08-16 17:03:24 +09005092
Nick Piggine692ab52007-07-26 13:40:43 +02005093static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005094set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005095 const char *procname, void *data, int maxlen,
Namhyung Kim201c3732012-08-16 17:03:24 +09005096 umode_t mode, proc_handler *proc_handler,
5097 bool load_idx)
Nick Piggine692ab52007-07-26 13:40:43 +02005098{
Nick Piggine692ab52007-07-26 13:40:43 +02005099 entry->procname = procname;
5100 entry->data = data;
5101 entry->maxlen = maxlen;
5102 entry->mode = mode;
5103 entry->proc_handler = proc_handler;
Namhyung Kim201c3732012-08-16 17:03:24 +09005104
5105 if (load_idx) {
5106 entry->extra1 = &min_load_idx;
5107 entry->extra2 = &max_load_idx;
5108 }
Nick Piggine692ab52007-07-26 13:40:43 +02005109}
5110
5111static struct ctl_table *
5112sd_alloc_ctl_domain_table(struct sched_domain *sd)
5113{
Alex Shi37e6bae2014-01-23 18:39:54 +08005114 struct ctl_table *table = sd_alloc_ctl_entry(14);
Nick Piggine692ab52007-07-26 13:40:43 +02005115
Milton Millerad1cdc12007-10-15 17:00:19 +02005116 if (table == NULL)
5117 return NULL;
5118
Alexey Dobriyane0361852007-08-09 11:16:46 +02005119 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005120 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005121 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005122 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005123 set_table_entry(&table[2], "busy_idx", &sd->busy_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[3], "idle_idx", &sd->idle_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[4], "newidle_idx", &sd->newidle_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[5], "wake_idx", &sd->wake_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005130 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005131 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005132 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005133 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Namhyung Kim201c3732012-08-16 17:03:24 +09005134 sizeof(int), 0644, proc_dointvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005135 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Namhyung Kim201c3732012-08-16 17:03:24 +09005136 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005137 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005138 &sd->cache_nice_tries,
Namhyung Kim201c3732012-08-16 17:03:24 +09005139 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005140 set_table_entry(&table[10], "flags", &sd->flags,
Namhyung Kim201c3732012-08-16 17:03:24 +09005141 sizeof(int), 0644, proc_dointvec_minmax, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005142 set_table_entry(&table[11], "max_newidle_lb_cost",
5143 &sd->max_newidle_lb_cost,
5144 sizeof(long), 0644, proc_doulongvec_minmax, false);
5145 set_table_entry(&table[12], "name", sd->name,
Namhyung Kim201c3732012-08-16 17:03:24 +09005146 CORENAME_MAX_SIZE, 0444, proc_dostring, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005147 /* &table[13] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005148
5149 return table;
5150}
5151
Joe Perchesbe7002e2013-06-12 11:55:36 -07005152static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005153{
5154 struct ctl_table *entry, *table;
5155 struct sched_domain *sd;
5156 int domain_num = 0, i;
5157 char buf[32];
5158
5159 for_each_domain(cpu, sd)
5160 domain_num++;
5161 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005162 if (table == NULL)
5163 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005164
5165 i = 0;
5166 for_each_domain(cpu, sd) {
5167 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005168 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005169 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005170 entry->child = sd_alloc_ctl_domain_table(sd);
5171 entry++;
5172 i++;
5173 }
5174 return table;
5175}
5176
5177static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005178static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005179{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005180 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005181 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5182 char buf[32];
5183
Milton Miller73785472007-10-24 18:23:48 +02005184 WARN_ON(sd_ctl_dir[0].child);
5185 sd_ctl_dir[0].child = entry;
5186
Milton Millerad1cdc12007-10-15 17:00:19 +02005187 if (entry == NULL)
5188 return;
5189
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005190 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005191 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005192 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005193 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005194 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005195 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005196 }
Milton Miller73785472007-10-24 18:23:48 +02005197
5198 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005199 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5200}
Milton Miller6382bc92007-10-15 17:00:19 +02005201
Milton Miller73785472007-10-24 18:23:48 +02005202/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005203static void unregister_sched_domain_sysctl(void)
5204{
Milton Miller73785472007-10-24 18:23:48 +02005205 if (sd_sysctl_header)
5206 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005207 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005208 if (sd_ctl_dir[0].child)
5209 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005210}
Nick Piggine692ab52007-07-26 13:40:43 +02005211#else
Milton Miller6382bc92007-10-15 17:00:19 +02005212static void register_sched_domain_sysctl(void)
5213{
5214}
5215static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005216{
5217}
5218#endif
5219
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005220static void set_rq_online(struct rq *rq)
5221{
5222 if (!rq->online) {
5223 const struct sched_class *class;
5224
Rusty Russellc6c49272008-11-25 02:35:05 +10305225 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005226 rq->online = 1;
5227
5228 for_each_class(class) {
5229 if (class->rq_online)
5230 class->rq_online(rq);
5231 }
5232 }
5233}
5234
5235static void set_rq_offline(struct rq *rq)
5236{
5237 if (rq->online) {
5238 const struct sched_class *class;
5239
5240 for_each_class(class) {
5241 if (class->rq_offline)
5242 class->rq_offline(rq);
5243 }
5244
Rusty Russellc6c49272008-11-25 02:35:05 +10305245 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005246 rq->online = 0;
5247 }
5248}
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250/*
5251 * migration_call - callback that gets triggered when a CPU is added.
5252 * Here we can start up the necessary migration thread for the new CPU.
5253 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005254static int
Ingo Molnar48f24c42006-07-03 00:25:40 -07005255migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005257 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005259 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005261 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005264 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005268 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005269 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005270 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305271 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005272
5273 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005274 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005275 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005279 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02005280 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01005281 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005282 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005283 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305284 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005285 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005286 }
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005287 migrate_tasks(cpu);
5288 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005289 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005290 break;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005291
Peter Zijlstra5d180232012-08-20 11:26:57 +02005292 case CPU_DEAD:
Peter Zijlstraf319da02012-08-20 11:26:57 +02005293 calc_load_migrate(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295#endif
5296 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005297
5298 update_max_interval();
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 return NOTIFY_OK;
5301}
5302
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005303/*
5304 * Register at high priority so that task migration (migrate_all_tasks)
5305 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005308static struct notifier_block migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005310 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311};
5312
Corey Minyarda803f022014-05-08 13:47:39 -05005313static void __cpuinit set_cpu_rq_start_time(void)
5314{
5315 int cpu = smp_processor_id();
5316 struct rq *rq = cpu_rq(cpu);
5317 rq->age_stamp = sched_clock_cpu(cpu);
5318}
5319
Paul Gortmaker0db06282013-06-19 14:53:51 -04005320static int sched_cpu_active(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005321 unsigned long action, void *hcpu)
5322{
5323 switch (action & ~CPU_TASKS_FROZEN) {
Corey Minyarda803f022014-05-08 13:47:39 -05005324 case CPU_STARTING:
5325 set_cpu_rq_start_time();
5326 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005327 case CPU_DOWN_FAILED:
5328 set_cpu_active((long)hcpu, true);
5329 return NOTIFY_OK;
5330 default:
5331 return NOTIFY_DONE;
5332 }
5333}
5334
Paul Gortmaker0db06282013-06-19 14:53:51 -04005335static int sched_cpu_inactive(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005336 unsigned long action, void *hcpu)
5337{
Peter Zijlstrade212f12013-12-19 11:54:45 +01005338 unsigned long flags;
5339 long cpu = (long)hcpu;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005340 struct dl_bw *dl_b;
Peter Zijlstrade212f12013-12-19 11:54:45 +01005341
Tejun Heo3a101d02010-06-08 21:40:36 +02005342 switch (action & ~CPU_TASKS_FROZEN) {
5343 case CPU_DOWN_PREPARE:
Peter Zijlstrade212f12013-12-19 11:54:45 +01005344 set_cpu_active(cpu, false);
5345
5346 /* explicitly allow suspend */
5347 if (!(action & CPU_TASKS_FROZEN)) {
Peter Zijlstrade212f12013-12-19 11:54:45 +01005348 bool overflow;
5349 int cpus;
5350
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005351 rcu_read_lock_sched();
5352 dl_b = dl_bw_of(cpu);
5353
Peter Zijlstrade212f12013-12-19 11:54:45 +01005354 raw_spin_lock_irqsave(&dl_b->lock, flags);
5355 cpus = dl_bw_cpus(cpu);
5356 overflow = __dl_overflow(dl_b, cpus, 0, 0);
5357 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
5358
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04005359 rcu_read_unlock_sched();
5360
Peter Zijlstrade212f12013-12-19 11:54:45 +01005361 if (overflow)
5362 return notifier_from_errno(-EBUSY);
5363 }
Tejun Heo3a101d02010-06-08 21:40:36 +02005364 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005365 }
Peter Zijlstrade212f12013-12-19 11:54:45 +01005366
5367 return NOTIFY_DONE;
Tejun Heo3a101d02010-06-08 21:40:36 +02005368}
5369
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005370static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
5372 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005373 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005374
Tejun Heo3a101d02010-06-08 21:40:36 +02005375 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005376 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5377 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5379 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005380
Tejun Heo3a101d02010-06-08 21:40:36 +02005381 /* Register cpu active notifiers */
5382 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5383 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5384
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005387early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388#endif
5389
5390#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005391
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005392static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5393
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005394#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005395
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005396static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005397
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005398static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005399{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005400 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005401
5402 return 0;
5403}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005404early_param("sched_debug", sched_debug_setup);
5405
5406static inline bool sched_debug(void)
5407{
5408 return sched_debug_enabled;
5409}
Mike Travisf6630112009-11-17 18:22:15 -06005410
Mike Travis7c16ec52008-04-04 18:11:11 -07005411static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305412 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005413{
5414 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07005415 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005416
Rusty Russell968ea6d2008-12-13 21:55:51 +10305417 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e22008-11-25 02:35:14 +10305418 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005419
5420 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5421
5422 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005423 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005424 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005425 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5426 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005427 return -1;
5428 }
5429
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005430 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005431
Rusty Russell758b2cd2008-11-25 02:35:04 +10305432 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005433 printk(KERN_ERR "ERROR: domain->span does not contain "
5434 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005435 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305436 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005437 printk(KERN_ERR "ERROR: domain->groups does not contain"
5438 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005439 }
5440
5441 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5442 do {
5443 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005444 printk("\n");
5445 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005446 break;
5447 }
5448
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005449 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005450 * Even though we initialize ->capacity to something semi-sane,
5451 * we leave capacity_orig unset. This allows us to detect if
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005452 * domain iteration is still funny without causing /0 traps.
5453 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005454 if (!group->sgc->capacity_orig) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005455 printk(KERN_CONT "\n");
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005456 printk(KERN_ERR "ERROR: domain->cpu_capacity not set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005457 break;
5458 }
5459
Rusty Russell758b2cd2008-11-25 02:35:04 +10305460 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005461 printk(KERN_CONT "\n");
5462 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005463 break;
5464 }
5465
Peter Zijlstracb83b622012-04-17 15:49:36 +02005466 if (!(sd->flags & SD_OVERLAP) &&
5467 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005468 printk(KERN_CONT "\n");
5469 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005470 break;
5471 }
5472
Rusty Russell758b2cd2008-11-25 02:35:04 +10305473 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005474
Rusty Russell968ea6d2008-12-13 21:55:51 +10305475 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305476
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005477 printk(KERN_CONT " %s", str);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005478 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005479 printk(KERN_CONT " (cpu_capacity = %d)",
5480 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305481 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005482
5483 group = group->next;
5484 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005485 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005486
Rusty Russell758b2cd2008-11-25 02:35:04 +10305487 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005488 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005489
Rusty Russell758b2cd2008-11-25 02:35:04 +10305490 if (sd->parent &&
5491 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005492 printk(KERN_ERR "ERROR: parent span is not a superset "
5493 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005494 return 0;
5495}
5496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497static void sched_domain_debug(struct sched_domain *sd, int cpu)
5498{
5499 int level = 0;
5500
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005501 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005502 return;
5503
Nick Piggin41c7ce92005-06-25 14:57:24 -07005504 if (!sd) {
5505 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5506 return;
5507 }
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5510
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005511 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005512 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 level++;
5515 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005516 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005517 break;
5518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005520#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005521# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005522static inline bool sched_debug(void)
5523{
5524 return false;
5525}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005526#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005528static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005529{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305530 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005531 return 1;
5532
5533 /* Following flags need at least 2 groups */
5534 if (sd->flags & (SD_LOAD_BALANCE |
5535 SD_BALANCE_NEWIDLE |
5536 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005537 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005538 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005539 SD_SHARE_PKG_RESOURCES |
5540 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005541 if (sd->groups != sd->groups->next)
5542 return 0;
5543 }
5544
5545 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005546 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005547 return 0;
5548
5549 return 1;
5550}
5551
Ingo Molnar48f24c42006-07-03 00:25:40 -07005552static int
5553sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005554{
5555 unsigned long cflags = sd->flags, pflags = parent->flags;
5556
5557 if (sd_degenerate(parent))
5558 return 1;
5559
Rusty Russell758b2cd2008-11-25 02:35:04 +10305560 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005561 return 0;
5562
Suresh Siddha245af2c2005-06-25 14:57:25 -07005563 /* Flags needing groups don't count if only 1 group in parent */
5564 if (parent->groups == parent->groups->next) {
5565 pflags &= ~(SD_LOAD_BALANCE |
5566 SD_BALANCE_NEWIDLE |
5567 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005568 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005569 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005570 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005571 SD_PREFER_SIBLING |
5572 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005573 if (nr_node_ids == 1)
5574 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005575 }
5576 if (~cflags & pflags)
5577 return 0;
5578
5579 return 1;
5580}
5581
Peter Zijlstradce840a2011-04-07 14:09:50 +02005582static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305583{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005584 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005585
Rusty Russell68e74562008-11-25 02:35:13 +10305586 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005587 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005588 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305589 free_cpumask_var(rd->rto_mask);
5590 free_cpumask_var(rd->online);
5591 free_cpumask_var(rd->span);
5592 kfree(rd);
5593}
5594
Gregory Haskins57d885f2008-01-25 21:08:18 +01005595static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5596{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005597 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005598 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005599
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005600 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005601
5602 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005603 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005604
Rusty Russellc6c49272008-11-25 02:35:05 +10305605 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005606 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005607
Rusty Russellc6c49272008-11-25 02:35:05 +10305608 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005609
Ingo Molnara0490fa2009-02-12 11:35:40 +01005610 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005611 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005612 * set old_rd to NULL to skip the freeing later
5613 * in this function:
5614 */
5615 if (!atomic_dec_and_test(&old_rd->refcount))
5616 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005617 }
5618
5619 atomic_inc(&rd->refcount);
5620 rq->rd = rd;
5621
Rusty Russellc6c49272008-11-25 02:35:05 +10305622 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005623 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005624 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005625
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005626 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005627
5628 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005629 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005630}
5631
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005632static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005633{
5634 memset(rd, 0, sizeof(*rd));
5635
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005636 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005637 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005638 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305639 goto free_span;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005640 if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305641 goto free_online;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005642 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
5643 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005644
Dario Faggioli332ac172013-11-07 14:43:45 +01005645 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005646 if (cpudl_init(&rd->cpudl) != 0)
5647 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005648
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005649 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305650 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305651 return 0;
5652
Rusty Russell68e74562008-11-25 02:35:13 +10305653free_rto_mask:
5654 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005655free_dlo_mask:
5656 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305657free_online:
5658 free_cpumask_var(rd->online);
5659free_span:
5660 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005661out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305662 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005663}
5664
Peter Zijlstra029632f2011-10-25 10:00:11 +02005665/*
5666 * By default the system creates a single root-domain with all cpus as
5667 * members (mimicking the global state we have today).
5668 */
5669struct root_domain def_root_domain;
5670
Gregory Haskins57d885f2008-01-25 21:08:18 +01005671static void init_defrootdomain(void)
5672{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005673 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305674
Gregory Haskins57d885f2008-01-25 21:08:18 +01005675 atomic_set(&def_root_domain.refcount, 1);
5676}
5677
Gregory Haskinsdc938522008-01-25 21:08:26 +01005678static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005679{
5680 struct root_domain *rd;
5681
5682 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5683 if (!rd)
5684 return NULL;
5685
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005686 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305687 kfree(rd);
5688 return NULL;
5689 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005690
5691 return rd;
5692}
5693
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005694static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005695{
5696 struct sched_group *tmp, *first;
5697
5698 if (!sg)
5699 return;
5700
5701 first = sg;
5702 do {
5703 tmp = sg->next;
5704
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005705 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5706 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005707
5708 kfree(sg);
5709 sg = tmp;
5710 } while (sg != first);
5711}
5712
Peter Zijlstradce840a2011-04-07 14:09:50 +02005713static void free_sched_domain(struct rcu_head *rcu)
5714{
5715 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005716
5717 /*
5718 * If its an overlapping domain it has private groups, iterate and
5719 * nuke them all.
5720 */
5721 if (sd->flags & SD_OVERLAP) {
5722 free_sched_groups(sd->groups, 1);
5723 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005724 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005725 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005726 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005727 kfree(sd);
5728}
5729
5730static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5731{
5732 call_rcu(&sd->rcu, free_sched_domain);
5733}
5734
5735static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5736{
5737 for (; sd; sd = sd->parent)
5738 destroy_sched_domain(sd, cpu);
5739}
5740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005742 * Keep a special pointer to the highest sched_domain that has
5743 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5744 * allows us to avoid some pointer chasing select_idle_sibling().
5745 *
5746 * Also keep a unique ID per domain (we use the first cpu number in
5747 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005748 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005749 */
5750DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005751DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005752DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005753DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305754DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5755DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005756
5757static void update_top_cache_domain(int cpu)
5758{
5759 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005760 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005761 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005762 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005763
5764 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005765 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005766 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005767 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005768 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005769 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005770 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005771
5772 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005773 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005774 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005775
5776 sd = lowest_flag_domain(cpu, SD_NUMA);
5777 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305778
5779 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
5780 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005781}
5782
5783/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01005784 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 * hold the hotplug lock.
5786 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01005787static void
5788cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005790 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005791 struct sched_domain *tmp;
5792
5793 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08005794 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005795 struct sched_domain *parent = tmp->parent;
5796 if (!parent)
5797 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08005798
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005799 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005800 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005801 if (parent->parent)
5802 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02005803 /*
5804 * Transfer SD_PREFER_SIBLING down in case of a
5805 * degenerate parent; the spans match for this
5806 * so the property transfers.
5807 */
5808 if (parent->flags & SD_PREFER_SIBLING)
5809 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005810 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08005811 } else
5812 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005813 }
5814
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005815 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005816 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005817 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005818 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005819 if (sd)
5820 sd->child = NULL;
5821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005823 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Gregory Haskins57d885f2008-01-25 21:08:18 +01005825 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005826 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07005827 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005828 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005829
5830 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831}
5832
5833/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10305834static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836/* Setup the mask of cpus configured for isolated domains */
5837static int __init isolated_cpu_setup(char *str)
5838{
Rusty Russellbdddd292009-12-02 14:09:16 +10305839 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10305840 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return 1;
5842}
5843
Ingo Molnar8927f492007-10-15 17:00:13 +02005844__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005846struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005847 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005848 struct root_domain *rd;
5849};
5850
Andreas Herrmann2109b992009-08-18 12:53:00 +02005851enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02005852 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005853 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02005854 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02005855 sa_none,
5856};
5857
Peter Zijlstrac1174872012-05-31 14:47:33 +02005858/*
5859 * Build an iteration mask that can exclude certain CPUs from the upwards
5860 * domain traversal.
5861 *
5862 * Asymmetric node setups can result in situations where the domain tree is of
5863 * unequal depth, make sure to skip domains that already cover the entire
5864 * range.
5865 *
5866 * In that case build_sched_domains() will have terminated the iteration early
5867 * and our sibling sd spans will be empty. Domains should always include the
5868 * cpu they're built on, so check that.
5869 *
5870 */
5871static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
5872{
5873 const struct cpumask *span = sched_domain_span(sd);
5874 struct sd_data *sdd = sd->private;
5875 struct sched_domain *sibling;
5876 int i;
5877
5878 for_each_cpu(i, span) {
5879 sibling = *per_cpu_ptr(sdd->sd, i);
5880 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
5881 continue;
5882
5883 cpumask_set_cpu(i, sched_group_mask(sg));
5884 }
5885}
5886
5887/*
5888 * Return the canonical balance cpu for this group, this is the first cpu
5889 * of this group that's also in the iteration mask.
5890 */
5891int group_balance_cpu(struct sched_group *sg)
5892{
5893 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
5894}
5895
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005896static int
5897build_overlap_sched_groups(struct sched_domain *sd, int cpu)
5898{
5899 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
5900 const struct cpumask *span = sched_domain_span(sd);
5901 struct cpumask *covered = sched_domains_tmpmask;
5902 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005903 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005904 int i;
5905
5906 cpumask_clear(covered);
5907
5908 for_each_cpu(i, span) {
5909 struct cpumask *sg_span;
5910
5911 if (cpumask_test_cpu(i, covered))
5912 continue;
5913
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005914 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02005915
5916 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005917 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02005918 continue;
5919
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005920 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08005921 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005922
5923 if (!sg)
5924 goto fail;
5925
5926 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005927 if (sibling->child)
5928 cpumask_copy(sg_span, sched_domain_span(sibling->child));
5929 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005930 cpumask_set_cpu(i, sg_span);
5931
5932 cpumask_or(covered, covered, sg_span);
5933
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005934 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
5935 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02005936 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005937
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005938 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005939 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005940 * domains and no possible iteration will get us here, we won't
5941 * die on a /0 trap.
5942 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005943 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005944 sg->sgc->capacity_orig = sg->sgc->capacity;
Peter Zijlstrac1174872012-05-31 14:47:33 +02005945
5946 /*
5947 * Make sure the first group of this domain contains the
5948 * canonical balance cpu. Otherwise the sched_domain iteration
5949 * breaks. See update_sg_lb_stats().
5950 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005951 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02005952 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005953 groups = sg;
5954
5955 if (!first)
5956 first = sg;
5957 if (last)
5958 last->next = sg;
5959 last = sg;
5960 last->next = first;
5961 }
5962 sd->groups = groups;
5963
5964 return 0;
5965
5966fail:
5967 free_sched_groups(first, 0);
5968
5969 return -ENOMEM;
5970}
5971
Peter Zijlstradce840a2011-04-07 14:09:50 +02005972static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005974 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
5975 struct sched_domain *child = sd->child;
5976
5977 if (child)
5978 cpu = cpumask_first(sched_domain_span(child));
5979
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005980 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005981 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005982 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
5983 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005984 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 return cpu;
5987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
Ingo Molnar48f24c42006-07-03 00:25:40 -07005989/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02005990 * build_sched_groups will build a circular linked list of the groups
5991 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04005992 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005993 *
5994 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07005995 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005996static int
5997build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005998{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005999 struct sched_group *first = NULL, *last = NULL;
6000 struct sd_data *sdd = sd->private;
6001 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006002 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006003 int i;
6004
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006005 get_group(cpu, sdd, &sd->groups);
6006 atomic_inc(&sd->groups->ref);
6007
Viresh Kumar09366292013-06-11 16:32:43 +05306008 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006009 return 0;
6010
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006011 lockdep_assert_held(&sched_domains_mutex);
6012 covered = sched_domains_tmpmask;
6013
Peter Zijlstradce840a2011-04-07 14:09:50 +02006014 cpumask_clear(covered);
6015
6016 for_each_cpu(i, span) {
6017 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306018 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006019
6020 if (cpumask_test_cpu(i, covered))
6021 continue;
6022
Viresh Kumarcd08e922013-06-11 16:32:44 +05306023 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006024 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006025
6026 for_each_cpu(j, span) {
6027 if (get_group(j, sdd, NULL) != group)
6028 continue;
6029
6030 cpumask_set_cpu(j, covered);
6031 cpumask_set_cpu(j, sched_group_cpus(sg));
6032 }
6033
6034 if (!first)
6035 first = sg;
6036 if (last)
6037 last->next = sg;
6038 last = sg;
6039 }
6040 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006041
6042 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006043}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006044
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006046 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006047 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006048 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006049 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006050 * Typically cpu_capacity for all the groups in a sched domain will be same
6051 * unless there are asymmetries in the topology. If there are asymmetries,
6052 * group having more cpu_capacity will pickup more load compared to the
6053 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006054 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006055static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006056{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006057 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006058
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306059 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006060
6061 do {
6062 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6063 sg = sg->next;
6064 } while (sg != sd->groups);
6065
Peter Zijlstrac1174872012-05-31 14:47:33 +02006066 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006067 return;
6068
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006069 update_group_capacity(sd, cpu);
6070 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006071}
6072
6073/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006074 * Initializers for schedule domains
6075 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6076 */
6077
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006078static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006079int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006080
6081static int __init setup_relax_domain_level(char *str)
6082{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006083 if (kstrtoint(str, 0, &default_relax_domain_level))
6084 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006085
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006086 return 1;
6087}
6088__setup("relax_domain_level=", setup_relax_domain_level);
6089
6090static void set_domain_attribute(struct sched_domain *sd,
6091 struct sched_domain_attr *attr)
6092{
6093 int request;
6094
6095 if (!attr || attr->relax_domain_level < 0) {
6096 if (default_relax_domain_level < 0)
6097 return;
6098 else
6099 request = default_relax_domain_level;
6100 } else
6101 request = attr->relax_domain_level;
6102 if (request < sd->level) {
6103 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006104 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006105 } else {
6106 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006107 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006108 }
6109}
6110
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006111static void __sdt_free(const struct cpumask *cpu_map);
6112static int __sdt_alloc(const struct cpumask *cpu_map);
6113
Andreas Herrmann2109b992009-08-18 12:53:00 +02006114static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6115 const struct cpumask *cpu_map)
6116{
6117 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006118 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006119 if (!atomic_read(&d->rd->refcount))
6120 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006121 case sa_sd:
6122 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006123 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006124 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006125 case sa_none:
6126 break;
6127 }
6128}
6129
6130static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6131 const struct cpumask *cpu_map)
6132{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006133 memset(d, 0, sizeof(*d));
6134
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006135 if (__sdt_alloc(cpu_map))
6136 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006137 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006138 if (!d->sd)
6139 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006140 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006141 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006142 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006143 return sa_rootdomain;
6144}
6145
Peter Zijlstradce840a2011-04-07 14:09:50 +02006146/*
6147 * NULL the sd_data elements we've used to build the sched_domain and
6148 * sched_group structure so that the subsequent __free_domain_allocs()
6149 * will not free the data we're using.
6150 */
6151static void claim_allocations(int cpu, struct sched_domain *sd)
6152{
6153 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006154
6155 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6156 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6157
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006158 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006159 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006160
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006161 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6162 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006163}
6164
Peter Zijlstracb83b622012-04-17 15:49:36 +02006165#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006166static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006167enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006168static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006169int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006170static struct cpumask ***sched_domains_numa_masks;
6171static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006172#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006173
Vincent Guittot143e1e22014-04-11 11:44:37 +02006174/*
6175 * SD_flags allowed in topology descriptions.
6176 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006177 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006178 * SD_SHARE_PKG_RESOURCES - describes shared caches
6179 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006180 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006181 *
6182 * Odd one out:
6183 * SD_ASYM_PACKING - describes SMT quirks
6184 */
6185#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006186 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006187 SD_SHARE_PKG_RESOURCES | \
6188 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006189 SD_ASYM_PACKING | \
6190 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006191
6192static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006193sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006194{
6195 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006196 int sd_weight, sd_flags = 0;
6197
6198#ifdef CONFIG_NUMA
6199 /*
6200 * Ugly hack to pass state to sd_numa_mask()...
6201 */
6202 sched_domains_curr_level = tl->numa_level;
6203#endif
6204
6205 sd_weight = cpumask_weight(tl->mask(cpu));
6206
6207 if (tl->sd_flags)
6208 sd_flags = (*tl->sd_flags)();
6209 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6210 "wrong sd_flags in topology description\n"))
6211 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006212
6213 *sd = (struct sched_domain){
6214 .min_interval = sd_weight,
6215 .max_interval = 2*sd_weight,
6216 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006217 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006218
6219 .cache_nice_tries = 0,
6220 .busy_idx = 0,
6221 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006222 .newidle_idx = 0,
6223 .wake_idx = 0,
6224 .forkexec_idx = 0,
6225
6226 .flags = 1*SD_LOAD_BALANCE
6227 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006228 | 1*SD_BALANCE_EXEC
6229 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006230 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006231 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006232 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006233 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006234 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006235 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006236 | 0*SD_NUMA
6237 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006238 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006239
Peter Zijlstracb83b622012-04-17 15:49:36 +02006240 .last_balance = jiffies,
6241 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006242 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006243 .max_newidle_lb_cost = 0,
6244 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006245#ifdef CONFIG_SCHED_DEBUG
6246 .name = tl->name,
6247#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006248 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006249
6250 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006251 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006252 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006253
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006254 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittot143e1e22014-04-11 11:44:37 +02006255 sd->imbalance_pct = 110;
6256 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006257
6258 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6259 sd->imbalance_pct = 117;
6260 sd->cache_nice_tries = 1;
6261 sd->busy_idx = 2;
6262
6263#ifdef CONFIG_NUMA
6264 } else if (sd->flags & SD_NUMA) {
6265 sd->cache_nice_tries = 2;
6266 sd->busy_idx = 3;
6267 sd->idle_idx = 2;
6268
6269 sd->flags |= SD_SERIALIZE;
6270 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6271 sd->flags &= ~(SD_BALANCE_EXEC |
6272 SD_BALANCE_FORK |
6273 SD_WAKE_AFFINE);
6274 }
6275
6276#endif
6277 } else {
6278 sd->flags |= SD_PREFER_SIBLING;
6279 sd->cache_nice_tries = 1;
6280 sd->busy_idx = 2;
6281 sd->idle_idx = 1;
6282 }
6283
6284 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006285
6286 return sd;
6287}
6288
Vincent Guittot143e1e22014-04-11 11:44:37 +02006289/*
6290 * Topology list, bottom-up.
6291 */
6292static struct sched_domain_topology_level default_topology[] = {
6293#ifdef CONFIG_SCHED_SMT
6294 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6295#endif
6296#ifdef CONFIG_SCHED_MC
6297 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6298#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006299 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6300 { NULL, },
6301};
6302
6303struct sched_domain_topology_level *sched_domain_topology = default_topology;
6304
6305#define for_each_sd_topology(tl) \
6306 for (tl = sched_domain_topology; tl->mask; tl++)
6307
6308void set_sched_topology(struct sched_domain_topology_level *tl)
6309{
6310 sched_domain_topology = tl;
6311}
6312
6313#ifdef CONFIG_NUMA
6314
Peter Zijlstracb83b622012-04-17 15:49:36 +02006315static const struct cpumask *sd_numa_mask(int cpu)
6316{
6317 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6318}
6319
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006320static void sched_numa_warn(const char *str)
6321{
6322 static int done = false;
6323 int i,j;
6324
6325 if (done)
6326 return;
6327
6328 done = true;
6329
6330 printk(KERN_WARNING "ERROR: %s\n\n", str);
6331
6332 for (i = 0; i < nr_node_ids; i++) {
6333 printk(KERN_WARNING " ");
6334 for (j = 0; j < nr_node_ids; j++)
6335 printk(KERN_CONT "%02d ", node_distance(i,j));
6336 printk(KERN_CONT "\n");
6337 }
6338 printk(KERN_WARNING "\n");
6339}
6340
Rik van Riel9942f792014-10-17 03:29:49 -04006341bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006342{
6343 int i;
6344
6345 if (distance == node_distance(0, 0))
6346 return true;
6347
6348 for (i = 0; i < sched_domains_numa_levels; i++) {
6349 if (sched_domains_numa_distance[i] == distance)
6350 return true;
6351 }
6352
6353 return false;
6354}
6355
Rik van Riele3fe70b2014-10-17 03:29:50 -04006356/*
6357 * A system can have three types of NUMA topology:
6358 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6359 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6360 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6361 *
6362 * The difference between a glueless mesh topology and a backplane
6363 * topology lies in whether communication between not directly
6364 * connected nodes goes through intermediary nodes (where programs
6365 * could run), or through backplane controllers. This affects
6366 * placement of programs.
6367 *
6368 * The type of topology can be discerned with the following tests:
6369 * - If the maximum distance between any nodes is 1 hop, the system
6370 * is directly connected.
6371 * - If for two nodes A and B, located N > 1 hops away from each other,
6372 * there is an intermediary node C, which is < N hops away from both
6373 * nodes A and B, the system is a glueless mesh.
6374 */
6375static void init_numa_topology_type(void)
6376{
6377 int a, b, c, n;
6378
6379 n = sched_max_numa_distance;
6380
6381 if (n <= 1)
6382 sched_numa_topology_type = NUMA_DIRECT;
6383
6384 for_each_online_node(a) {
6385 for_each_online_node(b) {
6386 /* Find two nodes furthest removed from each other. */
6387 if (node_distance(a, b) < n)
6388 continue;
6389
6390 /* Is there an intermediary node between a and b? */
6391 for_each_online_node(c) {
6392 if (node_distance(a, c) < n &&
6393 node_distance(b, c) < n) {
6394 sched_numa_topology_type =
6395 NUMA_GLUELESS_MESH;
6396 return;
6397 }
6398 }
6399
6400 sched_numa_topology_type = NUMA_BACKPLANE;
6401 return;
6402 }
6403 }
6404}
6405
Peter Zijlstracb83b622012-04-17 15:49:36 +02006406static void sched_init_numa(void)
6407{
6408 int next_distance, curr_distance = node_distance(0, 0);
6409 struct sched_domain_topology_level *tl;
6410 int level = 0;
6411 int i, j, k;
6412
Peter Zijlstracb83b622012-04-17 15:49:36 +02006413 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6414 if (!sched_domains_numa_distance)
6415 return;
6416
6417 /*
6418 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6419 * unique distances in the node_distance() table.
6420 *
6421 * Assumes node_distance(0,j) includes all distances in
6422 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006423 */
6424 next_distance = curr_distance;
6425 for (i = 0; i < nr_node_ids; i++) {
6426 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006427 for (k = 0; k < nr_node_ids; k++) {
6428 int distance = node_distance(i, k);
6429
6430 if (distance > curr_distance &&
6431 (distance < next_distance ||
6432 next_distance == curr_distance))
6433 next_distance = distance;
6434
6435 /*
6436 * While not a strong assumption it would be nice to know
6437 * about cases where if node A is connected to B, B is not
6438 * equally connected to A.
6439 */
6440 if (sched_debug() && node_distance(k, i) != distance)
6441 sched_numa_warn("Node-distance not symmetric");
6442
6443 if (sched_debug() && i && !find_numa_distance(distance))
6444 sched_numa_warn("Node-0 not representative");
6445 }
6446 if (next_distance != curr_distance) {
6447 sched_domains_numa_distance[level++] = next_distance;
6448 sched_domains_numa_levels = level;
6449 curr_distance = next_distance;
6450 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006451 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006452
6453 /*
6454 * In case of sched_debug() we verify the above assumption.
6455 */
6456 if (!sched_debug())
6457 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006458 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006459
6460 if (!level)
6461 return;
6462
Peter Zijlstracb83b622012-04-17 15:49:36 +02006463 /*
6464 * 'level' contains the number of unique distances, excluding the
6465 * identity distance node_distance(i,i).
6466 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306467 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006468 * numbers.
6469 */
6470
Tang Chen5f7865f2012-09-25 21:12:30 +08006471 /*
6472 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6473 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6474 * the array will contain less then 'level' members. This could be
6475 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6476 * in other functions.
6477 *
6478 * We reset it to 'level' at the end of this function.
6479 */
6480 sched_domains_numa_levels = 0;
6481
Peter Zijlstracb83b622012-04-17 15:49:36 +02006482 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6483 if (!sched_domains_numa_masks)
6484 return;
6485
6486 /*
6487 * Now for each level, construct a mask per node which contains all
6488 * cpus of nodes that are that many hops away from us.
6489 */
6490 for (i = 0; i < level; i++) {
6491 sched_domains_numa_masks[i] =
6492 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6493 if (!sched_domains_numa_masks[i])
6494 return;
6495
6496 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006497 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006498 if (!mask)
6499 return;
6500
6501 sched_domains_numa_masks[i][j] = mask;
6502
6503 for (k = 0; k < nr_node_ids; k++) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006504 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006505 continue;
6506
6507 cpumask_or(mask, mask, cpumask_of_node(k));
6508 }
6509 }
6510 }
6511
Vincent Guittot143e1e22014-04-11 11:44:37 +02006512 /* Compute default topology size */
6513 for (i = 0; sched_domain_topology[i].mask; i++);
6514
Vincent Guittotc515db82014-05-13 11:11:01 +02006515 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006516 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6517 if (!tl)
6518 return;
6519
6520 /*
6521 * Copy the default topology bits..
6522 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006523 for (i = 0; sched_domain_topology[i].mask; i++)
6524 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006525
6526 /*
6527 * .. and append 'j' levels of NUMA goodness.
6528 */
6529 for (j = 0; j < level; i++, j++) {
6530 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006531 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006532 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006533 .flags = SDTL_OVERLAP,
6534 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006535 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006536 };
6537 }
6538
6539 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006540
6541 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006542 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006543
6544 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006545}
Tang Chen301a5cb2012-09-25 21:12:31 +08006546
6547static void sched_domains_numa_masks_set(int cpu)
6548{
6549 int i, j;
6550 int node = cpu_to_node(cpu);
6551
6552 for (i = 0; i < sched_domains_numa_levels; i++) {
6553 for (j = 0; j < nr_node_ids; j++) {
6554 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6555 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6556 }
6557 }
6558}
6559
6560static void sched_domains_numa_masks_clear(int cpu)
6561{
6562 int i, j;
6563 for (i = 0; i < sched_domains_numa_levels; i++) {
6564 for (j = 0; j < nr_node_ids; j++)
6565 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6566 }
6567}
6568
6569/*
6570 * Update sched_domains_numa_masks[level][node] array when new cpus
6571 * are onlined.
6572 */
6573static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6574 unsigned long action,
6575 void *hcpu)
6576{
6577 int cpu = (long)hcpu;
6578
6579 switch (action & ~CPU_TASKS_FROZEN) {
6580 case CPU_ONLINE:
6581 sched_domains_numa_masks_set(cpu);
6582 break;
6583
6584 case CPU_DEAD:
6585 sched_domains_numa_masks_clear(cpu);
6586 break;
6587
6588 default:
6589 return NOTIFY_DONE;
6590 }
6591
6592 return NOTIFY_OK;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006593}
6594#else
6595static inline void sched_init_numa(void)
6596{
6597}
Tang Chen301a5cb2012-09-25 21:12:31 +08006598
6599static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6600 unsigned long action,
6601 void *hcpu)
6602{
6603 return 0;
6604}
Peter Zijlstracb83b622012-04-17 15:49:36 +02006605#endif /* CONFIG_NUMA */
6606
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006607static int __sdt_alloc(const struct cpumask *cpu_map)
6608{
6609 struct sched_domain_topology_level *tl;
6610 int j;
6611
Viresh Kumar27723a62013-06-10 16:27:20 +05306612 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006613 struct sd_data *sdd = &tl->data;
6614
6615 sdd->sd = alloc_percpu(struct sched_domain *);
6616 if (!sdd->sd)
6617 return -ENOMEM;
6618
6619 sdd->sg = alloc_percpu(struct sched_group *);
6620 if (!sdd->sg)
6621 return -ENOMEM;
6622
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006623 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6624 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006625 return -ENOMEM;
6626
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006627 for_each_cpu(j, cpu_map) {
6628 struct sched_domain *sd;
6629 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006630 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006631
6632 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
6633 GFP_KERNEL, cpu_to_node(j));
6634 if (!sd)
6635 return -ENOMEM;
6636
6637 *per_cpu_ptr(sdd->sd, j) = sd;
6638
6639 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6640 GFP_KERNEL, cpu_to_node(j));
6641 if (!sg)
6642 return -ENOMEM;
6643
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006644 sg->next = sg;
6645
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006646 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006647
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006648 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006649 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006650 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006651 return -ENOMEM;
6652
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006653 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006654 }
6655 }
6656
6657 return 0;
6658}
6659
6660static void __sdt_free(const struct cpumask *cpu_map)
6661{
6662 struct sched_domain_topology_level *tl;
6663 int j;
6664
Viresh Kumar27723a62013-06-10 16:27:20 +05306665 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006666 struct sd_data *sdd = &tl->data;
6667
6668 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006669 struct sched_domain *sd;
6670
6671 if (sdd->sd) {
6672 sd = *per_cpu_ptr(sdd->sd, j);
6673 if (sd && (sd->flags & SD_OVERLAP))
6674 free_sched_groups(sd->groups, 0);
6675 kfree(*per_cpu_ptr(sdd->sd, j));
6676 }
6677
6678 if (sdd->sg)
6679 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006680 if (sdd->sgc)
6681 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006682 }
6683 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006684 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006685 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006686 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006687 free_percpu(sdd->sgc);
6688 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006689 }
6690}
6691
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006692struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306693 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6694 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006695{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006696 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006697 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006698 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006699
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006700 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006701 if (child) {
6702 sd->level = child->level + 1;
6703 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006704 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306705 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006706
6707 if (!cpumask_subset(sched_domain_span(child),
6708 sched_domain_span(sd))) {
6709 pr_err("BUG: arch topology borken\n");
6710#ifdef CONFIG_SCHED_DEBUG
6711 pr_err(" the %s domain not a subset of the %s domain\n",
6712 child->name, sd->name);
6713#endif
6714 /* Fixup, ensure @sd has at least @child cpus. */
6715 cpumask_or(sched_domain_span(sd),
6716 sched_domain_span(sd),
6717 sched_domain_span(child));
6718 }
6719
Peter Zijlstra60495e72011-04-07 14:10:04 +02006720 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006721 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006722
6723 return sd;
6724}
6725
Mike Travis7c16ec52008-04-04 18:11:11 -07006726/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006727 * Build sched domains for a given set of cpus and attach the sched domains
6728 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006730static int build_sched_domains(const struct cpumask *cpu_map,
6731 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732{
Viresh Kumar1c632162013-06-10 16:27:18 +05306733 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006734 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006735 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006736 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306737
Andreas Herrmann2109b992009-08-18 12:53:00 +02006738 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6739 if (alloc_state != sa_rootdomain)
6740 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006741
Peter Zijlstradce840a2011-04-07 14:09:50 +02006742 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306743 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006744 struct sched_domain_topology_level *tl;
6745
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006746 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306747 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306748 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306749 if (tl == sched_domain_topology)
6750 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006751 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6752 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006753 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6754 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006755 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006756 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006757
Peter Zijlstradce840a2011-04-07 14:09:50 +02006758 /* Build the groups for the domains */
6759 for_each_cpu(i, cpu_map) {
6760 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6761 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006762 if (sd->flags & SD_OVERLAP) {
6763 if (build_overlap_sched_groups(sd, i))
6764 goto error;
6765 } else {
6766 if (build_sched_groups(sd, i))
6767 goto error;
6768 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006769 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
Nicolas Pitreced549f2014-05-26 18:19:38 -04006772 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006773 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6774 if (!cpumask_test_cpu(i, cpu_map))
6775 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Peter Zijlstradce840a2011-04-07 14:09:50 +02006777 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6778 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006779 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006780 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006781 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006782
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006784 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306785 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006786 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006787 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006789 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006790
Peter Zijlstra822ff792011-04-07 14:09:51 +02006791 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006792error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02006793 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02006794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795}
Paul Jackson029190c2007-10-18 23:40:20 -07006796
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306797static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07006798static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02006799static struct sched_domain_attr *dattr_cur;
6800 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07006801
6802/*
6803 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10306804 * cpumask) fails, then fallback to a single sched domain,
6805 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07006806 */
Rusty Russell42128232008-11-25 02:35:12 +10306807static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07006808
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006809/*
6810 * arch_update_cpu_topology lets virtualized architectures update the
6811 * cpu core maps. It is supposed to return 1 if the topology changed
6812 * or 0 if it stayed the same.
6813 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006814int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01006815{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006816 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01006817}
6818
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306819cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
6820{
6821 int i;
6822 cpumask_var_t *doms;
6823
6824 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
6825 if (!doms)
6826 return NULL;
6827 for (i = 0; i < ndoms; i++) {
6828 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
6829 free_sched_domains(doms, i);
6830 return NULL;
6831 }
6832 }
6833 return doms;
6834}
6835
6836void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
6837{
6838 unsigned int i;
6839 for (i = 0; i < ndoms; i++)
6840 free_cpumask_var(doms[i]);
6841 kfree(doms);
6842}
6843
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006844/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006845 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006846 * For now this just excludes isolated cpus, but could be used to
6847 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006848 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02006849static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006850{
Milton Miller73785472007-10-24 18:23:48 +02006851 int err;
6852
Heiko Carstens22e52b02008-03-12 18:31:59 +01006853 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006854 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306855 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07006856 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306857 doms_cur = &fallback_doms;
6858 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006859 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02006860 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006861
6862 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006863}
6864
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006865/*
6866 * Detach sched domains from a group of cpus specified in cpu_map
6867 * These cpus will now be attached to the NULL domain
6868 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306869static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006870{
6871 int i;
6872
Peter Zijlstradce840a2011-04-07 14:09:50 +02006873 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306874 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006875 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006876 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006877}
6878
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006879/* handle null as "default" */
6880static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
6881 struct sched_domain_attr *new, int idx_new)
6882{
6883 struct sched_domain_attr tmp;
6884
6885 /* fast path */
6886 if (!new && !cur)
6887 return 1;
6888
6889 tmp = SD_ATTR_INIT;
6890 return !memcmp(cur ? (cur + idx_cur) : &tmp,
6891 new ? (new + idx_new) : &tmp,
6892 sizeof(struct sched_domain_attr));
6893}
6894
Paul Jackson029190c2007-10-18 23:40:20 -07006895/*
6896 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006897 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006898 * doms_new[] to the current sched domain partitioning, doms_cur[].
6899 * It destroys each deleted domain and builds each new domain.
6900 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306901 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006902 * The masks don't intersect (don't overlap.) We should setup one
6903 * sched domain for each mask. CPUs not in any of the cpumasks will
6904 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006905 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6906 * it as it is.
6907 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306908 * The passed in 'doms_new' should be allocated using
6909 * alloc_sched_domains. This routine takes ownership of it and will
6910 * free_sched_domains it when done with it. If the caller failed the
6911 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
6912 * and partition_sched_domains() will fallback to the single partition
6913 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07006914 *
Rusty Russell96f874e22008-11-25 02:35:14 +10306915 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08006916 * ndoms_new == 0 is a special case for destroying existing domains,
6917 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006918 *
Paul Jackson029190c2007-10-18 23:40:20 -07006919 * Call with hotplug lock held
6920 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306921void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006922 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07006923{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006924 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006925 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07006926
Heiko Carstens712555e2008-04-28 11:33:07 +02006927 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006928
Milton Miller73785472007-10-24 18:23:48 +02006929 /* always unregister in case we don't destroy any domains */
6930 unregister_sched_domain_sysctl();
6931
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006932 /* Let architecture update cpu core mappings. */
6933 new_topology = arch_update_cpu_topology();
6934
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006935 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07006936
6937 /* Destroy deleted domains */
6938 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006939 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306940 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006941 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006942 goto match1;
6943 }
6944 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306945 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07006946match1:
6947 ;
6948 }
6949
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006950 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07006951 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006952 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306953 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006954 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08006955 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07006956 }
6957
Paul Jackson029190c2007-10-18 23:40:20 -07006958 /* Build new domains */
6959 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006960 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306961 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006962 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006963 goto match2;
6964 }
6965 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006966 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07006967match2:
6968 ;
6969 }
6970
6971 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306972 if (doms_cur != &fallback_doms)
6973 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006974 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07006975 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006976 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07006977 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02006978
6979 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006980
Heiko Carstens712555e2008-04-28 11:33:07 +02006981 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07006982}
6983
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306984static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
6985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986/*
Tejun Heo3a101d02010-06-08 21:40:36 +02006987 * Update cpusets according to cpu_active mask. If cpusets are
6988 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
6989 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306990 *
6991 * If we come here as part of a suspend/resume, don't touch cpusets because we
6992 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02006994static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
6995 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306997 switch (action) {
6998 case CPU_ONLINE_FROZEN:
6999 case CPU_DOWN_FAILED_FROZEN:
7000
7001 /*
7002 * num_cpus_frozen tracks how many CPUs are involved in suspend
7003 * resume sequence. As long as this is not the last online
7004 * operation in the resume sequence, just build a single sched
7005 * domain, ignoring cpusets.
7006 */
7007 num_cpus_frozen--;
7008 if (likely(num_cpus_frozen)) {
7009 partition_sched_domains(1, NULL, NULL);
7010 break;
7011 }
7012
7013 /*
7014 * This is the last CPU online operation. So fall through and
7015 * restore the original sched domains by considering the
7016 * cpuset configurations.
7017 */
7018
Max Krasnyanskye761b772008-07-15 04:43:49 -07007019 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007020 case CPU_DOWN_FAILED:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307021 cpuset_update_active_cpus(true);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307022 break;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007023 default:
7024 return NOTIFY_DONE;
7025 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307026 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007027}
Tejun Heo3a101d02010-06-08 21:40:36 +02007028
Tejun Heo0b2e9182010-06-21 23:53:31 +02007029static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7030 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007031{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307032 switch (action) {
Tejun Heo3a101d02010-06-08 21:40:36 +02007033 case CPU_DOWN_PREPARE:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307034 cpuset_update_active_cpus(false);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307035 break;
7036 case CPU_DOWN_PREPARE_FROZEN:
7037 num_cpus_frozen++;
7038 partition_sched_domains(1, NULL, NULL);
7039 break;
Tejun Heo3a101d02010-06-08 21:40:36 +02007040 default:
7041 return NOTIFY_DONE;
7042 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307043 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02007044}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007045
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046void __init sched_init_smp(void)
7047{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307048 cpumask_var_t non_isolated_cpus;
7049
7050 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007051 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007052
Peter Zijlstracb83b622012-04-17 15:49:36 +02007053 sched_init_numa();
7054
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007055 /*
7056 * There's no userspace yet to cause hotplug operations; hence all the
7057 * cpu masks are stable and all blatant races in the below code cannot
7058 * happen.
7059 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007060 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007061 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307062 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7063 if (cpumask_empty(non_isolated_cpus))
7064 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007065 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007066
Tang Chen301a5cb2012-09-25 21:12:31 +08007067 hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
Tejun Heo3a101d02010-06-08 21:40:36 +02007068 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7069 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007070
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007071 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007072
7073 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307074 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007075 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007076 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307077 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307078
Rusty Russell0e3900e2008-11-25 02:35:13 +10307079 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007080 init_sched_dl_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081}
7082#else
7083void __init sched_init_smp(void)
7084{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007085 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086}
7087#endif /* CONFIG_SMP */
7088
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307089const_debug unsigned int sysctl_timer_migration = 1;
7090
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091int in_sched_functions(unsigned long addr)
7092{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 return in_lock_functions(addr) ||
7094 (addr >= (unsigned long)__sched_text_start
7095 && addr < (unsigned long)__sched_text_end);
7096}
7097
Peter Zijlstra029632f2011-10-25 10:00:11 +02007098#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007099/*
7100 * Default task group.
7101 * Every task in system belongs to this group at bootup.
7102 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007103struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007104LIST_HEAD(task_groups);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007105#endif
7106
Joonsoo Kime6252c32013-04-23 17:27:41 +09007107DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007108
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109void __init sched_init(void)
7110{
Ingo Molnardd41f592007-07-09 18:51:59 +02007111 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007112 unsigned long alloc_size = 0, ptr;
7113
7114#ifdef CONFIG_FAIR_GROUP_SCHED
7115 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7116#endif
7117#ifdef CONFIG_RT_GROUP_SCHED
7118 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7119#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007120 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007121 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007122
7123#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007124 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007125 ptr += nr_cpu_ids * sizeof(void **);
7126
Yong Zhang07e06b02011-01-07 15:17:36 +08007127 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007128 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007129
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007130#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007131#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007132 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007133 ptr += nr_cpu_ids * sizeof(void **);
7134
Yong Zhang07e06b02011-01-07 15:17:36 +08007135 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007136 ptr += nr_cpu_ids * sizeof(void **);
7137
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007138#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007139 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007140#ifdef CONFIG_CPUMASK_OFFSTACK
7141 for_each_possible_cpu(i) {
7142 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7143 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7144 }
7145#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007146
Dario Faggioli332ac172013-11-07 14:43:45 +01007147 init_rt_bandwidth(&def_rt_bandwidth,
7148 global_rt_period(), global_rt_runtime());
7149 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007150 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007151
Gregory Haskins57d885f2008-01-25 21:08:18 +01007152#ifdef CONFIG_SMP
7153 init_defrootdomain();
7154#endif
7155
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007156#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007157 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007158 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007159#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007160
Dhaval Giani7c941432010-01-20 13:26:18 +01007161#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007162 list_add(&root_task_group.list, &task_groups);
7163 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007164 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007165 autogroup_init(&init_task);
Glauber Costa54c707e2011-11-28 14:45:19 -02007166
Dhaval Giani7c941432010-01-20 13:26:18 +01007167#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007168
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007169 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007170 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
7172 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007173 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007174 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007175 rq->calc_load_active = 0;
7176 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007177 init_cfs_rq(&rq->cfs);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007178 init_rt_rq(&rq->rt, rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01007179 init_dl_rq(&rq->dl, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007180#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007181 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007182 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007183 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007184 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007185 *
7186 * In case of task-groups formed thr' the cgroup filesystem, it
7187 * gets 100% of the cpu resources in the system. This overall
7188 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007189 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007190 * based on each entity's (task or task-group's) weight
7191 * (se->load.weight).
7192 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007193 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007194 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7195 * then A0's share of the cpu resource is:
7196 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007197 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007198 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007199 * We achieve this by letting root_task_group's tasks sit
7200 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007201 */
Paul Turnerab84d312011-07-21 09:43:28 -07007202 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007203 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007204#endif /* CONFIG_FAIR_GROUP_SCHED */
7205
7206 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007207#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007208 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
Ingo Molnardd41f592007-07-09 18:51:59 +02007211 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7212 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007213
7214 rq->last_load_update_tick = jiffies;
7215
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007217 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007218 rq->rd = NULL;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007219 rq->cpu_capacity = SCHED_CAPACITY_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04007220 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007222 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007224 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007225 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007226 rq->idle_stamp = 0;
7227 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007228 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007229
7230 INIT_LIST_HEAD(&rq->cfs_tasks);
7231
Gregory Haskinsdc938522008-01-25 21:08:26 +01007232 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007233#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007234 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007235#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007236#ifdef CONFIG_NO_HZ_FULL
7237 rq->last_sched_tick = 0;
7238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007240 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 }
7243
Peter Williams2dd73a42006-06-27 02:54:34 -07007244 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007245
Avi Kivitye107be32007-07-26 13:40:43 +02007246#ifdef CONFIG_PREEMPT_NOTIFIERS
7247 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7248#endif
7249
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 /*
7251 * The boot idle thread does lazy MMU switching as well:
7252 */
7253 atomic_inc(&init_mm.mm_count);
7254 enter_lazy_tlb(&init_mm, current);
7255
7256 /*
Yao Dongdong1b537c72014-12-29 14:41:43 +08007257 * During early bootup we pretend to be a normal task:
7258 */
7259 current->sched_class = &fair_sched_class;
7260
7261 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 * Make us the idle thread. Technically, schedule() should not be
7263 * called from this thread, however somewhere below it might be,
7264 * but because we are the idle thread, we just pick up running again
7265 * when this runqueue becomes "idle".
7266 */
7267 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007268
7269 calc_load_update = jiffies + LOAD_FREQ;
7270
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307271#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007272 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307273 /* May be allocated at isolcpus cmdline parse time */
7274 if (cpu_isolated_map == NULL)
7275 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007276 idle_thread_set_boot_cpu();
Corey Minyarda803f022014-05-08 13:47:39 -05007277 set_cpu_rq_start_time();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007278#endif
7279 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307280
Ingo Molnar6892b752008-02-13 14:02:36 +01007281 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282}
7283
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007284#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007285static inline int preempt_count_equals(int preempt_offset)
7286{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007287 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007288
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007289 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007290}
7291
Simon Kagstromd8948372009-12-23 11:08:18 +01007292void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007294 /*
7295 * Blocking primitives will set (and therefore destroy) current->state,
7296 * since we will exit with TASK_RUNNING make sure we enter with it,
7297 * otherwise we will destroy state.
7298 */
Peter Zijlstrae7097e82014-10-29 17:08:45 +01007299 if (WARN_ONCE(current->state != TASK_RUNNING,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007300 "do not call blocking ops when !TASK_RUNNING; "
7301 "state=%lx set at [<%p>] %pS\n",
7302 current->state,
7303 (void *)current->task_state_change,
7304 (void *)current->task_state_change))
7305 __set_current_state(TASK_RUNNING);
7306
Peter Zijlstra34274452014-09-24 10:18:56 +02007307 ___might_sleep(file, line, preempt_offset);
7308}
7309EXPORT_SYMBOL(__might_sleep);
7310
7311void ___might_sleep(const char *file, int line, int preempt_offset)
7312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 static unsigned long prev_jiffy; /* ratelimiting */
7314
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007315 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007316 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7317 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007318 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007319 return;
7320 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7321 return;
7322 prev_jiffy = jiffies;
7323
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007324 printk(KERN_ERR
7325 "BUG: sleeping function called from invalid context at %s:%d\n",
7326 file, line);
7327 printk(KERN_ERR
7328 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7329 in_atomic(), irqs_disabled(),
7330 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007331
Eric Sandeena8b686b2014-12-16 16:25:28 -06007332 if (task_stack_end_corrupted(current))
7333 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7334
Ingo Molnaraef745f2008-08-28 11:34:43 +02007335 debug_show_held_locks(current);
7336 if (irqs_disabled())
7337 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007338#ifdef CONFIG_DEBUG_PREEMPT
7339 if (!preempt_count_equals(preempt_offset)) {
7340 pr_err("Preemption disabled at:");
7341 print_ip_sym(current->preempt_disable_ip);
7342 pr_cont("\n");
7343 }
7344#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007345 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346}
Peter Zijlstra34274452014-09-24 10:18:56 +02007347EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348#endif
7349
7350#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007351static void normalize_task(struct rq *rq, struct task_struct *p)
7352{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007353 const struct sched_class *prev_class = p->sched_class;
Dario Faggiolid50dde52013-11-07 14:43:36 +01007354 struct sched_attr attr = {
7355 .sched_policy = SCHED_NORMAL,
7356 };
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007357 int old_prio = p->prio;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007358 int queued;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007359
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007360 queued = task_on_rq_queued(p);
7361 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01007362 dequeue_task(rq, p, 0);
Dario Faggiolid50dde52013-11-07 14:43:36 +01007363 __setscheduler(rq, p, &attr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007364 if (queued) {
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01007365 enqueue_task(rq, p, 0);
Kirill Tkhai88751252014-06-29 00:03:57 +04007366 resched_curr(rq);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007367 }
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007368
7369 check_class_changed(rq, p, prev_class, old_prio);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007370}
7371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372void normalize_rt_tasks(void)
7373{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007374 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007376 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007378 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007379 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007380 /*
7381 * Only normalize user tasks:
7382 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007383 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007384 continue;
7385
Ingo Molnardd41f592007-07-09 18:51:59 +02007386 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007387#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007388 p->se.statistics.wait_start = 0;
7389 p->se.statistics.sleep_start = 0;
7390 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007391#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007392
Dario Faggioliaab03e02013-11-28 11:14:43 +01007393 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007394 /*
7395 * Renice negative nice level userspace
7396 * tasks back to 0:
7397 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007398 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007399 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007403 rq = task_rq_lock(p, &flags);
Ingo Molnar178be792007-10-15 17:00:18 +02007404 normalize_task(rq, p);
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007405 task_rq_unlock(rq, p, &flags);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007406 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007407 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408}
7409
7410#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007411
Jason Wessel67fc4e02010-05-20 21:04:21 -05007412#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007413/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007414 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007415 *
7416 * They can only be called when the whole system has been
7417 * stopped - every CPU needs to be quiescent, and no scheduling
7418 * activity can take place. Using them for anything else would
7419 * be a serious bug, and as a result, they aren't even visible
7420 * under any other configuration.
7421 */
7422
7423/**
7424 * curr_task - return the current task for a given cpu.
7425 * @cpu: the processor in question.
7426 *
7427 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007428 *
7429 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007430 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007431struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007432{
7433 return cpu_curr(cpu);
7434}
7435
Jason Wessel67fc4e02010-05-20 21:04:21 -05007436#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7437
7438#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007439/**
7440 * set_curr_task - set the current task for a given cpu.
7441 * @cpu: the processor in question.
7442 * @p: the task pointer to set.
7443 *
7444 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007445 * are serviced on a separate stack. It allows the architecture to switch the
7446 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007447 * must be called with all CPU's synchronized, and interrupts disabled, the
7448 * and caller must save the original value of the current task (see
7449 * curr_task() above) and restore that value before reenabling interrupts and
7450 * re-starting the system.
7451 *
7452 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7453 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007454void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007455{
7456 cpu_curr(cpu) = p;
7457}
7458
7459#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007460
Dhaval Giani7c941432010-01-20 13:26:18 +01007461#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007462/* task_group_lock serializes the addition/removal of task groups */
7463static DEFINE_SPINLOCK(task_group_lock);
7464
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007465static void free_sched_group(struct task_group *tg)
7466{
7467 free_fair_sched_group(tg);
7468 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007469 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007470 kfree(tg);
7471}
7472
7473/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007474struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007475{
7476 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007477
7478 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7479 if (!tg)
7480 return ERR_PTR(-ENOMEM);
7481
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007482 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007483 goto err;
7484
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007485 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007486 goto err;
7487
Li Zefanace783b2013-01-24 14:30:48 +08007488 return tg;
7489
7490err:
7491 free_sched_group(tg);
7492 return ERR_PTR(-ENOMEM);
7493}
7494
7495void sched_online_group(struct task_group *tg, struct task_group *parent)
7496{
7497 unsigned long flags;
7498
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007499 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007500 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007501
7502 WARN_ON(!parent); /* root should already exist */
7503
7504 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007505 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007506 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007507 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007508}
7509
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007510/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007511static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007512{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007513 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007514 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007515}
7516
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007517/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007518void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007519{
Li Zefanace783b2013-01-24 14:30:48 +08007520 /* wait for possible concurrent references to cfs_rqs complete */
7521 call_rcu(&tg->rcu, free_sched_group_rcu);
7522}
7523
7524void sched_offline_group(struct task_group *tg)
7525{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007526 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007527 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007528
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007529 /* end participation in shares distribution */
7530 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007531 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007532
7533 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007534 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007535 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007536 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007537}
7538
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007539/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007540 * The caller of this function should have put the task in its new group
7541 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7542 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007543 */
7544void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007545{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007546 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007547 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007548 unsigned long flags;
7549 struct rq *rq;
7550
7551 rq = task_rq_lock(tsk, &flags);
7552
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007553 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007554 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007555
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007556 if (queued)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007557 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007558 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007559 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007560
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007561 /*
7562 * All callers are synchronized by task_rq_lock(); we do not use RCU
7563 * which is pointless here. Thus, we pass "true" to task_css_check()
7564 * to prevent lockdep warnings.
7565 */
7566 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007567 struct task_group, css);
7568 tg = autogroup_task_group(tsk, tg);
7569 tsk->sched_task_group = tg;
7570
Peter Zijlstra810b3812008-02-29 15:21:01 -05007571#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007572 if (tsk->sched_class->task_move_group)
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007573 tsk->sched_class->task_move_group(tsk, queued);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007574 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007575#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007576 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007577
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007578 if (unlikely(running))
7579 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007580 if (queued)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01007581 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007582
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007583 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007584}
Dhaval Giani7c941432010-01-20 13:26:18 +01007585#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007586
Paul Turnera790de92011-07-21 09:43:29 -07007587#ifdef CONFIG_RT_GROUP_SCHED
7588/*
7589 * Ensure that the real time constraints are schedulable.
7590 */
7591static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007592
Dhaval Giani521f1a242008-02-28 15:21:56 +05307593/* Must be called with tasklist_lock held */
7594static inline int tg_has_rt_tasks(struct task_group *tg)
7595{
7596 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007597
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007598 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007599 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307600 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007601 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007602
Dhaval Giani521f1a242008-02-28 15:21:56 +05307603 return 0;
7604}
7605
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007606struct rt_schedulable_data {
7607 struct task_group *tg;
7608 u64 rt_period;
7609 u64 rt_runtime;
7610};
7611
Paul Turnera790de92011-07-21 09:43:29 -07007612static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007613{
7614 struct rt_schedulable_data *d = data;
7615 struct task_group *child;
7616 unsigned long total, sum = 0;
7617 u64 period, runtime;
7618
7619 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7620 runtime = tg->rt_bandwidth.rt_runtime;
7621
7622 if (tg == d->tg) {
7623 period = d->rt_period;
7624 runtime = d->rt_runtime;
7625 }
7626
Peter Zijlstra4653f802008-09-23 15:33:44 +02007627 /*
7628 * Cannot have more runtime than the period.
7629 */
7630 if (runtime > period && runtime != RUNTIME_INF)
7631 return -EINVAL;
7632
7633 /*
7634 * Ensure we don't starve existing RT tasks.
7635 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007636 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7637 return -EBUSY;
7638
7639 total = to_ratio(period, runtime);
7640
Peter Zijlstra4653f802008-09-23 15:33:44 +02007641 /*
7642 * Nobody can have more than the global setting allows.
7643 */
7644 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7645 return -EINVAL;
7646
7647 /*
7648 * The sum of our children's runtime should not exceed our own.
7649 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007650 list_for_each_entry_rcu(child, &tg->children, siblings) {
7651 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7652 runtime = child->rt_bandwidth.rt_runtime;
7653
7654 if (child == d->tg) {
7655 period = d->rt_period;
7656 runtime = d->rt_runtime;
7657 }
7658
7659 sum += to_ratio(period, runtime);
7660 }
7661
7662 if (sum > total)
7663 return -EINVAL;
7664
7665 return 0;
7666}
7667
7668static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7669{
Paul Turner82774342011-07-21 09:43:35 -07007670 int ret;
7671
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007672 struct rt_schedulable_data data = {
7673 .tg = tg,
7674 .rt_period = period,
7675 .rt_runtime = runtime,
7676 };
7677
Paul Turner82774342011-07-21 09:43:35 -07007678 rcu_read_lock();
7679 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7680 rcu_read_unlock();
7681
7682 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007683}
7684
Paul Turnerab84d312011-07-21 09:43:28 -07007685static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007686 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007687{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007688 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007689
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007690 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307691 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007692 err = __rt_schedulable(tg, rt_period, rt_runtime);
7693 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307694 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007695
Thomas Gleixner0986b112009-11-17 15:32:06 +01007696 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007697 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7698 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007699
7700 for_each_possible_cpu(i) {
7701 struct rt_rq *rt_rq = tg->rt_rq[i];
7702
Thomas Gleixner0986b112009-11-17 15:32:06 +01007703 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007704 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007705 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007706 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007707 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007708unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307709 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007710 mutex_unlock(&rt_constraints_mutex);
7711
7712 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007713}
7714
Li Zefan25cc7da2013-03-05 16:07:33 +08007715static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007716{
7717 u64 rt_runtime, rt_period;
7718
7719 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7720 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7721 if (rt_runtime_us < 0)
7722 rt_runtime = RUNTIME_INF;
7723
Paul Turnerab84d312011-07-21 09:43:28 -07007724 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007725}
7726
Li Zefan25cc7da2013-03-05 16:07:33 +08007727static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007728{
7729 u64 rt_runtime_us;
7730
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007731 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007732 return -1;
7733
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007734 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007735 do_div(rt_runtime_us, NSEC_PER_USEC);
7736 return rt_runtime_us;
7737}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007738
Li Zefan25cc7da2013-03-05 16:07:33 +08007739static int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007740{
7741 u64 rt_runtime, rt_period;
7742
7743 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
7744 rt_runtime = tg->rt_bandwidth.rt_runtime;
7745
Raistlin619b0482008-06-26 18:54:09 +02007746 if (rt_period == 0)
7747 return -EINVAL;
7748
Paul Turnerab84d312011-07-21 09:43:28 -07007749 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007750}
7751
Li Zefan25cc7da2013-03-05 16:07:33 +08007752static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007753{
7754 u64 rt_period_us;
7755
7756 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
7757 do_div(rt_period_us, NSEC_PER_USEC);
7758 return rt_period_us;
7759}
Dario Faggioli332ac172013-11-07 14:43:45 +01007760#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007761
Dario Faggioli332ac172013-11-07 14:43:45 +01007762#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007763static int sched_rt_global_constraints(void)
7764{
7765 int ret = 0;
7766
7767 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007768 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02007769 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007770 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007771 mutex_unlock(&rt_constraints_mutex);
7772
7773 return ret;
7774}
Dhaval Giani54e99122009-02-27 15:13:54 +05307775
Li Zefan25cc7da2013-03-05 16:07:33 +08007776static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05307777{
7778 /* Don't accept realtime tasks when there is no way for them to run */
7779 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
7780 return 0;
7781
7782 return 1;
7783}
7784
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007785#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007786static int sched_rt_global_constraints(void)
7787{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007788 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007789 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07007790
Thomas Gleixner0986b112009-11-17 15:32:06 +01007791 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007792 for_each_possible_cpu(i) {
7793 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
7794
Thomas Gleixner0986b112009-11-17 15:32:06 +01007795 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007796 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01007797 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007798 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007799 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007800
Dario Faggioli332ac172013-11-07 14:43:45 +01007801 return ret;
7802}
7803#endif /* CONFIG_RT_GROUP_SCHED */
7804
Dario Faggioli332ac172013-11-07 14:43:45 +01007805static int sched_dl_global_constraints(void)
7806{
Peter Zijlstra17248132013-12-17 12:44:49 +01007807 u64 runtime = global_rt_runtime();
7808 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01007809 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007810 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007811 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01007812 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007813
7814 /*
7815 * Here we want to check the bandwidth not being set to some
7816 * value smaller than the currently allocated bandwidth in
7817 * any of the root_domains.
7818 *
7819 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
7820 * cycling on root_domains... Discussion on different/better
7821 * solutions is welcome!
7822 */
Peter Zijlstra17248132013-12-17 12:44:49 +01007823 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007824 rcu_read_lock_sched();
7825 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01007826
Juri Lelli49516342014-02-11 09:24:27 +01007827 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007828 if (new_bw < dl_b->total_bw)
7829 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01007830 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007831
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007832 rcu_read_unlock_sched();
7833
Peter Zijlstra17248132013-12-17 12:44:49 +01007834 if (ret)
7835 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01007836 }
7837
Peter Zijlstra17248132013-12-17 12:44:49 +01007838 return ret;
7839}
7840
7841static void sched_dl_do_global(void)
7842{
7843 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007844 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007845 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01007846 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01007847
7848 def_dl_bandwidth.dl_period = global_rt_period();
7849 def_dl_bandwidth.dl_runtime = global_rt_runtime();
7850
7851 if (global_rt_runtime() != RUNTIME_INF)
7852 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
7853
7854 /*
7855 * FIXME: As above...
7856 */
7857 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007858 rcu_read_lock_sched();
7859 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01007860
Juri Lelli49516342014-02-11 09:24:27 +01007861 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007862 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01007863 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007864
7865 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01007866 }
7867}
7868
7869static int sched_rt_global_validate(void)
7870{
7871 if (sysctl_sched_rt_period <= 0)
7872 return -EINVAL;
7873
Juri Lellie9e7cb32014-02-11 09:24:26 +01007874 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
7875 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01007876 return -EINVAL;
7877
Dario Faggioli332ac172013-11-07 14:43:45 +01007878 return 0;
7879}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007880
Peter Zijlstra17248132013-12-17 12:44:49 +01007881static void sched_rt_do_global(void)
7882{
7883 def_rt_bandwidth.rt_runtime = global_rt_runtime();
7884 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
7885}
7886
7887int sched_rt_handler(struct ctl_table *table, int write,
7888 void __user *buffer, size_t *lenp,
7889 loff_t *ppos)
7890{
7891 int old_period, old_runtime;
7892 static DEFINE_MUTEX(mutex);
7893 int ret;
7894
7895 mutex_lock(&mutex);
7896 old_period = sysctl_sched_rt_period;
7897 old_runtime = sysctl_sched_rt_runtime;
7898
7899 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7900
7901 if (!ret && write) {
7902 ret = sched_rt_global_validate();
7903 if (ret)
7904 goto undo;
7905
7906 ret = sched_rt_global_constraints();
7907 if (ret)
7908 goto undo;
7909
7910 ret = sched_dl_global_constraints();
7911 if (ret)
7912 goto undo;
7913
7914 sched_rt_do_global();
7915 sched_dl_do_global();
7916 }
7917 if (0) {
7918undo:
7919 sysctl_sched_rt_period = old_period;
7920 sysctl_sched_rt_runtime = old_runtime;
7921 }
7922 mutex_unlock(&mutex);
7923
7924 return ret;
7925}
7926
Clark Williamsce0dbbb2013-02-07 09:47:04 -06007927int sched_rr_handler(struct ctl_table *table, int write,
7928 void __user *buffer, size_t *lenp,
7929 loff_t *ppos)
7930{
7931 int ret;
7932 static DEFINE_MUTEX(mutex);
7933
7934 mutex_lock(&mutex);
7935 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7936 /* make sure that internally we keep jiffies */
7937 /* also, writing zero resets timeslice to default */
7938 if (!ret && write) {
7939 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
7940 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
7941 }
7942 mutex_unlock(&mutex);
7943 return ret;
7944}
7945
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007946#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007947
Tejun Heoa7c6d552013-08-08 20:11:23 -04007948static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007949{
Tejun Heoa7c6d552013-08-08 20:11:23 -04007950 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007951}
7952
Tejun Heoeb954192013-08-08 20:11:23 -04007953static struct cgroup_subsys_state *
7954cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007955{
Tejun Heoeb954192013-08-08 20:11:23 -04007956 struct task_group *parent = css_tg(parent_css);
7957 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007958
Tejun Heoeb954192013-08-08 20:11:23 -04007959 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007960 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08007961 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007962 }
7963
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007964 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007965 if (IS_ERR(tg))
7966 return ERR_PTR(-ENOMEM);
7967
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007968 return &tg->css;
7969}
7970
Tejun Heoeb954192013-08-08 20:11:23 -04007971static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007972{
Tejun Heoeb954192013-08-08 20:11:23 -04007973 struct task_group *tg = css_tg(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04007974 struct task_group *parent = css_tg(css->parent);
Li Zefanace783b2013-01-24 14:30:48 +08007975
Tejun Heo63876982013-08-08 20:11:23 -04007976 if (parent)
7977 sched_online_group(tg, parent);
Li Zefanace783b2013-01-24 14:30:48 +08007978 return 0;
7979}
7980
Tejun Heoeb954192013-08-08 20:11:23 -04007981static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007982{
Tejun Heoeb954192013-08-08 20:11:23 -04007983 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007984
7985 sched_destroy_group(tg);
7986}
7987
Tejun Heoeb954192013-08-08 20:11:23 -04007988static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007989{
Tejun Heoeb954192013-08-08 20:11:23 -04007990 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08007991
7992 sched_offline_group(tg);
7993}
7994
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007995static void cpu_cgroup_fork(struct task_struct *task)
7996{
7997 sched_move_task(task);
7998}
7999
Tejun Heoeb954192013-08-08 20:11:23 -04008000static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008001 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008002{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008003 struct task_struct *task;
8004
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008005 cgroup_taskset_for_each(task, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008006#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008007 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008008 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008009#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008010 /* We don't support RT-tasks being in separate groups */
8011 if (task->sched_class != &fair_sched_class)
8012 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008013#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008014 }
Ben Blumbe367d02009-09-23 15:56:31 -07008015 return 0;
8016}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008017
Tejun Heoeb954192013-08-08 20:11:23 -04008018static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008019 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008020{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008021 struct task_struct *task;
8022
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008023 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008024 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008025}
8026
Tejun Heoeb954192013-08-08 20:11:23 -04008027static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
8028 struct cgroup_subsys_state *old_css,
8029 struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008030{
8031 /*
8032 * cgroup_exit() is called in the copy_process() failure path.
8033 * Ignore this case since the task hasn't ran yet, this avoids
8034 * trying to poke a half freed task state from generic code.
8035 */
8036 if (!(task->flags & PF_EXITING))
8037 return;
8038
8039 sched_move_task(task);
8040}
8041
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008042#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008043static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8044 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008045{
Tejun Heo182446d2013-08-08 20:11:24 -04008046 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008047}
8048
Tejun Heo182446d2013-08-08 20:11:24 -04008049static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8050 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008051{
Tejun Heo182446d2013-08-08 20:11:24 -04008052 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008053
Nikhil Raoc8b28112011-05-18 14:37:48 -07008054 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008055}
Paul Turnerab84d312011-07-21 09:43:28 -07008056
8057#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008058static DEFINE_MUTEX(cfs_constraints_mutex);
8059
Paul Turnerab84d312011-07-21 09:43:28 -07008060const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8061const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8062
Paul Turnera790de92011-07-21 09:43:29 -07008063static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8064
Paul Turnerab84d312011-07-21 09:43:28 -07008065static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8066{
Paul Turner56f570e2011-11-07 20:26:33 -08008067 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008068 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008069
8070 if (tg == &root_task_group)
8071 return -EINVAL;
8072
8073 /*
8074 * Ensure we have at some amount of bandwidth every period. This is
8075 * to prevent reaching a state of large arrears when throttled via
8076 * entity_tick() resulting in prolonged exit starvation.
8077 */
8078 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8079 return -EINVAL;
8080
8081 /*
8082 * Likewise, bound things on the otherside by preventing insane quota
8083 * periods. This also allows us to normalize in computing quota
8084 * feasibility.
8085 */
8086 if (period > max_cfs_quota_period)
8087 return -EINVAL;
8088
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008089 /*
8090 * Prevent race between setting of cfs_rq->runtime_enabled and
8091 * unthrottle_offline_cfs_rqs().
8092 */
8093 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008094 mutex_lock(&cfs_constraints_mutex);
8095 ret = __cfs_schedulable(tg, period, quota);
8096 if (ret)
8097 goto out_unlock;
8098
Paul Turner58088ad2011-07-21 09:43:31 -07008099 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008100 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008101 /*
8102 * If we need to toggle cfs_bandwidth_used, off->on must occur
8103 * before making related changes, and on->off must occur afterwards
8104 */
8105 if (runtime_enabled && !runtime_was_enabled)
8106 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008107 raw_spin_lock_irq(&cfs_b->lock);
8108 cfs_b->period = ns_to_ktime(period);
8109 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008110
Paul Turnera9cf55b2011-07-21 09:43:32 -07008111 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008112 /* restart the period timer (if active) to handle new period expiry */
8113 if (runtime_enabled && cfs_b->timer_active) {
8114 /* force a reprogram */
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04008115 __start_cfs_bandwidth(cfs_b, true);
Paul Turner58088ad2011-07-21 09:43:31 -07008116 }
Paul Turnerab84d312011-07-21 09:43:28 -07008117 raw_spin_unlock_irq(&cfs_b->lock);
8118
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008119 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008120 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008121 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008122
8123 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008124 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008125 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008126
Peter Zijlstra029632f2011-10-25 10:00:11 +02008127 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008128 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008129 raw_spin_unlock_irq(&rq->lock);
8130 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008131 if (runtime_was_enabled && !runtime_enabled)
8132 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008133out_unlock:
8134 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008135 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008136
Paul Turnera790de92011-07-21 09:43:29 -07008137 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008138}
8139
8140int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8141{
8142 u64 quota, period;
8143
Peter Zijlstra029632f2011-10-25 10:00:11 +02008144 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008145 if (cfs_quota_us < 0)
8146 quota = RUNTIME_INF;
8147 else
8148 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8149
8150 return tg_set_cfs_bandwidth(tg, period, quota);
8151}
8152
8153long tg_get_cfs_quota(struct task_group *tg)
8154{
8155 u64 quota_us;
8156
Peter Zijlstra029632f2011-10-25 10:00:11 +02008157 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008158 return -1;
8159
Peter Zijlstra029632f2011-10-25 10:00:11 +02008160 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008161 do_div(quota_us, NSEC_PER_USEC);
8162
8163 return quota_us;
8164}
8165
8166int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8167{
8168 u64 quota, period;
8169
8170 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008171 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008172
Paul Turnerab84d312011-07-21 09:43:28 -07008173 return tg_set_cfs_bandwidth(tg, period, quota);
8174}
8175
8176long tg_get_cfs_period(struct task_group *tg)
8177{
8178 u64 cfs_period_us;
8179
Peter Zijlstra029632f2011-10-25 10:00:11 +02008180 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008181 do_div(cfs_period_us, NSEC_PER_USEC);
8182
8183 return cfs_period_us;
8184}
8185
Tejun Heo182446d2013-08-08 20:11:24 -04008186static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8187 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008188{
Tejun Heo182446d2013-08-08 20:11:24 -04008189 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008190}
8191
Tejun Heo182446d2013-08-08 20:11:24 -04008192static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8193 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008194{
Tejun Heo182446d2013-08-08 20:11:24 -04008195 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008196}
8197
Tejun Heo182446d2013-08-08 20:11:24 -04008198static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8199 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008200{
Tejun Heo182446d2013-08-08 20:11:24 -04008201 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008202}
8203
Tejun Heo182446d2013-08-08 20:11:24 -04008204static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8205 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008206{
Tejun Heo182446d2013-08-08 20:11:24 -04008207 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008208}
8209
Paul Turnera790de92011-07-21 09:43:29 -07008210struct cfs_schedulable_data {
8211 struct task_group *tg;
8212 u64 period, quota;
8213};
8214
8215/*
8216 * normalize group quota/period to be quota/max_period
8217 * note: units are usecs
8218 */
8219static u64 normalize_cfs_quota(struct task_group *tg,
8220 struct cfs_schedulable_data *d)
8221{
8222 u64 quota, period;
8223
8224 if (tg == d->tg) {
8225 period = d->period;
8226 quota = d->quota;
8227 } else {
8228 period = tg_get_cfs_period(tg);
8229 quota = tg_get_cfs_quota(tg);
8230 }
8231
8232 /* note: these should typically be equivalent */
8233 if (quota == RUNTIME_INF || quota == -1)
8234 return RUNTIME_INF;
8235
8236 return to_ratio(period, quota);
8237}
8238
8239static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8240{
8241 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008242 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008243 s64 quota = 0, parent_quota = -1;
8244
8245 if (!tg->parent) {
8246 quota = RUNTIME_INF;
8247 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008248 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008249
8250 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008251 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008252
8253 /*
8254 * ensure max(child_quota) <= parent_quota, inherit when no
8255 * limit is set
8256 */
8257 if (quota == RUNTIME_INF)
8258 quota = parent_quota;
8259 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8260 return -EINVAL;
8261 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008262 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008263
8264 return 0;
8265}
8266
8267static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8268{
Paul Turner82774342011-07-21 09:43:35 -07008269 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008270 struct cfs_schedulable_data data = {
8271 .tg = tg,
8272 .period = period,
8273 .quota = quota,
8274 };
8275
8276 if (quota != RUNTIME_INF) {
8277 do_div(data.period, NSEC_PER_USEC);
8278 do_div(data.quota, NSEC_PER_USEC);
8279 }
8280
Paul Turner82774342011-07-21 09:43:35 -07008281 rcu_read_lock();
8282 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8283 rcu_read_unlock();
8284
8285 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008286}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008287
Tejun Heo2da8ca82013-12-05 12:28:04 -05008288static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008289{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008290 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008291 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008292
Tejun Heo44ffc752013-12-05 12:28:01 -05008293 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8294 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8295 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008296
8297 return 0;
8298}
Paul Turnerab84d312011-07-21 09:43:28 -07008299#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008300#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008301
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008302#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008303static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8304 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008305{
Tejun Heo182446d2013-08-08 20:11:24 -04008306 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008307}
8308
Tejun Heo182446d2013-08-08 20:11:24 -04008309static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8310 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008311{
Tejun Heo182446d2013-08-08 20:11:24 -04008312 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008313}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008314
Tejun Heo182446d2013-08-08 20:11:24 -04008315static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8316 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008317{
Tejun Heo182446d2013-08-08 20:11:24 -04008318 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008319}
8320
Tejun Heo182446d2013-08-08 20:11:24 -04008321static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8322 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008323{
Tejun Heo182446d2013-08-08 20:11:24 -04008324 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008325}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008326#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008327
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008328static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008329#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008330 {
8331 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008332 .read_u64 = cpu_shares_read_u64,
8333 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008334 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008335#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008336#ifdef CONFIG_CFS_BANDWIDTH
8337 {
8338 .name = "cfs_quota_us",
8339 .read_s64 = cpu_cfs_quota_read_s64,
8340 .write_s64 = cpu_cfs_quota_write_s64,
8341 },
8342 {
8343 .name = "cfs_period_us",
8344 .read_u64 = cpu_cfs_period_read_u64,
8345 .write_u64 = cpu_cfs_period_write_u64,
8346 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008347 {
8348 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008349 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008350 },
Paul Turnerab84d312011-07-21 09:43:28 -07008351#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008352#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008353 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008354 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008355 .read_s64 = cpu_rt_runtime_read,
8356 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008357 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008358 {
8359 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008360 .read_u64 = cpu_rt_period_read_uint,
8361 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008362 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008363#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008364 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008365};
8366
Tejun Heo073219e2014-02-08 10:36:58 -05008367struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008368 .css_alloc = cpu_cgroup_css_alloc,
8369 .css_free = cpu_cgroup_css_free,
Li Zefanace783b2013-01-24 14:30:48 +08008370 .css_online = cpu_cgroup_css_online,
8371 .css_offline = cpu_cgroup_css_offline,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008372 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008373 .can_attach = cpu_cgroup_can_attach,
8374 .attach = cpu_cgroup_attach,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008375 .exit = cpu_cgroup_exit,
Tejun Heo55779642014-07-15 11:05:09 -04008376 .legacy_cftypes = cpu_files,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008377 .early_init = 1,
8378};
8379
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008380#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008381
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008382void dump_cpu_task(int cpu)
8383{
8384 pr_info("Task dump for CPU %d:\n", cpu);
8385 sched_show_task(cpu_curr(cpu));
8386}