core.c 190 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *  kernel/sched/core.c
Linus Torvalds's avatar
Linus Torvalds committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  Kernel scheduler and related syscalls
 *
 *  Copyright (C) 1991-2002  Linus Torvalds
 *
 *  1996-12-23  Modified by Dave Grothe to fix bugs in semaphores and
 *		make semaphores SMP safe
 *  1998-11-19	Implemented schedule_timeout() and related stuff
 *		by Andrea Arcangeli
 *  2002-01-04	New ultra-scalable O(1) scheduler by Ingo Molnar:
 *		hybrid priority-list and round-robin design with
 *		an array-switch method of distributing timeslices
 *		and per-CPU runqueues.  Cleanups and useful suggestions
 *		by Davide Libenzi, preemptible kernel bits by Robert Love.
 *  2003-09-03	Interactivity tuning by Con Kolivas.
 *  2004-04-02	Scheduler domains code by Nick Piggin
Ingo Molnar's avatar
Ingo Molnar committed
19
20
21
22
23
24
 *  2007-04-15  Work begun on replacing all interactivity tuning with a
 *              fair scheduling design by Con Kolivas.
 *  2007-05-05  Load balancing (smp-nice) and other improvements
 *              by Peter Williams
 *  2007-05-06  Interactivity improvements to CFS by Mike Galbraith
 *  2007-07-01  Group scheduling enhancements by Srivatsa Vaddagiri
25
26
 *  2007-11-29  RT balancing improvements by Steven Rostedt, Gregory Haskins,
 *              Thomas Gleixner, Mike Kravetz
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
30
31
32
 */

#include <linux/mm.h>
#include <linux/module.h>
#include <linux/nmi.h>
#include <linux/init.h>
33
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
35
36
#include <linux/highmem.h>
#include <asm/mmu_context.h>
#include <linux/interrupt.h>
37
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
38
39
#include <linux/completion.h>
#include <linux/kernel_stat.h>
40
#include <linux/debug_locks.h>
41
#include <linux/perf_event.h>
Linus Torvalds's avatar
Linus Torvalds committed
42
43
44
#include <linux/security.h>
#include <linux/notifier.h>
#include <linux/profile.h>
45
#include <linux/freezer.h>
46
#include <linux/vmalloc.h>
Linus Torvalds's avatar
Linus Torvalds committed
47
48
#include <linux/blkdev.h>
#include <linux/delay.h>
49
#include <linux/pid_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
50
51
52
53
54
55
56
#include <linux/smp.h>
#include <linux/threads.h>
#include <linux/timer.h>
#include <linux/rcupdate.h>
#include <linux/cpu.h>
#include <linux/cpuset.h>
#include <linux/percpu.h>
57
#include <linux/proc_fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
58
#include <linux/seq_file.h>
59
#include <linux/sysctl.h>
Linus Torvalds's avatar
Linus Torvalds committed
60
61
#include <linux/syscalls.h>
#include <linux/times.h>
62
#include <linux/tsacct_kern.h>
63
#include <linux/kprobes.h>
64
#include <linux/delayacct.h>
65
#include <linux/unistd.h>
Jens Axboe's avatar
Jens Axboe committed
66
#include <linux/pagemap.h>
67
#include <linux/hrtimer.h>
Reynes Philippe's avatar
Reynes Philippe committed
68
#include <linux/tick.h>
Peter Zijlstra's avatar
Peter Zijlstra committed
69
70
#include <linux/debugfs.h>
#include <linux/ctype.h>
71
#include <linux/ftrace.h>
72
#include <linux/slab.h>
73
#include <linux/init_task.h>
Al Viro's avatar
Al Viro committed
74
#include <linux/binfmts.h>
75
#include <linux/context_tracking.h>
Linus Torvalds's avatar
Linus Torvalds committed
76

77
#include <asm/switch_to.h>
78
#include <asm/tlb.h>
79
#include <asm/irq_regs.h>
80
#include <asm/mutex.h>
81
82
83
#ifdef CONFIG_PARAVIRT
#include <asm/paravirt.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
84

85
#include "sched.h"
86
#include "../workqueue_internal.h"
87
#include "../smpboot.h"
88

89
#define CREATE_TRACE_POINTS
90
#include <trace/events/sched.h>
91

92
void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period)
93
{
94
95
	unsigned long delta;
	ktime_t soft, hard, now;
96

97
98
99
100
101
102
	for (;;) {
		if (hrtimer_active(period_timer))
			break;

		now = hrtimer_cb_get_time(period_timer);
		hrtimer_forward(period_timer, now, period);
103

104
105
106
107
108
109
110
111
		soft = hrtimer_get_softexpires(period_timer);
		hard = hrtimer_get_expires(period_timer);
		delta = ktime_to_ns(ktime_sub(hard, soft));
		__hrtimer_start_range_ns(period_timer, soft, delta,
					 HRTIMER_MODE_ABS_PINNED, 0);
	}
}

112
113
DEFINE_MUTEX(sched_domains_mutex);
DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
114

115
static void update_rq_clock_task(struct rq *rq, s64 delta);
116

