]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/sched_fair.c
sched: Implement demand based update_cfs_load()
[mirror_ubuntu-artful-kernel.git] / kernel / sched_fair.c
CommitLineData
bf0f6f24
IM
1/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
21805085
PZ
18 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
bf0f6f24
IM
21 */
22
9745512c 23#include <linux/latencytop.h>
1983a922 24#include <linux/sched.h>
9745512c 25
bf0f6f24 26/*
21805085 27 * Targeted preemption latency for CPU-bound tasks:
864616ee 28 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24 29 *
21805085 30 * NOTE: this latency value is not the same as the concept of
d274a4ce
IM
31 * 'timeslice length' - timeslices in CFS are of variable length
32 * and have no persistent notion like in traditional, time-slice
33 * based scheduling concepts.
bf0f6f24 34 *
d274a4ce
IM
35 * (to see the precise effective timeslice length of your workload,
36 * run vmstat and monitor the context-switches (cs) field)
bf0f6f24 37 */
21406928
MG
38unsigned int sysctl_sched_latency = 6000000ULL;
39unsigned int normalized_sysctl_sched_latency = 6000000ULL;
2bd8e6d4 40
1983a922
CE
41/*
42 * The initial- and re-scaling of tunables is configurable
43 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
44 *
45 * Options are:
46 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
47 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
48 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
49 */
50enum sched_tunable_scaling sysctl_sched_tunable_scaling
51 = SCHED_TUNABLESCALING_LOG;
52
2bd8e6d4 53/*
b2be5e96 54 * Minimal preemption granularity for CPU-bound tasks:
864616ee 55 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
2bd8e6d4 56 */
0bf377bb
IM
57unsigned int sysctl_sched_min_granularity = 750000ULL;
58unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
21805085
PZ
59
60/*
b2be5e96
PZ
61 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
62 */
0bf377bb 63static unsigned int sched_nr_latency = 8;
b2be5e96
PZ
64
65/*
2bba22c5 66 * After fork, child runs first. If set to 0 (default) then
b2be5e96 67 * parent will (try to) run first.
21805085 68 */
2bba22c5 69unsigned int sysctl_sched_child_runs_first __read_mostly;
bf0f6f24 70
1799e35d
IM
71/*
72 * sys_sched_yield() compat mode
73 *
74 * This option switches the agressive yield implementation of the
75 * old scheduler back on.
76 */
77unsigned int __read_mostly sysctl_sched_compat_yield;
78
bf0f6f24
IM
79/*
80 * SCHED_OTHER wake-up granularity.
172e082a 81 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24
IM
82 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
172e082a 87unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
0bcdcf28 88unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
bf0f6f24 89
da84d961
IM
90const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
a7a4f8a7
PT
92/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
a4c2f00f
PZ
99static const struct sched_class fair_sched_class;
100
bf0f6f24
IM
101/**************************************************************
102 * CFS operations on generic schedulable entities:
103 */
104
62160e3f 105#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24 106
62160e3f 107/* cpu runqueue to which this cfs_rq is attached */
bf0f6f24
IM
108static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
109{
62160e3f 110 return cfs_rq->rq;
bf0f6f24
IM
111}
112
62160e3f
IM
113/* An entity is a task if it doesn't "own" a runqueue */
114#define entity_is_task(se) (!se->my_q)
bf0f6f24 115
8f48894f
PZ
116static inline struct task_struct *task_of(struct sched_entity *se)
117{
118#ifdef CONFIG_SCHED_DEBUG
119 WARN_ON_ONCE(!entity_is_task(se));
120#endif
121 return container_of(se, struct task_struct, se);
122}
123
b758149c
PZ
124/* Walk up scheduling entities hierarchy */
125#define for_each_sched_entity(se) \
126 for (; se; se = se->parent)
127
128static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
129{
130 return p->se.cfs_rq;
131}
132
133/* runqueue on which this entity is (to be) queued */
134static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
135{
136 return se->cfs_rq;
137}
138
139/* runqueue "owned" by this group */
140static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
141{
142 return grp->my_q;
143}
144
145/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
146 * another cpu ('this_cpu')
147 */
148static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
149{
150 return cfs_rq->tg->cfs_rq[this_cpu];
151}
152
3d4b47b4
PZ
153static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
154{
155 if (!cfs_rq->on_list) {
67e86250
PT
156 /*
157 * Ensure we either appear before our parent (if already
158 * enqueued) or force our parent to appear after us when it is
159 * enqueued. The fact that we always enqueue bottom-up
160 * reduces this to two cases.
161 */
162 if (cfs_rq->tg->parent &&
163 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
164 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
165 &rq_of(cfs_rq)->leaf_cfs_rq_list);
166 } else {
167 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
3d4b47b4 168 &rq_of(cfs_rq)->leaf_cfs_rq_list);
67e86250 169 }
3d4b47b4
PZ
170
171 cfs_rq->on_list = 1;
172 }
173}
174
175static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
176{
177 if (cfs_rq->on_list) {
178 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
179 cfs_rq->on_list = 0;
180 }
181}
182
b758149c
PZ
183/* Iterate thr' all leaf cfs_rq's on a runqueue */
184#define for_each_leaf_cfs_rq(rq, cfs_rq) \
185 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
186
187/* Do the two (enqueued) entities belong to the same group ? */
188static inline int
189is_same_group(struct sched_entity *se, struct sched_entity *pse)
190{
191 if (se->cfs_rq == pse->cfs_rq)
192 return 1;
193
194 return 0;
195}
196
197static inline struct sched_entity *parent_entity(struct sched_entity *se)
198{
199 return se->parent;
200}
201
464b7527
PZ
202/* return depth at which a sched entity is present in the hierarchy */
203static inline int depth_se(struct sched_entity *se)
204{
205 int depth = 0;
206
207 for_each_sched_entity(se)
208 depth++;
209
210 return depth;
211}
212
213static void
214find_matching_se(struct sched_entity **se, struct sched_entity **pse)
215{
216 int se_depth, pse_depth;
217
218 /*
219 * preemption test can be made between sibling entities who are in the
220 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
221 * both tasks until we find their ancestors who are siblings of common
222 * parent.
223 */
224
225 /* First walk up until both entities are at same depth */
226 se_depth = depth_se(*se);
227 pse_depth = depth_se(*pse);
228
229 while (se_depth > pse_depth) {
230 se_depth--;
231 *se = parent_entity(*se);
232 }
233
234 while (pse_depth > se_depth) {
235 pse_depth--;
236 *pse = parent_entity(*pse);
237 }
238
239 while (!is_same_group(*se, *pse)) {
240 *se = parent_entity(*se);
241 *pse = parent_entity(*pse);
242 }
243}
244
8f48894f
PZ
245#else /* !CONFIG_FAIR_GROUP_SCHED */
246
247static inline struct task_struct *task_of(struct sched_entity *se)
248{
249 return container_of(se, struct task_struct, se);
250}
bf0f6f24 251
62160e3f
IM
252static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
253{
254 return container_of(cfs_rq, struct rq, cfs);
bf0f6f24
IM
255}
256
257#define entity_is_task(se) 1
258
b758149c
PZ
259#define for_each_sched_entity(se) \
260 for (; se; se = NULL)
bf0f6f24 261
b758149c 262static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
bf0f6f24 263{
b758149c 264 return &task_rq(p)->cfs;
bf0f6f24
IM
265}
266
b758149c
PZ
267static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
268{
269 struct task_struct *p = task_of(se);
270 struct rq *rq = task_rq(p);
271
272 return &rq->cfs;
273}
274
275/* runqueue "owned" by this group */
276static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
277{
278 return NULL;
279}
280
281static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
282{
283 return &cpu_rq(this_cpu)->cfs;
284}
285
3d4b47b4
PZ
286static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
287{
288}
289
290static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
291{
292}
293
b758149c
PZ
294#define for_each_leaf_cfs_rq(rq, cfs_rq) \
295 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
296
297static inline int
298is_same_group(struct sched_entity *se, struct sched_entity *pse)
299{
300 return 1;
301}
302
303static inline struct sched_entity *parent_entity(struct sched_entity *se)
304{
305 return NULL;
306}
307
464b7527
PZ
308static inline void
309find_matching_se(struct sched_entity **se, struct sched_entity **pse)
310{
311}
312
b758149c
PZ
313#endif /* CONFIG_FAIR_GROUP_SCHED */
314
bf0f6f24
IM
315
316/**************************************************************
317 * Scheduling class tree data structure manipulation methods:
318 */
319
0702e3eb 320static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
02e0431a 321{
368059a9
PZ
322 s64 delta = (s64)(vruntime - min_vruntime);
323 if (delta > 0)
02e0431a
PZ
324 min_vruntime = vruntime;
325
326 return min_vruntime;
327}
328
0702e3eb 329static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
b0ffd246
PZ
330{
331 s64 delta = (s64)(vruntime - min_vruntime);
332 if (delta < 0)
333 min_vruntime = vruntime;
334
335 return min_vruntime;
336}
337
54fdc581
FC
338static inline int entity_before(struct sched_entity *a,
339 struct sched_entity *b)
340{
341 return (s64)(a->vruntime - b->vruntime) < 0;
342}
343
0702e3eb 344static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
9014623c 345{
30cfdcfc 346 return se->vruntime - cfs_rq->min_vruntime;
9014623c
PZ
347}
348
1af5f730
PZ
349static void update_min_vruntime(struct cfs_rq *cfs_rq)
350{
351 u64 vruntime = cfs_rq->min_vruntime;
352
353 if (cfs_rq->curr)
354 vruntime = cfs_rq->curr->vruntime;
355
356 if (cfs_rq->rb_leftmost) {
357 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
358 struct sched_entity,
359 run_node);
360
e17036da 361 if (!cfs_rq->curr)
1af5f730
PZ
362 vruntime = se->vruntime;
363 else
364 vruntime = min_vruntime(vruntime, se->vruntime);
365 }
366
367 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
368}
369
bf0f6f24
IM
370/*
371 * Enqueue an entity into the rb-tree:
372 */
0702e3eb 373static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
374{
375 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
376 struct rb_node *parent = NULL;
377 struct sched_entity *entry;
9014623c 378 s64 key = entity_key(cfs_rq, se);
bf0f6f24
IM
379 int leftmost = 1;
380
381 /*
382 * Find the right place in the rbtree:
383 */
384 while (*link) {
385 parent = *link;
386 entry = rb_entry(parent, struct sched_entity, run_node);
387 /*
388 * We dont care about collisions. Nodes with
389 * the same key stay together.
390 */
9014623c 391 if (key < entity_key(cfs_rq, entry)) {
bf0f6f24
IM
392 link = &parent->rb_left;
393 } else {
394 link = &parent->rb_right;
395 leftmost = 0;
396 }
397 }
398
399 /*
400 * Maintain a cache of leftmost tree entries (it is frequently
401 * used):
402 */
1af5f730 403 if (leftmost)
57cb499d 404 cfs_rq->rb_leftmost = &se->run_node;
bf0f6f24
IM
405
406 rb_link_node(&se->run_node, parent, link);
407 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24
IM
408}
409
0702e3eb 410static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 411{
3fe69747
PZ
412 if (cfs_rq->rb_leftmost == &se->run_node) {
413 struct rb_node *next_node;
3fe69747
PZ
414
415 next_node = rb_next(&se->run_node);
416 cfs_rq->rb_leftmost = next_node;
3fe69747 417 }
e9acbff6 418
bf0f6f24 419 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24
IM
420}
421
bf0f6f24
IM
422static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
423{
f4b6755f
PZ
424 struct rb_node *left = cfs_rq->rb_leftmost;
425
426 if (!left)
427 return NULL;
428
429 return rb_entry(left, struct sched_entity, run_node);
bf0f6f24
IM
430}
431
f4b6755f 432static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
aeb73b04 433{
7eee3e67 434 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
aeb73b04 435
70eee74b
BS
436 if (!last)
437 return NULL;
7eee3e67
IM
438
439 return rb_entry(last, struct sched_entity, run_node);
aeb73b04
PZ
440}
441
bf0f6f24
IM
442/**************************************************************
443 * Scheduling class statistics methods:
444 */
445
b2be5e96 446#ifdef CONFIG_SCHED_DEBUG
acb4a848 447int sched_proc_update_handler(struct ctl_table *table, int write,
8d65af78 448 void __user *buffer, size_t *lenp,
b2be5e96
PZ
449 loff_t *ppos)
450{
8d65af78 451 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
acb4a848 452 int factor = get_update_sysctl_factor();
b2be5e96
PZ
453
454 if (ret || !write)
455 return ret;
456
457 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
458 sysctl_sched_min_granularity);
459
acb4a848
CE
460#define WRT_SYSCTL(name) \
461 (normalized_sysctl_##name = sysctl_##name / (factor))
462 WRT_SYSCTL(sched_min_granularity);
463 WRT_SYSCTL(sched_latency);
464 WRT_SYSCTL(sched_wakeup_granularity);
acb4a848
CE
465#undef WRT_SYSCTL
466
b2be5e96
PZ
467 return 0;
468}
469#endif
647e7cac 470
a7be37ac 471/*
f9c0b095 472 * delta /= w
a7be37ac
PZ
473 */
474static inline unsigned long
475calc_delta_fair(unsigned long delta, struct sched_entity *se)
476{
f9c0b095
PZ
477 if (unlikely(se->load.weight != NICE_0_LOAD))
478 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
a7be37ac
PZ
479
480 return delta;
481}
482
647e7cac
IM
483/*
484 * The idea is to set a period in which each task runs once.
485 *
486 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
487 * this period because otherwise the slices get too small.
488 *
489 * p = (nr <= nl) ? l : l*nr/nl
490 */
4d78e7b6
PZ
491static u64 __sched_period(unsigned long nr_running)
492{
493 u64 period = sysctl_sched_latency;
b2be5e96 494 unsigned long nr_latency = sched_nr_latency;
4d78e7b6
PZ
495
496 if (unlikely(nr_running > nr_latency)) {
4bf0b771 497 period = sysctl_sched_min_granularity;
4d78e7b6 498 period *= nr_running;
4d78e7b6
PZ
499 }
500
501 return period;
502}
503
647e7cac
IM
504/*
505 * We calculate the wall-time slice from the period by taking a part
506 * proportional to the weight.
507 *
f9c0b095 508 * s = p*P[w/rw]
647e7cac 509 */
6d0f0ebd 510static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
21805085 511{
0a582440 512 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
f9c0b095 513
0a582440 514 for_each_sched_entity(se) {
6272d68c 515 struct load_weight *load;
3104bf03 516 struct load_weight lw;
6272d68c
LM
517
518 cfs_rq = cfs_rq_of(se);
519 load = &cfs_rq->load;
f9c0b095 520
0a582440 521 if (unlikely(!se->on_rq)) {
3104bf03 522 lw = cfs_rq->load;
0a582440
MG
523
524 update_load_add(&lw, se->load.weight);
525 load = &lw;
526 }
527 slice = calc_delta_mine(slice, se->load.weight, load);
528 }
529 return slice;
bf0f6f24
IM
530}
531
647e7cac 532/*
ac884dec 533 * We calculate the vruntime slice of a to be inserted task
647e7cac 534 *
f9c0b095 535 * vs = s/w
647e7cac 536 */
f9c0b095 537static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
67e9fb2a 538{
f9c0b095 539 return calc_delta_fair(sched_slice(cfs_rq, se), se);
a7be37ac
PZ
540}
541
3b3d190e
PT
542static void update_cfs_load(struct cfs_rq *cfs_rq);
543static void update_cfs_shares(struct cfs_rq *cfs_rq, long weight_delta);
544
bf0f6f24
IM
545/*
546 * Update the current task's runtime statistics. Skip current tasks that
547 * are not in our scheduling class.
548 */
549static inline void
8ebc91d9
IM
550__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
551 unsigned long delta_exec)
bf0f6f24 552{
bbdba7c0 553 unsigned long delta_exec_weighted;
bf0f6f24 554
41acab88
LDM
555 schedstat_set(curr->statistics.exec_max,
556 max((u64)delta_exec, curr->statistics.exec_max));
bf0f6f24
IM
557
558 curr->sum_exec_runtime += delta_exec;
7a62eabc 559 schedstat_add(cfs_rq, exec_clock, delta_exec);
a7be37ac 560 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
88ec22d3 561
e9acbff6 562 curr->vruntime += delta_exec_weighted;
1af5f730 563 update_min_vruntime(cfs_rq);
3b3d190e
PT
564
565#ifdef CONFIG_FAIR_GROUP_SCHED
566 cfs_rq->load_unacc_exec_time += delta_exec;
567 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
568 update_cfs_load(cfs_rq);
569 update_cfs_shares(cfs_rq, 0);
570 }
571#endif
bf0f6f24
IM
572}
573
b7cc0896 574static void update_curr(struct cfs_rq *cfs_rq)
bf0f6f24 575{
429d43bc 576 struct sched_entity *curr = cfs_rq->curr;
305e6835 577 u64 now = rq_of(cfs_rq)->clock_task;
bf0f6f24
IM
578 unsigned long delta_exec;
579
580 if (unlikely(!curr))
581 return;
582
583 /*
584 * Get the amount of time the current task was running
585 * since the last time we changed load (this cannot
586 * overflow on 32 bits):
587 */
8ebc91d9 588 delta_exec = (unsigned long)(now - curr->exec_start);
34f28ecd
PZ
589 if (!delta_exec)
590 return;
bf0f6f24 591
8ebc91d9
IM
592 __update_curr(cfs_rq, curr, delta_exec);
593 curr->exec_start = now;
d842de87
SV
594
595 if (entity_is_task(curr)) {
596 struct task_struct *curtask = task_of(curr);
597
f977bb49 598 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
d842de87 599 cpuacct_charge(curtask, delta_exec);
f06febc9 600 account_group_exec_runtime(curtask, delta_exec);
d842de87 601 }
bf0f6f24
IM
602}
603
604static inline void
5870db5b 605update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 606{
41acab88 607 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
bf0f6f24
IM
608}
609
bf0f6f24
IM
610/*
611 * Task is being enqueued - update stats:
612 */
d2417e5a 613static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 614{
bf0f6f24
IM
615 /*
616 * Are we enqueueing a waiting task? (for current tasks
617 * a dequeue/enqueue event is a NOP)
618 */
429d43bc 619 if (se != cfs_rq->curr)
5870db5b 620 update_stats_wait_start(cfs_rq, se);
bf0f6f24
IM
621}
622
bf0f6f24 623static void
9ef0a961 624update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 625{
41acab88
LDM
626 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
627 rq_of(cfs_rq)->clock - se->statistics.wait_start));
628 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
629 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
630 rq_of(cfs_rq)->clock - se->statistics.wait_start);
768d0c27
PZ
631#ifdef CONFIG_SCHEDSTATS
632 if (entity_is_task(se)) {
633 trace_sched_stat_wait(task_of(se),
41acab88 634 rq_of(cfs_rq)->clock - se->statistics.wait_start);
768d0c27
PZ
635 }
636#endif
41acab88 637 schedstat_set(se->statistics.wait_start, 0);
bf0f6f24
IM
638}
639
640static inline void
19b6a2e3 641update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 642{
bf0f6f24
IM
643 /*
644 * Mark the end of the wait period if dequeueing a
645 * waiting task:
646 */
429d43bc 647 if (se != cfs_rq->curr)
9ef0a961 648 update_stats_wait_end(cfs_rq, se);
bf0f6f24
IM
649}
650
651/*
652 * We are picking a new current task - update its stats:
653 */
654static inline void
79303e9e 655update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
656{
657 /*
658 * We are starting a new run period:
659 */
305e6835 660 se->exec_start = rq_of(cfs_rq)->clock_task;
bf0f6f24
IM
661}
662
bf0f6f24
IM
663/**************************************************
664 * Scheduling class queueing methods:
665 */
666
c09595f6
PZ
667#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
668static void
669add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
670{
671 cfs_rq->task_weight += weight;
672}
673#else
674static inline void
675add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
676{
677}
678#endif
679
30cfdcfc
DA
680static void
681account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
682{
683 update_load_add(&cfs_rq->load, se->load.weight);
c09595f6
PZ
684 if (!parent_entity(se))
685 inc_cpu_load(rq_of(cfs_rq), se->load.weight);
b87f1724 686 if (entity_is_task(se)) {
c09595f6 687 add_cfs_task_weight(cfs_rq, se->load.weight);
b87f1724
BR
688 list_add(&se->group_node, &cfs_rq->tasks);
689 }
30cfdcfc 690 cfs_rq->nr_running++;
30cfdcfc
DA
691}
692
693static void
694account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
695{
696 update_load_sub(&cfs_rq->load, se->load.weight);
c09595f6
PZ
697 if (!parent_entity(se))
698 dec_cpu_load(rq_of(cfs_rq), se->load.weight);
b87f1724 699 if (entity_is_task(se)) {
c09595f6 700 add_cfs_task_weight(cfs_rq, -se->load.weight);
b87f1724
BR
701 list_del_init(&se->group_node);
702 }
30cfdcfc 703 cfs_rq->nr_running--;
30cfdcfc
DA
704}
705
2069dd75 706#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
e33078ba 707static void update_cfs_load(struct cfs_rq *cfs_rq)
2069dd75 708{
a7a4f8a7 709 u64 period = sysctl_sched_shares_window;
2069dd75 710 u64 now, delta;
e33078ba 711 unsigned long load = cfs_rq->load.weight;
2069dd75
PZ
712
713 if (!cfs_rq)
714 return;
715
716 now = rq_of(cfs_rq)->clock;
717 delta = now - cfs_rq->load_stamp;
718
e33078ba
PT
719 /* truncate load history at 4 idle periods */
720 if (cfs_rq->load_stamp > cfs_rq->load_last &&
721 now - cfs_rq->load_last > 4 * period) {
722 cfs_rq->load_period = 0;
723 cfs_rq->load_avg = 0;
724 }
725
2069dd75 726 cfs_rq->load_stamp = now;
3b3d190e 727 cfs_rq->load_unacc_exec_time = 0;
2069dd75 728 cfs_rq->load_period += delta;
e33078ba
PT
729 if (load) {
730 cfs_rq->load_last = now;
731 cfs_rq->load_avg += delta * load;
732 }
2069dd75
PZ
733
734 while (cfs_rq->load_period > period) {
735 /*
736 * Inline assembly required to prevent the compiler
737 * optimising this loop into a divmod call.
738 * See __iter_div_u64_rem() for another example of this.
739 */
740 asm("" : "+rm" (cfs_rq->load_period));
741 cfs_rq->load_period /= 2;
742 cfs_rq->load_avg /= 2;
743 }
3d4b47b4 744
e33078ba
PT
745 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
746 list_del_leaf_cfs_rq(cfs_rq);
2069dd75
PZ
747}
748
749static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
750 unsigned long weight)
751{
752 if (se->on_rq)
753 account_entity_dequeue(cfs_rq, se);
754
755 update_load_set(&se->load, weight);
756
757 if (se->on_rq)
758 account_entity_enqueue(cfs_rq, se);
759}
760
f0d7442a 761static void update_cfs_shares(struct cfs_rq *cfs_rq, long weight_delta)
2069dd75
PZ
762{
763 struct task_group *tg;
764 struct sched_entity *se;
765 long load_weight, load, shares;
766
767 if (!cfs_rq)
768 return;
769
770 tg = cfs_rq->tg;
771 se = tg->se[cpu_of(rq_of(cfs_rq))];
772 if (!se)
773 return;
774
f0d7442a 775 load = cfs_rq->load.weight + weight_delta;
2069dd75
PZ
776
777 load_weight = atomic_read(&tg->load_weight);
778 load_weight -= cfs_rq->load_contribution;
779 load_weight += load;
780
781 shares = (tg->shares * load);
782 if (load_weight)
783 shares /= load_weight;
784
785 if (shares < MIN_SHARES)
786 shares = MIN_SHARES;
787 if (shares > tg->shares)
788 shares = tg->shares;
789
790 reweight_entity(cfs_rq_of(se), se, shares);
791}
792#else /* CONFIG_FAIR_GROUP_SCHED */
e33078ba 793static inline void update_cfs_load(struct cfs_rq *cfs_rq)
2069dd75
PZ
794{
795}
796
f0d7442a 797static inline void update_cfs_shares(struct cfs_rq *cfs_rq, long weight_delta)
2069dd75
PZ
798{
799}
800#endif /* CONFIG_FAIR_GROUP_SCHED */
801
2396af69 802static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 803{
bf0f6f24 804#ifdef CONFIG_SCHEDSTATS
e414314c
PZ
805 struct task_struct *tsk = NULL;
806
807 if (entity_is_task(se))
808 tsk = task_of(se);
809
41acab88
LDM
810 if (se->statistics.sleep_start) {
811 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
bf0f6f24
IM
812
813 if ((s64)delta < 0)
814 delta = 0;
815
41acab88
LDM
816 if (unlikely(delta > se->statistics.sleep_max))
817 se->statistics.sleep_max = delta;
bf0f6f24 818
41acab88
LDM
819 se->statistics.sleep_start = 0;
820 se->statistics.sum_sleep_runtime += delta;
9745512c 821
768d0c27 822 if (tsk) {
e414314c 823 account_scheduler_latency(tsk, delta >> 10, 1);
768d0c27
PZ
824 trace_sched_stat_sleep(tsk, delta);
825 }
bf0f6f24 826 }
41acab88
LDM
827 if (se->statistics.block_start) {
828 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
bf0f6f24
IM
829
830 if ((s64)delta < 0)
831 delta = 0;
832
41acab88
LDM
833 if (unlikely(delta > se->statistics.block_max))
834 se->statistics.block_max = delta;
bf0f6f24 835
41acab88
LDM
836 se->statistics.block_start = 0;
837 se->statistics.sum_sleep_runtime += delta;
30084fbd 838
e414314c 839 if (tsk) {
8f0dfc34 840 if (tsk->in_iowait) {
41acab88
LDM
841 se->statistics.iowait_sum += delta;
842 se->statistics.iowait_count++;
768d0c27 843 trace_sched_stat_iowait(tsk, delta);
8f0dfc34
AV
844 }
845
e414314c
PZ
846 /*
847 * Blocking time is in units of nanosecs, so shift by
848 * 20 to get a milliseconds-range estimation of the
849 * amount of time that the task spent sleeping:
850 */
851 if (unlikely(prof_on == SLEEP_PROFILING)) {
852 profile_hits(SLEEP_PROFILING,
853 (void *)get_wchan(tsk),
854 delta >> 20);
855 }
856 account_scheduler_latency(tsk, delta >> 10, 0);
30084fbd 857 }
bf0f6f24
IM
858 }
859#endif
860}
861
ddc97297
PZ
862static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
863{
864#ifdef CONFIG_SCHED_DEBUG
865 s64 d = se->vruntime - cfs_rq->min_vruntime;
866
867 if (d < 0)
868 d = -d;
869
870 if (d > 3*sysctl_sched_latency)
871 schedstat_inc(cfs_rq, nr_spread_over);
872#endif
873}
874
aeb73b04
PZ
875static void
876place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
877{
1af5f730 878 u64 vruntime = cfs_rq->min_vruntime;
94dfb5e7 879
2cb8600e
PZ
880 /*
881 * The 'current' period is already promised to the current tasks,
882 * however the extra weight of the new task will slow them down a
883 * little, place the new task so that it fits in the slot that
884 * stays open at the end.
885 */
94dfb5e7 886 if (initial && sched_feat(START_DEBIT))
f9c0b095 887 vruntime += sched_vslice(cfs_rq, se);
aeb73b04 888
a2e7a7eb 889 /* sleeps up to a single latency don't count. */
5ca9880c 890 if (!initial) {
a2e7a7eb 891 unsigned long thresh = sysctl_sched_latency;
a7be37ac 892
a2e7a7eb
MG
893 /*
894 * Halve their sleep time's effect, to allow
895 * for a gentler effect of sleepers:
896 */
897 if (sched_feat(GENTLE_FAIR_SLEEPERS))
898 thresh >>= 1;
51e0304c 899
a2e7a7eb 900 vruntime -= thresh;
aeb73b04
PZ
901 }
902
b5d9d734
MG
903 /* ensure we never gain time by being placed backwards. */
904 vruntime = max_vruntime(se->vruntime, vruntime);
905
67e9fb2a 906 se->vruntime = vruntime;
aeb73b04
PZ
907}
908
bf0f6f24 909static void
88ec22d3 910enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
bf0f6f24 911{
88ec22d3
PZ
912 /*
913 * Update the normalized vruntime before updating min_vruntime
914 * through callig update_curr().
915 */
371fd7e7 916 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
88ec22d3
PZ
917 se->vruntime += cfs_rq->min_vruntime;
918
bf0f6f24 919 /*
a2a2d680 920 * Update run-time statistics of the 'current'.
bf0f6f24 921 */
b7cc0896 922 update_curr(cfs_rq);
e33078ba 923 update_cfs_load(cfs_rq);
f0d7442a 924 update_cfs_shares(cfs_rq, se->load.weight);
a992241d 925 account_entity_enqueue(cfs_rq, se);
bf0f6f24 926
88ec22d3 927 if (flags & ENQUEUE_WAKEUP) {
aeb73b04 928 place_entity(cfs_rq, se, 0);
2396af69 929 enqueue_sleeper(cfs_rq, se);
e9acbff6 930 }
bf0f6f24 931
d2417e5a 932 update_stats_enqueue(cfs_rq, se);
ddc97297 933 check_spread(cfs_rq, se);
83b699ed
SV
934 if (se != cfs_rq->curr)
935 __enqueue_entity(cfs_rq, se);
2069dd75 936 se->on_rq = 1;
3d4b47b4
PZ
937
938 if (cfs_rq->nr_running == 1)
939 list_add_leaf_cfs_rq(cfs_rq);
bf0f6f24
IM
940}
941
a571bbea 942static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
2002c695 943{
de69a80b 944 if (!se || cfs_rq->last == se)
2002c695
PZ
945 cfs_rq->last = NULL;
946
de69a80b 947 if (!se || cfs_rq->next == se)
2002c695
PZ
948 cfs_rq->next = NULL;
949}
950
a571bbea
PZ
951static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
952{
953 for_each_sched_entity(se)
954 __clear_buddies(cfs_rq_of(se), se);
955}
956
bf0f6f24 957static void
371fd7e7 958dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
bf0f6f24 959{
a2a2d680
DA
960 /*
961 * Update run-time statistics of the 'current'.
962 */
963 update_curr(cfs_rq);
964
19b6a2e3 965 update_stats_dequeue(cfs_rq, se);
371fd7e7 966 if (flags & DEQUEUE_SLEEP) {
67e9fb2a 967#ifdef CONFIG_SCHEDSTATS
bf0f6f24
IM
968 if (entity_is_task(se)) {
969 struct task_struct *tsk = task_of(se);
970
971 if (tsk->state & TASK_INTERRUPTIBLE)
41acab88 972 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
bf0f6f24 973 if (tsk->state & TASK_UNINTERRUPTIBLE)
41acab88 974 se->statistics.block_start = rq_of(cfs_rq)->clock;
bf0f6f24 975 }
db36cc7d 976#endif
67e9fb2a
PZ
977 }
978
2002c695 979 clear_buddies(cfs_rq, se);
4793241b 980
83b699ed 981 if (se != cfs_rq->curr)
30cfdcfc 982 __dequeue_entity(cfs_rq, se);
2069dd75 983 se->on_rq = 0;
e33078ba 984 update_cfs_load(cfs_rq);
30cfdcfc 985 account_entity_dequeue(cfs_rq, se);
1af5f730 986 update_min_vruntime(cfs_rq);
f0d7442a 987 update_cfs_shares(cfs_rq, 0);
88ec22d3
PZ
988
989 /*
990 * Normalize the entity after updating the min_vruntime because the
991 * update can refer to the ->curr item and we need to reflect this
992 * movement in our normalized position.
993 */
371fd7e7 994 if (!(flags & DEQUEUE_SLEEP))
88ec22d3 995 se->vruntime -= cfs_rq->min_vruntime;
bf0f6f24
IM
996}
997
998/*
999 * Preempt the current task with a newly woken task if needed:
1000 */
7c92e54f 1001static void
2e09bf55 1002check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
bf0f6f24 1003{
11697830
PZ
1004 unsigned long ideal_runtime, delta_exec;
1005
6d0f0ebd 1006 ideal_runtime = sched_slice(cfs_rq, curr);
11697830 1007 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
a9f3e2b5 1008 if (delta_exec > ideal_runtime) {
bf0f6f24 1009 resched_task(rq_of(cfs_rq)->curr);
a9f3e2b5
MG
1010 /*
1011 * The current task ran long enough, ensure it doesn't get
1012 * re-elected due to buddy favours.
1013 */
1014 clear_buddies(cfs_rq, curr);
f685ceac
MG
1015 return;
1016 }
1017
1018 /*
1019 * Ensure that a task that missed wakeup preemption by a
1020 * narrow margin doesn't have to wait for a full slice.
1021 * This also mitigates buddy induced latencies under load.
1022 */
1023 if (!sched_feat(WAKEUP_PREEMPT))
1024 return;
1025
1026 if (delta_exec < sysctl_sched_min_granularity)
1027 return;
1028
1029 if (cfs_rq->nr_running > 1) {
1030 struct sched_entity *se = __pick_next_entity(cfs_rq);
1031 s64 delta = curr->vruntime - se->vruntime;
1032
1033 if (delta > ideal_runtime)
1034 resched_task(rq_of(cfs_rq)->curr);
a9f3e2b5 1035 }
bf0f6f24
IM
1036}
1037
83b699ed 1038static void
8494f412 1039set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 1040{
83b699ed
SV
1041 /* 'current' is not kept within the tree. */
1042 if (se->on_rq) {
1043 /*
1044 * Any task has to be enqueued before it get to execute on
1045 * a CPU. So account for the time it spent waiting on the
1046 * runqueue.
1047 */
1048 update_stats_wait_end(cfs_rq, se);
1049 __dequeue_entity(cfs_rq, se);
1050 }
1051
79303e9e 1052 update_stats_curr_start(cfs_rq, se);
429d43bc 1053 cfs_rq->curr = se;
eba1ed4b
IM
1054#ifdef CONFIG_SCHEDSTATS
1055 /*
1056 * Track our maximum slice length, if the CPU's load is at
1057 * least twice that of our own weight (i.e. dont track it
1058 * when there are only lesser-weight tasks around):
1059 */
495eca49 1060 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
41acab88 1061 se->statistics.slice_max = max(se->statistics.slice_max,
eba1ed4b
IM
1062 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1063 }
1064#endif
4a55b450 1065 se->prev_sum_exec_runtime = se->sum_exec_runtime;
bf0f6f24
IM
1066}
1067
3f3a4904
PZ
1068static int
1069wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1070
f4b6755f 1071static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
aa2ac252 1072{
f4b6755f 1073 struct sched_entity *se = __pick_next_entity(cfs_rq);
f685ceac 1074 struct sched_entity *left = se;
f4b6755f 1075
f685ceac
MG
1076 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1077 se = cfs_rq->next;
aa2ac252 1078
f685ceac
MG
1079 /*
1080 * Prefer last buddy, try to return the CPU to a preempted task.
1081 */
1082 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1083 se = cfs_rq->last;
1084
1085 clear_buddies(cfs_rq, se);
4793241b
PZ
1086
1087 return se;
aa2ac252
PZ
1088}
1089
ab6cde26 1090static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
bf0f6f24
IM
1091{
1092 /*
1093 * If still on the runqueue then deactivate_task()
1094 * was not called and update_curr() has to be done:
1095 */
1096 if (prev->on_rq)
b7cc0896 1097 update_curr(cfs_rq);
bf0f6f24 1098
ddc97297 1099 check_spread(cfs_rq, prev);
30cfdcfc 1100 if (prev->on_rq) {
5870db5b 1101 update_stats_wait_start(cfs_rq, prev);
30cfdcfc
DA
1102 /* Put 'current' back into the tree. */
1103 __enqueue_entity(cfs_rq, prev);
1104 }
429d43bc 1105 cfs_rq->curr = NULL;
bf0f6f24
IM
1106}
1107
8f4d37ec
PZ
1108static void
1109entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
bf0f6f24 1110{
bf0f6f24 1111 /*
30cfdcfc 1112 * Update run-time statistics of the 'current'.
bf0f6f24 1113 */
30cfdcfc 1114 update_curr(cfs_rq);
bf0f6f24 1115
8f4d37ec
PZ
1116#ifdef CONFIG_SCHED_HRTICK
1117 /*
1118 * queued ticks are scheduled to match the slice, so don't bother
1119 * validating it and just reschedule.
1120 */
983ed7a6
HH
1121 if (queued) {
1122 resched_task(rq_of(cfs_rq)->curr);
1123 return;
1124 }
8f4d37ec
PZ
1125 /*
1126 * don't let the period tick interfere with the hrtick preemption
1127 */
1128 if (!sched_feat(DOUBLE_TICK) &&
1129 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1130 return;
1131#endif
1132
ce6c1311 1133 if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
2e09bf55 1134 check_preempt_tick(cfs_rq, curr);
bf0f6f24
IM
1135}
1136
1137/**************************************************
1138 * CFS operations on tasks:
1139 */
1140
8f4d37ec
PZ
1141#ifdef CONFIG_SCHED_HRTICK
1142static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
1143{
8f4d37ec
PZ
1144 struct sched_entity *se = &p->se;
1145 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1146
1147 WARN_ON(task_rq(p) != rq);
1148
1149 if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
1150 u64 slice = sched_slice(cfs_rq, se);
1151 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
1152 s64 delta = slice - ran;
1153
1154 if (delta < 0) {
1155 if (rq->curr == p)
1156 resched_task(p);
1157 return;
1158 }
1159
1160 /*
1161 * Don't schedule slices shorter than 10000ns, that just
1162 * doesn't make sense. Rely on vruntime for fairness.
1163 */
31656519 1164 if (rq->curr != p)
157124c1 1165 delta = max_t(s64, 10000LL, delta);
8f4d37ec 1166
31656519 1167 hrtick_start(rq, delta);
8f4d37ec
PZ
1168 }
1169}
a4c2f00f
PZ
1170
1171/*
1172 * called from enqueue/dequeue and updates the hrtick when the
1173 * current task is from our class and nr_running is low enough
1174 * to matter.
1175 */
1176static void hrtick_update(struct rq *rq)
1177{
1178 struct task_struct *curr = rq->curr;
1179
1180 if (curr->sched_class != &fair_sched_class)
1181 return;
1182
1183 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
1184 hrtick_start_fair(rq, curr);
1185}
55e12e5e 1186#else /* !CONFIG_SCHED_HRTICK */
8f4d37ec
PZ
1187static inline void
1188hrtick_start_fair(struct rq *rq, struct task_struct *p)
1189{
1190}
a4c2f00f
PZ
1191
1192static inline void hrtick_update(struct rq *rq)
1193{
1194}
8f4d37ec
PZ
1195#endif
1196
bf0f6f24
IM
1197/*
1198 * The enqueue_task method is called before nr_running is
1199 * increased. Here we update the fair scheduling stats and
1200 * then put the task into the rbtree:
1201 */
ea87bb78 1202static void
371fd7e7 1203enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
bf0f6f24
IM
1204{
1205 struct cfs_rq *cfs_rq;
62fb1851 1206 struct sched_entity *se = &p->se;
bf0f6f24
IM
1207
1208 for_each_sched_entity(se) {
62fb1851 1209 if (se->on_rq)
bf0f6f24
IM
1210 break;
1211 cfs_rq = cfs_rq_of(se);
88ec22d3
PZ
1212 enqueue_entity(cfs_rq, se, flags);
1213 flags = ENQUEUE_WAKEUP;
bf0f6f24 1214 }
8f4d37ec 1215
2069dd75
PZ
1216 for_each_sched_entity(se) {
1217 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1218
e33078ba 1219 update_cfs_load(cfs_rq);
f0d7442a 1220 update_cfs_shares(cfs_rq, 0);
2069dd75
PZ
1221 }
1222
a4c2f00f 1223 hrtick_update(rq);
bf0f6f24
IM
1224}
1225
1226/*
1227 * The dequeue_task method is called before nr_running is
1228 * decreased. We remove the task from the rbtree and
1229 * update the fair scheduling stats:
1230 */
371fd7e7 1231static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
bf0f6f24
IM
1232{
1233 struct cfs_rq *cfs_rq;
62fb1851 1234 struct sched_entity *se = &p->se;
bf0f6f24
IM
1235
1236 for_each_sched_entity(se) {
1237 cfs_rq = cfs_rq_of(se);
371fd7e7 1238 dequeue_entity(cfs_rq, se, flags);
2069dd75 1239
bf0f6f24 1240 /* Don't dequeue parent if it has other entities besides us */
62fb1851 1241 if (cfs_rq->load.weight)
bf0f6f24 1242 break;
371fd7e7 1243 flags |= DEQUEUE_SLEEP;
bf0f6f24 1244 }
8f4d37ec 1245
2069dd75
PZ
1246 for_each_sched_entity(se) {
1247 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1248
e33078ba 1249 update_cfs_load(cfs_rq);
f0d7442a 1250 update_cfs_shares(cfs_rq, 0);
2069dd75
PZ
1251 }
1252
a4c2f00f 1253 hrtick_update(rq);
bf0f6f24
IM
1254}
1255
1256/*
1799e35d
IM
1257 * sched_yield() support is very simple - we dequeue and enqueue.
1258 *
1259 * If compat_yield is turned on then we requeue to the end of the tree.
bf0f6f24 1260 */
4530d7ab 1261static void yield_task_fair(struct rq *rq)
bf0f6f24 1262{
db292ca3
IM
1263 struct task_struct *curr = rq->curr;
1264 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
1265 struct sched_entity *rightmost, *se = &curr->se;
bf0f6f24
IM
1266
1267 /*
1799e35d
IM
1268 * Are we the only task in the tree?
1269 */
1270 if (unlikely(cfs_rq->nr_running == 1))
1271 return;
1272
2002c695
PZ
1273 clear_buddies(cfs_rq, se);
1274
db292ca3 1275 if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
3e51f33f 1276 update_rq_clock(rq);
1799e35d 1277 /*
a2a2d680 1278 * Update run-time statistics of the 'current'.
1799e35d 1279 */
2b1e315d 1280 update_curr(cfs_rq);
1799e35d
IM
1281
1282 return;
1283 }
1284 /*
1285 * Find the rightmost entry in the rbtree:
bf0f6f24 1286 */
2b1e315d 1287 rightmost = __pick_last_entity(cfs_rq);
1799e35d
IM
1288 /*
1289 * Already in the rightmost position?
1290 */
54fdc581 1291 if (unlikely(!rightmost || entity_before(rightmost, se)))
1799e35d
IM
1292 return;
1293
1294 /*
1295 * Minimally necessary key value to be last in the tree:
2b1e315d
DA
1296 * Upon rescheduling, sched_class::put_prev_task() will place
1297 * 'current' within the tree based on its new key value.
1799e35d 1298 */
30cfdcfc 1299 se->vruntime = rightmost->vruntime + 1;
bf0f6f24
IM
1300}
1301
e7693a36 1302#ifdef CONFIG_SMP
098fb9db 1303
88ec22d3
PZ
1304static void task_waking_fair(struct rq *rq, struct task_struct *p)
1305{
1306 struct sched_entity *se = &p->se;
1307 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1308
1309 se->vruntime -= cfs_rq->min_vruntime;
1310}
1311
bb3469ac 1312#ifdef CONFIG_FAIR_GROUP_SCHED
f5bfb7d9
PZ
1313/*
1314 * effective_load() calculates the load change as seen from the root_task_group
1315 *
1316 * Adding load to a group doesn't make a group heavier, but can cause movement
1317 * of group shares between cpus. Assuming the shares were perfectly aligned one
1318 * can calculate the shift in shares.
f5bfb7d9 1319 */
2069dd75 1320static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
bb3469ac 1321{
4be9daaa 1322 struct sched_entity *se = tg->se[cpu];
f1d239f7
PZ
1323
1324 if (!tg->parent)
1325 return wl;
1326
4be9daaa 1327 for_each_sched_entity(se) {
cb5ef42a 1328 long S, rw, s, a, b;
4be9daaa
PZ
1329
1330 S = se->my_q->tg->shares;
2069dd75
PZ
1331 s = se->load.weight;
1332 rw = se->my_q->load.weight;
bb3469ac 1333
cb5ef42a
PZ
1334 a = S*(rw + wl);
1335 b = S*rw + s*wg;
4be9daaa 1336
940959e9
PZ
1337 wl = s*(a-b);
1338
1339 if (likely(b))
1340 wl /= b;
1341
83378269
PZ
1342 /*
1343 * Assume the group is already running and will
1344 * thus already be accounted for in the weight.
1345 *
1346 * That is, moving shares between CPUs, does not
1347 * alter the group weight.
1348 */
4be9daaa 1349 wg = 0;
4be9daaa 1350 }
bb3469ac 1351
4be9daaa 1352 return wl;
bb3469ac 1353}
4be9daaa 1354
bb3469ac 1355#else
4be9daaa 1356
83378269
PZ
1357static inline unsigned long effective_load(struct task_group *tg, int cpu,
1358 unsigned long wl, unsigned long wg)
4be9daaa 1359{
83378269 1360 return wl;
bb3469ac 1361}
4be9daaa 1362
bb3469ac
PZ
1363#endif
1364
c88d5910 1365static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
098fb9db 1366{
c88d5910
PZ
1367 unsigned long this_load, load;
1368 int idx, this_cpu, prev_cpu;
098fb9db 1369 unsigned long tl_per_task;
c88d5910 1370 struct task_group *tg;
83378269 1371 unsigned long weight;
b3137bc8 1372 int balanced;
098fb9db 1373
c88d5910
PZ
1374 idx = sd->wake_idx;
1375 this_cpu = smp_processor_id();
1376 prev_cpu = task_cpu(p);
1377 load = source_load(prev_cpu, idx);
1378 this_load = target_load(this_cpu, idx);
098fb9db 1379
b3137bc8
MG
1380 /*
1381 * If sync wakeup then subtract the (maximum possible)
1382 * effect of the currently running task from the load
1383 * of the current CPU:
1384 */
f3b577de 1385 rcu_read_lock();
83378269
PZ
1386 if (sync) {
1387 tg = task_group(current);
1388 weight = current->se.load.weight;
1389
c88d5910 1390 this_load += effective_load(tg, this_cpu, -weight, -weight);
83378269
PZ
1391 load += effective_load(tg, prev_cpu, 0, -weight);
1392 }
b3137bc8 1393
83378269
PZ
1394 tg = task_group(p);
1395 weight = p->se.load.weight;
b3137bc8 1396
71a29aa7
PZ
1397 /*
1398 * In low-load situations, where prev_cpu is idle and this_cpu is idle
c88d5910
PZ
1399 * due to the sync cause above having dropped this_load to 0, we'll
1400 * always have an imbalance, but there's really nothing you can do
1401 * about that, so that's good too.
71a29aa7
PZ
1402 *
1403 * Otherwise check if either cpus are near enough in load to allow this
1404 * task to be woken on this_cpu.
1405 */
e51fd5e2
PZ
1406 if (this_load) {
1407 unsigned long this_eff_load, prev_eff_load;
1408
1409 this_eff_load = 100;
1410 this_eff_load *= power_of(prev_cpu);
1411 this_eff_load *= this_load +
1412 effective_load(tg, this_cpu, weight, weight);
1413
1414 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
1415 prev_eff_load *= power_of(this_cpu);
1416 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
1417
1418 balanced = this_eff_load <= prev_eff_load;
1419 } else
1420 balanced = true;
f3b577de 1421 rcu_read_unlock();
b3137bc8 1422
098fb9db 1423 /*
4ae7d5ce
IM
1424 * If the currently running task will sleep within
1425 * a reasonable amount of time then attract this newly
1426 * woken task:
098fb9db 1427 */
2fb7635c
PZ
1428 if (sync && balanced)
1429 return 1;
098fb9db 1430
41acab88 1431 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
098fb9db
IM
1432 tl_per_task = cpu_avg_load_per_task(this_cpu);
1433
c88d5910
PZ
1434 if (balanced ||
1435 (this_load <= load &&
1436 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
098fb9db
IM
1437 /*
1438 * This domain has SD_WAKE_AFFINE and
1439 * p is cache cold in this domain, and
1440 * there is no bad imbalance.
1441 */
c88d5910 1442 schedstat_inc(sd, ttwu_move_affine);
41acab88 1443 schedstat_inc(p, se.statistics.nr_wakeups_affine);
098fb9db
IM
1444
1445 return 1;
1446 }
1447 return 0;
1448}
1449
aaee1203
PZ
1450/*
1451 * find_idlest_group finds and returns the least busy CPU group within the
1452 * domain.
1453 */
1454static struct sched_group *
78e7ed53 1455find_idlest_group(struct sched_domain *sd, struct task_struct *p,
5158f4e4 1456 int this_cpu, int load_idx)
e7693a36 1457{
b3bd3de6 1458 struct sched_group *idlest = NULL, *group = sd->groups;
aaee1203 1459 unsigned long min_load = ULONG_MAX, this_load = 0;
aaee1203 1460 int imbalance = 100 + (sd->imbalance_pct-100)/2;
e7693a36 1461
aaee1203
PZ
1462 do {
1463 unsigned long load, avg_load;
1464 int local_group;
1465 int i;
e7693a36 1466
aaee1203
PZ
1467 /* Skip over this group if it has no CPUs allowed */
1468 if (!cpumask_intersects(sched_group_cpus(group),
1469 &p->cpus_allowed))
1470 continue;
1471
1472 local_group = cpumask_test_cpu(this_cpu,
1473 sched_group_cpus(group));
1474
1475 /* Tally up the load of all CPUs in the group */
1476 avg_load = 0;
1477
1478 for_each_cpu(i, sched_group_cpus(group)) {
1479 /* Bias balancing toward cpus of our domain */
1480 if (local_group)
1481 load = source_load(i, load_idx);
1482 else
1483 load = target_load(i, load_idx);
1484
1485 avg_load += load;
1486 }
1487
1488 /* Adjust by relative CPU power of the group */
1489 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
1490
1491 if (local_group) {
1492 this_load = avg_load;
aaee1203
PZ
1493 } else if (avg_load < min_load) {
1494 min_load = avg_load;
1495 idlest = group;
1496 }
1497 } while (group = group->next, group != sd->groups);
1498
1499 if (!idlest || 100*this_load < imbalance*min_load)
1500 return NULL;
1501 return idlest;
1502}
1503
1504/*
1505 * find_idlest_cpu - find the idlest cpu among the cpus in group.
1506 */
1507static int
1508find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
1509{
1510 unsigned long load, min_load = ULONG_MAX;
1511 int idlest = -1;
1512 int i;
1513
1514 /* Traverse only the allowed CPUs */
1515 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
1516 load = weighted_cpuload(i);
1517
1518 if (load < min_load || (load == min_load && i == this_cpu)) {
1519 min_load = load;
1520 idlest = i;
e7693a36
GH
1521 }
1522 }
1523
aaee1203
PZ
1524 return idlest;
1525}
e7693a36 1526
a50bde51
PZ
1527/*
1528 * Try and locate an idle CPU in the sched_domain.
1529 */
99bd5e2f 1530static int select_idle_sibling(struct task_struct *p, int target)
a50bde51
PZ
1531{
1532 int cpu = smp_processor_id();
1533 int prev_cpu = task_cpu(p);
99bd5e2f 1534 struct sched_domain *sd;
a50bde51
PZ
1535 int i;
1536
1537 /*
99bd5e2f
SS
1538 * If the task is going to be woken-up on this cpu and if it is
1539 * already idle, then it is the right target.
a50bde51 1540 */
99bd5e2f
SS
1541 if (target == cpu && idle_cpu(cpu))
1542 return cpu;
1543
1544 /*
1545 * If the task is going to be woken-up on the cpu where it previously
1546 * ran and if it is currently idle, then it the right target.
1547 */
1548 if (target == prev_cpu && idle_cpu(prev_cpu))
fe3bcfe1 1549 return prev_cpu;
a50bde51
PZ
1550
1551 /*
99bd5e2f 1552 * Otherwise, iterate the domains and find an elegible idle cpu.
a50bde51 1553 */
99bd5e2f
SS
1554 for_each_domain(target, sd) {
1555 if (!(sd->flags & SD_SHARE_PKG_RESOURCES))
fe3bcfe1 1556 break;
99bd5e2f
SS
1557
1558 for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
1559 if (idle_cpu(i)) {
1560 target = i;
1561 break;
1562 }
a50bde51 1563 }
99bd5e2f
SS
1564
1565 /*
1566 * Lets stop looking for an idle sibling when we reached
1567 * the domain that spans the current cpu and prev_cpu.
1568 */
1569 if (cpumask_test_cpu(cpu, sched_domain_span(sd)) &&
1570 cpumask_test_cpu(prev_cpu, sched_domain_span(sd)))
1571 break;
a50bde51
PZ
1572 }
1573
1574 return target;
1575}
1576
aaee1203
PZ
1577/*
1578 * sched_balance_self: balance the current task (running on cpu) in domains
1579 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1580 * SD_BALANCE_EXEC.
1581 *
1582 * Balance, ie. select the least loaded group.
1583 *
1584 * Returns the target CPU number, or the same CPU if no balancing is needed.
1585 *
1586 * preempt must be disabled.
1587 */
0017d735
PZ
1588static int
1589select_task_rq_fair(struct rq *rq, struct task_struct *p, int sd_flag, int wake_flags)
aaee1203 1590{
29cd8bae 1591 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
c88d5910
PZ
1592 int cpu = smp_processor_id();
1593 int prev_cpu = task_cpu(p);
1594 int new_cpu = cpu;
99bd5e2f 1595 int want_affine = 0;
29cd8bae 1596 int want_sd = 1;
5158f4e4 1597 int sync = wake_flags & WF_SYNC;
c88d5910 1598
0763a660 1599 if (sd_flag & SD_BALANCE_WAKE) {
beac4c7e 1600 if (cpumask_test_cpu(cpu, &p->cpus_allowed))
c88d5910
PZ
1601 want_affine = 1;
1602 new_cpu = prev_cpu;
1603 }
aaee1203
PZ
1604
1605 for_each_domain(cpu, tmp) {
e4f42888
PZ
1606 if (!(tmp->flags & SD_LOAD_BALANCE))
1607 continue;
1608
aaee1203 1609 /*
ae154be1
PZ
1610 * If power savings logic is enabled for a domain, see if we
1611 * are not overloaded, if so, don't balance wider.
aaee1203 1612 */
59abf026 1613 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
ae154be1
PZ
1614 unsigned long power = 0;
1615 unsigned long nr_running = 0;
1616 unsigned long capacity;
1617 int i;
1618
1619 for_each_cpu(i, sched_domain_span(tmp)) {
1620 power += power_of(i);
1621 nr_running += cpu_rq(i)->cfs.nr_running;
1622 }
1623
1624 capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
1625
59abf026
PZ
1626 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1627 nr_running /= 2;
1628
1629 if (nr_running < capacity)
29cd8bae 1630 want_sd = 0;
ae154be1 1631 }
aaee1203 1632
fe3bcfe1 1633 /*
99bd5e2f
SS
1634 * If both cpu and prev_cpu are part of this domain,
1635 * cpu is a valid SD_WAKE_AFFINE target.
fe3bcfe1 1636 */
99bd5e2f
SS
1637 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
1638 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
1639 affine_sd = tmp;
1640 want_affine = 0;
c88d5910
PZ
1641 }
1642
29cd8bae
PZ
1643 if (!want_sd && !want_affine)
1644 break;
1645
0763a660 1646 if (!(tmp->flags & sd_flag))
c88d5910
PZ
1647 continue;
1648
29cd8bae
PZ
1649 if (want_sd)
1650 sd = tmp;
1651 }
1652
8b911acd 1653 if (affine_sd) {
99bd5e2f
SS
1654 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
1655 return select_idle_sibling(p, cpu);
1656 else
1657 return select_idle_sibling(p, prev_cpu);
8b911acd 1658 }
e7693a36 1659
aaee1203 1660 while (sd) {
5158f4e4 1661 int load_idx = sd->forkexec_idx;
aaee1203 1662 struct sched_group *group;
c88d5910 1663 int weight;
098fb9db 1664
0763a660 1665 if (!(sd->flags & sd_flag)) {
aaee1203
PZ
1666 sd = sd->child;
1667 continue;
1668 }
098fb9db 1669
5158f4e4
PZ
1670 if (sd_flag & SD_BALANCE_WAKE)
1671 load_idx = sd->wake_idx;
098fb9db 1672
5158f4e4 1673 group = find_idlest_group(sd, p, cpu, load_idx);
aaee1203
PZ
1674 if (!group) {
1675 sd = sd->child;
1676 continue;
1677 }
4ae7d5ce 1678
d7c33c49 1679 new_cpu = find_idlest_cpu(group, p, cpu);
aaee1203
PZ
1680 if (new_cpu == -1 || new_cpu == cpu) {
1681 /* Now try balancing at a lower domain level of cpu */
1682 sd = sd->child;
1683 continue;
e7693a36 1684 }
aaee1203
PZ
1685
1686 /* Now try balancing at a lower domain level of new_cpu */
1687 cpu = new_cpu;
669c55e9 1688 weight = sd->span_weight;
aaee1203
PZ
1689 sd = NULL;
1690 for_each_domain(cpu, tmp) {
669c55e9 1691 if (weight <= tmp->span_weight)
aaee1203 1692 break;
0763a660 1693 if (tmp->flags & sd_flag)
aaee1203
PZ
1694 sd = tmp;
1695 }
1696 /* while loop will break here if sd == NULL */
e7693a36
GH
1697 }
1698
c88d5910 1699 return new_cpu;
e7693a36
GH
1700}
1701#endif /* CONFIG_SMP */
1702
e52fb7c0
PZ
1703static unsigned long
1704wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
0bbd3336
PZ
1705{
1706 unsigned long gran = sysctl_sched_wakeup_granularity;
1707
1708 /*
e52fb7c0
PZ
1709 * Since its curr running now, convert the gran from real-time
1710 * to virtual-time in his units.
13814d42
MG
1711 *
1712 * By using 'se' instead of 'curr' we penalize light tasks, so
1713 * they get preempted easier. That is, if 'se' < 'curr' then
1714 * the resulting gran will be larger, therefore penalizing the
1715 * lighter, if otoh 'se' > 'curr' then the resulting gran will
1716 * be smaller, again penalizing the lighter task.
1717 *
1718 * This is especially important for buddies when the leftmost
1719 * task is higher priority than the buddy.
0bbd3336 1720 */
13814d42
MG
1721 if (unlikely(se->load.weight != NICE_0_LOAD))
1722 gran = calc_delta_fair(gran, se);
0bbd3336
PZ
1723
1724 return gran;
1725}
1726
464b7527
PZ
1727/*
1728 * Should 'se' preempt 'curr'.
1729 *
1730 * |s1
1731 * |s2
1732 * |s3
1733 * g
1734 * |<--->|c
1735 *
1736 * w(c, s1) = -1
1737 * w(c, s2) = 0
1738 * w(c, s3) = 1
1739 *
1740 */
1741static int
1742wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
1743{
1744 s64 gran, vdiff = curr->vruntime - se->vruntime;
1745
1746 if (vdiff <= 0)
1747 return -1;
1748
e52fb7c0 1749 gran = wakeup_gran(curr, se);
464b7527
PZ
1750 if (vdiff > gran)
1751 return 1;
1752
1753 return 0;
1754}
1755
02479099
PZ
1756static void set_last_buddy(struct sched_entity *se)
1757{
6bc912b7
PZ
1758 if (likely(task_of(se)->policy != SCHED_IDLE)) {
1759 for_each_sched_entity(se)
1760 cfs_rq_of(se)->last = se;
1761 }
02479099
PZ
1762}
1763
1764static void set_next_buddy(struct sched_entity *se)
1765{
6bc912b7
PZ
1766 if (likely(task_of(se)->policy != SCHED_IDLE)) {
1767 for_each_sched_entity(se)
1768 cfs_rq_of(se)->next = se;
1769 }
02479099
PZ
1770}
1771
bf0f6f24
IM
1772/*
1773 * Preempt the current task with a newly woken task if needed:
1774 */
5a9b86f6 1775static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
bf0f6f24
IM
1776{
1777 struct task_struct *curr = rq->curr;
8651a86c 1778 struct sched_entity *se = &curr->se, *pse = &p->se;
03e89e45 1779 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
f685ceac 1780 int scale = cfs_rq->nr_running >= sched_nr_latency;
bf0f6f24 1781
3a7e73a2
PZ
1782 if (unlikely(rt_prio(p->prio)))
1783 goto preempt;
aa2ac252 1784
d95f98d0
PZ
1785 if (unlikely(p->sched_class != &fair_sched_class))
1786 return;
1787
4ae7d5ce
IM
1788 if (unlikely(se == pse))
1789 return;
1790
f685ceac 1791 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
3cb63d52 1792 set_next_buddy(pse);
57fdc26d 1793
aec0a514
BR
1794 /*
1795 * We can come here with TIF_NEED_RESCHED already set from new task
1796 * wake up path.
1797 */
1798 if (test_tsk_need_resched(curr))
1799 return;
1800
91c234b4 1801 /*
6bc912b7 1802 * Batch and idle tasks do not preempt (their preemption is driven by
91c234b4
IM
1803 * the tick):
1804 */
6bc912b7 1805 if (unlikely(p->policy != SCHED_NORMAL))
91c234b4 1806 return;
bf0f6f24 1807
6bc912b7 1808 /* Idle tasks are by definition preempted by everybody. */
3a7e73a2
PZ
1809 if (unlikely(curr->policy == SCHED_IDLE))
1810 goto preempt;
bf0f6f24 1811
ad4b78bb
PZ
1812 if (!sched_feat(WAKEUP_PREEMPT))
1813 return;
1814
3a7e73a2 1815 update_curr(cfs_rq);
464b7527 1816 find_matching_se(&se, &pse);
002f128b 1817 BUG_ON(!pse);
3a7e73a2
PZ
1818 if (wakeup_preempt_entity(se, pse) == 1)
1819 goto preempt;
464b7527 1820
3a7e73a2 1821 return;
a65ac745 1822
3a7e73a2
PZ
1823preempt:
1824 resched_task(curr);
1825 /*
1826 * Only set the backward buddy when the current task is still
1827 * on the rq. This can happen when a wakeup gets interleaved
1828 * with schedule on the ->pre_schedule() or idle_balance()
1829 * point, either of which can * drop the rq lock.
1830 *
1831 * Also, during early boot the idle thread is in the fair class,
1832 * for obvious reasons its a bad idea to schedule back to it.
1833 */
1834 if (unlikely(!se->on_rq || curr == rq->idle))
1835 return;
1836
1837 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
1838 set_last_buddy(se);
bf0f6f24
IM
1839}
1840
fb8d4724 1841static struct task_struct *pick_next_task_fair(struct rq *rq)
bf0f6f24 1842{
8f4d37ec 1843 struct task_struct *p;
bf0f6f24
IM
1844 struct cfs_rq *cfs_rq = &rq->cfs;
1845 struct sched_entity *se;
1846
36ace27e 1847 if (!cfs_rq->nr_running)
bf0f6f24
IM
1848 return NULL;
1849
1850 do {
9948f4b2 1851 se = pick_next_entity(cfs_rq);
f4b6755f 1852 set_next_entity(cfs_rq, se);
bf0f6f24
IM
1853 cfs_rq = group_cfs_rq(se);
1854 } while (cfs_rq);
1855
8f4d37ec
PZ
1856 p = task_of(se);
1857 hrtick_start_fair(rq, p);
1858
1859 return p;
bf0f6f24
IM
1860}
1861
1862/*
1863 * Account for a descheduled task:
1864 */
31ee529c 1865static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
bf0f6f24
IM
1866{
1867 struct sched_entity *se = &prev->se;
1868 struct cfs_rq *cfs_rq;
1869
1870 for_each_sched_entity(se) {
1871 cfs_rq = cfs_rq_of(se);
ab6cde26 1872 put_prev_entity(cfs_rq, se);
bf0f6f24
IM
1873 }
1874}
1875
681f3e68 1876#ifdef CONFIG_SMP
bf0f6f24
IM
1877/**************************************************
1878 * Fair scheduling class load-balancing methods:
1879 */
1880
1e3c88bd
PZ
1881/*
1882 * pull_task - move a task from a remote runqueue to the local runqueue.
1883 * Both runqueues must be locked.
1884 */
1885static void pull_task(struct rq *src_rq, struct task_struct *p,
1886 struct rq *this_rq, int this_cpu)
1887{
1888 deactivate_task(src_rq, p, 0);
1889 set_task_cpu(p, this_cpu);
1890 activate_task(this_rq, p, 0);
1891 check_preempt_curr(this_rq, p, 0);
fab47622
NR
1892
1893 /* re-arm NEWIDLE balancing when moving tasks */
1894 src_rq->avg_idle = this_rq->avg_idle = 2*sysctl_sched_migration_cost;
1895 this_rq->idle_stamp = 0;
1e3c88bd
PZ
1896}
1897
1898/*
1899 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
1900 */
1901static
1902int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
1903 struct sched_domain *sd, enum cpu_idle_type idle,
1904 int *all_pinned)
1905{
1906 int tsk_cache_hot = 0;
1907 /*
1908 * We do not migrate tasks that are:
1909 * 1) running (obviously), or
1910 * 2) cannot be migrated to this CPU due to cpus_allowed, or
1911 * 3) are cache-hot on their current CPU.
1912 */
1913 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
41acab88 1914 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
1e3c88bd
PZ
1915 return 0;
1916 }
1917 *all_pinned = 0;
1918
1919 if (task_running(rq, p)) {
41acab88 1920 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
1e3c88bd
PZ
1921 return 0;
1922 }
1923
1924 /*
1925 * Aggressive migration if:
1926 * 1) task is cache cold, or
1927 * 2) too many balance attempts have failed.
1928 */
1929
305e6835 1930 tsk_cache_hot = task_hot(p, rq->clock_task, sd);
1e3c88bd
PZ
1931 if (!tsk_cache_hot ||
1932 sd->nr_balance_failed > sd->cache_nice_tries) {
1933#ifdef CONFIG_SCHEDSTATS
1934 if (tsk_cache_hot) {
1935 schedstat_inc(sd, lb_hot_gained[idle]);
41acab88 1936 schedstat_inc(p, se.statistics.nr_forced_migrations);
1e3c88bd
PZ
1937 }
1938#endif
1939 return 1;
1940 }
1941
1942 if (tsk_cache_hot) {
41acab88 1943 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
1e3c88bd
PZ
1944 return 0;
1945 }
1946 return 1;
1947}
1948
897c395f
PZ
1949/*
1950 * move_one_task tries to move exactly one task from busiest to this_rq, as
1951 * part of active balancing operations within "domain".
1952 * Returns 1 if successful and 0 otherwise.
1953 *
1954 * Called with both runqueues locked.
1955 */
1956static int
1957move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1958 struct sched_domain *sd, enum cpu_idle_type idle)
1959{
1960 struct task_struct *p, *n;
1961 struct cfs_rq *cfs_rq;
1962 int pinned = 0;
1963
1964 for_each_leaf_cfs_rq(busiest, cfs_rq) {
1965 list_for_each_entry_safe(p, n, &cfs_rq->tasks, se.group_node) {
1966
1967 if (!can_migrate_task(p, busiest, this_cpu,
1968 sd, idle, &pinned))
1969 continue;
1970
1971 pull_task(busiest, p, this_rq, this_cpu);
1972 /*
1973 * Right now, this is only the second place pull_task()
1974 * is called, so we can safely collect pull_task()
1975 * stats here rather than inside pull_task().
1976 */
1977 schedstat_inc(sd, lb_gained[idle]);
1978 return 1;
1979 }
1980 }
1981
1982 return 0;
1983}
1984
1e3c88bd
PZ
1985static unsigned long
1986balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1987 unsigned long max_load_move, struct sched_domain *sd,
1988 enum cpu_idle_type idle, int *all_pinned,
ee00e66f 1989 int *this_best_prio, struct cfs_rq *busiest_cfs_rq)
1e3c88bd
PZ
1990{
1991 int loops = 0, pulled = 0, pinned = 0;
1e3c88bd 1992 long rem_load_move = max_load_move;
ee00e66f 1993 struct task_struct *p, *n;
1e3c88bd
PZ
1994
1995 if (max_load_move == 0)
1996 goto out;
1997
1998 pinned = 1;
1999
ee00e66f
PZ
2000 list_for_each_entry_safe(p, n, &busiest_cfs_rq->tasks, se.group_node) {
2001 if (loops++ > sysctl_sched_nr_migrate)
2002 break;
1e3c88bd 2003
ee00e66f
PZ
2004 if ((p->se.load.weight >> 1) > rem_load_move ||
2005 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned))
2006 continue;
1e3c88bd 2007
ee00e66f
PZ
2008 pull_task(busiest, p, this_rq, this_cpu);
2009 pulled++;
2010 rem_load_move -= p->se.load.weight;
1e3c88bd
PZ
2011
2012#ifdef CONFIG_PREEMPT
ee00e66f
PZ
2013 /*
2014 * NEWIDLE balancing is a source of latency, so preemptible
2015 * kernels will stop after the first task is pulled to minimize
2016 * the critical section.
2017 */
2018 if (idle == CPU_NEWLY_IDLE)
2019 break;
1e3c88bd
PZ
2020#endif
2021
ee00e66f
PZ
2022 /*
2023 * We only want to steal up to the prescribed amount of
2024 * weighted load.
2025 */
2026 if (rem_load_move <= 0)
2027 break;
2028
1e3c88bd
PZ
2029 if (p->prio < *this_best_prio)
2030 *this_best_prio = p->prio;
1e3c88bd
PZ
2031 }
2032out:
2033 /*
2034 * Right now, this is one of only two places pull_task() is called,
2035 * so we can safely collect pull_task() stats here rather than
2036 * inside pull_task().
2037 */
2038 schedstat_add(sd, lb_gained[idle], pulled);
2039
2040 if (all_pinned)
2041 *all_pinned = pinned;
2042
2043 return max_load_move - rem_load_move;
2044}
2045
230059de 2046#ifdef CONFIG_FAIR_GROUP_SCHED
9e3081ca
PZ
2047/*
2048 * update tg->load_weight by folding this cpu's load_avg
2049 */
67e86250 2050static int update_shares_cpu(struct task_group *tg, int cpu)
9e3081ca
PZ
2051{
2052 struct cfs_rq *cfs_rq;
2053 unsigned long flags;
2054 struct rq *rq;
2055 long load_avg;
2056
2057 if (!tg->se[cpu])
2058 return 0;
2059
2060 rq = cpu_rq(cpu);
2061 cfs_rq = tg->cfs_rq[cpu];
2062
2063 raw_spin_lock_irqsave(&rq->lock, flags);
2064
2065 update_rq_clock(rq);
e33078ba 2066 update_cfs_load(cfs_rq);
9e3081ca
PZ
2067
2068 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
2069 load_avg -= cfs_rq->load_contribution;
2070 atomic_add(load_avg, &tg->load_weight);
2071 cfs_rq->load_contribution += load_avg;
2072
2073 /*
2074 * We need to update shares after updating tg->load_weight in
2075 * order to adjust the weight of groups with long running tasks.
2076 */
f0d7442a 2077 update_cfs_shares(cfs_rq, 0);
9e3081ca
PZ
2078
2079 raw_spin_unlock_irqrestore(&rq->lock, flags);
2080
2081 return 0;
2082}
2083
2084static void update_shares(int cpu)
2085{
2086 struct cfs_rq *cfs_rq;
2087 struct rq *rq = cpu_rq(cpu);
2088
2089 rcu_read_lock();
67e86250
PT
2090 for_each_leaf_cfs_rq(rq, cfs_rq)
2091 update_shares_cpu(cfs_rq->tg, cpu);
9e3081ca
PZ
2092 rcu_read_unlock();
2093}
2094
230059de
PZ
2095static unsigned long
2096load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
2097 unsigned long max_load_move,
2098 struct sched_domain *sd, enum cpu_idle_type idle,
2099 int *all_pinned, int *this_best_prio)
2100{
2101 long rem_load_move = max_load_move;
2102 int busiest_cpu = cpu_of(busiest);
2103 struct task_group *tg;
2104
2105 rcu_read_lock();
2106 update_h_load(busiest_cpu);
2107
2108 list_for_each_entry_rcu(tg, &task_groups, list) {
2109 struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
2110 unsigned long busiest_h_load = busiest_cfs_rq->h_load;
2111 unsigned long busiest_weight = busiest_cfs_rq->load.weight;
2112 u64 rem_load, moved_load;
2113
2114 /*
2115 * empty group
2116 */
2117 if (!busiest_cfs_rq->task_weight)
2118 continue;
2119
2120 rem_load = (u64)rem_load_move * busiest_weight;
2121 rem_load = div_u64(rem_load, busiest_h_load + 1);
2122
2123 moved_load = balance_tasks(this_rq, this_cpu, busiest,
2124 rem_load, sd, idle, all_pinned, this_best_prio,
2125 busiest_cfs_rq);
2126
2127 if (!moved_load)
2128 continue;
2129
2130 moved_load *= busiest_h_load;
2131 moved_load = div_u64(moved_load, busiest_weight + 1);
2132
2133 rem_load_move -= moved_load;
2134 if (rem_load_move < 0)
2135 break;
2136 }
2137 rcu_read_unlock();
2138
2139 return max_load_move - rem_load_move;
2140}
2141#else
9e3081ca
PZ
2142static inline void update_shares(int cpu)
2143{
2144}
2145
230059de
PZ
2146static unsigned long
2147load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
2148 unsigned long max_load_move,
2149 struct sched_domain *sd, enum cpu_idle_type idle,
2150 int *all_pinned, int *this_best_prio)
2151{
2152 return balance_tasks(this_rq, this_cpu, busiest,
2153 max_load_move, sd, idle, all_pinned,
2154 this_best_prio, &busiest->cfs);
2155}
2156#endif
2157
1e3c88bd
PZ
2158/*
2159 * move_tasks tries to move up to max_load_move weighted load from busiest to
2160 * this_rq, as part of a balancing operation within domain "sd".
2161 * Returns 1 if successful and 0 otherwise.
2162 *
2163 * Called with both runqueues locked.
2164 */
2165static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2166 unsigned long max_load_move,
2167 struct sched_domain *sd, enum cpu_idle_type idle,
2168 int *all_pinned)
2169{
3d45fd80 2170 unsigned long total_load_moved = 0, load_moved;
1e3c88bd
PZ
2171 int this_best_prio = this_rq->curr->prio;
2172
2173 do {
3d45fd80 2174 load_moved = load_balance_fair(this_rq, this_cpu, busiest,
1e3c88bd
PZ
2175 max_load_move - total_load_moved,
2176 sd, idle, all_pinned, &this_best_prio);
3d45fd80
PZ
2177
2178 total_load_moved += load_moved;
1e3c88bd
PZ
2179
2180#ifdef CONFIG_PREEMPT
2181 /*
2182 * NEWIDLE balancing is a source of latency, so preemptible
2183 * kernels will stop after the first task is pulled to minimize
2184 * the critical section.
2185 */
2186 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
2187 break;
baa8c110
PZ
2188
2189 if (raw_spin_is_contended(&this_rq->lock) ||
2190 raw_spin_is_contended(&busiest->lock))
2191 break;
1e3c88bd 2192#endif
3d45fd80 2193 } while (load_moved && max_load_move > total_load_moved);
1e3c88bd
PZ
2194
2195 return total_load_moved > 0;
2196}
2197
1e3c88bd
PZ
2198/********** Helpers for find_busiest_group ************************/
2199/*
2200 * sd_lb_stats - Structure to store the statistics of a sched_domain
2201 * during load balancing.
2202 */
2203struct sd_lb_stats {
2204 struct sched_group *busiest; /* Busiest group in this sd */
2205 struct sched_group *this; /* Local group in this sd */
2206 unsigned long total_load; /* Total load of all groups in sd */
2207 unsigned long total_pwr; /* Total power of all groups in sd */
2208 unsigned long avg_load; /* Average load across all groups in sd */
2209
2210 /** Statistics of this group */
2211 unsigned long this_load;
2212 unsigned long this_load_per_task;
2213 unsigned long this_nr_running;
fab47622 2214 unsigned long this_has_capacity;
1e3c88bd
PZ
2215
2216 /* Statistics of the busiest group */
2217 unsigned long max_load;
2218 unsigned long busiest_load_per_task;
2219 unsigned long busiest_nr_running;
dd5feea1 2220 unsigned long busiest_group_capacity;
fab47622 2221 unsigned long busiest_has_capacity;
1e3c88bd
PZ
2222
2223 int group_imb; /* Is there imbalance in this sd */
2224#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2225 int power_savings_balance; /* Is powersave balance needed for this sd */
2226 struct sched_group *group_min; /* Least loaded group in sd */
2227 struct sched_group *group_leader; /* Group which relieves group_min */
2228 unsigned long min_load_per_task; /* load_per_task in group_min */
2229 unsigned long leader_nr_running; /* Nr running of group_leader */
2230 unsigned long min_nr_running; /* Nr running of group_min */
2231#endif
2232};
2233
2234/*
2235 * sg_lb_stats - stats of a sched_group required for load_balancing
2236 */
2237struct sg_lb_stats {
2238 unsigned long avg_load; /*Avg load across the CPUs of the group */
2239 unsigned long group_load; /* Total load over the CPUs of the group */
2240 unsigned long sum_nr_running; /* Nr tasks running in the group */
2241 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
2242 unsigned long group_capacity;
2243 int group_imb; /* Is there an imbalance in the group ? */
fab47622 2244 int group_has_capacity; /* Is there extra capacity in the group? */
1e3c88bd
PZ
2245};
2246
2247/**
2248 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
2249 * @group: The group whose first cpu is to be returned.
2250 */
2251static inline unsigned int group_first_cpu(struct sched_group *group)
2252{
2253 return cpumask_first(sched_group_cpus(group));
2254}
2255
2256/**
2257 * get_sd_load_idx - Obtain the load index for a given sched domain.
2258 * @sd: The sched_domain whose load_idx is to be obtained.
2259 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
2260 */
2261static inline int get_sd_load_idx(struct sched_domain *sd,
2262 enum cpu_idle_type idle)
2263{
2264 int load_idx;
2265
2266 switch (idle) {
2267 case CPU_NOT_IDLE:
2268 load_idx = sd->busy_idx;
2269 break;
2270
2271 case CPU_NEWLY_IDLE:
2272 load_idx = sd->newidle_idx;
2273 break;
2274 default:
2275 load_idx = sd->idle_idx;
2276 break;
2277 }
2278
2279 return load_idx;
2280}
2281
2282
2283#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2284/**
2285 * init_sd_power_savings_stats - Initialize power savings statistics for
2286 * the given sched_domain, during load balancing.
2287 *
2288 * @sd: Sched domain whose power-savings statistics are to be initialized.
2289 * @sds: Variable containing the statistics for sd.
2290 * @idle: Idle status of the CPU at which we're performing load-balancing.
2291 */
2292static inline void init_sd_power_savings_stats(struct sched_domain *sd,
2293 struct sd_lb_stats *sds, enum cpu_idle_type idle)
2294{
2295 /*
2296 * Busy processors will not participate in power savings
2297 * balance.
2298 */
2299 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
2300 sds->power_savings_balance = 0;
2301 else {
2302 sds->power_savings_balance = 1;
2303 sds->min_nr_running = ULONG_MAX;
2304 sds->leader_nr_running = 0;
2305 }
2306}
2307
2308/**
2309 * update_sd_power_savings_stats - Update the power saving stats for a
2310 * sched_domain while performing load balancing.
2311 *
2312 * @group: sched_group belonging to the sched_domain under consideration.
2313 * @sds: Variable containing the statistics of the sched_domain
2314 * @local_group: Does group contain the CPU for which we're performing
2315 * load balancing ?
2316 * @sgs: Variable containing the statistics of the group.
2317 */
2318static inline void update_sd_power_savings_stats(struct sched_group *group,
2319 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
2320{
2321
2322 if (!sds->power_savings_balance)
2323 return;
2324
2325 /*
2326 * If the local group is idle or completely loaded
2327 * no need to do power savings balance at this domain
2328 */
2329 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
2330 !sds->this_nr_running))
2331 sds->power_savings_balance = 0;
2332
2333 /*
2334 * If a group is already running at full capacity or idle,
2335 * don't include that group in power savings calculations
2336 */
2337 if (!sds->power_savings_balance ||
2338 sgs->sum_nr_running >= sgs->group_capacity ||
2339 !sgs->sum_nr_running)
2340 return;
2341
2342 /*
2343 * Calculate the group which has the least non-idle load.
2344 * This is the group from where we need to pick up the load
2345 * for saving power
2346 */
2347 if ((sgs->sum_nr_running < sds->min_nr_running) ||
2348 (sgs->sum_nr_running == sds->min_nr_running &&
2349 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
2350 sds->group_min = group;
2351 sds->min_nr_running = sgs->sum_nr_running;
2352 sds->min_load_per_task = sgs->sum_weighted_load /
2353 sgs->sum_nr_running;
2354 }
2355
2356 /*
2357 * Calculate the group which is almost near its
2358 * capacity but still has some space to pick up some load
2359 * from other group and save more power
2360 */
2361 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
2362 return;
2363
2364 if (sgs->sum_nr_running > sds->leader_nr_running ||
2365 (sgs->sum_nr_running == sds->leader_nr_running &&
2366 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
2367 sds->group_leader = group;
2368 sds->leader_nr_running = sgs->sum_nr_running;
2369 }
2370}
2371
2372/**
2373 * check_power_save_busiest_group - see if there is potential for some power-savings balance
2374 * @sds: Variable containing the statistics of the sched_domain
2375 * under consideration.
2376 * @this_cpu: Cpu at which we're currently performing load-balancing.
2377 * @imbalance: Variable to store the imbalance.
2378 *
2379 * Description:
2380 * Check if we have potential to perform some power-savings balance.
2381 * If yes, set the busiest group to be the least loaded group in the
2382 * sched_domain, so that it's CPUs can be put to idle.
2383 *
2384 * Returns 1 if there is potential to perform power-savings balance.
2385 * Else returns 0.
2386 */
2387static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
2388 int this_cpu, unsigned long *imbalance)
2389{
2390 if (!sds->power_savings_balance)
2391 return 0;
2392
2393 if (sds->this != sds->group_leader ||
2394 sds->group_leader == sds->group_min)
2395 return 0;
2396
2397 *imbalance = sds->min_load_per_task;
2398 sds->busiest = sds->group_min;
2399
2400 return 1;
2401
2402}
2403#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
2404static inline void init_sd_power_savings_stats(struct sched_domain *sd,
2405 struct sd_lb_stats *sds, enum cpu_idle_type idle)
2406{
2407 return;
2408}
2409
2410static inline void update_sd_power_savings_stats(struct sched_group *group,
2411 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
2412{
2413 return;
2414}
2415
2416static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
2417 int this_cpu, unsigned long *imbalance)
2418{
2419 return 0;
2420}
2421#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
2422
2423
2424unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
2425{
2426 return SCHED_LOAD_SCALE;
2427}
2428
2429unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
2430{
2431 return default_scale_freq_power(sd, cpu);
2432}
2433
2434unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
2435{
669c55e9 2436 unsigned long weight = sd->span_weight;
1e3c88bd
PZ
2437 unsigned long smt_gain = sd->smt_gain;
2438
2439 smt_gain /= weight;
2440
2441 return smt_gain;
2442}
2443
2444unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
2445{
2446 return default_scale_smt_power(sd, cpu);
2447}
2448
2449unsigned long scale_rt_power(int cpu)
2450{
2451 struct rq *rq = cpu_rq(cpu);
2452 u64 total, available;
2453
1e3c88bd 2454 total = sched_avg_period() + (rq->clock - rq->age_stamp);
aa483808
VP
2455
2456 if (unlikely(total < rq->rt_avg)) {
2457 /* Ensures that power won't end up being negative */
2458 available = 0;
2459 } else {
2460 available = total - rq->rt_avg;
2461 }
1e3c88bd
PZ
2462
2463 if (unlikely((s64)total < SCHED_LOAD_SCALE))
2464 total = SCHED_LOAD_SCALE;
2465
2466 total >>= SCHED_LOAD_SHIFT;
2467
2468 return div_u64(available, total);
2469}
2470
2471static void update_cpu_power(struct sched_domain *sd, int cpu)
2472{
669c55e9 2473 unsigned long weight = sd->span_weight;
1e3c88bd
PZ
2474 unsigned long power = SCHED_LOAD_SCALE;
2475 struct sched_group *sdg = sd->groups;
2476
1e3c88bd
PZ
2477 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
2478 if (sched_feat(ARCH_POWER))
2479 power *= arch_scale_smt_power(sd, cpu);
2480 else
2481 power *= default_scale_smt_power(sd, cpu);
2482
2483 power >>= SCHED_LOAD_SHIFT;
2484 }
2485
9d5efe05
SV
2486 sdg->cpu_power_orig = power;
2487
2488 if (sched_feat(ARCH_POWER))
2489 power *= arch_scale_freq_power(sd, cpu);
2490 else
2491 power *= default_scale_freq_power(sd, cpu);
2492
2493 power >>= SCHED_LOAD_SHIFT;
2494
1e3c88bd
PZ
2495 power *= scale_rt_power(cpu);
2496 power >>= SCHED_LOAD_SHIFT;
2497
2498 if (!power)
2499 power = 1;
2500
e51fd5e2 2501 cpu_rq(cpu)->cpu_power = power;
1e3c88bd
PZ
2502 sdg->cpu_power = power;
2503}
2504
2505static void update_group_power(struct sched_domain *sd, int cpu)
2506{
2507 struct sched_domain *child = sd->child;
2508 struct sched_group *group, *sdg = sd->groups;
2509 unsigned long power;
2510
2511 if (!child) {
2512 update_cpu_power(sd, cpu);
2513 return;
2514 }
2515
2516 power = 0;
2517
2518 group = child->groups;
2519 do {
2520 power += group->cpu_power;
2521 group = group->next;
2522 } while (group != child->groups);
2523
2524 sdg->cpu_power = power;
2525}
2526
9d5efe05
SV
2527/*
2528 * Try and fix up capacity for tiny siblings, this is needed when
2529 * things like SD_ASYM_PACKING need f_b_g to select another sibling
2530 * which on its own isn't powerful enough.
2531 *
2532 * See update_sd_pick_busiest() and check_asym_packing().
2533 */
2534static inline int
2535fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
2536{
2537 /*
2538 * Only siblings can have significantly less than SCHED_LOAD_SCALE
2539 */
2540 if (sd->level != SD_LV_SIBLING)
2541 return 0;
2542
2543 /*
2544 * If ~90% of the cpu_power is still there, we're good.
2545 */
694f5a11 2546 if (group->cpu_power * 32 > group->cpu_power_orig * 29)
9d5efe05
SV
2547 return 1;
2548
2549 return 0;
2550}
2551
1e3c88bd
PZ
2552/**
2553 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
2554 * @sd: The sched_domain whose statistics are to be updated.
2555 * @group: sched_group whose statistics are to be updated.
2556 * @this_cpu: Cpu for which load balance is currently performed.
2557 * @idle: Idle status of this_cpu
2558 * @load_idx: Load index of sched_domain of this_cpu for load calc.
2559 * @sd_idle: Idle status of the sched_domain containing group.
2560 * @local_group: Does group contain this_cpu.
2561 * @cpus: Set of cpus considered for load balancing.
2562 * @balance: Should we balance.
2563 * @sgs: variable to hold the statistics for this group.
2564 */
2565static inline void update_sg_lb_stats(struct sched_domain *sd,
2566 struct sched_group *group, int this_cpu,
2567 enum cpu_idle_type idle, int load_idx, int *sd_idle,
2568 int local_group, const struct cpumask *cpus,
2569 int *balance, struct sg_lb_stats *sgs)
2570{
2582f0eb 2571 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
1e3c88bd
PZ
2572 int i;
2573 unsigned int balance_cpu = -1, first_idle_cpu = 0;
dd5feea1 2574 unsigned long avg_load_per_task = 0;
1e3c88bd 2575
871e35bc 2576 if (local_group)
1e3c88bd 2577 balance_cpu = group_first_cpu(group);
1e3c88bd
PZ
2578
2579 /* Tally up the load of all CPUs in the group */
1e3c88bd
PZ
2580 max_cpu_load = 0;
2581 min_cpu_load = ~0UL;
2582f0eb 2582 max_nr_running = 0;
1e3c88bd
PZ
2583
2584 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
2585 struct rq *rq = cpu_rq(i);
2586
2587 if (*sd_idle && rq->nr_running)
2588 *sd_idle = 0;
2589
2590 /* Bias balancing toward cpus of our domain */
2591 if (local_group) {
2592 if (idle_cpu(i) && !first_idle_cpu) {
2593 first_idle_cpu = 1;
2594 balance_cpu = i;
2595 }
2596
2597 load = target_load(i, load_idx);
2598 } else {
2599 load = source_load(i, load_idx);
2582f0eb 2600 if (load > max_cpu_load) {
1e3c88bd 2601 max_cpu_load = load;
2582f0eb
NR
2602 max_nr_running = rq->nr_running;
2603 }
1e3c88bd
PZ
2604 if (min_cpu_load > load)
2605 min_cpu_load = load;
2606 }
2607
2608 sgs->group_load += load;
2609 sgs->sum_nr_running += rq->nr_running;
2610 sgs->sum_weighted_load += weighted_cpuload(i);
2611
1e3c88bd
PZ
2612 }
2613
2614 /*
2615 * First idle cpu or the first cpu(busiest) in this sched group
2616 * is eligible for doing load balancing at this and above
2617 * domains. In the newly idle case, we will allow all the cpu's
2618 * to do the newly idle load balance.
2619 */
bbc8cb5b
PZ
2620 if (idle != CPU_NEWLY_IDLE && local_group) {
2621 if (balance_cpu != this_cpu) {
2622 *balance = 0;
2623 return;
2624 }
2625 update_group_power(sd, this_cpu);
1e3c88bd
PZ
2626 }
2627
2628 /* Adjust by relative CPU power of the group */
2629 sgs->avg_load = (sgs->group_load * SCHED_LOAD_SCALE) / group->cpu_power;
2630
1e3c88bd
PZ
2631 /*
2632 * Consider the group unbalanced when the imbalance is larger
2633 * than the average weight of two tasks.
2634 *
2635 * APZ: with cgroup the avg task weight can vary wildly and
2636 * might not be a suitable number - should we keep a
2637 * normalized nr_running number somewhere that negates
2638 * the hierarchy?
2639 */
dd5feea1
SS
2640 if (sgs->sum_nr_running)
2641 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
1e3c88bd 2642
2582f0eb 2643 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task && max_nr_running > 1)
1e3c88bd
PZ
2644 sgs->group_imb = 1;
2645
2582f0eb 2646 sgs->group_capacity = DIV_ROUND_CLOSEST(group->cpu_power, SCHED_LOAD_SCALE);
9d5efe05
SV
2647 if (!sgs->group_capacity)
2648 sgs->group_capacity = fix_small_capacity(sd, group);
fab47622
NR
2649
2650 if (sgs->group_capacity > sgs->sum_nr_running)
2651 sgs->group_has_capacity = 1;
1e3c88bd
PZ
2652}
2653
532cb4c4
MN
2654/**
2655 * update_sd_pick_busiest - return 1 on busiest group
2656 * @sd: sched_domain whose statistics are to be checked
2657 * @sds: sched_domain statistics
2658 * @sg: sched_group candidate to be checked for being the busiest
b6b12294
MN
2659 * @sgs: sched_group statistics
2660 * @this_cpu: the current cpu
532cb4c4
MN
2661 *
2662 * Determine if @sg is a busier group than the previously selected
2663 * busiest group.
2664 */
2665static bool update_sd_pick_busiest(struct sched_domain *sd,
2666 struct sd_lb_stats *sds,
2667 struct sched_group *sg,
2668 struct sg_lb_stats *sgs,
2669 int this_cpu)
2670{
2671 if (sgs->avg_load <= sds->max_load)
2672 return false;
2673
2674 if (sgs->sum_nr_running > sgs->group_capacity)
2675 return true;
2676
2677 if (sgs->group_imb)
2678 return true;
2679
2680 /*
2681 * ASYM_PACKING needs to move all the work to the lowest
2682 * numbered CPUs in the group, therefore mark all groups
2683 * higher than ourself as busy.
2684 */
2685 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
2686 this_cpu < group_first_cpu(sg)) {
2687 if (!sds->busiest)
2688 return true;
2689
2690 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
2691 return true;
2692 }
2693
2694 return false;
2695}
2696
1e3c88bd
PZ
2697/**
2698 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
2699 * @sd: sched_domain whose statistics are to be updated.
2700 * @this_cpu: Cpu for which load balance is currently performed.
2701 * @idle: Idle status of this_cpu
532cb4c4 2702 * @sd_idle: Idle status of the sched_domain containing sg.
1e3c88bd
PZ
2703 * @cpus: Set of cpus considered for load balancing.
2704 * @balance: Should we balance.
2705 * @sds: variable to hold the statistics for this sched_domain.
2706 */
2707static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
2708 enum cpu_idle_type idle, int *sd_idle,
2709 const struct cpumask *cpus, int *balance,
2710 struct sd_lb_stats *sds)
2711{
2712 struct sched_domain *child = sd->child;
532cb4c4 2713 struct sched_group *sg = sd->groups;
1e3c88bd
PZ
2714 struct sg_lb_stats sgs;
2715 int load_idx, prefer_sibling = 0;
2716
2717 if (child && child->flags & SD_PREFER_SIBLING)
2718 prefer_sibling = 1;
2719
2720 init_sd_power_savings_stats(sd, sds, idle);
2721 load_idx = get_sd_load_idx(sd, idle);
2722
2723 do {
2724 int local_group;
2725
532cb4c4 2726 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
1e3c88bd 2727 memset(&sgs, 0, sizeof(sgs));
532cb4c4 2728 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx, sd_idle,
1e3c88bd
PZ
2729 local_group, cpus, balance, &sgs);
2730
8f190fb3 2731 if (local_group && !(*balance))
1e3c88bd
PZ
2732 return;
2733
2734 sds->total_load += sgs.group_load;
532cb4c4 2735 sds->total_pwr += sg->cpu_power;
1e3c88bd
PZ
2736
2737 /*
2738 * In case the child domain prefers tasks go to siblings
532cb4c4 2739 * first, lower the sg capacity to one so that we'll try
75dd321d
NR
2740 * and move all the excess tasks away. We lower the capacity
2741 * of a group only if the local group has the capacity to fit
2742 * these excess tasks, i.e. nr_running < group_capacity. The
2743 * extra check prevents the case where you always pull from the
2744 * heaviest group when it is already under-utilized (possible
2745 * with a large weight task outweighs the tasks on the system).
1e3c88bd 2746 */
75dd321d 2747 if (prefer_sibling && !local_group && sds->this_has_capacity)
1e3c88bd
PZ
2748 sgs.group_capacity = min(sgs.group_capacity, 1UL);
2749
2750 if (local_group) {
2751 sds->this_load = sgs.avg_load;
532cb4c4 2752 sds->this = sg;
1e3c88bd
PZ
2753 sds->this_nr_running = sgs.sum_nr_running;
2754 sds->this_load_per_task = sgs.sum_weighted_load;
fab47622 2755 sds->this_has_capacity = sgs.group_has_capacity;
532cb4c4 2756 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
1e3c88bd 2757 sds->max_load = sgs.avg_load;
532cb4c4 2758 sds->busiest = sg;
1e3c88bd 2759 sds->busiest_nr_running = sgs.sum_nr_running;
dd5feea1 2760 sds->busiest_group_capacity = sgs.group_capacity;
1e3c88bd 2761 sds->busiest_load_per_task = sgs.sum_weighted_load;
fab47622 2762 sds->busiest_has_capacity = sgs.group_has_capacity;
1e3c88bd
PZ
2763 sds->group_imb = sgs.group_imb;
2764 }
2765
532cb4c4
MN
2766 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
2767 sg = sg->next;
2768 } while (sg != sd->groups);
2769}
2770
2ec57d44 2771int __weak arch_sd_sibling_asym_packing(void)
532cb4c4
MN
2772{
2773 return 0*SD_ASYM_PACKING;
2774}
2775
2776/**
2777 * check_asym_packing - Check to see if the group is packed into the
2778 * sched doman.
2779 *
2780 * This is primarily intended to used at the sibling level. Some
2781 * cores like POWER7 prefer to use lower numbered SMT threads. In the
2782 * case of POWER7, it can move to lower SMT modes only when higher
2783 * threads are idle. When in lower SMT modes, the threads will
2784 * perform better since they share less core resources. Hence when we
2785 * have idle threads, we want them to be the higher ones.
2786 *
2787 * This packing function is run on idle threads. It checks to see if
2788 * the busiest CPU in this domain (core in the P7 case) has a higher
2789 * CPU number than the packing function is being run on. Here we are
2790 * assuming lower CPU number will be equivalent to lower a SMT thread
2791 * number.
2792 *
b6b12294
MN
2793 * Returns 1 when packing is required and a task should be moved to
2794 * this CPU. The amount of the imbalance is returned in *imbalance.
2795 *
532cb4c4
MN
2796 * @sd: The sched_domain whose packing is to be checked.
2797 * @sds: Statistics of the sched_domain which is to be packed
2798 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
2799 * @imbalance: returns amount of imbalanced due to packing.
532cb4c4
MN
2800 */
2801static int check_asym_packing(struct sched_domain *sd,
2802 struct sd_lb_stats *sds,
2803 int this_cpu, unsigned long *imbalance)
2804{
2805 int busiest_cpu;
2806
2807 if (!(sd->flags & SD_ASYM_PACKING))
2808 return 0;
2809
2810 if (!sds->busiest)
2811 return 0;
2812
2813 busiest_cpu = group_first_cpu(sds->busiest);
2814 if (this_cpu > busiest_cpu)
2815 return 0;
2816
2817 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->cpu_power,
2818 SCHED_LOAD_SCALE);
2819 return 1;
1e3c88bd
PZ
2820}
2821
2822/**
2823 * fix_small_imbalance - Calculate the minor imbalance that exists
2824 * amongst the groups of a sched_domain, during
2825 * load balancing.
2826 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
2827 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
2828 * @imbalance: Variable to store the imbalance.
2829 */
2830static inline void fix_small_imbalance(struct sd_lb_stats *sds,
2831 int this_cpu, unsigned long *imbalance)
2832{
2833 unsigned long tmp, pwr_now = 0, pwr_move = 0;
2834 unsigned int imbn = 2;
dd5feea1 2835 unsigned long scaled_busy_load_per_task;
1e3c88bd
PZ
2836
2837 if (sds->this_nr_running) {
2838 sds->this_load_per_task /= sds->this_nr_running;
2839 if (sds->busiest_load_per_task >
2840 sds->this_load_per_task)
2841 imbn = 1;
2842 } else
2843 sds->this_load_per_task =
2844 cpu_avg_load_per_task(this_cpu);
2845
dd5feea1
SS
2846 scaled_busy_load_per_task = sds->busiest_load_per_task
2847 * SCHED_LOAD_SCALE;
2848 scaled_busy_load_per_task /= sds->busiest->cpu_power;
2849
2850 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
2851 (scaled_busy_load_per_task * imbn)) {
1e3c88bd
PZ
2852 *imbalance = sds->busiest_load_per_task;
2853 return;
2854 }
2855
2856 /*
2857 * OK, we don't have enough imbalance to justify moving tasks,
2858 * however we may be able to increase total CPU power used by
2859 * moving them.
2860 */
2861
2862 pwr_now += sds->busiest->cpu_power *
2863 min(sds->busiest_load_per_task, sds->max_load);
2864 pwr_now += sds->this->cpu_power *
2865 min(sds->this_load_per_task, sds->this_load);
2866 pwr_now /= SCHED_LOAD_SCALE;
2867
2868 /* Amount of load we'd subtract */
2869 tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
2870 sds->busiest->cpu_power;
2871 if (sds->max_load > tmp)
2872 pwr_move += sds->busiest->cpu_power *
2873 min(sds->busiest_load_per_task, sds->max_load - tmp);
2874
2875 /* Amount of load we'd add */
2876 if (sds->max_load * sds->busiest->cpu_power <
2877 sds->busiest_load_per_task * SCHED_LOAD_SCALE)
2878 tmp = (sds->max_load * sds->busiest->cpu_power) /
2879 sds->this->cpu_power;
2880 else
2881 tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
2882 sds->this->cpu_power;
2883 pwr_move += sds->this->cpu_power *
2884 min(sds->this_load_per_task, sds->this_load + tmp);
2885 pwr_move /= SCHED_LOAD_SCALE;
2886
2887 /* Move if we gain throughput */
2888 if (pwr_move > pwr_now)
2889 *imbalance = sds->busiest_load_per_task;
2890}
2891
2892/**
2893 * calculate_imbalance - Calculate the amount of imbalance present within the
2894 * groups of a given sched_domain during load balance.
2895 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
2896 * @this_cpu: Cpu for which currently load balance is being performed.
2897 * @imbalance: The variable to store the imbalance.
2898 */
2899static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
2900 unsigned long *imbalance)
2901{
dd5feea1
SS
2902 unsigned long max_pull, load_above_capacity = ~0UL;
2903
2904 sds->busiest_load_per_task /= sds->busiest_nr_running;
2905 if (sds->group_imb) {
2906 sds->busiest_load_per_task =
2907 min(sds->busiest_load_per_task, sds->avg_load);
2908 }
2909
1e3c88bd
PZ
2910 /*
2911 * In the presence of smp nice balancing, certain scenarios can have
2912 * max load less than avg load(as we skip the groups at or below
2913 * its cpu_power, while calculating max_load..)
2914 */
2915 if (sds->max_load < sds->avg_load) {
2916 *imbalance = 0;
2917 return fix_small_imbalance(sds, this_cpu, imbalance);
2918 }
2919
dd5feea1
SS
2920 if (!sds->group_imb) {
2921 /*
2922 * Don't want to pull so many tasks that a group would go idle.
2923 */
2924 load_above_capacity = (sds->busiest_nr_running -
2925 sds->busiest_group_capacity);
2926
2927 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_LOAD_SCALE);
2928
2929 load_above_capacity /= sds->busiest->cpu_power;
2930 }
2931
2932 /*
2933 * We're trying to get all the cpus to the average_load, so we don't
2934 * want to push ourselves above the average load, nor do we wish to
2935 * reduce the max loaded cpu below the average load. At the same time,
2936 * we also don't want to reduce the group load below the group capacity
2937 * (so that we can implement power-savings policies etc). Thus we look
2938 * for the minimum possible imbalance.
2939 * Be careful of negative numbers as they'll appear as very large values
2940 * with unsigned longs.
2941 */
2942 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
1e3c88bd
PZ
2943
2944 /* How much load to actually move to equalise the imbalance */
2945 *imbalance = min(max_pull * sds->busiest->cpu_power,
2946 (sds->avg_load - sds->this_load) * sds->this->cpu_power)
2947 / SCHED_LOAD_SCALE;
2948
2949 /*
2950 * if *imbalance is less than the average load per runnable task
2951 * there is no gaurantee that any tasks will be moved so we'll have
2952 * a think about bumping its value to force at least one task to be
2953 * moved
2954 */
2955 if (*imbalance < sds->busiest_load_per_task)
2956 return fix_small_imbalance(sds, this_cpu, imbalance);
2957
2958}
fab47622 2959
1e3c88bd
PZ
2960/******* find_busiest_group() helpers end here *********************/
2961
2962/**
2963 * find_busiest_group - Returns the busiest group within the sched_domain
2964 * if there is an imbalance. If there isn't an imbalance, and
2965 * the user has opted for power-savings, it returns a group whose
2966 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
2967 * such a group exists.
2968 *
2969 * Also calculates the amount of weighted load which should be moved
2970 * to restore balance.
2971 *
2972 * @sd: The sched_domain whose busiest group is to be returned.
2973 * @this_cpu: The cpu for which load balancing is currently being performed.
2974 * @imbalance: Variable which stores amount of weighted load which should
2975 * be moved to restore balance/put a group to idle.
2976 * @idle: The idle status of this_cpu.
2977 * @sd_idle: The idleness of sd
2978 * @cpus: The set of CPUs under consideration for load-balancing.
2979 * @balance: Pointer to a variable indicating if this_cpu
2980 * is the appropriate cpu to perform load balancing at this_level.
2981 *
2982 * Returns: - the busiest group if imbalance exists.
2983 * - If no imbalance and user has opted for power-savings balance,
2984 * return the least loaded group whose CPUs can be
2985 * put to idle by rebalancing its tasks onto our group.
2986 */
2987static struct sched_group *
2988find_busiest_group(struct sched_domain *sd, int this_cpu,
2989 unsigned long *imbalance, enum cpu_idle_type idle,
2990 int *sd_idle, const struct cpumask *cpus, int *balance)
2991{
2992 struct sd_lb_stats sds;
2993
2994 memset(&sds, 0, sizeof(sds));
2995
2996 /*
2997 * Compute the various statistics relavent for load balancing at
2998 * this level.
2999 */
3000 update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
3001 balance, &sds);
3002
3003 /* Cases where imbalance does not exist from POV of this_cpu */
3004 /* 1) this_cpu is not the appropriate cpu to perform load balancing
3005 * at this level.
3006 * 2) There is no busy sibling group to pull from.
3007 * 3) This group is the busiest group.
3008 * 4) This group is more busy than the avg busieness at this
3009 * sched_domain.
3010 * 5) The imbalance is within the specified limit.
fab47622
NR
3011 *
3012 * Note: when doing newidle balance, if the local group has excess
3013 * capacity (i.e. nr_running < group_capacity) and the busiest group
3014 * does not have any capacity, we force a load balance to pull tasks
3015 * to the local group. In this case, we skip past checks 3, 4 and 5.
1e3c88bd 3016 */
8f190fb3 3017 if (!(*balance))
1e3c88bd
PZ
3018 goto ret;
3019
532cb4c4
MN
3020 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
3021 check_asym_packing(sd, &sds, this_cpu, imbalance))
3022 return sds.busiest;
3023
1e3c88bd
PZ
3024 if (!sds.busiest || sds.busiest_nr_running == 0)
3025 goto out_balanced;
3026
fab47622
NR
3027 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
3028 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
3029 !sds.busiest_has_capacity)
3030 goto force_balance;
3031
1e3c88bd
PZ
3032 if (sds.this_load >= sds.max_load)
3033 goto out_balanced;
3034
3035 sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
3036
3037 if (sds.this_load >= sds.avg_load)
3038 goto out_balanced;
3039
3040 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
3041 goto out_balanced;
3042
fab47622 3043force_balance:
1e3c88bd
PZ
3044 /* Looks like there is an imbalance. Compute it */
3045 calculate_imbalance(&sds, this_cpu, imbalance);
3046 return sds.busiest;
3047
3048out_balanced:
3049 /*
3050 * There is no obvious imbalance. But check if we can do some balancing
3051 * to save power.
3052 */
3053 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
3054 return sds.busiest;
3055ret:
3056 *imbalance = 0;
3057 return NULL;
3058}
3059
3060/*
3061 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3062 */
3063static struct rq *
9d5efe05
SV
3064find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
3065 enum cpu_idle_type idle, unsigned long imbalance,
3066 const struct cpumask *cpus)
1e3c88bd
PZ
3067{
3068 struct rq *busiest = NULL, *rq;
3069 unsigned long max_load = 0;
3070 int i;
3071
3072 for_each_cpu(i, sched_group_cpus(group)) {
3073 unsigned long power = power_of(i);
3074 unsigned long capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
3075 unsigned long wl;
3076
9d5efe05
SV
3077 if (!capacity)
3078 capacity = fix_small_capacity(sd, group);
3079
1e3c88bd
PZ
3080 if (!cpumask_test_cpu(i, cpus))
3081 continue;
3082
3083 rq = cpu_rq(i);
6e40f5bb 3084 wl = weighted_cpuload(i);
1e3c88bd 3085
6e40f5bb
TG
3086 /*
3087 * When comparing with imbalance, use weighted_cpuload()
3088 * which is not scaled with the cpu power.
3089 */
1e3c88bd
PZ
3090 if (capacity && rq->nr_running == 1 && wl > imbalance)
3091 continue;
3092
6e40f5bb
TG
3093 /*
3094 * For the load comparisons with the other cpu's, consider
3095 * the weighted_cpuload() scaled with the cpu power, so that
3096 * the load can be moved away from the cpu that is potentially
3097 * running at a lower capacity.
3098 */
3099 wl = (wl * SCHED_LOAD_SCALE) / power;
3100
1e3c88bd
PZ
3101 if (wl > max_load) {
3102 max_load = wl;
3103 busiest = rq;
3104 }
3105 }
3106
3107 return busiest;
3108}
3109
3110/*
3111 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3112 * so long as it is large enough.
3113 */
3114#define MAX_PINNED_INTERVAL 512
3115
3116/* Working cpumask for load_balance and load_balance_newidle. */
3117static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
3118
532cb4c4
MN
3119static int need_active_balance(struct sched_domain *sd, int sd_idle, int idle,
3120 int busiest_cpu, int this_cpu)
1af3ed3d
PZ
3121{
3122 if (idle == CPU_NEWLY_IDLE) {
532cb4c4
MN
3123
3124 /*
3125 * ASYM_PACKING needs to force migrate tasks from busy but
3126 * higher numbered CPUs in order to pack all tasks in the
3127 * lowest numbered CPUs.
3128 */
3129 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
3130 return 1;
3131
1af3ed3d
PZ
3132 /*
3133 * The only task running in a non-idle cpu can be moved to this
3134 * cpu in an attempt to completely freeup the other CPU
3135 * package.
3136 *
3137 * The package power saving logic comes from
3138 * find_busiest_group(). If there are no imbalance, then
3139 * f_b_g() will return NULL. However when sched_mc={1,2} then
3140 * f_b_g() will select a group from which a running task may be
3141 * pulled to this cpu in order to make the other package idle.
3142 * If there is no opportunity to make a package idle and if
3143 * there are no imbalance, then f_b_g() will return NULL and no
3144 * action will be taken in load_balance_newidle().
3145 *
3146 * Under normal task pull operation due to imbalance, there
3147 * will be more than one task in the source run queue and
3148 * move_tasks() will succeed. ld_moved will be true and this
3149 * active balance code will not be triggered.
3150 */
3151 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3152 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3153 return 0;
3154
3155 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
3156 return 0;
3157 }
3158
3159 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
3160}
3161
969c7921
TH
3162static int active_load_balance_cpu_stop(void *data);
3163
1e3c88bd
PZ
3164/*
3165 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3166 * tasks if there is an imbalance.
3167 */
3168static int load_balance(int this_cpu, struct rq *this_rq,
3169 struct sched_domain *sd, enum cpu_idle_type idle,
3170 int *balance)
3171{
3172 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
3173 struct sched_group *group;
3174 unsigned long imbalance;
3175 struct rq *busiest;
3176 unsigned long flags;
3177 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
3178
3179 cpumask_copy(cpus, cpu_active_mask);
3180
3181 /*
3182 * When power savings policy is enabled for the parent domain, idle
3183 * sibling can pick up load irrespective of busy siblings. In this case,
3184 * let the state of idle sibling percolate up as CPU_IDLE, instead of
3185 * portraying it as CPU_NOT_IDLE.
3186 */
3187 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
3188 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3189 sd_idle = 1;
3190
3191 schedstat_inc(sd, lb_count[idle]);
3192
3193redo:
1e3c88bd
PZ
3194 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
3195 cpus, balance);
3196
3197 if (*balance == 0)
3198 goto out_balanced;
3199
3200 if (!group) {
3201 schedstat_inc(sd, lb_nobusyg[idle]);
3202 goto out_balanced;
3203 }
3204
9d5efe05 3205 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
1e3c88bd
PZ
3206 if (!busiest) {
3207 schedstat_inc(sd, lb_nobusyq[idle]);
3208 goto out_balanced;
3209 }
3210
3211 BUG_ON(busiest == this_rq);
3212
3213 schedstat_add(sd, lb_imbalance[idle], imbalance);
3214
3215 ld_moved = 0;
3216 if (busiest->nr_running > 1) {
3217 /*
3218 * Attempt to move tasks. If find_busiest_group has found
3219 * an imbalance but busiest->nr_running <= 1, the group is
3220 * still unbalanced. ld_moved simply stays zero, so it is
3221 * correctly treated as an imbalance.
3222 */
3223 local_irq_save(flags);
3224 double_rq_lock(this_rq, busiest);
3225 ld_moved = move_tasks(this_rq, this_cpu, busiest,
3226 imbalance, sd, idle, &all_pinned);
3227 double_rq_unlock(this_rq, busiest);
3228 local_irq_restore(flags);
3229
3230 /*
3231 * some other cpu did the load balance for us.
3232 */
3233 if (ld_moved && this_cpu != smp_processor_id())
3234 resched_cpu(this_cpu);
3235
3236 /* All tasks on this runqueue were pinned by CPU affinity */
3237 if (unlikely(all_pinned)) {
3238 cpumask_clear_cpu(cpu_of(busiest), cpus);
3239 if (!cpumask_empty(cpus))
3240 goto redo;
3241 goto out_balanced;
3242 }
3243 }
3244
3245 if (!ld_moved) {
3246 schedstat_inc(sd, lb_failed[idle]);
58b26c4c
VP
3247 /*
3248 * Increment the failure counter only on periodic balance.
3249 * We do not want newidle balance, which can be very
3250 * frequent, pollute the failure counter causing
3251 * excessive cache_hot migrations and active balances.
3252 */
3253 if (idle != CPU_NEWLY_IDLE)
3254 sd->nr_balance_failed++;
1e3c88bd 3255
532cb4c4
MN
3256 if (need_active_balance(sd, sd_idle, idle, cpu_of(busiest),
3257 this_cpu)) {
1e3c88bd
PZ
3258 raw_spin_lock_irqsave(&busiest->lock, flags);
3259
969c7921
TH
3260 /* don't kick the active_load_balance_cpu_stop,
3261 * if the curr task on busiest cpu can't be
3262 * moved to this_cpu
1e3c88bd
PZ
3263 */
3264 if (!cpumask_test_cpu(this_cpu,
3265 &busiest->curr->cpus_allowed)) {
3266 raw_spin_unlock_irqrestore(&busiest->lock,
3267 flags);
3268 all_pinned = 1;
3269 goto out_one_pinned;
3270 }
3271
969c7921
TH
3272 /*
3273 * ->active_balance synchronizes accesses to
3274 * ->active_balance_work. Once set, it's cleared
3275 * only after active load balance is finished.
3276 */
1e3c88bd
PZ
3277 if (!busiest->active_balance) {
3278 busiest->active_balance = 1;
3279 busiest->push_cpu = this_cpu;
3280 active_balance = 1;
3281 }
3282 raw_spin_unlock_irqrestore(&busiest->lock, flags);
969c7921 3283
1e3c88bd 3284 if (active_balance)
969c7921
TH
3285 stop_one_cpu_nowait(cpu_of(busiest),
3286 active_load_balance_cpu_stop, busiest,
3287 &busiest->active_balance_work);
1e3c88bd
PZ
3288
3289 /*
3290 * We've kicked active balancing, reset the failure
3291 * counter.
3292 */
3293 sd->nr_balance_failed = sd->cache_nice_tries+1;
3294 }
3295 } else
3296 sd->nr_balance_failed = 0;
3297
3298 if (likely(!active_balance)) {
3299 /* We were unbalanced, so reset the balancing interval */
3300 sd->balance_interval = sd->min_interval;
3301 } else {
3302 /*
3303 * If we've begun active balancing, start to back off. This
3304 * case may not be covered by the all_pinned logic if there
3305 * is only 1 task on the busy runqueue (because we don't call
3306 * move_tasks).
3307 */
3308 if (sd->balance_interval < sd->max_interval)
3309 sd->balance_interval *= 2;
3310 }
3311
3312 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3313 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3314 ld_moved = -1;
3315
3316 goto out;
3317
3318out_balanced:
3319 schedstat_inc(sd, lb_balanced[idle]);
3320
3321 sd->nr_balance_failed = 0;
3322
3323out_one_pinned:
3324 /* tune up the balancing interval */
3325 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3326 (sd->balance_interval < sd->max_interval))
3327 sd->balance_interval *= 2;
3328
3329 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3330 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3331 ld_moved = -1;
3332 else
3333 ld_moved = 0;
3334out:
1e3c88bd
PZ
3335 return ld_moved;
3336}
3337
1e3c88bd
PZ
3338/*
3339 * idle_balance is called by schedule() if this_cpu is about to become
3340 * idle. Attempts to pull tasks from other CPUs.
3341 */
3342static void idle_balance(int this_cpu, struct rq *this_rq)
3343{
3344 struct sched_domain *sd;
3345 int pulled_task = 0;
3346 unsigned long next_balance = jiffies + HZ;
3347
3348 this_rq->idle_stamp = this_rq->clock;
3349
3350 if (this_rq->avg_idle < sysctl_sched_migration_cost)
3351 return;
3352
f492e12e
PZ
3353 /*
3354 * Drop the rq->lock, but keep IRQ/preempt disabled.
3355 */
3356 raw_spin_unlock(&this_rq->lock);
3357
c66eaf61 3358 update_shares(this_cpu);
1e3c88bd
PZ
3359 for_each_domain(this_cpu, sd) {
3360 unsigned long interval;
f492e12e 3361 int balance = 1;
1e3c88bd
PZ
3362
3363 if (!(sd->flags & SD_LOAD_BALANCE))
3364 continue;
3365
f492e12e 3366 if (sd->flags & SD_BALANCE_NEWIDLE) {
1e3c88bd 3367 /* If we've pulled tasks over stop searching: */
f492e12e
PZ
3368 pulled_task = load_balance(this_cpu, this_rq,
3369 sd, CPU_NEWLY_IDLE, &balance);
3370 }
1e3c88bd
PZ
3371
3372 interval = msecs_to_jiffies(sd->balance_interval);
3373 if (time_after(next_balance, sd->last_balance + interval))
3374 next_balance = sd->last_balance + interval;
fab47622 3375 if (pulled_task)
1e3c88bd 3376 break;
1e3c88bd 3377 }
f492e12e
PZ
3378
3379 raw_spin_lock(&this_rq->lock);
3380
1e3c88bd
PZ
3381 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
3382 /*
3383 * We are going idle. next_balance may be set based on
3384 * a busy processor. So reset next_balance.
3385 */
3386 this_rq->next_balance = next_balance;
3387 }
3388}
3389
3390/*
969c7921
TH
3391 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
3392 * running tasks off the busiest CPU onto idle CPUs. It requires at
3393 * least 1 task to be running on each physical CPU where possible, and
3394 * avoids physical / logical imbalances.
1e3c88bd 3395 */
969c7921 3396static int active_load_balance_cpu_stop(void *data)
1e3c88bd 3397{
969c7921
TH
3398 struct rq *busiest_rq = data;
3399 int busiest_cpu = cpu_of(busiest_rq);
1e3c88bd 3400 int target_cpu = busiest_rq->push_cpu;
969c7921 3401 struct rq *target_rq = cpu_rq(target_cpu);
1e3c88bd 3402 struct sched_domain *sd;
969c7921
TH
3403
3404 raw_spin_lock_irq(&busiest_rq->lock);
3405
3406 /* make sure the requested cpu hasn't gone down in the meantime */
3407 if (unlikely(busiest_cpu != smp_processor_id() ||
3408 !busiest_rq->active_balance))
3409 goto out_unlock;
1e3c88bd
PZ
3410
3411 /* Is there any task to move? */
3412 if (busiest_rq->nr_running <= 1)
969c7921 3413 goto out_unlock;
1e3c88bd
PZ
3414
3415 /*
3416 * This condition is "impossible", if it occurs
3417 * we need to fix it. Originally reported by
3418 * Bjorn Helgaas on a 128-cpu setup.
3419 */
3420 BUG_ON(busiest_rq == target_rq);
3421
3422 /* move a task from busiest_rq to target_rq */
3423 double_lock_balance(busiest_rq, target_rq);
1e3c88bd
PZ
3424
3425 /* Search for an sd spanning us and the target CPU. */
3426 for_each_domain(target_cpu, sd) {
3427 if ((sd->flags & SD_LOAD_BALANCE) &&
3428 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
3429 break;
3430 }
3431
3432 if (likely(sd)) {
3433 schedstat_inc(sd, alb_count);
3434
3435 if (move_one_task(target_rq, target_cpu, busiest_rq,
3436 sd, CPU_IDLE))
3437 schedstat_inc(sd, alb_pushed);
3438 else
3439 schedstat_inc(sd, alb_failed);
3440 }
3441 double_unlock_balance(busiest_rq, target_rq);
969c7921
TH
3442out_unlock:
3443 busiest_rq->active_balance = 0;
3444 raw_spin_unlock_irq(&busiest_rq->lock);
3445 return 0;
1e3c88bd
PZ
3446}
3447
3448#ifdef CONFIG_NO_HZ
83cd4fe2
VP
3449
3450static DEFINE_PER_CPU(struct call_single_data, remote_sched_softirq_cb);
3451
3452static void trigger_sched_softirq(void *data)
3453{
3454 raise_softirq_irqoff(SCHED_SOFTIRQ);
3455}
3456
3457static inline void init_sched_softirq_csd(struct call_single_data *csd)
3458{
3459 csd->func = trigger_sched_softirq;
3460 csd->info = NULL;
3461 csd->flags = 0;
3462 csd->priv = 0;
3463}
3464
3465/*
3466 * idle load balancing details
3467 * - One of the idle CPUs nominates itself as idle load_balancer, while
3468 * entering idle.
3469 * - This idle load balancer CPU will also go into tickless mode when
3470 * it is idle, just like all other idle CPUs
3471 * - When one of the busy CPUs notice that there may be an idle rebalancing
3472 * needed, they will kick the idle load balancer, which then does idle
3473 * load balancing for all the idle CPUs.
3474 */
1e3c88bd
PZ
3475static struct {
3476 atomic_t load_balancer;
83cd4fe2
VP
3477 atomic_t first_pick_cpu;
3478 atomic_t second_pick_cpu;
3479 cpumask_var_t idle_cpus_mask;
3480 cpumask_var_t grp_idle_mask;
3481 unsigned long next_balance; /* in jiffy units */
3482} nohz ____cacheline_aligned;
1e3c88bd
PZ
3483
3484int get_nohz_load_balancer(void)
3485{
3486 return atomic_read(&nohz.load_balancer);
3487}
3488
3489#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3490/**
3491 * lowest_flag_domain - Return lowest sched_domain containing flag.
3492 * @cpu: The cpu whose lowest level of sched domain is to
3493 * be returned.
3494 * @flag: The flag to check for the lowest sched_domain
3495 * for the given cpu.
3496 *
3497 * Returns the lowest sched_domain of a cpu which contains the given flag.
3498 */
3499static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
3500{
3501 struct sched_domain *sd;
3502
3503 for_each_domain(cpu, sd)
3504 if (sd && (sd->flags & flag))
3505 break;
3506
3507 return sd;
3508}
3509
3510/**
3511 * for_each_flag_domain - Iterates over sched_domains containing the flag.
3512 * @cpu: The cpu whose domains we're iterating over.
3513 * @sd: variable holding the value of the power_savings_sd
3514 * for cpu.
3515 * @flag: The flag to filter the sched_domains to be iterated.
3516 *
3517 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
3518 * set, starting from the lowest sched_domain to the highest.
3519 */
3520#define for_each_flag_domain(cpu, sd, flag) \
3521 for (sd = lowest_flag_domain(cpu, flag); \
3522 (sd && (sd->flags & flag)); sd = sd->parent)
3523
3524/**
3525 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
3526 * @ilb_group: group to be checked for semi-idleness
3527 *
3528 * Returns: 1 if the group is semi-idle. 0 otherwise.
3529 *
3530 * We define a sched_group to be semi idle if it has atleast one idle-CPU
3531 * and atleast one non-idle CPU. This helper function checks if the given
3532 * sched_group is semi-idle or not.
3533 */
3534static inline int is_semi_idle_group(struct sched_group *ilb_group)
3535{
83cd4fe2 3536 cpumask_and(nohz.grp_idle_mask, nohz.idle_cpus_mask,
1e3c88bd
PZ
3537 sched_group_cpus(ilb_group));
3538
3539 /*
3540 * A sched_group is semi-idle when it has atleast one busy cpu
3541 * and atleast one idle cpu.
3542 */
83cd4fe2 3543 if (cpumask_empty(nohz.grp_idle_mask))
1e3c88bd
PZ
3544 return 0;
3545
83cd4fe2 3546 if (cpumask_equal(nohz.grp_idle_mask, sched_group_cpus(ilb_group)))
1e3c88bd
PZ
3547 return 0;
3548
3549 return 1;
3550}
3551/**
3552 * find_new_ilb - Finds the optimum idle load balancer for nomination.
3553 * @cpu: The cpu which is nominating a new idle_load_balancer.
3554 *
3555 * Returns: Returns the id of the idle load balancer if it exists,
3556 * Else, returns >= nr_cpu_ids.
3557 *
3558 * This algorithm picks the idle load balancer such that it belongs to a
3559 * semi-idle powersavings sched_domain. The idea is to try and avoid
3560 * completely idle packages/cores just for the purpose of idle load balancing
3561 * when there are other idle cpu's which are better suited for that job.
3562 */
3563static int find_new_ilb(int cpu)
3564{
3565 struct sched_domain *sd;
3566 struct sched_group *ilb_group;
3567
3568 /*
3569 * Have idle load balancer selection from semi-idle packages only
3570 * when power-aware load balancing is enabled
3571 */
3572 if (!(sched_smt_power_savings || sched_mc_power_savings))
3573 goto out_done;
3574
3575 /*
3576 * Optimize for the case when we have no idle CPUs or only one
3577 * idle CPU. Don't walk the sched_domain hierarchy in such cases
3578 */
83cd4fe2 3579 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
1e3c88bd
PZ
3580 goto out_done;
3581
3582 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
3583 ilb_group = sd->groups;
3584
3585 do {
3586 if (is_semi_idle_group(ilb_group))
83cd4fe2 3587 return cpumask_first(nohz.grp_idle_mask);
1e3c88bd
PZ
3588
3589 ilb_group = ilb_group->next;
3590
3591 } while (ilb_group != sd->groups);
3592 }
3593
3594out_done:
83cd4fe2 3595 return nr_cpu_ids;
1e3c88bd
PZ
3596}
3597#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
3598static inline int find_new_ilb(int call_cpu)
3599{
83cd4fe2 3600 return nr_cpu_ids;
1e3c88bd
PZ
3601}
3602#endif
3603
83cd4fe2
VP
3604/*
3605 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
3606 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
3607 * CPU (if there is one).
3608 */
3609static void nohz_balancer_kick(int cpu)
3610{
3611 int ilb_cpu;
3612
3613 nohz.next_balance++;
3614
3615 ilb_cpu = get_nohz_load_balancer();
3616
3617 if (ilb_cpu >= nr_cpu_ids) {
3618 ilb_cpu = cpumask_first(nohz.idle_cpus_mask);
3619 if (ilb_cpu >= nr_cpu_ids)
3620 return;
3621 }
3622
3623 if (!cpu_rq(ilb_cpu)->nohz_balance_kick) {
3624 struct call_single_data *cp;
3625
3626 cpu_rq(ilb_cpu)->nohz_balance_kick = 1;
3627 cp = &per_cpu(remote_sched_softirq_cb, cpu);
3628 __smp_call_function_single(ilb_cpu, cp, 0);
3629 }
3630 return;
3631}
3632
1e3c88bd
PZ
3633/*
3634 * This routine will try to nominate the ilb (idle load balancing)
3635 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
83cd4fe2 3636 * load balancing on behalf of all those cpus.
1e3c88bd 3637 *
83cd4fe2
VP
3638 * When the ilb owner becomes busy, we will not have new ilb owner until some
3639 * idle CPU wakes up and goes back to idle or some busy CPU tries to kick
3640 * idle load balancing by kicking one of the idle CPUs.
1e3c88bd 3641 *
83cd4fe2
VP
3642 * Ticks are stopped for the ilb owner as well, with busy CPU kicking this
3643 * ilb owner CPU in future (when there is a need for idle load balancing on
3644 * behalf of all idle CPUs).
1e3c88bd 3645 */
83cd4fe2 3646void select_nohz_load_balancer(int stop_tick)
1e3c88bd
PZ
3647{
3648 int cpu = smp_processor_id();
3649
3650 if (stop_tick) {
1e3c88bd
PZ
3651 if (!cpu_active(cpu)) {
3652 if (atomic_read(&nohz.load_balancer) != cpu)
83cd4fe2 3653 return;
1e3c88bd
PZ
3654
3655 /*
3656 * If we are going offline and still the leader,
3657 * give up!
3658 */
83cd4fe2
VP
3659 if (atomic_cmpxchg(&nohz.load_balancer, cpu,
3660 nr_cpu_ids) != cpu)
1e3c88bd
PZ
3661 BUG();
3662
83cd4fe2 3663 return;
1e3c88bd
PZ
3664 }
3665
83cd4fe2 3666 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
1e3c88bd 3667
83cd4fe2
VP
3668 if (atomic_read(&nohz.first_pick_cpu) == cpu)
3669 atomic_cmpxchg(&nohz.first_pick_cpu, cpu, nr_cpu_ids);
3670 if (atomic_read(&nohz.second_pick_cpu) == cpu)
3671 atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids);
1e3c88bd 3672
83cd4fe2 3673 if (atomic_read(&nohz.load_balancer) >= nr_cpu_ids) {
1e3c88bd
PZ
3674 int new_ilb;
3675
83cd4fe2
VP
3676 /* make me the ilb owner */
3677 if (atomic_cmpxchg(&nohz.load_balancer, nr_cpu_ids,
3678 cpu) != nr_cpu_ids)
3679 return;
3680
1e3c88bd
PZ
3681 /*
3682 * Check to see if there is a more power-efficient
3683 * ilb.
3684 */
3685 new_ilb = find_new_ilb(cpu);
3686 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
83cd4fe2 3687 atomic_set(&nohz.load_balancer, nr_cpu_ids);
1e3c88bd 3688 resched_cpu(new_ilb);
83cd4fe2 3689 return;
1e3c88bd 3690 }
83cd4fe2 3691 return;
1e3c88bd
PZ
3692 }
3693 } else {
83cd4fe2
VP
3694 if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask))
3695 return;
1e3c88bd 3696
83cd4fe2 3697 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
1e3c88bd
PZ
3698
3699 if (atomic_read(&nohz.load_balancer) == cpu)
83cd4fe2
VP
3700 if (atomic_cmpxchg(&nohz.load_balancer, cpu,
3701 nr_cpu_ids) != cpu)
1e3c88bd
PZ
3702 BUG();
3703 }
83cd4fe2 3704 return;
1e3c88bd
PZ
3705}
3706#endif
3707
3708static DEFINE_SPINLOCK(balancing);
3709
3710/*
3711 * It checks each scheduling domain to see if it is due to be balanced,
3712 * and initiates a balancing operation if so.
3713 *
3714 * Balancing parameters are set up in arch_init_sched_domains.
3715 */
3716static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3717{
3718 int balance = 1;
3719 struct rq *rq = cpu_rq(cpu);
3720 unsigned long interval;
3721 struct sched_domain *sd;
3722 /* Earliest time when we have to do rebalance again */
3723 unsigned long next_balance = jiffies + 60*HZ;
3724 int update_next_balance = 0;
3725 int need_serialize;
3726
2069dd75
PZ
3727 update_shares(cpu);
3728
1e3c88bd
PZ
3729 for_each_domain(cpu, sd) {
3730 if (!(sd->flags & SD_LOAD_BALANCE))
3731 continue;
3732
3733 interval = sd->balance_interval;
3734 if (idle != CPU_IDLE)
3735 interval *= sd->busy_factor;
3736
3737 /* scale ms to jiffies */
3738 interval = msecs_to_jiffies(interval);
3739 if (unlikely(!interval))
3740 interval = 1;
3741 if (interval > HZ*NR_CPUS/10)
3742 interval = HZ*NR_CPUS/10;
3743
3744 need_serialize = sd->flags & SD_SERIALIZE;
3745
3746 if (need_serialize) {
3747 if (!spin_trylock(&balancing))
3748 goto out;
3749 }
3750
3751 if (time_after_eq(jiffies, sd->last_balance + interval)) {
3752 if (load_balance(cpu, rq, sd, idle, &balance)) {
3753 /*
3754 * We've pulled tasks over so either we're no
3755 * longer idle, or one of our SMT siblings is
3756 * not idle.
3757 */
3758 idle = CPU_NOT_IDLE;
3759 }
3760 sd->last_balance = jiffies;
3761 }
3762 if (need_serialize)
3763 spin_unlock(&balancing);
3764out:
3765 if (time_after(next_balance, sd->last_balance + interval)) {
3766 next_balance = sd->last_balance + interval;
3767 update_next_balance = 1;
3768 }
3769
3770 /*
3771 * Stop the load balance at this level. There is another
3772 * CPU in our sched group which is doing load balancing more
3773 * actively.
3774 */
3775 if (!balance)
3776 break;
3777 }
3778
3779 /*
3780 * next_balance will be updated only when there is a need.
3781 * When the cpu is attached to null domain for ex, it will not be
3782 * updated.
3783 */
3784 if (likely(update_next_balance))
3785 rq->next_balance = next_balance;
3786}
3787
83cd4fe2 3788#ifdef CONFIG_NO_HZ
1e3c88bd 3789/*
83cd4fe2 3790 * In CONFIG_NO_HZ case, the idle balance kickee will do the
1e3c88bd
PZ
3791 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3792 */
83cd4fe2
VP
3793static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
3794{
3795 struct rq *this_rq = cpu_rq(this_cpu);
3796 struct rq *rq;
3797 int balance_cpu;
3798
3799 if (idle != CPU_IDLE || !this_rq->nohz_balance_kick)
3800 return;
3801
3802 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
3803 if (balance_cpu == this_cpu)
3804 continue;
3805
3806 /*
3807 * If this cpu gets work to do, stop the load balancing
3808 * work being done for other cpus. Next load
3809 * balancing owner will pick it up.
3810 */
3811 if (need_resched()) {
3812 this_rq->nohz_balance_kick = 0;
3813 break;
3814 }
3815
3816 raw_spin_lock_irq(&this_rq->lock);
5343bdb8 3817 update_rq_clock(this_rq);
83cd4fe2
VP
3818 update_cpu_load(this_rq);
3819 raw_spin_unlock_irq(&this_rq->lock);
3820
3821 rebalance_domains(balance_cpu, CPU_IDLE);
3822
3823 rq = cpu_rq(balance_cpu);
3824 if (time_after(this_rq->next_balance, rq->next_balance))
3825 this_rq->next_balance = rq->next_balance;
3826 }
3827 nohz.next_balance = this_rq->next_balance;
3828 this_rq->nohz_balance_kick = 0;
3829}
3830
3831/*
3832 * Current heuristic for kicking the idle load balancer
3833 * - first_pick_cpu is the one of the busy CPUs. It will kick
3834 * idle load balancer when it has more than one process active. This
3835 * eliminates the need for idle load balancing altogether when we have
3836 * only one running process in the system (common case).
3837 * - If there are more than one busy CPU, idle load balancer may have
3838 * to run for active_load_balance to happen (i.e., two busy CPUs are
3839 * SMT or core siblings and can run better if they move to different
3840 * physical CPUs). So, second_pick_cpu is the second of the busy CPUs
3841 * which will kick idle load balancer as soon as it has any load.
3842 */
3843static inline int nohz_kick_needed(struct rq *rq, int cpu)
3844{
3845 unsigned long now = jiffies;
3846 int ret;
3847 int first_pick_cpu, second_pick_cpu;
3848
3849 if (time_before(now, nohz.next_balance))
3850 return 0;
3851
f6c3f168 3852 if (rq->idle_at_tick)
83cd4fe2
VP
3853 return 0;
3854
3855 first_pick_cpu = atomic_read(&nohz.first_pick_cpu);
3856 second_pick_cpu = atomic_read(&nohz.second_pick_cpu);
3857
3858 if (first_pick_cpu < nr_cpu_ids && first_pick_cpu != cpu &&
3859 second_pick_cpu < nr_cpu_ids && second_pick_cpu != cpu)
3860 return 0;
3861
3862 ret = atomic_cmpxchg(&nohz.first_pick_cpu, nr_cpu_ids, cpu);
3863 if (ret == nr_cpu_ids || ret == cpu) {
3864 atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids);
3865 if (rq->nr_running > 1)
3866 return 1;
3867 } else {
3868 ret = atomic_cmpxchg(&nohz.second_pick_cpu, nr_cpu_ids, cpu);
3869 if (ret == nr_cpu_ids || ret == cpu) {
3870 if (rq->nr_running)
3871 return 1;
3872 }
3873 }
3874 return 0;
3875}
3876#else
3877static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
3878#endif
3879
3880/*
3881 * run_rebalance_domains is triggered when needed from the scheduler tick.
3882 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
3883 */
1e3c88bd
PZ
3884static void run_rebalance_domains(struct softirq_action *h)
3885{
3886 int this_cpu = smp_processor_id();
3887 struct rq *this_rq = cpu_rq(this_cpu);
3888 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3889 CPU_IDLE : CPU_NOT_IDLE;
3890
3891 rebalance_domains(this_cpu, idle);
3892
1e3c88bd 3893 /*
83cd4fe2 3894 * If this cpu has a pending nohz_balance_kick, then do the
1e3c88bd
PZ
3895 * balancing on behalf of the other idle cpus whose ticks are
3896 * stopped.
3897 */
83cd4fe2 3898 nohz_idle_balance(this_cpu, idle);
1e3c88bd
PZ
3899}
3900
3901static inline int on_null_domain(int cpu)
3902{
90a6501f 3903 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
1e3c88bd
PZ
3904}
3905
3906/*
3907 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
1e3c88bd
PZ
3908 */
3909static inline void trigger_load_balance(struct rq *rq, int cpu)
3910{
1e3c88bd
PZ
3911 /* Don't need to rebalance while attached to NULL domain */
3912 if (time_after_eq(jiffies, rq->next_balance) &&
3913 likely(!on_null_domain(cpu)))
3914 raise_softirq(SCHED_SOFTIRQ);
83cd4fe2
VP
3915#ifdef CONFIG_NO_HZ
3916 else if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
3917 nohz_balancer_kick(cpu);
3918#endif
1e3c88bd
PZ
3919}
3920
0bcdcf28
CE
3921static void rq_online_fair(struct rq *rq)
3922{
3923 update_sysctl();
3924}
3925
3926static void rq_offline_fair(struct rq *rq)
3927{
3928 update_sysctl();
3929}
3930
1e3c88bd
PZ
3931#else /* CONFIG_SMP */
3932
3933/*
3934 * on UP we do not need to balance between CPUs:
3935 */
3936static inline void idle_balance(int cpu, struct rq *rq)
3937{
3938}
3939
55e12e5e 3940#endif /* CONFIG_SMP */
e1d1484f 3941
bf0f6f24
IM
3942/*
3943 * scheduler tick hitting a task of our scheduling class:
3944 */
8f4d37ec 3945static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
bf0f6f24
IM
3946{
3947 struct cfs_rq *cfs_rq;
3948 struct sched_entity *se = &curr->se;
3949
3950 for_each_sched_entity(se) {
3951 cfs_rq = cfs_rq_of(se);
8f4d37ec 3952 entity_tick(cfs_rq, se, queued);
bf0f6f24
IM
3953 }
3954}
3955
3956/*
cd29fe6f
PZ
3957 * called on fork with the child task as argument from the parent's context
3958 * - child not yet on the tasklist
3959 * - preemption disabled
bf0f6f24 3960 */
cd29fe6f 3961static void task_fork_fair(struct task_struct *p)
bf0f6f24 3962{
cd29fe6f 3963 struct cfs_rq *cfs_rq = task_cfs_rq(current);
429d43bc 3964 struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
00bf7bfc 3965 int this_cpu = smp_processor_id();
cd29fe6f
PZ
3966 struct rq *rq = this_rq();
3967 unsigned long flags;
3968
05fa785c 3969 raw_spin_lock_irqsave(&rq->lock, flags);
bf0f6f24 3970
861d034e
PZ
3971 update_rq_clock(rq);
3972
b0a0f667
PM
3973 if (unlikely(task_cpu(p) != this_cpu)) {
3974 rcu_read_lock();
cd29fe6f 3975 __set_task_cpu(p, this_cpu);
b0a0f667
PM
3976 rcu_read_unlock();
3977 }
bf0f6f24 3978
7109c442 3979 update_curr(cfs_rq);
cd29fe6f 3980
b5d9d734
MG
3981 if (curr)
3982 se->vruntime = curr->vruntime;
aeb73b04 3983 place_entity(cfs_rq, se, 1);
4d78e7b6 3984
cd29fe6f 3985 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
87fefa38 3986 /*
edcb60a3
IM
3987 * Upon rescheduling, sched_class::put_prev_task() will place
3988 * 'current' within the tree based on its new key value.
3989 */
4d78e7b6 3990 swap(curr->vruntime, se->vruntime);
aec0a514 3991 resched_task(rq->curr);
4d78e7b6 3992 }
bf0f6f24 3993
88ec22d3
PZ
3994 se->vruntime -= cfs_rq->min_vruntime;
3995
05fa785c 3996 raw_spin_unlock_irqrestore(&rq->lock, flags);
bf0f6f24
IM
3997}
3998
cb469845
SR
3999/*
4000 * Priority of the task has changed. Check to see if we preempt
4001 * the current task.
4002 */
4003static void prio_changed_fair(struct rq *rq, struct task_struct *p,
4004 int oldprio, int running)
4005{
4006 /*
4007 * Reschedule if we are currently running on this runqueue and
4008 * our priority decreased, or if we are not currently running on
4009 * this runqueue and our priority is higher than the current's
4010 */
4011 if (running) {
4012 if (p->prio > oldprio)
4013 resched_task(rq->curr);
4014 } else
15afe09b 4015 check_preempt_curr(rq, p, 0);
cb469845
SR
4016}
4017
4018/*
4019 * We switched to the sched_fair class.
4020 */
4021static void switched_to_fair(struct rq *rq, struct task_struct *p,
4022 int running)
4023{
4024 /*
4025 * We were most likely switched from sched_rt, so
4026 * kick off the schedule if running, otherwise just see
4027 * if we can still preempt the current task.
4028 */
4029 if (running)
4030 resched_task(rq->curr);
4031 else
15afe09b 4032 check_preempt_curr(rq, p, 0);
cb469845
SR
4033}
4034
83b699ed
SV
4035/* Account for a task changing its policy or group.
4036 *
4037 * This routine is mostly called to set cfs_rq->curr field when a task
4038 * migrates between groups/classes.
4039 */
4040static void set_curr_task_fair(struct rq *rq)
4041{
4042 struct sched_entity *se = &rq->curr->se;
4043
4044 for_each_sched_entity(se)
4045 set_next_entity(cfs_rq_of(se), se);
4046}
4047
810b3817 4048#ifdef CONFIG_FAIR_GROUP_SCHED
b2b5ce02 4049static void task_move_group_fair(struct task_struct *p, int on_rq)
810b3817 4050{
b2b5ce02
PZ
4051 /*
4052 * If the task was not on the rq at the time of this cgroup movement
4053 * it must have been asleep, sleeping tasks keep their ->vruntime
4054 * absolute on their old rq until wakeup (needed for the fair sleeper
4055 * bonus in place_entity()).
4056 *
4057 * If it was on the rq, we've just 'preempted' it, which does convert
4058 * ->vruntime to a relative base.
4059 *
4060 * Make sure both cases convert their relative position when migrating
4061 * to another cgroup's rq. This does somewhat interfere with the
4062 * fair sleeper stuff for the first placement, but who cares.
4063 */
4064 if (!on_rq)
4065 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
4066 set_task_rq(p, task_cpu(p));
88ec22d3 4067 if (!on_rq)
b2b5ce02 4068 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
810b3817
PZ
4069}
4070#endif
4071
6d686f45 4072static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
0d721cea
PW
4073{
4074 struct sched_entity *se = &task->se;
0d721cea
PW
4075 unsigned int rr_interval = 0;
4076
4077 /*
4078 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
4079 * idle runqueue:
4080 */
0d721cea
PW
4081 if (rq->cfs.load.weight)
4082 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
0d721cea
PW
4083
4084 return rr_interval;
4085}
4086
bf0f6f24
IM
4087/*
4088 * All the scheduling class methods:
4089 */
5522d5d5
IM
4090static const struct sched_class fair_sched_class = {
4091 .next = &idle_sched_class,
bf0f6f24
IM
4092 .enqueue_task = enqueue_task_fair,
4093 .dequeue_task = dequeue_task_fair,
4094 .yield_task = yield_task_fair,
4095
2e09bf55 4096 .check_preempt_curr = check_preempt_wakeup,
bf0f6f24
IM
4097
4098 .pick_next_task = pick_next_task_fair,
4099 .put_prev_task = put_prev_task_fair,
4100
681f3e68 4101#ifdef CONFIG_SMP
4ce72a2c
LZ
4102 .select_task_rq = select_task_rq_fair,
4103
0bcdcf28
CE
4104 .rq_online = rq_online_fair,
4105 .rq_offline = rq_offline_fair,
88ec22d3
PZ
4106
4107 .task_waking = task_waking_fair,
681f3e68 4108#endif
bf0f6f24 4109
83b699ed 4110 .set_curr_task = set_curr_task_fair,
bf0f6f24 4111 .task_tick = task_tick_fair,
cd29fe6f 4112 .task_fork = task_fork_fair,
cb469845
SR
4113
4114 .prio_changed = prio_changed_fair,
4115 .switched_to = switched_to_fair,
810b3817 4116
0d721cea
PW
4117 .get_rr_interval = get_rr_interval_fair,
4118
810b3817 4119#ifdef CONFIG_FAIR_GROUP_SCHED
b2b5ce02 4120 .task_move_group = task_move_group_fair,
810b3817 4121#endif
bf0f6f24
IM
4122};
4123
4124#ifdef CONFIG_SCHED_DEBUG
5cef9eca 4125static void print_cfs_stats(struct seq_file *m, int cpu)
bf0f6f24 4126{
bf0f6f24
IM
4127 struct cfs_rq *cfs_rq;
4128
5973e5b9 4129 rcu_read_lock();
c3b64f1e 4130 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
5cef9eca 4131 print_cfs_rq(m, cpu, cfs_rq);
5973e5b9 4132 rcu_read_unlock();
bf0f6f24
IM
4133}
4134#endif