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