117
void update_rq_clock(struct rq *rq)
118
{
119
	s64 delta;
120

121
	if (rq->skip_clock_update > 0)
122
		return;
123

124
125
126
	delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
	rq->clock += delta;
	update_rq_clock_task(rq, delta);
127
128
}

129
130
131
/*
 * Debugging: various feature bits
 */
Peter Zijlstra's avatar
Peter Zijlstra committed
132
133
134
135

#define SCHED_FEAT(name, enabled)	\
	(1UL << __SCHED_FEAT_##name) * enabled |

136
const_debug unsigned int sysctl_sched_features =
137
#include "features.h"
Peter Zijlstra's avatar
Peter Zijlstra committed
138
139
140
141
142
143
144
145
	0;

#undef SCHED_FEAT

#ifdef CONFIG_SCHED_DEBUG
#define SCHED_FEAT(name, enabled)	\
	#name ,

146
static const char * const sched_feat_names[] = {
147
#include "features.h"
Peter Zijlstra's avatar
Peter Zijlstra committed
148
149
150
151
};

#undef SCHED_FEAT

152
static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstra's avatar
Peter Zijlstra committed
153
154
155
{
	int i;

156
	for (i = 0; i < __SCHED_FEAT_NR; i++) {
157
158
159
		if (!(sysctl_sched_features & (1UL << i)))
			seq_puts(m, "NO_");
		seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstra's avatar
Peter Zijlstra committed
160
	}
161
	seq_puts(m, "\n");
Peter Zijlstra's avatar
Peter Zijlstra committed
162

163
	return 0;
Peter Zijlstra's avatar
Peter Zijlstra committed
164
165
}

166
167
#ifdef HAVE_JUMP_LABEL

168
169
#define jump_label_key__true  STATIC_KEY_INIT_TRUE
#define jump_label_key__false STATIC_KEY_INIT_FALSE
170
171
172
173

#define SCHED_FEAT(name, enabled)	\
	jump_label_key__##enabled ,

174
struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
175
176
177
178
179
180
181
#include "features.h"
};

#undef SCHED_FEAT

static void sched_feat_disable(int i)
{
182
183
	if (static_key_enabled(&sched_feat_keys[i]))
		static_key_slow_dec(&sched_feat_keys[i]);
184
185
186
187
}

static void sched_feat_enable(int i)
{
188
189
	if (!static_key_enabled(&sched_feat_keys[i]))
		static_key_slow_inc(&sched_feat_keys[i]);
190
191
192
193
194
195
}
#else
static void sched_feat_disable(int i) { };
static void sched_feat_enable(int i) { };
#endif /* HAVE_JUMP_LABEL */

196
static int sched_feat_set(char *cmp)
Peter Zijlstra's avatar
Peter Zijlstra committed
197
198
{
	int i;
199
	int neg = 0;
Peter Zijlstra's avatar
Peter Zijlstra committed
200

Hillf Danton's avatar
Hillf Danton committed
201
	if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstra's avatar
Peter Zijlstra committed
202
203
204
205
		neg = 1;
		cmp += 3;
	}

206
	for (i = 0; i < __SCHED_FEAT_NR; i++) {
207
		if (strcmp(cmp, sched_feat_names[i]) == 0) {
208
			if (neg) {
Peter Zijlstra's avatar
Peter Zijlstra committed
209
				sysctl_sched_features &= ~(1UL << i);
210
211
				sched_feat_disable(i);
			} else {
Peter Zijlstra's avatar
Peter Zijlstra committed
212
				sysctl_sched_features |= (1UL << i);
213
214
				sched_feat_enable(i);
			}
Peter Zijlstra's avatar
Peter Zijlstra committed
215
216
217
218
			break;
		}
	}

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	return i;
}

static ssize_t
sched_feat_write(struct file *filp, const char __user *ubuf,
		size_t cnt, loff_t *ppos)
{
	char buf[64];
	char *cmp;
	int i;

	if (cnt > 63)
		cnt = 63;

	if (copy_from_user(&buf, ubuf, cnt))
		return -EFAULT;

	buf[cnt] = 0;
	cmp = strstrip(buf);

	i = sched_feat_set(cmp);
240
	if (i == __SCHED_FEAT_NR)
Peter Zijlstra's avatar
Peter Zijlstra committed
241
242
		return -EINVAL;

243
	*ppos += cnt;
Peter Zijlstra's avatar
Peter Zijlstra committed
244
245
246
247

	return cnt;
}

248
249
250
251
252
static int sched_feat_open(struct inode *inode, struct file *filp)
{
	return single_open(filp, sched_feat_show, NULL);
}

253
static const struct file_operations sched_feat_fops = {
254
255
256
257
258
	.open		= sched_feat_open,
	.write		= sched_feat_write,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
Peter Zijlstra's avatar
Peter Zijlstra committed
259
260
261
262
263
264
265
266
267
268
};

static __init int sched_init_debug(void)
{
	debugfs_create_file("sched_features", 0644, NULL, NULL,
			&sched_feat_fops);

	return 0;
}
late_initcall(sched_init_debug);
269
#endif /* CONFIG_SCHED_DEBUG */
270

271
272
273
274
275
276
/*
 * Number of tasks to iterate in a single balance run.
 * Limited because this is done with IRQs disabled.
 */
const_debug unsigned int sysctl_sched_nr_migrate = 32;

277
278
279
280
281
282
283
284
/*
 * period over which we average the RT time consumption, measured
 * in ms.
 *
 * default: 1s
 */
const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;

Peter Zijlstra's avatar
Peter Zijlstra committed
285
/*
Peter Zijlstra's avatar
Peter Zijlstra committed
286
 * period over which we measure -rt task cpu usage in us.
Peter Zijlstra's avatar
Peter Zijlstra committed
287
288
 * default: 1s
 */
Peter Zijlstra's avatar
Peter Zijlstra committed
289
unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstra's avatar
Peter Zijlstra committed
290

291
__read_mostly int scheduler_running;
292

Peter Zijlstra's avatar
Peter Zijlstra committed
293
294
295
296
297
/*
 * part of the period that we allow rt tasks to run in us.
 * default: 0.95s
 */
int sysctl_sched_rt_runtime = 950000;
Peter Zijlstra's avatar
Peter Zijlstra committed
298

299
300
301
302
303
304
305
306
307
/*
 * Maximum bandwidth available for all -deadline tasks and groups
 * (if group scheduling is configured) on each CPU.
 *
 * default: 5%
 */
unsigned int sysctl_sched_dl_period = 1000000;
int sysctl_sched_dl_runtime = 50000;

Peter Zijlstra's avatar
Peter Zijlstra committed
308

Linus Torvalds's avatar
Linus Torvalds committed
309

310
/*
311
 * __task_rq_lock - lock the rq @p resides on.
312
 */
313
static inline struct rq *__task_rq_lock(struct task_struct *p)
314
315
	__acquires(rq->lock)
{
316
317
	struct rq *rq;

318
319
	lockdep_assert_held(&p->pi_lock);

320
	for (;;) {
321
		rq = task_rq(p);
322
		raw_spin_lock(&rq->lock);
323
		if (likely(rq == task_rq(p)))
324
			return rq;
325
		raw_spin_unlock(&rq->lock);
326
327
328
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
329
/*
330
 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
Linus Torvalds's avatar
Linus Torvalds committed
331
 */
332
static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
333
	__acquires(p->pi_lock)
Linus Torvalds's avatar
Linus Torvalds committed
334
335
	__acquires(rq->lock)
{
336
	struct rq *rq;
Linus Torvalds's avatar
Linus Torvalds committed
337

338
	for (;;) {
339
		raw_spin_lock_irqsave(&p->pi_lock, *flags);
340
		rq = task_rq(p);
341
		raw_spin_lock(&rq->lock);
342
		if (likely(rq == task_rq(p)))
343
			return rq;
344
345
		raw_spin_unlock(&rq->lock);
		raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds's avatar
Linus Torvalds committed
346
347
348
	}
}

Alexey Dobriyan's avatar
Alexey Dobriyan committed
349
static void __task_rq_unlock(struct rq *rq)
350
351
	__releases(rq->lock)
{
352
	raw_spin_unlock(&rq->lock);
353
354
}

355
356
static inline void
task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
Linus Torvalds's avatar
Linus Torvalds committed
357
	__releases(rq->lock)
358
	__releases(p->pi_lock)
Linus Torvalds's avatar
Linus Torvalds committed
359
{
360
361
	raw_spin_unlock(&rq->lock);
	raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds's avatar
Linus Torvalds committed
362
363
364
}

/*
365
 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds's avatar
Linus Torvalds committed
366
 */
Alexey Dobriyan's avatar
Alexey Dobriyan committed
367
static struct rq *this_rq_lock(void)
Linus Torvalds's avatar
Linus Torvalds committed
368
369
	__acquires(rq->lock)
{
370
	struct rq *rq;
Linus Torvalds's avatar
Linus Torvalds committed
371
372
373

	local_irq_disable();
	rq = this_rq();
374
	raw_spin_lock(&rq->lock);
Linus Torvalds's avatar
Linus Torvalds committed
375
376
377
378

	return rq;
}

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#ifdef CONFIG_SCHED_HRTICK
/*
 * Use HR-timers to deliver accurate preemption points.
 */

static void hrtick_clear(struct rq *rq)
{
	if (hrtimer_active(&rq->hrtick_timer))
		hrtimer_cancel(&rq->hrtick_timer);
}

/*
 * High-resolution timer tick.
 * Runs from hardirq context with interrupts disabled.
 */
static enum hrtimer_restart hrtick(struct hrtimer *timer)
{
	struct rq *rq = container_of(timer, struct rq, hrtick_timer);

	WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());

400
	raw_spin_lock(&rq->lock);
401
	update_rq_clock(rq);
402
	rq->curr->sched_class->task_tick(rq, rq->curr, 1);
403
	raw_spin_unlock(&rq->lock);
404
405
406
407

	return HRTIMER_NORESTART;
}

408
#ifdef CONFIG_SMP
Peter Zijlstra's avatar
Peter Zijlstra committed
409
410
411
412
413
414
415
416
417

static int __hrtick_restart(struct rq *rq)
{
	struct hrtimer *timer = &rq->hrtick_timer;
	ktime_t time = hrtimer_get_softexpires(timer);

	return __hrtimer_start_range_ns(timer, time, 0, HRTIMER_MODE_ABS_PINNED, 0);
}

418
419
420
421
/*
 * called from hardirq (IPI) context
 */
static void __hrtick_start(void *arg)
422
{
423
	struct rq *rq = arg;
424

425
	raw_spin_lock(&rq->lock);
Peter Zijlstra's avatar
Peter Zijlstra committed
426
	__hrtick_restart(rq);
427
	rq->hrtick_csd_pending = 0;
428
	raw_spin_unlock(&rq->lock);
429
430
}

431
432
433
434
435
/*
 * Called to set the hrtick timer state.
 *
 * called with rq->lock held and irqs disabled
 */
436
void hrtick_start(struct rq *rq, u64 delay)
437
{
438
439
	struct hrtimer *timer = &rq->hrtick_timer;
	ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
440

441
	hrtimer_set_expires(timer, time);
442
443

	if (rq == this_rq()) {
Peter Zijlstra's avatar
Peter Zijlstra committed
444
		__hrtick_restart(rq);
445
	} else if (!rq->hrtick_csd_pending) {
446
		__smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
447
448
		rq->hrtick_csd_pending = 1;
	}
449
450
451
452
453
454
455
456
457
458
459
460
461
462
}

static int
hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
{
	int cpu = (int)(long)hcpu;

	switch (action) {
	case CPU_UP_CANCELED:
	case CPU_UP_CANCELED_FROZEN:
	case CPU_DOWN_PREPARE:
	case CPU_DOWN_PREPARE_FROZEN:
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
463
		hrtick_clear(cpu_rq(cpu));
464
465
466
467
468
469
		return NOTIFY_OK;
	}

	return NOTIFY_DONE;
}

470
static __init void init_hrtick(void)
471
472
473
{
	hotcpu_notifier(hotplug_hrtick, 0);
}
474
475
476
477
478
479
#else
/*
 * Called to set the hrtick timer state.
 *
 * called with rq->lock held and irqs disabled
 */
480
void hrtick_start(struct rq *rq, u64 delay)
481
{
482
	__hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
483
			HRTIMER_MODE_REL_PINNED, 0);
484
}
485

Andrew Morton's avatar
Andrew Morton committed
486
static inline void init_hrtick(void)
487
488
{
}
489
#endif /* CONFIG_SMP */
490

491
static void init_rq_hrtick(struct rq *rq)
492
{
493
494
#ifdef CONFIG_SMP
	rq->hrtick_csd_pending = 0;
495

496
497
498
499
	rq->hrtick_csd.flags = 0;
	rq->hrtick_csd.func = __hrtick_start;
	rq->hrtick_csd.info = rq;
#endif
500

501
502
	hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	rq->hrtick_timer.function = hrtick;
503
}
Andrew Morton's avatar
Andrew Morton committed
504
#else	/* CONFIG_SCHED_HRTICK */
505
506
507
508
509
510
511
512
static inline void hrtick_clear(struct rq *rq)
{
}

static inline void init_rq_hrtick(struct rq *rq)
{
}

513
514
515
static inline void init_hrtick(void)
{
}
Andrew Morton's avatar
Andrew Morton committed
516
#endif	/* CONFIG_SCHED_HRTICK */
517

518
519
520
521
522
523
524
/*
 * resched_task - mark a task 'to be rescheduled now'.
 *
 * On UP this means the setting of the need_resched flag, on SMP it
 * might also involve a cross-CPU call to trigger the scheduler on
 * the target CPU.
 */
525
void resched_task(struct task_struct *p)
526
527
528
{
	int cpu;

529
	lockdep_assert_held(&task_rq(p)->lock);
530

531
	if (test_tsk_need_resched(p))
532
533
		return;

534
	set_tsk_need_resched(p);
535
536

	cpu = task_cpu(p);
537
538
	if (cpu == smp_processor_id()) {
		set_preempt_need_resched();
539
		return;
540
	}
541
542
543
544
545
546
547

	/* NEED_RESCHED must be visible before we test polling */
	smp_mb();
	if (!tsk_is_polling(p))
		smp_send_reschedule(cpu);
}

548
void resched_cpu(int cpu)
549
550
551
552
{
	struct rq *rq = cpu_rq(cpu);
	unsigned long flags;

553
	if (!raw_spin_trylock_irqsave(&rq->lock, flags))
554
555
		return;
	resched_task(cpu_curr(cpu));
556
	raw_spin_unlock_irqrestore(&rq->lock, flags);
557
}
558

559
#ifdef CONFIG_SMP
560
#ifdef CONFIG_NO_HZ_COMMON
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * In the semi idle case, use the nearest busy cpu for migrating timers
 * from an idle cpu.  This is good for power-savings.
 *
 * We don't do similar optimization for completely idle system, as
 * selecting an idle cpu will add more delays to the timers than intended
 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
 */
int get_nohz_timer_target(void)
{
	int cpu = smp_processor_id();
	int i;
	struct sched_domain *sd;

575
	rcu_read_lock();
576
	for_each_domain(cpu, sd) {
577
578
579
580
581
582
		for_each_cpu(i, sched_domain_span(sd)) {
			if (!idle_cpu(i)) {
				cpu = i;
				goto unlock;
			}
		}
583
	}
584
585
unlock:
	rcu_read_unlock();
586
587
	return cpu;
}
588
589
590
591
592
593
594
595
596
597
/*
 * When add_timer_on() enqueues a timer into the timer wheel of an
 * idle CPU then this timer might expire before the next timer event
 * which is scheduled to wake up that CPU. In case of a completely
 * idle system the next event might even be infinite time into the
 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
 * leaves the inner idle loop so the newly added timer is taken into
 * account when the CPU goes back to idle and evaluates the timer
 * wheel for the next timer event.
 */
598
static void wake_up_idle_cpu(int cpu)
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
{
	struct rq *rq = cpu_rq(cpu);

	if (cpu == smp_processor_id())
		return;

	/*
	 * This is safe, as this function is called with the timer
	 * wheel base lock of (cpu) held. When the CPU is on the way
	 * to idle and has not yet set rq->curr to idle then it will
	 * be serialized on the timer wheel base lock and take the new
	 * timer into account automatically.
	 */
	if (rq->curr != rq->idle)
		return;
614
615

	/*
616
617
618
	 * We can set TIF_RESCHED on the idle task of the other CPU
	 * lockless. The worst case is that the other CPU runs the
	 * idle task through an additional NOOP schedule()
619
	 */
620
	set_tsk_need_resched(rq->idle);
621

622
623
624
625
	/* NEED_RESCHED must be visible before we test polling */
	smp_mb();
	if (!tsk_is_polling(rq->idle))
		smp_send_reschedule(cpu);
626
627
}

628
static bool wake_up_full_nohz_cpu(int cpu)
629
{
630
	if (tick_nohz_full_cpu(cpu)) {
631
632
633
634
635
636
637
638
639
640
641
		if (cpu != smp_processor_id() ||
		    tick_nohz_tick_stopped())
			smp_send_reschedule(cpu);
		return true;
	}

	return false;
}

void wake_up_nohz_cpu(int cpu)
{
642
	if (!wake_up_full_nohz_cpu(cpu))
643
644
645
		wake_up_idle_cpu(cpu);
}

646
static inline bool got_nohz_idle_kick(void)
647
{
648
	int cpu = smp_processor_id();
649
650
651
652
653
654
655
656
657
658
659
660
661

	if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
		return false;

	if (idle_cpu(cpu) && !need_resched())
		return true;

	/*
	 * We can't run Idle Load Balance on this CPU for this time so we
	 * cancel it and clear NOHZ_BALANCE_KICK
	 */
	clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
	return false;
662
663
}

664
#else /* CONFIG_NO_HZ_COMMON */
665

666
static inline bool got_nohz_idle_kick(void)
Peter Zijlstra's avatar
Peter Zijlstra committed
667
{
668
	return false;
Peter Zijlstra's avatar
Peter Zijlstra committed
669
670
}

671
#endif /* CONFIG_NO_HZ_COMMON */
672

673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#ifdef CONFIG_NO_HZ_FULL
bool sched_can_stop_tick(void)
{
       struct rq *rq;

       rq = this_rq();

       /* Make sure rq->nr_running update is visible after the IPI */
       smp_rmb();

       /* More than one running task need preemption */
       if (rq->nr_running > 1)
               return false;

       return true;
}
#endif /* CONFIG_NO_HZ_FULL */
690

691
void sched_avg_update(struct rq *rq)
692
{
693
694
	s64 period = sched_avg_period();

695
	while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
696
697
698
699
700
701
		/*
		 * Inline assembly required to prevent the compiler
		 * optimising this loop into a divmod call.
		 * See __iter_div_u64_rem() for another example of this.
		 */
		asm("" : "+rm" (rq->age_stamp));
702
703
704
		rq->age_stamp += period;
		rq->rt_avg /= 2;
	}
705
706
}

707
#endif /* CONFIG_SMP */
708

709
710
#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
			(defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
711
/*
712
713
714
715
 * Iterate task_group tree rooted at *from, calling @down when first entering a
 * node and @up when leaving it for the final time.
 *
 * Caller must hold rcu_lock or sufficient equivalent.
716
 */
717
int walk_tg_tree_from(struct task_group *from,
718
			     tg_visitor down, tg_visitor up, void *data)
719
720
{
	struct task_group *parent, *child;
Peter Zijlstra's avatar
Peter Zijlstra committed
721
	int ret;
722

723
724
	parent = from;

725
down:
Peter Zijlstra's avatar
Peter Zijlstra committed
726
727
	ret = (*down)(parent, data);
	if (ret)
728
		goto out;
729
730
731
732
733
734
735
	list_for_each_entry_rcu(child, &parent->children, siblings) {
		parent = child;
		goto down;

up:
		continue;
	}
Peter Zijlstra's avatar
Peter Zijlstra committed
736
	ret = (*up)(parent, data);
737
738
	if (ret || parent == from)
		goto out;
739
740
741
742
743

	child = parent;
	parent = parent->parent;
	if (parent)
		goto up;
744
out:
Peter Zijlstra's avatar
Peter Zijlstra committed
745
	return ret;
746
747
}

748
int tg_nop(struct task_group *tg, void *data)
Peter Zijlstra's avatar
Peter Zijlstra committed
749
{
750
	return 0;
Peter Zijlstra's avatar
Peter Zijlstra committed
751
}
752
753
#endif

754
755
static void set_load_weight(struct task_struct *p)
{
Nikhil Rao's avatar
Nikhil Rao committed
756
757
758
	int prio = p->static_prio - MAX_RT_PRIO;
	struct load_weight *load = &p->se.load;

Ingo Molnar's avatar
Ingo Molnar committed
759
760
761
762
	/*
	 * SCHED_IDLE tasks get minimal weight:
	 */
	if (p->policy == SCHED_IDLE) {
763
		load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Rao's avatar
Nikhil Rao committed
764
		load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnar's avatar
Ingo Molnar committed
765
766
		return;
	}
767

768
	load->weight = scale_load(prio_to_weight[prio]);
Nikhil Rao's avatar
Nikhil Rao committed
769
	load->inv_weight = prio_to_wmult[prio];
770
771
}

772
static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
773
{
774
	update_rq_clock(rq);
775
	sched_info_queued(rq, p);
776
	p->sched_class->enqueue_task(rq, p, flags);
777
778
}

779
static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
780
{
781
	update_rq_clock(rq);
782
	sched_info_dequeued(rq, p);
783
	p->sched_class->dequeue_task(rq, p, flags);
784
785
}

786
void activate_task(struct rq *rq, struct task_struct *p, int flags)
787
788
789
790
{
	if (task_contributes_to_load(p))
		rq->nr_uninterruptible--;

791
	enqueue_task(rq, p, flags);
792
793
}

794
void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
795
796
797
798
{
	if (task_contributes_to_load(p))
		rq->nr_uninterruptible++;

799
	dequeue_task(rq, p, flags);
800
801
}

802
static void update_rq_clock_task(struct rq *rq, s64 delta)
803
{
804
805
806
807
808
809
810
811
/*
 * In theory, the compile should just see 0 here, and optimize out the call
 * to sched_rt_avg_update. But I don't trust it...
 */
#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
	s64 steal = 0, irq_delta = 0;
#endif
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
812
	irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833

	/*
	 * Since irq_time is only updated on {soft,}irq_exit, we might run into
	 * this case when a previous update_rq_clock() happened inside a
	 * {soft,}irq region.
	 *
	 * When this happens, we stop ->clock_task and only update the
	 * prev_irq_time stamp to account for the part that fit, so that a next
	 * update will consume the rest. This ensures ->clock_task is
	 * monotonic.
	 *
	 * It does however cause some slight miss-attribution of {soft,}irq
	 * time, a more accurate solution would be to update the irq_time using
	 * the current rq->clock timestamp, except that would require using
	 * atomic ops.
	 */
	if (irq_delta > delta)
		irq_delta = delta;

	rq->prev_irq_time += irq_delta;
	delta -= irq_delta;
834
835
#endif
#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
836
	if (static_key_false((&paravirt_steal_rq_enabled))) {
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
		u64 st;

		steal = paravirt_steal_clock(cpu_of(rq));
		steal -= rq->prev_steal_time_rq;

		if (unlikely(steal > delta))
			steal = delta;

		st = steal_ticks(steal);
		steal = st * TICK_NSEC;

		rq->prev_steal_time_rq += steal;

		delta -= steal;
	}
#endif

854
855
	rq->clock_task += delta;

856
857
858
859
#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
	if ((irq_delta + steal) && sched_feat(NONTASK_POWER))
		sched_rt_avg_update(rq, irq_delta + steal);
#endif
860
861
}

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
void sched_set_stop_task(int cpu, struct task_struct *stop)
{
	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
	struct task_struct *old_stop = cpu_rq(cpu)->stop;

	if (stop) {
		/*
		 * Make it appear like a SCHED_FIFO task, its something
		 * userspace knows about and won't get confused about.
		 *
		 * Also, it will make PI more or less work without too
		 * much confusion -- but then, stop work should not
		 * rely on PI working anyway.
		 */
		sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);

		stop->sched_class = &stop_sched_class;
	}

	cpu_rq(cpu)->stop = stop;

	if (old_stop) {
		/*
		 * Reset it back to a normal scheduling class so that
		 * it can die in pieces.
		 */
		old_stop->sched_class = &rt_sched_class;
	}
}

892
/*
Ingo Molnar's avatar
Ingo Molnar committed
893
 * __normal_prio - return the priority that is based on the static prio
894
895
896
 */
static inline int __normal_prio(struct task_struct *p)
{
Ingo Molnar's avatar
Ingo Molnar committed
897
	return p->static_prio;
898
899
}

900
901
902
903
904
905
906
/*
 * Calculate the expected normal priority: i.e. priority
 * without taking RT-inheritance into account. Might be
 * boosted by interactivity modifiers. Changes upon fork,
 * setprio syscalls, and whenever the interactivity
 * estimator recalculates.
 */
907
static inline int normal_prio(struct task_struct *p)
908
909
910
{
	int prio;

911
912
913
	if (task_has_dl_policy(p))
		prio = MAX_DL_PRIO-1;
	else if (task_has_rt_policy(p))
914
915
916
917
918
919
920
921
922
923
924
925
926
		prio = MAX_RT_PRIO-1 - p->rt_priority;
	else
		prio = __normal_prio(p);
	return prio;
}

/*
 * Calculate the current priority, i.e. the priority
 * taken into account by the scheduler. This value might
 * be boosted by RT tasks, or might be boosted by
 * interactivity modifiers. Will be RT if the task got
 * RT-boosted. If not then it returns p->normal_prio.
 */
927
static int effective_prio(struct task_struct *p)
928
929
930
931
932
933
934
935
936
937
938
939
{
	p->normal_prio = normal_prio(p);
	/*
	 * If we are RT tasks or we were boosted to RT priority,
	 * keep the priority unchanged. Otherwise, update priority
	 * to the normal priority:
	 */
	if (!rt_prio(p->prio))
		return p->normal_prio;
	return p->prio;
}

Linus Torvalds's avatar
Linus Torvalds committed
940
941
942
/**
 * task_curr - is this task currently executing on a CPU?
 * @p: the task in question.
943
944
 *
 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds's avatar
Linus Torvalds committed
945
 */
946
inline int task_curr(const struct task_struct *p)
Linus Torvalds's avatar
Linus Torvalds committed
947
948
949
950
{
	return cpu_curr(task_cpu(p)) == p;
}

951
952
static inline void check_class_changed(struct rq *rq, struct task_struct *p,
				       const struct sched_class *prev_class,
Peter Zijlstra's avatar
Peter Zijlstra committed
953
				       int oldprio)
954
955
956
{
	if (prev_class != p->sched_class) {
		if (prev_class->switched_from)
Peter Zijlstra's avatar
Peter Zijlstra committed
957
958
			prev_class->switched_from(rq, p);
		p->sched_class->switched_to(rq, p);
959
	} else if (oldprio != p->prio || dl_task(p))
Peter Zijlstra's avatar
Peter Zijlstra committed
960
		p->sched_class->prio_changed(rq, p, oldprio);
961
962
}

963
void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
{
	const struct sched_class *class;

	if (p->sched_class == rq->curr->sched_class) {
		rq->curr->sched_class->check_preempt_curr(rq, p, flags);
	} else {
		for_each_class(class) {
			if (class == rq->curr->sched_class)
				break;
			if (class == p->sched_class) {
				resched_task(rq->curr);
				break;
			}
		}
	}

	/*
	 * A queue event has occurred, and we're going to schedule.  In
	 * this case, we can save a useless back to back clock update.
	 */
Peter Zijlstra's avatar
Peter Zijlstra committed
984
	if (rq->curr->on_rq && test_tsk_need_resched(rq->curr))
985
986
987
		rq->skip_clock_update = 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
988
#ifdef CONFIG_SMP
Ingo Molnar's avatar
Ingo Molnar committed
989
void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnar's avatar
Ingo Molnar committed
990
{
991
992
993
994
995
#ifdef CONFIG_SCHED_DEBUG
	/*
	 * We should never call set_task_cpu() on a blocked task,
	 * ttwu() will sort out the placement.
	 */
Peter Zijlstra's avatar
Peter Zijlstra committed
996
	WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
997
			!(task_preempt_count(p) & PREEMPT_ACTIVE));
998
999

#ifdef CONFIG_LOCKDEP
1000
1001
1002
1003
1004
	/*
	 * The caller should hold either p->pi_lock or rq->lock, when changing
	 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
	 *
	 * sched_move_task() holds both and thus holding either pins the cgroup,
1005
	 * see task_group().
1006
1007
1008
1009
	 *
	 * Furthermore, all task_rq users should acquire both locks, see
	 * task_rq_lock().
	 */
1010
1011
1012
	WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
				      lockdep_is_held(&task_rq(p)->lock)));
#endif
1013
1014
#endif

1015
	trace_sched_migrate_task(p, new_cpu);
1016

1017
	if (task_cpu(p) != new_cpu) {
1018
1019
		if (p->sched_class->migrate_task_rq)
			p->sched_class->migrate_task_rq(p, new_cpu);
1020
		p->se.nr_migrations++;
1021
		perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
1022
	}
Ingo Molnar's avatar
Ingo Molnar committed
1023
1024

	__set_task_cpu(p, new_cpu);
Ingo Molnar's avatar
Ingo Molnar committed
1025
1026
}

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
static void __migrate_swap_task(struct task_struct *p, int cpu)
{
	if (p->on_rq) {
		struct rq *src_rq, *dst_rq;

		src_rq = task_rq(p);
		dst_rq = cpu_rq(cpu);

		deactivate_task(src_rq, p, 0);
		set_task_cpu(p, cpu);
		activate_task(dst_rq, p, 0);
		check_preempt_curr(dst_rq, p, 0);
	} else {
		/*
		 * Task isn't running anymore; make it appear like we migrated
		 * it before it went to sleep. This means on wakeup we make the
		 * previous cpu our targer instead of where it really is.
		 */
		p->wake_cpu = cpu;
	}
}

struct migration_swap_arg {
	struct task_struct *src_task, *dst_task;
	int src_cpu, dst_cpu;
};

static int migrate_swap_stop(void *data)
{
	struct migration_swap_arg *arg = data;
	struct rq *src_rq, *dst_rq;
	int ret = -EAGAIN;

	src_rq = cpu_rq(arg->src_cpu);
	dst_rq = cpu_rq(arg->dst_cpu);

1063
1064
	double_raw_lock(&arg->src_task->pi_lock,
			&arg->dst_task->pi_lock);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
	double_rq_lock(src_rq, dst_rq);
	if (task_cpu(arg->dst_task) != arg->dst_cpu)
		goto unlock;

	if (task_cpu(arg->src_task) != arg->src_cpu)
		goto unlock;

	if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
		goto unlock;

	if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
		goto unlock;

	__migrate_swap_task(arg->src_task, arg->dst_cpu);
	__migrate_swap_task(arg->dst_task, arg->src_cpu);

	ret = 0;

unlock:
	double_rq_unlock(src_rq, dst_rq);
1085
1086
	raw_spin_unlock(&arg->dst_task->pi_lock);
	raw_spin_unlock(&arg->src_task->pi_lock);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108

	return ret;
}

/*
 * Cross migrate two tasks
 */
int migrate_swap(struct task_struct *cur, struct task_struct *p)
{
	struct migration_swap_arg arg;
	int ret = -EINVAL;

	arg = (struct migration_swap_arg){
		.src_task = cur,
		.src_cpu = task_cpu(cur),
		.dst_task = p,
		.dst_cpu = task_cpu(p),
	};

	if (arg.src_cpu == arg.dst_cpu)
		goto out;

1109
1110
1111
1112
	/*
	 * These three tests are all lockless; this is OK since all of them
	 * will be re-checked with proper locks held further down the line.
	 */
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
	if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
		goto out;

	if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
		goto out;

	if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
		goto out;

	ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);

out:
	return ret;
}

1128
struct migration_arg {
1129
	struct task_struct *task;
Linus Torvalds's avatar
Linus Torvalds committed
1130
	int dest_cpu;
1131
};
Linus Torvalds's avatar
Linus Torvalds committed
1132

1133
1134
static int migration_cpu_stop(void *data);

Linus Torvalds's avatar
Linus Torvalds committed
1135
1136
1137
/*
 * wait_task_inactive - wait for a thread to unschedule.
 *
Roland McGrath's avatar
Roland McGrath committed
1138
1139
1140
1141
1142
1143
1144
 * If @match_state is nonzero, it's the @p->state value just checked and
 * not expected to change.  If it changes, i.e. @p might have woken up,
 * then return zero.  When we succeed in waiting for @p to be off its CPU,
 * we return a positive number (its total switch count).  If a second call
 * a short while later returns the same number, the caller can be sure that
 * @p has remained unscheduled the whole time.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1145
1146
1147
1148
1149
1150
 * The caller must ensure that the task *will* unschedule sometime soon,
 * else this function might spin for a *long* time. This function can't
 * be called with interrupts off, or it may introduce deadlock with
 * smp_call_function() if an IPI is sent by the same process we are
 * waiting to become inactive.
 */
Roland McGrath's avatar
Roland McGrath committed
1151
unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds's avatar
Linus Torvalds committed
1152
1153
{
	unsigned long flags;
Ingo Molnar's avatar
Ingo Molnar committed
1154
	int running, on_rq;
Roland McGrath's avatar
Roland McGrath committed
1155
	unsigned long ncsw;
1156
	struct rq *rq;
Linus Torvalds's avatar
Linus Torvalds committed
1157

1158
1159
1160
1161
1162
1163
1164
1165
	for (;;) {
		/*
		 * We do the initial early heuristics without holding
		 * any task-queue locks at all. We'll only try to get
		 * the runqueue lock when things look like they will
		 * work out!
		 */
		rq = task_rq(p);
1166

1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
		/*
		 * If the task is actively running on another CPU
		 * still, just relax and busy-wait without holding
		 * any locks.
		 *
		 * NOTE! Since we don't hold any locks, it's not
		 * even sure that "rq" stays as the right runqueue!
		 * But we don't care, since "task_running()" will
		 * return false if the runqueue has changed and p
		 * is actually now running somewhere else!
		 */
Roland McGrath's avatar
Roland McGrath committed
1178
1179
1180
		while (task_running(rq, p)) {
			if (match_state && unlikely(p->state != match_state))
				return 0;
1181
			cpu_relax();
Roland McGrath's avatar