sched: Maintain the load contribution of blocked entities
[deliverable/linux.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>
029632fb
PZ
26#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
9745512c 33
bf0f6f24 34/*
21805085 35 * Targeted preemption latency for CPU-bound tasks:
864616ee 36 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24 37 *
21805085 38 * NOTE: this latency value is not the same as the concept of
d274a4ce
IM
39 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
bf0f6f24 42 *
d274a4ce
IM
43 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
bf0f6f24 45 */
21406928
MG
46unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
2bd8e6d4 48
1983a922
CE
49/*
50 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
2bd8e6d4 61/*
b2be5e96 62 * Minimal preemption granularity for CPU-bound tasks:
864616ee 63 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
2bd8e6d4 64 */
0bf377bb
IM
65unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
21805085
PZ
67
68/*
b2be5e96
PZ
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
0bf377bb 71static unsigned int sched_nr_latency = 8;
b2be5e96
PZ
72
73/*
2bba22c5 74 * After fork, child runs first. If set to 0 (default) then
b2be5e96 75 * parent will (try to) run first.
21805085 76 */
2bba22c5 77unsigned int sysctl_sched_child_runs_first __read_mostly;
bf0f6f24 78
bf0f6f24
IM
79/*
80 * SCHED_OTHER wake-up granularity.
172e082a 81 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24
IM
82 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
172e082a 87unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
0bcdcf28 88unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
bf0f6f24 89
da84d961
IM
90const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
a7a4f8a7
PT
92/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
ec12cb7f
PT
99#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
029632fb
PZ
113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
a4c2f00f 217
bf0f6f24
IM
218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
62160e3f 222#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24 223
62160e3f 224/* cpu runqueue to which this cfs_rq is attached */
bf0f6f24
IM
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
62160e3f 227 return cfs_rq->rq;
bf0f6f24
IM
228}
229
62160e3f
IM
230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
bf0f6f24 232
8f48894f
PZ
233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
b758149c
PZ
241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
9ee474f5
PT
262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq);
263
3d4b47b4
PZ
264static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
265{
266 if (!cfs_rq->on_list) {
67e86250
PT
267 /*
268 * Ensure we either appear before our parent (if already
269 * enqueued) or force our parent to appear after us when it is
270 * enqueued. The fact that we always enqueue bottom-up
271 * reduces this to two cases.
272 */
273 if (cfs_rq->tg->parent &&
274 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
275 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
276 &rq_of(cfs_rq)->leaf_cfs_rq_list);
277 } else {
278 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
3d4b47b4 279 &rq_of(cfs_rq)->leaf_cfs_rq_list);
67e86250 280 }
3d4b47b4
PZ
281
282 cfs_rq->on_list = 1;
9ee474f5
PT
283 /* We should have no load, but we need to update last_decay. */
284 update_cfs_rq_blocked_load(cfs_rq);
3d4b47b4
PZ
285 }
286}
287
288static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
289{
290 if (cfs_rq->on_list) {
291 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
292 cfs_rq->on_list = 0;
293 }
294}
295
b758149c
PZ
296/* Iterate thr' all leaf cfs_rq's on a runqueue */
297#define for_each_leaf_cfs_rq(rq, cfs_rq) \
298 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
299
300/* Do the two (enqueued) entities belong to the same group ? */
301static inline int
302is_same_group(struct sched_entity *se, struct sched_entity *pse)
303{
304 if (se->cfs_rq == pse->cfs_rq)
305 return 1;
306
307 return 0;
308}
309
310static inline struct sched_entity *parent_entity(struct sched_entity *se)
311{
312 return se->parent;
313}
314
464b7527
PZ
315/* return depth at which a sched entity is present in the hierarchy */
316static inline int depth_se(struct sched_entity *se)
317{
318 int depth = 0;
319
320 for_each_sched_entity(se)
321 depth++;
322
323 return depth;
324}
325
326static void
327find_matching_se(struct sched_entity **se, struct sched_entity **pse)
328{
329 int se_depth, pse_depth;
330
331 /*
332 * preemption test can be made between sibling entities who are in the
333 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
334 * both tasks until we find their ancestors who are siblings of common
335 * parent.
336 */
337
338 /* First walk up until both entities are at same depth */
339 se_depth = depth_se(*se);
340 pse_depth = depth_se(*pse);
341
342 while (se_depth > pse_depth) {
343 se_depth--;
344 *se = parent_entity(*se);
345 }
346
347 while (pse_depth > se_depth) {
348 pse_depth--;
349 *pse = parent_entity(*pse);
350 }
351
352 while (!is_same_group(*se, *pse)) {
353 *se = parent_entity(*se);
354 *pse = parent_entity(*pse);
355 }
356}
357
8f48894f
PZ
358#else /* !CONFIG_FAIR_GROUP_SCHED */
359
360static inline struct task_struct *task_of(struct sched_entity *se)
361{
362 return container_of(se, struct task_struct, se);
363}
bf0f6f24 364
62160e3f
IM
365static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
366{
367 return container_of(cfs_rq, struct rq, cfs);
bf0f6f24
IM
368}
369
370#define entity_is_task(se) 1
371
b758149c
PZ
372#define for_each_sched_entity(se) \
373 for (; se; se = NULL)
bf0f6f24 374
b758149c 375static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
bf0f6f24 376{
b758149c 377 return &task_rq(p)->cfs;
bf0f6f24
IM
378}
379
b758149c
PZ
380static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
381{
382 struct task_struct *p = task_of(se);
383 struct rq *rq = task_rq(p);
384
385 return &rq->cfs;
386}
387
388/* runqueue "owned" by this group */
389static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
390{
391 return NULL;
392}
393
3d4b47b4
PZ
394static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
395{
396}
397
398static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
399{
400}
401
b758149c
PZ
402#define for_each_leaf_cfs_rq(rq, cfs_rq) \
403 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
404
405static inline int
406is_same_group(struct sched_entity *se, struct sched_entity *pse)
407{
408 return 1;
409}
410
411static inline struct sched_entity *parent_entity(struct sched_entity *se)
412{
413 return NULL;
414}
415
464b7527
PZ
416static inline void
417find_matching_se(struct sched_entity **se, struct sched_entity **pse)
418{
419}
420
b758149c
PZ
421#endif /* CONFIG_FAIR_GROUP_SCHED */
422
6c16a6dc
PZ
423static __always_inline
424void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
bf0f6f24
IM
425
426/**************************************************************
427 * Scheduling class tree data structure manipulation methods:
428 */
429
0702e3eb 430static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
02e0431a 431{
368059a9
PZ
432 s64 delta = (s64)(vruntime - min_vruntime);
433 if (delta > 0)
02e0431a
PZ
434 min_vruntime = vruntime;
435
436 return min_vruntime;
437}
438
0702e3eb 439static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
b0ffd246
PZ
440{
441 s64 delta = (s64)(vruntime - min_vruntime);
442 if (delta < 0)
443 min_vruntime = vruntime;
444
445 return min_vruntime;
446}
447
54fdc581
FC
448static inline int entity_before(struct sched_entity *a,
449 struct sched_entity *b)
450{
451 return (s64)(a->vruntime - b->vruntime) < 0;
452}
453
1af5f730
PZ
454static void update_min_vruntime(struct cfs_rq *cfs_rq)
455{
456 u64 vruntime = cfs_rq->min_vruntime;
457
458 if (cfs_rq->curr)
459 vruntime = cfs_rq->curr->vruntime;
460
461 if (cfs_rq->rb_leftmost) {
462 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
463 struct sched_entity,
464 run_node);
465
e17036da 466 if (!cfs_rq->curr)
1af5f730
PZ
467 vruntime = se->vruntime;
468 else
469 vruntime = min_vruntime(vruntime, se->vruntime);
470 }
471
472 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
3fe1698b
PZ
473#ifndef CONFIG_64BIT
474 smp_wmb();
475 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
476#endif
1af5f730
PZ
477}
478
bf0f6f24
IM
479/*
480 * Enqueue an entity into the rb-tree:
481 */
0702e3eb 482static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
483{
484 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
485 struct rb_node *parent = NULL;
486 struct sched_entity *entry;
bf0f6f24
IM
487 int leftmost = 1;
488
489 /*
490 * Find the right place in the rbtree:
491 */
492 while (*link) {
493 parent = *link;
494 entry = rb_entry(parent, struct sched_entity, run_node);
495 /*
496 * We dont care about collisions. Nodes with
497 * the same key stay together.
498 */
2bd2d6f2 499 if (entity_before(se, entry)) {
bf0f6f24
IM
500 link = &parent->rb_left;
501 } else {
502 link = &parent->rb_right;
503 leftmost = 0;
504 }
505 }
506
507 /*
508 * Maintain a cache of leftmost tree entries (it is frequently
509 * used):
510 */
1af5f730 511 if (leftmost)
57cb499d 512 cfs_rq->rb_leftmost = &se->run_node;
bf0f6f24
IM
513
514 rb_link_node(&se->run_node, parent, link);
515 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24
IM
516}
517
0702e3eb 518static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 519{
3fe69747
PZ
520 if (cfs_rq->rb_leftmost == &se->run_node) {
521 struct rb_node *next_node;
3fe69747
PZ
522
523 next_node = rb_next(&se->run_node);
524 cfs_rq->rb_leftmost = next_node;
3fe69747 525 }
e9acbff6 526
bf0f6f24 527 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24
IM
528}
529
029632fb 530struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
bf0f6f24 531{
f4b6755f
PZ
532 struct rb_node *left = cfs_rq->rb_leftmost;
533
534 if (!left)
535 return NULL;
536
537 return rb_entry(left, struct sched_entity, run_node);
bf0f6f24
IM
538}
539
ac53db59
RR
540static struct sched_entity *__pick_next_entity(struct sched_entity *se)
541{
542 struct rb_node *next = rb_next(&se->run_node);
543
544 if (!next)
545 return NULL;
546
547 return rb_entry(next, struct sched_entity, run_node);
548}
549
550#ifdef CONFIG_SCHED_DEBUG
029632fb 551struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
aeb73b04 552{
7eee3e67 553 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
aeb73b04 554
70eee74b
BS
555 if (!last)
556 return NULL;
7eee3e67
IM
557
558 return rb_entry(last, struct sched_entity, run_node);
aeb73b04
PZ
559}
560
bf0f6f24
IM
561/**************************************************************
562 * Scheduling class statistics methods:
563 */
564
acb4a848 565int sched_proc_update_handler(struct ctl_table *table, int write,
8d65af78 566 void __user *buffer, size_t *lenp,
b2be5e96
PZ
567 loff_t *ppos)
568{
8d65af78 569 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
acb4a848 570 int factor = get_update_sysctl_factor();
b2be5e96
PZ
571
572 if (ret || !write)
573 return ret;
574
575 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
576 sysctl_sched_min_granularity);
577
acb4a848
CE
578#define WRT_SYSCTL(name) \
579 (normalized_sysctl_##name = sysctl_##name / (factor))
580 WRT_SYSCTL(sched_min_granularity);
581 WRT_SYSCTL(sched_latency);
582 WRT_SYSCTL(sched_wakeup_granularity);
acb4a848
CE
583#undef WRT_SYSCTL
584
b2be5e96
PZ
585 return 0;
586}
587#endif
647e7cac 588
a7be37ac 589/*
f9c0b095 590 * delta /= w
a7be37ac
PZ
591 */
592static inline unsigned long
593calc_delta_fair(unsigned long delta, struct sched_entity *se)
594{
f9c0b095
PZ
595 if (unlikely(se->load.weight != NICE_0_LOAD))
596 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
a7be37ac
PZ
597
598 return delta;
599}
600
647e7cac
IM
601/*
602 * The idea is to set a period in which each task runs once.
603 *
532b1858 604 * When there are too many tasks (sched_nr_latency) we have to stretch
647e7cac
IM
605 * this period because otherwise the slices get too small.
606 *
607 * p = (nr <= nl) ? l : l*nr/nl
608 */
4d78e7b6
PZ
609static u64 __sched_period(unsigned long nr_running)
610{
611 u64 period = sysctl_sched_latency;
b2be5e96 612 unsigned long nr_latency = sched_nr_latency;
4d78e7b6
PZ
613
614 if (unlikely(nr_running > nr_latency)) {
4bf0b771 615 period = sysctl_sched_min_granularity;
4d78e7b6 616 period *= nr_running;
4d78e7b6
PZ
617 }
618
619 return period;
620}
621
647e7cac
IM
622/*
623 * We calculate the wall-time slice from the period by taking a part
624 * proportional to the weight.
625 *
f9c0b095 626 * s = p*P[w/rw]
647e7cac 627 */
6d0f0ebd 628static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
21805085 629{
0a582440 630 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
f9c0b095 631
0a582440 632 for_each_sched_entity(se) {
6272d68c 633 struct load_weight *load;
3104bf03 634 struct load_weight lw;
6272d68c
LM
635
636 cfs_rq = cfs_rq_of(se);
637 load = &cfs_rq->load;
f9c0b095 638
0a582440 639 if (unlikely(!se->on_rq)) {
3104bf03 640 lw = cfs_rq->load;
0a582440
MG
641
642 update_load_add(&lw, se->load.weight);
643 load = &lw;
644 }
645 slice = calc_delta_mine(slice, se->load.weight, load);
646 }
647 return slice;
bf0f6f24
IM
648}
649
647e7cac 650/*
ac884dec 651 * We calculate the vruntime slice of a to be inserted task
647e7cac 652 *
f9c0b095 653 * vs = s/w
647e7cac 654 */
f9c0b095 655static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
67e9fb2a 656{
f9c0b095 657 return calc_delta_fair(sched_slice(cfs_rq, se), se);
a7be37ac
PZ
658}
659
d6b55918 660static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
6d5ab293 661static void update_cfs_shares(struct cfs_rq *cfs_rq);
3b3d190e 662
bf0f6f24
IM
663/*
664 * Update the current task's runtime statistics. Skip current tasks that
665 * are not in our scheduling class.
666 */
667static inline void
8ebc91d9
IM
668__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
669 unsigned long delta_exec)
bf0f6f24 670{
bbdba7c0 671 unsigned long delta_exec_weighted;
bf0f6f24 672
41acab88
LDM
673 schedstat_set(curr->statistics.exec_max,
674 max((u64)delta_exec, curr->statistics.exec_max));
bf0f6f24
IM
675
676 curr->sum_exec_runtime += delta_exec;
7a62eabc 677 schedstat_add(cfs_rq, exec_clock, delta_exec);
a7be37ac 678 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
88ec22d3 679
e9acbff6 680 curr->vruntime += delta_exec_weighted;
1af5f730 681 update_min_vruntime(cfs_rq);
3b3d190e 682
70caf8a6 683#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
3b3d190e 684 cfs_rq->load_unacc_exec_time += delta_exec;
3b3d190e 685#endif
bf0f6f24
IM
686}
687
b7cc0896 688static void update_curr(struct cfs_rq *cfs_rq)
bf0f6f24 689{
429d43bc 690 struct sched_entity *curr = cfs_rq->curr;
305e6835 691 u64 now = rq_of(cfs_rq)->clock_task;
bf0f6f24
IM
692 unsigned long delta_exec;
693
694 if (unlikely(!curr))
695 return;
696
697 /*
698 * Get the amount of time the current task was running
699 * since the last time we changed load (this cannot
700 * overflow on 32 bits):
701 */
8ebc91d9 702 delta_exec = (unsigned long)(now - curr->exec_start);
34f28ecd
PZ
703 if (!delta_exec)
704 return;
bf0f6f24 705
8ebc91d9
IM
706 __update_curr(cfs_rq, curr, delta_exec);
707 curr->exec_start = now;
d842de87
SV
708
709 if (entity_is_task(curr)) {
710 struct task_struct *curtask = task_of(curr);
711
f977bb49 712 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
d842de87 713 cpuacct_charge(curtask, delta_exec);
f06febc9 714 account_group_exec_runtime(curtask, delta_exec);
d842de87 715 }
ec12cb7f
PT
716
717 account_cfs_rq_runtime(cfs_rq, delta_exec);
bf0f6f24
IM
718}
719
720static inline void
5870db5b 721update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 722{
41acab88 723 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
bf0f6f24
IM
724}
725
bf0f6f24
IM
726/*
727 * Task is being enqueued - update stats:
728 */
d2417e5a 729static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 730{
bf0f6f24
IM
731 /*
732 * Are we enqueueing a waiting task? (for current tasks
733 * a dequeue/enqueue event is a NOP)
734 */
429d43bc 735 if (se != cfs_rq->curr)
5870db5b 736 update_stats_wait_start(cfs_rq, se);
bf0f6f24
IM
737}
738
bf0f6f24 739static void
9ef0a961 740update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 741{
41acab88
LDM
742 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
743 rq_of(cfs_rq)->clock - se->statistics.wait_start));
744 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
745 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
746 rq_of(cfs_rq)->clock - se->statistics.wait_start);
768d0c27
PZ
747#ifdef CONFIG_SCHEDSTATS
748 if (entity_is_task(se)) {
749 trace_sched_stat_wait(task_of(se),
41acab88 750 rq_of(cfs_rq)->clock - se->statistics.wait_start);
768d0c27
PZ
751 }
752#endif
41acab88 753 schedstat_set(se->statistics.wait_start, 0);
bf0f6f24
IM
754}
755
756static inline void
19b6a2e3 757update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 758{
bf0f6f24
IM
759 /*
760 * Mark the end of the wait period if dequeueing a
761 * waiting task:
762 */
429d43bc 763 if (se != cfs_rq->curr)
9ef0a961 764 update_stats_wait_end(cfs_rq, se);
bf0f6f24
IM
765}
766
767/*
768 * We are picking a new current task - update its stats:
769 */
770static inline void
79303e9e 771update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
772{
773 /*
774 * We are starting a new run period:
775 */
305e6835 776 se->exec_start = rq_of(cfs_rq)->clock_task;
bf0f6f24
IM
777}
778
bf0f6f24
IM
779/**************************************************
780 * Scheduling class queueing methods:
781 */
782
30cfdcfc
DA
783static void
784account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
785{
786 update_load_add(&cfs_rq->load, se->load.weight);
c09595f6 787 if (!parent_entity(se))
029632fb 788 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
367456c7
PZ
789#ifdef CONFIG_SMP
790 if (entity_is_task(se))
eb95308e 791 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
367456c7 792#endif
30cfdcfc 793 cfs_rq->nr_running++;
30cfdcfc
DA
794}
795
796static void
797account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
798{
799 update_load_sub(&cfs_rq->load, se->load.weight);
c09595f6 800 if (!parent_entity(se))
029632fb 801 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
367456c7 802 if (entity_is_task(se))
b87f1724 803 list_del_init(&se->group_node);
30cfdcfc 804 cfs_rq->nr_running--;
30cfdcfc
DA
805}
806
3ff6dcac 807#ifdef CONFIG_FAIR_GROUP_SCHED
64660c86
PT
808/* we need this in update_cfs_load and load-balance functions below */
809static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
3ff6dcac 810# ifdef CONFIG_SMP
d6b55918
PT
811static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
812 int global_update)
813{
814 struct task_group *tg = cfs_rq->tg;
815 long load_avg;
816
817 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
818 load_avg -= cfs_rq->load_contribution;
819
820 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
821 atomic_add(load_avg, &tg->load_weight);
822 cfs_rq->load_contribution += load_avg;
823 }
824}
825
826static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
2069dd75 827{
a7a4f8a7 828 u64 period = sysctl_sched_shares_window;
2069dd75 829 u64 now, delta;
e33078ba 830 unsigned long load = cfs_rq->load.weight;
2069dd75 831
64660c86 832 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
2069dd75
PZ
833 return;
834
05ca62c6 835 now = rq_of(cfs_rq)->clock_task;
2069dd75
PZ
836 delta = now - cfs_rq->load_stamp;
837
e33078ba
PT
838 /* truncate load history at 4 idle periods */
839 if (cfs_rq->load_stamp > cfs_rq->load_last &&
840 now - cfs_rq->load_last > 4 * period) {
841 cfs_rq->load_period = 0;
842 cfs_rq->load_avg = 0;
f07333bf 843 delta = period - 1;
e33078ba
PT
844 }
845
2069dd75 846 cfs_rq->load_stamp = now;
3b3d190e 847 cfs_rq->load_unacc_exec_time = 0;
2069dd75 848 cfs_rq->load_period += delta;
e33078ba
PT
849 if (load) {
850 cfs_rq->load_last = now;
851 cfs_rq->load_avg += delta * load;
852 }
2069dd75 853
d6b55918
PT
854 /* consider updating load contribution on each fold or truncate */
855 if (global_update || cfs_rq->load_period > period
856 || !cfs_rq->load_period)
857 update_cfs_rq_load_contribution(cfs_rq, global_update);
858
2069dd75
PZ
859 while (cfs_rq->load_period > period) {
860 /*
861 * Inline assembly required to prevent the compiler
862 * optimising this loop into a divmod call.
863 * See __iter_div_u64_rem() for another example of this.
864 */
865 asm("" : "+rm" (cfs_rq->load_period));
866 cfs_rq->load_period /= 2;
867 cfs_rq->load_avg /= 2;
868 }
3d4b47b4 869
e33078ba
PT
870 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
871 list_del_leaf_cfs_rq(cfs_rq);
2069dd75
PZ
872}
873
cf5f0acf
PZ
874static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
875{
876 long tg_weight;
877
878 /*
879 * Use this CPU's actual weight instead of the last load_contribution
880 * to gain a more accurate current total weight. See
881 * update_cfs_rq_load_contribution().
882 */
883 tg_weight = atomic_read(&tg->load_weight);
884 tg_weight -= cfs_rq->load_contribution;
885 tg_weight += cfs_rq->load.weight;
886
887 return tg_weight;
888}
889
6d5ab293 890static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
3ff6dcac 891{
cf5f0acf 892 long tg_weight, load, shares;
3ff6dcac 893
cf5f0acf 894 tg_weight = calc_tg_weight(tg, cfs_rq);
6d5ab293 895 load = cfs_rq->load.weight;
3ff6dcac 896
3ff6dcac 897 shares = (tg->shares * load);
cf5f0acf
PZ
898 if (tg_weight)
899 shares /= tg_weight;
3ff6dcac
YZ
900
901 if (shares < MIN_SHARES)
902 shares = MIN_SHARES;
903 if (shares > tg->shares)
904 shares = tg->shares;
905
906 return shares;
907}
908
909static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
910{
911 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
912 update_cfs_load(cfs_rq, 0);
6d5ab293 913 update_cfs_shares(cfs_rq);
3ff6dcac
YZ
914 }
915}
916# else /* CONFIG_SMP */
917static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
918{
919}
920
6d5ab293 921static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
3ff6dcac
YZ
922{
923 return tg->shares;
924}
925
926static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
927{
928}
929# endif /* CONFIG_SMP */
2069dd75
PZ
930static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
931 unsigned long weight)
932{
19e5eebb
PT
933 if (se->on_rq) {
934 /* commit outstanding execution time */
935 if (cfs_rq->curr == se)
936 update_curr(cfs_rq);
2069dd75 937 account_entity_dequeue(cfs_rq, se);
19e5eebb 938 }
2069dd75
PZ
939
940 update_load_set(&se->load, weight);
941
942 if (se->on_rq)
943 account_entity_enqueue(cfs_rq, se);
944}
945
6d5ab293 946static void update_cfs_shares(struct cfs_rq *cfs_rq)
2069dd75
PZ
947{
948 struct task_group *tg;
949 struct sched_entity *se;
3ff6dcac 950 long shares;
2069dd75 951
2069dd75
PZ
952 tg = cfs_rq->tg;
953 se = tg->se[cpu_of(rq_of(cfs_rq))];
64660c86 954 if (!se || throttled_hierarchy(cfs_rq))
2069dd75 955 return;
3ff6dcac
YZ
956#ifndef CONFIG_SMP
957 if (likely(se->load.weight == tg->shares))
958 return;
959#endif
6d5ab293 960 shares = calc_cfs_shares(cfs_rq, tg);
2069dd75
PZ
961
962 reweight_entity(cfs_rq_of(se), se, shares);
963}
964#else /* CONFIG_FAIR_GROUP_SCHED */
d6b55918 965static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
2069dd75
PZ
966{
967}
968
6d5ab293 969static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
2069dd75
PZ
970{
971}
43365bd7
PT
972
973static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
974{
975}
2069dd75
PZ
976#endif /* CONFIG_FAIR_GROUP_SCHED */
977
9d85f21c
PT
978#ifdef CONFIG_SMP
979/*
980 * Approximate:
981 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
982 */
983static __always_inline u64 decay_load(u64 val, u64 n)
984{
985 for (; n && val; n--) {
986 val *= 4008;
987 val >>= 12;
988 }
989
990 return val;
991}
992
993/*
994 * We can represent the historical contribution to runnable average as the
995 * coefficients of a geometric series. To do this we sub-divide our runnable
996 * history into segments of approximately 1ms (1024us); label the segment that
997 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
998 *
999 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
1000 * p0 p1 p2
1001 * (now) (~1ms ago) (~2ms ago)
1002 *
1003 * Let u_i denote the fraction of p_i that the entity was runnable.
1004 *
1005 * We then designate the fractions u_i as our co-efficients, yielding the
1006 * following representation of historical load:
1007 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
1008 *
1009 * We choose y based on the with of a reasonably scheduling period, fixing:
1010 * y^32 = 0.5
1011 *
1012 * This means that the contribution to load ~32ms ago (u_32) will be weighted
1013 * approximately half as much as the contribution to load within the last ms
1014 * (u_0).
1015 *
1016 * When a period "rolls over" and we have new u_0`, multiplying the previous
1017 * sum again by y is sufficient to update:
1018 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1019 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1020 */
1021static __always_inline int __update_entity_runnable_avg(u64 now,
1022 struct sched_avg *sa,
1023 int runnable)
1024{
1025 u64 delta;
1026 int delta_w, decayed = 0;
1027
1028 delta = now - sa->last_runnable_update;
1029 /*
1030 * This should only happen when time goes backwards, which it
1031 * unfortunately does during sched clock init when we swap over to TSC.
1032 */
1033 if ((s64)delta < 0) {
1034 sa->last_runnable_update = now;
1035 return 0;
1036 }
1037
1038 /*
1039 * Use 1024ns as the unit of measurement since it's a reasonable
1040 * approximation of 1us and fast to compute.
1041 */
1042 delta >>= 10;
1043 if (!delta)
1044 return 0;
1045 sa->last_runnable_update = now;
1046
1047 /* delta_w is the amount already accumulated against our next period */
1048 delta_w = sa->runnable_avg_period % 1024;
1049 if (delta + delta_w >= 1024) {
1050 /* period roll-over */
1051 decayed = 1;
1052
1053 /*
1054 * Now that we know we're crossing a period boundary, figure
1055 * out how much from delta we need to complete the current
1056 * period and accrue it.
1057 */
1058 delta_w = 1024 - delta_w;
1059 BUG_ON(delta_w > delta);
1060 do {
1061 if (runnable)
1062 sa->runnable_avg_sum += delta_w;
1063 sa->runnable_avg_period += delta_w;
1064
1065 /*
1066 * Remainder of delta initiates a new period, roll over
1067 * the previous.
1068 */
1069 sa->runnable_avg_sum =
1070 decay_load(sa->runnable_avg_sum, 1);
1071 sa->runnable_avg_period =
1072 decay_load(sa->runnable_avg_period, 1);
1073
1074 delta -= delta_w;
1075 /* New period is empty */
1076 delta_w = 1024;
1077 } while (delta >= 1024);
1078 }
1079
1080 /* Remainder of delta accrued against u_0` */
1081 if (runnable)
1082 sa->runnable_avg_sum += delta;
1083 sa->runnable_avg_period += delta;
1084
1085 return decayed;
1086}
1087
9ee474f5
PT
1088/* Synchronize an entity's decay with its parenting cfs_rq.*/
1089static inline void __synchronize_entity_decay(struct sched_entity *se)
1090{
1091 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1092 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1093
1094 decays -= se->avg.decay_count;
1095 if (!decays)
1096 return;
1097
1098 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1099 se->avg.decay_count = 0;
1100}
1101
2dac754e
PT
1102/* Compute the current contribution to load_avg by se, return any delta */
1103static long __update_entity_load_avg_contrib(struct sched_entity *se)
1104{
1105 long old_contrib = se->avg.load_avg_contrib;
1106
1107 if (!entity_is_task(se))
1108 return 0;
1109
1110 se->avg.load_avg_contrib = div64_u64(se->avg.runnable_avg_sum *
1111 se->load.weight,
1112 se->avg.runnable_avg_period + 1);
1113
1114 return se->avg.load_avg_contrib - old_contrib;
1115}
1116
9ee474f5
PT
1117static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1118 long load_contrib)
1119{
1120 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1121 cfs_rq->blocked_load_avg -= load_contrib;
1122 else
1123 cfs_rq->blocked_load_avg = 0;
1124}
1125
9d85f21c 1126/* Update a sched_entity's runnable average */
9ee474f5
PT
1127static inline void update_entity_load_avg(struct sched_entity *se,
1128 int update_cfs_rq)
9d85f21c 1129{
2dac754e
PT
1130 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1131 long contrib_delta;
1132
1133 if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
1134 se->on_rq))
1135 return;
1136
1137 contrib_delta = __update_entity_load_avg_contrib(se);
9ee474f5
PT
1138
1139 if (!update_cfs_rq)
1140 return;
1141
2dac754e
PT
1142 if (se->on_rq)
1143 cfs_rq->runnable_load_avg += contrib_delta;
9ee474f5
PT
1144 else
1145 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1146}
1147
1148/*
1149 * Decay the load contributed by all blocked children and account this so that
1150 * their contribution may appropriately discounted when they wake up.
1151 */
1152static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq)
1153{
1154 u64 now = rq_of(cfs_rq)->clock_task >> 20;
1155 u64 decays;
1156
1157 decays = now - cfs_rq->last_decay;
1158 if (!decays)
1159 return;
1160
1161 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1162 decays);
1163 atomic64_add(decays, &cfs_rq->decay_counter);
1164
1165 cfs_rq->last_decay = now;
9d85f21c 1166}
18bf2805
BS
1167
1168static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1169{
1170 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
1171}
2dac754e
PT
1172
1173/* Add the load generated by se into cfs_rq's child load-average */
1174static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
9ee474f5
PT
1175 struct sched_entity *se,
1176 int wakeup)
2dac754e 1177{
9ee474f5
PT
1178 /* we track migrations using entity decay_count == 0 */
1179 if (unlikely(!se->avg.decay_count)) {
1180 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
1181 wakeup = 0;
1182 } else {
1183 __synchronize_entity_decay(se);
1184 }
1185
1186 if (wakeup)
1187 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
1188
1189 update_entity_load_avg(se, 0);
2dac754e 1190 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
9ee474f5 1191 update_cfs_rq_blocked_load(cfs_rq);
2dac754e
PT
1192}
1193
9ee474f5
PT
1194/*
1195 * Remove se's load from this cfs_rq child load-average, if the entity is
1196 * transitioning to a blocked state we track its projected decay using
1197 * blocked_load_avg.
1198 */
2dac754e 1199static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
9ee474f5
PT
1200 struct sched_entity *se,
1201 int sleep)
2dac754e 1202{
9ee474f5
PT
1203 update_entity_load_avg(se, 1);
1204
2dac754e 1205 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
9ee474f5
PT
1206 if (sleep) {
1207 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1208 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1209 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
2dac754e 1210}
9d85f21c 1211#else
9ee474f5
PT
1212static inline void update_entity_load_avg(struct sched_entity *se,
1213 int update_cfs_rq) {}
18bf2805 1214static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
2dac754e 1215static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
9ee474f5
PT
1216 struct sched_entity *se,
1217 int wakeup) {}
2dac754e 1218static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
9ee474f5
PT
1219 struct sched_entity *se,
1220 int sleep) {}
1221static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq) {}
9d85f21c
PT
1222#endif
1223
2396af69 1224static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 1225{
bf0f6f24 1226#ifdef CONFIG_SCHEDSTATS
e414314c
PZ
1227 struct task_struct *tsk = NULL;
1228
1229 if (entity_is_task(se))
1230 tsk = task_of(se);
1231
41acab88
LDM
1232 if (se->statistics.sleep_start) {
1233 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
bf0f6f24
IM
1234
1235 if ((s64)delta < 0)
1236 delta = 0;
1237
41acab88
LDM
1238 if (unlikely(delta > se->statistics.sleep_max))
1239 se->statistics.sleep_max = delta;
bf0f6f24 1240
8c79a045 1241 se->statistics.sleep_start = 0;
41acab88 1242 se->statistics.sum_sleep_runtime += delta;
9745512c 1243
768d0c27 1244 if (tsk) {
e414314c 1245 account_scheduler_latency(tsk, delta >> 10, 1);
768d0c27
PZ
1246 trace_sched_stat_sleep(tsk, delta);
1247 }
bf0f6f24 1248 }
41acab88
LDM
1249 if (se->statistics.block_start) {
1250 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
bf0f6f24
IM
1251
1252 if ((s64)delta < 0)
1253 delta = 0;
1254
41acab88
LDM
1255 if (unlikely(delta > se->statistics.block_max))
1256 se->statistics.block_max = delta;
bf0f6f24 1257
8c79a045 1258 se->statistics.block_start = 0;
41acab88 1259 se->statistics.sum_sleep_runtime += delta;
30084fbd 1260
e414314c 1261 if (tsk) {
8f0dfc34 1262 if (tsk->in_iowait) {
41acab88
LDM
1263 se->statistics.iowait_sum += delta;
1264 se->statistics.iowait_count++;
768d0c27 1265 trace_sched_stat_iowait(tsk, delta);
8f0dfc34
AV
1266 }
1267
b781a602
AV
1268 trace_sched_stat_blocked(tsk, delta);
1269
e414314c
PZ
1270 /*
1271 * Blocking time is in units of nanosecs, so shift by
1272 * 20 to get a milliseconds-range estimation of the
1273 * amount of time that the task spent sleeping:
1274 */
1275 if (unlikely(prof_on == SLEEP_PROFILING)) {
1276 profile_hits(SLEEP_PROFILING,
1277 (void *)get_wchan(tsk),
1278 delta >> 20);
1279 }
1280 account_scheduler_latency(tsk, delta >> 10, 0);
30084fbd 1281 }
bf0f6f24
IM
1282 }
1283#endif
1284}
1285
ddc97297
PZ
1286static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1287{
1288#ifdef CONFIG_SCHED_DEBUG
1289 s64 d = se->vruntime - cfs_rq->min_vruntime;
1290
1291 if (d < 0)
1292 d = -d;
1293
1294 if (d > 3*sysctl_sched_latency)
1295 schedstat_inc(cfs_rq, nr_spread_over);
1296#endif
1297}
1298
aeb73b04
PZ
1299static void
1300place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1301{
1af5f730 1302 u64 vruntime = cfs_rq->min_vruntime;
94dfb5e7 1303
2cb8600e
PZ
1304 /*
1305 * The 'current' period is already promised to the current tasks,
1306 * however the extra weight of the new task will slow them down a
1307 * little, place the new task so that it fits in the slot that
1308 * stays open at the end.
1309 */
94dfb5e7 1310 if (initial && sched_feat(START_DEBIT))
f9c0b095 1311 vruntime += sched_vslice(cfs_rq, se);
aeb73b04 1312
a2e7a7eb 1313 /* sleeps up to a single latency don't count. */
5ca9880c 1314 if (!initial) {
a2e7a7eb 1315 unsigned long thresh = sysctl_sched_latency;
a7be37ac 1316
a2e7a7eb
MG
1317 /*
1318 * Halve their sleep time's effect, to allow
1319 * for a gentler effect of sleepers:
1320 */
1321 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1322 thresh >>= 1;
51e0304c 1323
a2e7a7eb 1324 vruntime -= thresh;
aeb73b04
PZ
1325 }
1326
b5d9d734
MG
1327 /* ensure we never gain time by being placed backwards. */
1328 vruntime = max_vruntime(se->vruntime, vruntime);
1329
67e9fb2a 1330 se->vruntime = vruntime;
aeb73b04
PZ
1331}
1332
d3d9dc33
PT
1333static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1334
bf0f6f24 1335static void
88ec22d3 1336enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
bf0f6f24 1337{
88ec22d3
PZ
1338 /*
1339 * Update the normalized vruntime before updating min_vruntime
1340 * through callig update_curr().
1341 */
371fd7e7 1342 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
88ec22d3
PZ
1343 se->vruntime += cfs_rq->min_vruntime;
1344
bf0f6f24 1345 /*
a2a2d680 1346 * Update run-time statistics of the 'current'.
bf0f6f24 1347 */
b7cc0896 1348 update_curr(cfs_rq);
d6b55918 1349 update_cfs_load(cfs_rq, 0);
9ee474f5 1350 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
a992241d 1351 account_entity_enqueue(cfs_rq, se);
6d5ab293 1352 update_cfs_shares(cfs_rq);
bf0f6f24 1353
88ec22d3 1354 if (flags & ENQUEUE_WAKEUP) {
aeb73b04 1355 place_entity(cfs_rq, se, 0);
2396af69 1356 enqueue_sleeper(cfs_rq, se);
e9acbff6 1357 }
bf0f6f24 1358
d2417e5a 1359 update_stats_enqueue(cfs_rq, se);
ddc97297 1360 check_spread(cfs_rq, se);
83b699ed
SV
1361 if (se != cfs_rq->curr)
1362 __enqueue_entity(cfs_rq, se);
2069dd75 1363 se->on_rq = 1;
3d4b47b4 1364
d3d9dc33 1365 if (cfs_rq->nr_running == 1) {
3d4b47b4 1366 list_add_leaf_cfs_rq(cfs_rq);
d3d9dc33
PT
1367 check_enqueue_throttle(cfs_rq);
1368 }
bf0f6f24
IM
1369}
1370
2c13c919 1371static void __clear_buddies_last(struct sched_entity *se)
2002c695 1372{
2c13c919
RR
1373 for_each_sched_entity(se) {
1374 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1375 if (cfs_rq->last == se)
1376 cfs_rq->last = NULL;
1377 else
1378 break;
1379 }
1380}
2002c695 1381
2c13c919
RR
1382static void __clear_buddies_next(struct sched_entity *se)
1383{
1384 for_each_sched_entity(se) {
1385 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1386 if (cfs_rq->next == se)
1387 cfs_rq->next = NULL;
1388 else
1389 break;
1390 }
2002c695
PZ
1391}
1392
ac53db59
RR
1393static void __clear_buddies_skip(struct sched_entity *se)
1394{
1395 for_each_sched_entity(se) {
1396 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1397 if (cfs_rq->skip == se)
1398 cfs_rq->skip = NULL;
1399 else
1400 break;
1401 }
1402}
1403
a571bbea
PZ
1404static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1405{
2c13c919
RR
1406 if (cfs_rq->last == se)
1407 __clear_buddies_last(se);
1408
1409 if (cfs_rq->next == se)
1410 __clear_buddies_next(se);
ac53db59
RR
1411
1412 if (cfs_rq->skip == se)
1413 __clear_buddies_skip(se);
a571bbea
PZ
1414}
1415
6c16a6dc 1416static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
d8b4986d 1417
bf0f6f24 1418static void
371fd7e7 1419dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
bf0f6f24 1420{
a2a2d680
DA
1421 /*
1422 * Update run-time statistics of the 'current'.
1423 */
1424 update_curr(cfs_rq);
9ee474f5 1425 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
a2a2d680 1426
19b6a2e3 1427 update_stats_dequeue(cfs_rq, se);
371fd7e7 1428 if (flags & DEQUEUE_SLEEP) {
67e9fb2a 1429#ifdef CONFIG_SCHEDSTATS
bf0f6f24
IM
1430 if (entity_is_task(se)) {
1431 struct task_struct *tsk = task_of(se);
1432
1433 if (tsk->state & TASK_INTERRUPTIBLE)
41acab88 1434 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
bf0f6f24 1435 if (tsk->state & TASK_UNINTERRUPTIBLE)
41acab88 1436 se->statistics.block_start = rq_of(cfs_rq)->clock;
bf0f6f24 1437 }
db36cc7d 1438#endif
67e9fb2a
PZ
1439 }
1440
2002c695 1441 clear_buddies(cfs_rq, se);
4793241b 1442
83b699ed 1443 if (se != cfs_rq->curr)
30cfdcfc 1444 __dequeue_entity(cfs_rq, se);
2069dd75 1445 se->on_rq = 0;
d6b55918 1446 update_cfs_load(cfs_rq, 0);
30cfdcfc 1447 account_entity_dequeue(cfs_rq, se);
88ec22d3
PZ
1448
1449 /*
1450 * Normalize the entity after updating the min_vruntime because the
1451 * update can refer to the ->curr item and we need to reflect this
1452 * movement in our normalized position.
1453 */
371fd7e7 1454 if (!(flags & DEQUEUE_SLEEP))
88ec22d3 1455 se->vruntime -= cfs_rq->min_vruntime;
1e876231 1456
d8b4986d
PT
1457 /* return excess runtime on last dequeue */
1458 return_cfs_rq_runtime(cfs_rq);
1459
1e876231
PZ
1460 update_min_vruntime(cfs_rq);
1461 update_cfs_shares(cfs_rq);
bf0f6f24
IM
1462}
1463
1464/*
1465 * Preempt the current task with a newly woken task if needed:
1466 */
7c92e54f 1467static void
2e09bf55 1468check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
bf0f6f24 1469{
11697830 1470 unsigned long ideal_runtime, delta_exec;
f4cfb33e
WX
1471 struct sched_entity *se;
1472 s64 delta;
11697830 1473
6d0f0ebd 1474 ideal_runtime = sched_slice(cfs_rq, curr);
11697830 1475 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
a9f3e2b5 1476 if (delta_exec > ideal_runtime) {
bf0f6f24 1477 resched_task(rq_of(cfs_rq)->curr);
a9f3e2b5
MG
1478 /*
1479 * The current task ran long enough, ensure it doesn't get
1480 * re-elected due to buddy favours.
1481 */
1482 clear_buddies(cfs_rq, curr);
f685ceac
MG
1483 return;
1484 }
1485
1486 /*
1487 * Ensure that a task that missed wakeup preemption by a
1488 * narrow margin doesn't have to wait for a full slice.
1489 * This also mitigates buddy induced latencies under load.
1490 */
f685ceac
MG
1491 if (delta_exec < sysctl_sched_min_granularity)
1492 return;
1493
f4cfb33e
WX
1494 se = __pick_first_entity(cfs_rq);
1495 delta = curr->vruntime - se->vruntime;
f685ceac 1496
f4cfb33e
WX
1497 if (delta < 0)
1498 return;
d7d82944 1499
f4cfb33e
WX
1500 if (delta > ideal_runtime)
1501 resched_task(rq_of(cfs_rq)->curr);
bf0f6f24
IM
1502}
1503
83b699ed 1504static void
8494f412 1505set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 1506{
83b699ed
SV
1507 /* 'current' is not kept within the tree. */
1508 if (se->on_rq) {
1509 /*
1510 * Any task has to be enqueued before it get to execute on
1511 * a CPU. So account for the time it spent waiting on the
1512 * runqueue.
1513 */
1514 update_stats_wait_end(cfs_rq, se);
1515 __dequeue_entity(cfs_rq, se);
1516 }
1517
79303e9e 1518 update_stats_curr_start(cfs_rq, se);
429d43bc 1519 cfs_rq->curr = se;
eba1ed4b
IM
1520#ifdef CONFIG_SCHEDSTATS
1521 /*
1522 * Track our maximum slice length, if the CPU's load is at
1523 * least twice that of our own weight (i.e. dont track it
1524 * when there are only lesser-weight tasks around):
1525 */
495eca49 1526 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
41acab88 1527 se->statistics.slice_max = max(se->statistics.slice_max,
eba1ed4b
IM
1528 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1529 }
1530#endif
4a55b450 1531 se->prev_sum_exec_runtime = se->sum_exec_runtime;
bf0f6f24
IM
1532}
1533
3f3a4904
PZ
1534static int
1535wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1536
ac53db59
RR
1537/*
1538 * Pick the next process, keeping these things in mind, in this order:
1539 * 1) keep things fair between processes/task groups
1540 * 2) pick the "next" process, since someone really wants that to run
1541 * 3) pick the "last" process, for cache locality
1542 * 4) do not run the "skip" process, if something else is available
1543 */
f4b6755f 1544static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
aa2ac252 1545{
ac53db59 1546 struct sched_entity *se = __pick_first_entity(cfs_rq);
f685ceac 1547 struct sched_entity *left = se;
f4b6755f 1548
ac53db59
RR
1549 /*
1550 * Avoid running the skip buddy, if running something else can
1551 * be done without getting too unfair.
1552 */
1553 if (cfs_rq->skip == se) {
1554 struct sched_entity *second = __pick_next_entity(se);
1555 if (second && wakeup_preempt_entity(second, left) < 1)
1556 se = second;
1557 }
aa2ac252 1558
f685ceac
MG
1559 /*
1560 * Prefer last buddy, try to return the CPU to a preempted task.
1561 */
1562 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1563 se = cfs_rq->last;
1564
ac53db59
RR
1565 /*
1566 * Someone really wants this to run. If it's not unfair, run it.
1567 */
1568 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1569 se = cfs_rq->next;
1570
f685ceac 1571 clear_buddies(cfs_rq, se);
4793241b
PZ
1572
1573 return se;
aa2ac252
PZ
1574}
1575
d3d9dc33
PT
1576static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1577
ab6cde26 1578static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
bf0f6f24
IM
1579{
1580 /*
1581 * If still on the runqueue then deactivate_task()
1582 * was not called and update_curr() has to be done:
1583 */
1584 if (prev->on_rq)
b7cc0896 1585 update_curr(cfs_rq);
bf0f6f24 1586
d3d9dc33
PT
1587 /* throttle cfs_rqs exceeding runtime */
1588 check_cfs_rq_runtime(cfs_rq);
1589
ddc97297 1590 check_spread(cfs_rq, prev);
30cfdcfc 1591 if (prev->on_rq) {
5870db5b 1592 update_stats_wait_start(cfs_rq, prev);
30cfdcfc
DA
1593 /* Put 'current' back into the tree. */
1594 __enqueue_entity(cfs_rq, prev);
9d85f21c 1595 /* in !on_rq case, update occurred at dequeue */
9ee474f5 1596 update_entity_load_avg(prev, 1);
30cfdcfc 1597 }
429d43bc 1598 cfs_rq->curr = NULL;
bf0f6f24
IM
1599}
1600
8f4d37ec
PZ
1601static void
1602entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
bf0f6f24 1603{
bf0f6f24 1604 /*
30cfdcfc 1605 * Update run-time statistics of the 'current'.
bf0f6f24 1606 */
30cfdcfc 1607 update_curr(cfs_rq);
bf0f6f24 1608
9d85f21c
PT
1609 /*
1610 * Ensure that runnable average is periodically updated.
1611 */
9ee474f5
PT
1612 update_entity_load_avg(curr, 1);
1613 update_cfs_rq_blocked_load(cfs_rq);
9d85f21c 1614
43365bd7
PT
1615 /*
1616 * Update share accounting for long-running entities.
1617 */
1618 update_entity_shares_tick(cfs_rq);
1619
8f4d37ec
PZ
1620#ifdef CONFIG_SCHED_HRTICK
1621 /*
1622 * queued ticks are scheduled to match the slice, so don't bother
1623 * validating it and just reschedule.
1624 */
983ed7a6
HH
1625 if (queued) {
1626 resched_task(rq_of(cfs_rq)->curr);
1627 return;
1628 }
8f4d37ec
PZ
1629 /*
1630 * don't let the period tick interfere with the hrtick preemption
1631 */
1632 if (!sched_feat(DOUBLE_TICK) &&
1633 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1634 return;
1635#endif
1636
2c2efaed 1637 if (cfs_rq->nr_running > 1)
2e09bf55 1638 check_preempt_tick(cfs_rq, curr);
bf0f6f24
IM
1639}
1640
ab84d31e
PT
1641
1642/**************************************************
1643 * CFS bandwidth control machinery
1644 */
1645
1646#ifdef CONFIG_CFS_BANDWIDTH
029632fb
PZ
1647
1648#ifdef HAVE_JUMP_LABEL
c5905afb 1649static struct static_key __cfs_bandwidth_used;
029632fb
PZ
1650
1651static inline bool cfs_bandwidth_used(void)
1652{
c5905afb 1653 return static_key_false(&__cfs_bandwidth_used);
029632fb
PZ
1654}
1655
1656void account_cfs_bandwidth_used(int enabled, int was_enabled)
1657{
1658 /* only need to count groups transitioning between enabled/!enabled */
1659 if (enabled && !was_enabled)
c5905afb 1660 static_key_slow_inc(&__cfs_bandwidth_used);
029632fb 1661 else if (!enabled && was_enabled)
c5905afb 1662 static_key_slow_dec(&__cfs_bandwidth_used);
029632fb
PZ
1663}
1664#else /* HAVE_JUMP_LABEL */
1665static bool cfs_bandwidth_used(void)
1666{
1667 return true;
1668}
1669
1670void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1671#endif /* HAVE_JUMP_LABEL */
1672
ab84d31e
PT
1673/*
1674 * default period for cfs group bandwidth.
1675 * default: 0.1s, units: nanoseconds
1676 */
1677static inline u64 default_cfs_period(void)
1678{
1679 return 100000000ULL;
1680}
ec12cb7f
PT
1681
1682static inline u64 sched_cfs_bandwidth_slice(void)
1683{
1684 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1685}
1686
a9cf55b2
PT
1687/*
1688 * Replenish runtime according to assigned quota and update expiration time.
1689 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1690 * additional synchronization around rq->lock.
1691 *
1692 * requires cfs_b->lock
1693 */
029632fb 1694void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
a9cf55b2
PT
1695{
1696 u64 now;
1697
1698 if (cfs_b->quota == RUNTIME_INF)
1699 return;
1700
1701 now = sched_clock_cpu(smp_processor_id());
1702 cfs_b->runtime = cfs_b->quota;
1703 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1704}
1705
029632fb
PZ
1706static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1707{
1708 return &tg->cfs_bandwidth;
1709}
1710
85dac906
PT
1711/* returns 0 on failure to allocate runtime */
1712static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
ec12cb7f
PT
1713{
1714 struct task_group *tg = cfs_rq->tg;
1715 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
a9cf55b2 1716 u64 amount = 0, min_amount, expires;
ec12cb7f
PT
1717
1718 /* note: this is a positive sum as runtime_remaining <= 0 */
1719 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1720
1721 raw_spin_lock(&cfs_b->lock);
1722 if (cfs_b->quota == RUNTIME_INF)
1723 amount = min_amount;
58088ad0 1724 else {
a9cf55b2
PT
1725 /*
1726 * If the bandwidth pool has become inactive, then at least one
1727 * period must have elapsed since the last consumption.
1728 * Refresh the global state and ensure bandwidth timer becomes
1729 * active.
1730 */
1731 if (!cfs_b->timer_active) {
1732 __refill_cfs_bandwidth_runtime(cfs_b);
58088ad0 1733 __start_cfs_bandwidth(cfs_b);
a9cf55b2 1734 }
58088ad0
PT
1735
1736 if (cfs_b->runtime > 0) {
1737 amount = min(cfs_b->runtime, min_amount);
1738 cfs_b->runtime -= amount;
1739 cfs_b->idle = 0;
1740 }
ec12cb7f 1741 }
a9cf55b2 1742 expires = cfs_b->runtime_expires;
ec12cb7f
PT
1743 raw_spin_unlock(&cfs_b->lock);
1744
1745 cfs_rq->runtime_remaining += amount;
a9cf55b2
PT
1746 /*
1747 * we may have advanced our local expiration to account for allowed
1748 * spread between our sched_clock and the one on which runtime was
1749 * issued.
1750 */
1751 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1752 cfs_rq->runtime_expires = expires;
85dac906
PT
1753
1754 return cfs_rq->runtime_remaining > 0;
ec12cb7f
PT
1755}
1756
a9cf55b2
PT
1757/*
1758 * Note: This depends on the synchronization provided by sched_clock and the
1759 * fact that rq->clock snapshots this value.
1760 */
1761static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
ec12cb7f 1762{
a9cf55b2
PT
1763 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1764 struct rq *rq = rq_of(cfs_rq);
1765
1766 /* if the deadline is ahead of our clock, nothing to do */
1767 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
ec12cb7f
PT
1768 return;
1769
a9cf55b2
PT
1770 if (cfs_rq->runtime_remaining < 0)
1771 return;
1772
1773 /*
1774 * If the local deadline has passed we have to consider the
1775 * possibility that our sched_clock is 'fast' and the global deadline
1776 * has not truly expired.
1777 *
1778 * Fortunately we can check determine whether this the case by checking
1779 * whether the global deadline has advanced.
1780 */
1781
1782 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1783 /* extend local deadline, drift is bounded above by 2 ticks */
1784 cfs_rq->runtime_expires += TICK_NSEC;
1785 } else {
1786 /* global deadline is ahead, expiration has passed */
1787 cfs_rq->runtime_remaining = 0;
1788 }
1789}
1790
1791static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1792 unsigned long delta_exec)
1793{
1794 /* dock delta_exec before expiring quota (as it could span periods) */
ec12cb7f 1795 cfs_rq->runtime_remaining -= delta_exec;
a9cf55b2
PT
1796 expire_cfs_rq_runtime(cfs_rq);
1797
1798 if (likely(cfs_rq->runtime_remaining > 0))
ec12cb7f
PT
1799 return;
1800
85dac906
PT
1801 /*
1802 * if we're unable to extend our runtime we resched so that the active
1803 * hierarchy can be throttled
1804 */
1805 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1806 resched_task(rq_of(cfs_rq)->curr);
ec12cb7f
PT
1807}
1808
6c16a6dc
PZ
1809static __always_inline
1810void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
ec12cb7f 1811{
56f570e5 1812 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
ec12cb7f
PT
1813 return;
1814
1815 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1816}
1817
85dac906
PT
1818static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1819{
56f570e5 1820 return cfs_bandwidth_used() && cfs_rq->throttled;
85dac906
PT
1821}
1822
64660c86
PT
1823/* check whether cfs_rq, or any parent, is throttled */
1824static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1825{
56f570e5 1826 return cfs_bandwidth_used() && cfs_rq->throttle_count;
64660c86
PT
1827}
1828
1829/*
1830 * Ensure that neither of the group entities corresponding to src_cpu or
1831 * dest_cpu are members of a throttled hierarchy when performing group
1832 * load-balance operations.
1833 */
1834static inline int throttled_lb_pair(struct task_group *tg,
1835 int src_cpu, int dest_cpu)
1836{
1837 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1838
1839 src_cfs_rq = tg->cfs_rq[src_cpu];
1840 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1841
1842 return throttled_hierarchy(src_cfs_rq) ||
1843 throttled_hierarchy(dest_cfs_rq);
1844}
1845
1846/* updated child weight may affect parent so we have to do this bottom up */
1847static int tg_unthrottle_up(struct task_group *tg, void *data)
1848{
1849 struct rq *rq = data;
1850 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1851
1852 cfs_rq->throttle_count--;
1853#ifdef CONFIG_SMP
1854 if (!cfs_rq->throttle_count) {
1855 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1856
1857 /* leaving throttled state, advance shares averaging windows */
1858 cfs_rq->load_stamp += delta;
1859 cfs_rq->load_last += delta;
1860
1861 /* update entity weight now that we are on_rq again */
1862 update_cfs_shares(cfs_rq);
1863 }
1864#endif
1865
1866 return 0;
1867}
1868
1869static int tg_throttle_down(struct task_group *tg, void *data)
1870{
1871 struct rq *rq = data;
1872 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1873
1874 /* group is entering throttled state, record last load */
1875 if (!cfs_rq->throttle_count)
1876 update_cfs_load(cfs_rq, 0);
1877 cfs_rq->throttle_count++;
1878
1879 return 0;
1880}
1881
d3d9dc33 1882static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
85dac906
PT
1883{
1884 struct rq *rq = rq_of(cfs_rq);
1885 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1886 struct sched_entity *se;
1887 long task_delta, dequeue = 1;
1888
1889 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1890
1891 /* account load preceding throttle */
64660c86
PT
1892 rcu_read_lock();
1893 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1894 rcu_read_unlock();
85dac906
PT
1895
1896 task_delta = cfs_rq->h_nr_running;
1897 for_each_sched_entity(se) {
1898 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1899 /* throttled entity or throttle-on-deactivate */
1900 if (!se->on_rq)
1901 break;
1902
1903 if (dequeue)
1904 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1905 qcfs_rq->h_nr_running -= task_delta;
1906
1907 if (qcfs_rq->load.weight)
1908 dequeue = 0;
1909 }
1910
1911 if (!se)
1912 rq->nr_running -= task_delta;
1913
1914 cfs_rq->throttled = 1;
e8da1b18 1915 cfs_rq->throttled_timestamp = rq->clock;
85dac906
PT
1916 raw_spin_lock(&cfs_b->lock);
1917 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1918 raw_spin_unlock(&cfs_b->lock);
1919}
1920
029632fb 1921void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
671fd9da
PT
1922{
1923 struct rq *rq = rq_of(cfs_rq);
1924 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1925 struct sched_entity *se;
1926 int enqueue = 1;
1927 long task_delta;
1928
1929 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1930
1931 cfs_rq->throttled = 0;
1932 raw_spin_lock(&cfs_b->lock);
e8da1b18 1933 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
671fd9da
PT
1934 list_del_rcu(&cfs_rq->throttled_list);
1935 raw_spin_unlock(&cfs_b->lock);
e8da1b18 1936 cfs_rq->throttled_timestamp = 0;
671fd9da 1937
64660c86
PT
1938 update_rq_clock(rq);
1939 /* update hierarchical throttle state */
1940 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1941
671fd9da
PT
1942 if (!cfs_rq->load.weight)
1943 return;
1944
1945 task_delta = cfs_rq->h_nr_running;
1946 for_each_sched_entity(se) {
1947 if (se->on_rq)
1948 enqueue = 0;
1949
1950 cfs_rq = cfs_rq_of(se);
1951 if (enqueue)
1952 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1953 cfs_rq->h_nr_running += task_delta;
1954
1955 if (cfs_rq_throttled(cfs_rq))
1956 break;
1957 }
1958
1959 if (!se)
1960 rq->nr_running += task_delta;
1961
1962 /* determine whether we need to wake up potentially idle cpu */
1963 if (rq->curr == rq->idle && rq->cfs.nr_running)
1964 resched_task(rq->curr);
1965}
1966
1967static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1968 u64 remaining, u64 expires)
1969{
1970 struct cfs_rq *cfs_rq;
1971 u64 runtime = remaining;
1972
1973 rcu_read_lock();
1974 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1975 throttled_list) {
1976 struct rq *rq = rq_of(cfs_rq);
1977
1978 raw_spin_lock(&rq->lock);
1979 if (!cfs_rq_throttled(cfs_rq))
1980 goto next;
1981
1982 runtime = -cfs_rq->runtime_remaining + 1;
1983 if (runtime > remaining)
1984 runtime = remaining;
1985 remaining -= runtime;
1986
1987 cfs_rq->runtime_remaining += runtime;
1988 cfs_rq->runtime_expires = expires;
1989
1990 /* we check whether we're throttled above */
1991 if (cfs_rq->runtime_remaining > 0)
1992 unthrottle_cfs_rq(cfs_rq);
1993
1994next:
1995 raw_spin_unlock(&rq->lock);
1996
1997 if (!remaining)
1998 break;
1999 }
2000 rcu_read_unlock();
2001
2002 return remaining;
2003}
2004
58088ad0
PT
2005/*
2006 * Responsible for refilling a task_group's bandwidth and unthrottling its
2007 * cfs_rqs as appropriate. If there has been no activity within the last
2008 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2009 * used to track this state.
2010 */
2011static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2012{
671fd9da
PT
2013 u64 runtime, runtime_expires;
2014 int idle = 1, throttled;
58088ad0
PT
2015
2016 raw_spin_lock(&cfs_b->lock);
2017 /* no need to continue the timer with no bandwidth constraint */
2018 if (cfs_b->quota == RUNTIME_INF)
2019 goto out_unlock;
2020
671fd9da
PT
2021 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2022 /* idle depends on !throttled (for the case of a large deficit) */
2023 idle = cfs_b->idle && !throttled;
e8da1b18 2024 cfs_b->nr_periods += overrun;
671fd9da 2025
a9cf55b2
PT
2026 /* if we're going inactive then everything else can be deferred */
2027 if (idle)
2028 goto out_unlock;
2029
2030 __refill_cfs_bandwidth_runtime(cfs_b);
2031
671fd9da
PT
2032 if (!throttled) {
2033 /* mark as potentially idle for the upcoming period */
2034 cfs_b->idle = 1;
2035 goto out_unlock;
2036 }
2037
e8da1b18
NR
2038 /* account preceding periods in which throttling occurred */
2039 cfs_b->nr_throttled += overrun;
2040
671fd9da
PT
2041 /*
2042 * There are throttled entities so we must first use the new bandwidth
2043 * to unthrottle them before making it generally available. This
2044 * ensures that all existing debts will be paid before a new cfs_rq is
2045 * allowed to run.
2046 */
2047 runtime = cfs_b->runtime;
2048 runtime_expires = cfs_b->runtime_expires;
2049 cfs_b->runtime = 0;
2050
2051 /*
2052 * This check is repeated as we are holding onto the new bandwidth
2053 * while we unthrottle. This can potentially race with an unthrottled
2054 * group trying to acquire new bandwidth from the global pool.
2055 */
2056 while (throttled && runtime > 0) {
2057 raw_spin_unlock(&cfs_b->lock);
2058 /* we can't nest cfs_b->lock while distributing bandwidth */
2059 runtime = distribute_cfs_runtime(cfs_b, runtime,
2060 runtime_expires);
2061 raw_spin_lock(&cfs_b->lock);
2062
2063 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2064 }
58088ad0 2065
671fd9da
PT
2066 /* return (any) remaining runtime */
2067 cfs_b->runtime = runtime;
2068 /*
2069 * While we are ensured activity in the period following an
2070 * unthrottle, this also covers the case in which the new bandwidth is
2071 * insufficient to cover the existing bandwidth deficit. (Forcing the
2072 * timer to remain active while there are any throttled entities.)
2073 */
2074 cfs_b->idle = 0;
58088ad0
PT
2075out_unlock:
2076 if (idle)
2077 cfs_b->timer_active = 0;
2078 raw_spin_unlock(&cfs_b->lock);
2079
2080 return idle;
2081}
d3d9dc33 2082
d8b4986d
PT
2083/* a cfs_rq won't donate quota below this amount */
2084static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2085/* minimum remaining period time to redistribute slack quota */
2086static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2087/* how long we wait to gather additional slack before distributing */
2088static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2089
2090/* are we near the end of the current quota period? */
2091static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2092{
2093 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2094 u64 remaining;
2095
2096 /* if the call-back is running a quota refresh is already occurring */
2097 if (hrtimer_callback_running(refresh_timer))
2098 return 1;
2099
2100 /* is a quota refresh about to occur? */
2101 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2102 if (remaining < min_expire)
2103 return 1;
2104
2105 return 0;
2106}
2107
2108static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2109{
2110 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2111
2112 /* if there's a quota refresh soon don't bother with slack */
2113 if (runtime_refresh_within(cfs_b, min_left))
2114 return;
2115
2116 start_bandwidth_timer(&cfs_b->slack_timer,
2117 ns_to_ktime(cfs_bandwidth_slack_period));
2118}
2119
2120/* we know any runtime found here is valid as update_curr() precedes return */
2121static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2122{
2123 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2124 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2125
2126 if (slack_runtime <= 0)
2127 return;
2128
2129 raw_spin_lock(&cfs_b->lock);
2130 if (cfs_b->quota != RUNTIME_INF &&
2131 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2132 cfs_b->runtime += slack_runtime;
2133
2134 /* we are under rq->lock, defer unthrottling using a timer */
2135 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2136 !list_empty(&cfs_b->throttled_cfs_rq))
2137 start_cfs_slack_bandwidth(cfs_b);
2138 }
2139 raw_spin_unlock(&cfs_b->lock);
2140
2141 /* even if it's not valid for return we don't want to try again */
2142 cfs_rq->runtime_remaining -= slack_runtime;
2143}
2144
2145static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2146{
56f570e5
PT
2147 if (!cfs_bandwidth_used())
2148 return;
2149
fccfdc6f 2150 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
d8b4986d
PT
2151 return;
2152
2153 __return_cfs_rq_runtime(cfs_rq);
2154}
2155
2156/*
2157 * This is done with a timer (instead of inline with bandwidth return) since
2158 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2159 */
2160static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2161{
2162 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2163 u64 expires;
2164
2165 /* confirm we're still not at a refresh boundary */
2166 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2167 return;
2168
2169 raw_spin_lock(&cfs_b->lock);
2170 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2171 runtime = cfs_b->runtime;
2172 cfs_b->runtime = 0;
2173 }
2174 expires = cfs_b->runtime_expires;
2175 raw_spin_unlock(&cfs_b->lock);
2176
2177 if (!runtime)
2178 return;
2179
2180 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2181
2182 raw_spin_lock(&cfs_b->lock);
2183 if (expires == cfs_b->runtime_expires)
2184 cfs_b->runtime = runtime;
2185 raw_spin_unlock(&cfs_b->lock);
2186}
2187
d3d9dc33
PT
2188/*
2189 * When a group wakes up we want to make sure that its quota is not already
2190 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2191 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2192 */
2193static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2194{
56f570e5
PT
2195 if (!cfs_bandwidth_used())
2196 return;
2197
d3d9dc33
PT
2198 /* an active group must be handled by the update_curr()->put() path */
2199 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2200 return;
2201
2202 /* ensure the group is not already throttled */
2203 if (cfs_rq_throttled(cfs_rq))
2204 return;
2205
2206 /* update runtime allocation */
2207 account_cfs_rq_runtime(cfs_rq, 0);
2208 if (cfs_rq->runtime_remaining <= 0)
2209 throttle_cfs_rq(cfs_rq);
2210}
2211
2212/* conditionally throttle active cfs_rq's from put_prev_entity() */
2213static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2214{
56f570e5
PT
2215 if (!cfs_bandwidth_used())
2216 return;
2217
d3d9dc33
PT
2218 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2219 return;
2220
2221 /*
2222 * it's possible for a throttled entity to be forced into a running
2223 * state (e.g. set_curr_task), in this case we're finished.
2224 */
2225 if (cfs_rq_throttled(cfs_rq))
2226 return;
2227
2228 throttle_cfs_rq(cfs_rq);
2229}
029632fb
PZ
2230
2231static inline u64 default_cfs_period(void);
2232static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2233static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2234
2235static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2236{
2237 struct cfs_bandwidth *cfs_b =
2238 container_of(timer, struct cfs_bandwidth, slack_timer);
2239 do_sched_cfs_slack_timer(cfs_b);
2240
2241 return HRTIMER_NORESTART;
2242}
2243
2244static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2245{
2246 struct cfs_bandwidth *cfs_b =
2247 container_of(timer, struct cfs_bandwidth, period_timer);
2248 ktime_t now;
2249 int overrun;
2250 int idle = 0;
2251
2252 for (;;) {
2253 now = hrtimer_cb_get_time(timer);
2254 overrun = hrtimer_forward(timer, now, cfs_b->period);
2255
2256 if (!overrun)
2257 break;
2258
2259 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2260 }
2261
2262 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2263}
2264
2265void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2266{
2267 raw_spin_lock_init(&cfs_b->lock);
2268 cfs_b->runtime = 0;
2269 cfs_b->quota = RUNTIME_INF;
2270 cfs_b->period = ns_to_ktime(default_cfs_period());
2271
2272 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2273 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2274 cfs_b->period_timer.function = sched_cfs_period_timer;
2275 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2276 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2277}
2278
2279static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2280{
2281 cfs_rq->runtime_enabled = 0;
2282 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2283}
2284
2285/* requires cfs_b->lock, may release to reprogram timer */
2286void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2287{
2288 /*
2289 * The timer may be active because we're trying to set a new bandwidth
2290 * period or because we're racing with the tear-down path
2291 * (timer_active==0 becomes visible before the hrtimer call-back
2292 * terminates). In either case we ensure that it's re-programmed
2293 */
2294 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2295 raw_spin_unlock(&cfs_b->lock);
2296 /* ensure cfs_b->lock is available while we wait */
2297 hrtimer_cancel(&cfs_b->period_timer);
2298
2299 raw_spin_lock(&cfs_b->lock);
2300 /* if someone else restarted the timer then we're done */
2301 if (cfs_b->timer_active)
2302 return;
2303 }
2304
2305 cfs_b->timer_active = 1;
2306 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2307}
2308
2309static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2310{
2311 hrtimer_cancel(&cfs_b->period_timer);
2312 hrtimer_cancel(&cfs_b->slack_timer);
2313}
2314
a4c96ae3 2315static void unthrottle_offline_cfs_rqs(struct rq *rq)
029632fb
PZ
2316{
2317 struct cfs_rq *cfs_rq;
2318
2319 for_each_leaf_cfs_rq(rq, cfs_rq) {
2320 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2321
2322 if (!cfs_rq->runtime_enabled)
2323 continue;
2324
2325 /*
2326 * clock_task is not advancing so we just need to make sure
2327 * there's some valid quota amount
2328 */
2329 cfs_rq->runtime_remaining = cfs_b->quota;
2330 if (cfs_rq_throttled(cfs_rq))
2331 unthrottle_cfs_rq(cfs_rq);
2332 }
2333}
2334
2335#else /* CONFIG_CFS_BANDWIDTH */
6c16a6dc
PZ
2336static __always_inline
2337void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
d3d9dc33
PT
2338static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2339static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
6c16a6dc 2340static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
85dac906
PT
2341
2342static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2343{
2344 return 0;
2345}
64660c86
PT
2346
2347static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2348{
2349 return 0;
2350}
2351
2352static inline int throttled_lb_pair(struct task_group *tg,
2353 int src_cpu, int dest_cpu)
2354{
2355 return 0;
2356}
029632fb
PZ
2357
2358void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2359
2360#ifdef CONFIG_FAIR_GROUP_SCHED
2361static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
ab84d31e
PT
2362#endif
2363
029632fb
PZ
2364static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2365{
2366 return NULL;
2367}
2368static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
a4c96ae3 2369static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
029632fb
PZ
2370
2371#endif /* CONFIG_CFS_BANDWIDTH */
2372
bf0f6f24
IM
2373/**************************************************
2374 * CFS operations on tasks:
2375 */
2376
8f4d37ec
PZ
2377#ifdef CONFIG_SCHED_HRTICK
2378static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2379{
8f4d37ec
PZ
2380 struct sched_entity *se = &p->se;
2381 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2382
2383 WARN_ON(task_rq(p) != rq);
2384
b39e66ea 2385 if (cfs_rq->nr_running > 1) {
8f4d37ec
PZ
2386 u64 slice = sched_slice(cfs_rq, se);
2387 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2388 s64 delta = slice - ran;
2389
2390 if (delta < 0) {
2391 if (rq->curr == p)
2392 resched_task(p);
2393 return;
2394 }
2395
2396 /*
2397 * Don't schedule slices shorter than 10000ns, that just
2398 * doesn't make sense. Rely on vruntime for fairness.
2399 */
31656519 2400 if (rq->curr != p)
157124c1 2401 delta = max_t(s64, 10000LL, delta);
8f4d37ec 2402
31656519 2403 hrtick_start(rq, delta);
8f4d37ec
PZ
2404 }
2405}
a4c2f00f
PZ
2406
2407/*
2408 * called from enqueue/dequeue and updates the hrtick when the
2409 * current task is from our class and nr_running is low enough
2410 * to matter.
2411 */
2412static void hrtick_update(struct rq *rq)
2413{
2414 struct task_struct *curr = rq->curr;
2415
b39e66ea 2416 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
a4c2f00f
PZ
2417 return;
2418
2419 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2420 hrtick_start_fair(rq, curr);
2421}
55e12e5e 2422#else /* !CONFIG_SCHED_HRTICK */
8f4d37ec
PZ
2423static inline void
2424hrtick_start_fair(struct rq *rq, struct task_struct *p)
2425{
2426}
a4c2f00f
PZ
2427
2428static inline void hrtick_update(struct rq *rq)
2429{
2430}
8f4d37ec
PZ
2431#endif
2432
bf0f6f24
IM
2433/*
2434 * The enqueue_task method is called before nr_running is
2435 * increased. Here we update the fair scheduling stats and
2436 * then put the task into the rbtree:
2437 */
ea87bb78 2438static void
371fd7e7 2439enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
bf0f6f24
IM
2440{
2441 struct cfs_rq *cfs_rq;
62fb1851 2442 struct sched_entity *se = &p->se;
bf0f6f24
IM
2443
2444 for_each_sched_entity(se) {
62fb1851 2445 if (se->on_rq)
bf0f6f24
IM
2446 break;
2447 cfs_rq = cfs_rq_of(se);
88ec22d3 2448 enqueue_entity(cfs_rq, se, flags);
85dac906
PT
2449
2450 /*
2451 * end evaluation on encountering a throttled cfs_rq
2452 *
2453 * note: in the case of encountering a throttled cfs_rq we will
2454 * post the final h_nr_running increment below.
2455 */
2456 if (cfs_rq_throttled(cfs_rq))
2457 break;
953bfcd1 2458 cfs_rq->h_nr_running++;
85dac906 2459
88ec22d3 2460 flags = ENQUEUE_WAKEUP;
bf0f6f24 2461 }
8f4d37ec 2462
2069dd75 2463 for_each_sched_entity(se) {
0f317143 2464 cfs_rq = cfs_rq_of(se);
953bfcd1 2465 cfs_rq->h_nr_running++;
2069dd75 2466
85dac906
PT
2467 if (cfs_rq_throttled(cfs_rq))
2468 break;
2469
d6b55918 2470 update_cfs_load(cfs_rq, 0);
6d5ab293 2471 update_cfs_shares(cfs_rq);
9ee474f5 2472 update_entity_load_avg(se, 1);
2069dd75
PZ
2473 }
2474
18bf2805
BS
2475 if (!se) {
2476 update_rq_runnable_avg(rq, rq->nr_running);
85dac906 2477 inc_nr_running(rq);
18bf2805 2478 }
a4c2f00f 2479 hrtick_update(rq);
bf0f6f24
IM
2480}
2481
2f36825b
VP
2482static void set_next_buddy(struct sched_entity *se);
2483
bf0f6f24
IM
2484/*
2485 * The dequeue_task method is called before nr_running is
2486 * decreased. We remove the task from the rbtree and
2487 * update the fair scheduling stats:
2488 */
371fd7e7 2489static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
bf0f6f24
IM
2490{
2491 struct cfs_rq *cfs_rq;
62fb1851 2492 struct sched_entity *se = &p->se;
2f36825b 2493 int task_sleep = flags & DEQUEUE_SLEEP;
bf0f6f24
IM
2494
2495 for_each_sched_entity(se) {
2496 cfs_rq = cfs_rq_of(se);
371fd7e7 2497 dequeue_entity(cfs_rq, se, flags);
85dac906
PT
2498
2499 /*
2500 * end evaluation on encountering a throttled cfs_rq
2501 *
2502 * note: in the case of encountering a throttled cfs_rq we will
2503 * post the final h_nr_running decrement below.
2504 */
2505 if (cfs_rq_throttled(cfs_rq))
2506 break;
953bfcd1 2507 cfs_rq->h_nr_running--;
2069dd75 2508
bf0f6f24 2509 /* Don't dequeue parent if it has other entities besides us */
2f36825b
VP
2510 if (cfs_rq->load.weight) {
2511 /*
2512 * Bias pick_next to pick a task from this cfs_rq, as
2513 * p is sleeping when it is within its sched_slice.
2514 */
2515 if (task_sleep && parent_entity(se))
2516 set_next_buddy(parent_entity(se));
9598c82d
PT
2517
2518 /* avoid re-evaluating load for this entity */
2519 se = parent_entity(se);
bf0f6f24 2520 break;
2f36825b 2521 }
371fd7e7 2522 flags |= DEQUEUE_SLEEP;
bf0f6f24 2523 }
8f4d37ec 2524
2069dd75 2525 for_each_sched_entity(se) {
0f317143 2526 cfs_rq = cfs_rq_of(se);
953bfcd1 2527 cfs_rq->h_nr_running--;
2069dd75 2528
85dac906
PT
2529 if (cfs_rq_throttled(cfs_rq))
2530 break;
2531
d6b55918 2532 update_cfs_load(cfs_rq, 0);
6d5ab293 2533 update_cfs_shares(cfs_rq);
9ee474f5 2534 update_entity_load_avg(se, 1);
2069dd75
PZ
2535 }
2536
18bf2805 2537 if (!se) {
85dac906 2538 dec_nr_running(rq);
18bf2805
BS
2539 update_rq_runnable_avg(rq, 1);
2540 }
a4c2f00f 2541 hrtick_update(rq);
bf0f6f24
IM
2542}
2543
e7693a36 2544#ifdef CONFIG_SMP
029632fb
PZ
2545/* Used instead of source_load when we know the type == 0 */
2546static unsigned long weighted_cpuload(const int cpu)
2547{
2548 return cpu_rq(cpu)->load.weight;
2549}
2550
2551/*
2552 * Return a low guess at the load of a migration-source cpu weighted
2553 * according to the scheduling class and "nice" value.
2554 *
2555 * We want to under-estimate the load of migration sources, to
2556 * balance conservatively.
2557 */
2558static unsigned long source_load(int cpu, int type)
2559{
2560 struct rq *rq = cpu_rq(cpu);
2561 unsigned long total = weighted_cpuload(cpu);
2562
2563 if (type == 0 || !sched_feat(LB_BIAS))
2564 return total;
2565
2566 return min(rq->cpu_load[type-1], total);
2567}
2568
2569/*
2570 * Return a high guess at the load of a migration-target cpu weighted
2571 * according to the scheduling class and "nice" value.
2572 */
2573static unsigned long target_load(int cpu, int type)
2574{
2575 struct rq *rq = cpu_rq(cpu);
2576 unsigned long total = weighted_cpuload(cpu);
2577
2578 if (type == 0 || !sched_feat(LB_BIAS))
2579 return total;
2580
2581 return max(rq->cpu_load[type-1], total);
2582}
2583
2584static unsigned long power_of(int cpu)
2585{
2586 return cpu_rq(cpu)->cpu_power;
2587}
2588
2589static unsigned long cpu_avg_load_per_task(int cpu)
2590{
2591 struct rq *rq = cpu_rq(cpu);
2592 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2593
2594 if (nr_running)
2595 return rq->load.weight / nr_running;
2596
2597 return 0;
2598}
2599
098fb9db 2600
74f8e4b2 2601static void task_waking_fair(struct task_struct *p)
88ec22d3
PZ
2602{
2603 struct sched_entity *se = &p->se;
2604 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3fe1698b
PZ
2605 u64 min_vruntime;
2606
2607#ifndef CONFIG_64BIT
2608 u64 min_vruntime_copy;
88ec22d3 2609
3fe1698b
PZ
2610 do {
2611 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2612 smp_rmb();
2613 min_vruntime = cfs_rq->min_vruntime;
2614 } while (min_vruntime != min_vruntime_copy);
2615#else
2616 min_vruntime = cfs_rq->min_vruntime;
2617#endif
88ec22d3 2618
3fe1698b 2619 se->vruntime -= min_vruntime;
88ec22d3
PZ
2620}
2621
bb3469ac 2622#ifdef CONFIG_FAIR_GROUP_SCHED
f5bfb7d9
PZ
2623/*
2624 * effective_load() calculates the load change as seen from the root_task_group
2625 *
2626 * Adding load to a group doesn't make a group heavier, but can cause movement
2627 * of group shares between cpus. Assuming the shares were perfectly aligned one
2628 * can calculate the shift in shares.
cf5f0acf
PZ
2629 *
2630 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2631 * on this @cpu and results in a total addition (subtraction) of @wg to the
2632 * total group weight.
2633 *
2634 * Given a runqueue weight distribution (rw_i) we can compute a shares
2635 * distribution (s_i) using:
2636 *
2637 * s_i = rw_i / \Sum rw_j (1)
2638 *
2639 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2640 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2641 * shares distribution (s_i):
2642 *
2643 * rw_i = { 2, 4, 1, 0 }
2644 * s_i = { 2/7, 4/7, 1/7, 0 }
2645 *
2646 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2647 * task used to run on and the CPU the waker is running on), we need to
2648 * compute the effect of waking a task on either CPU and, in case of a sync
2649 * wakeup, compute the effect of the current task going to sleep.
2650 *
2651 * So for a change of @wl to the local @cpu with an overall group weight change
2652 * of @wl we can compute the new shares distribution (s'_i) using:
2653 *
2654 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2655 *
2656 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2657 * differences in waking a task to CPU 0. The additional task changes the
2658 * weight and shares distributions like:
2659 *
2660 * rw'_i = { 3, 4, 1, 0 }
2661 * s'_i = { 3/8, 4/8, 1/8, 0 }
2662 *
2663 * We can then compute the difference in effective weight by using:
2664 *
2665 * dw_i = S * (s'_i - s_i) (3)
2666 *
2667 * Where 'S' is the group weight as seen by its parent.
2668 *
2669 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2670 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2671 * 4/7) times the weight of the group.
f5bfb7d9 2672 */
2069dd75 2673static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
bb3469ac 2674{
4be9daaa 2675 struct sched_entity *se = tg->se[cpu];
f1d239f7 2676
cf5f0acf 2677 if (!tg->parent) /* the trivial, non-cgroup case */
f1d239f7
PZ
2678 return wl;
2679
4be9daaa 2680 for_each_sched_entity(se) {
cf5f0acf 2681 long w, W;
4be9daaa 2682
977dda7c 2683 tg = se->my_q->tg;
bb3469ac 2684
cf5f0acf
PZ
2685 /*
2686 * W = @wg + \Sum rw_j
2687 */
2688 W = wg + calc_tg_weight(tg, se->my_q);
4be9daaa 2689
cf5f0acf
PZ
2690 /*
2691 * w = rw_i + @wl
2692 */
2693 w = se->my_q->load.weight + wl;
940959e9 2694
cf5f0acf
PZ
2695 /*
2696 * wl = S * s'_i; see (2)
2697 */
2698 if (W > 0 && w < W)
2699 wl = (w * tg->shares) / W;
977dda7c
PT
2700 else
2701 wl = tg->shares;
940959e9 2702
cf5f0acf
PZ
2703 /*
2704 * Per the above, wl is the new se->load.weight value; since
2705 * those are clipped to [MIN_SHARES, ...) do so now. See
2706 * calc_cfs_shares().
2707 */
977dda7c
PT
2708 if (wl < MIN_SHARES)
2709 wl = MIN_SHARES;
cf5f0acf
PZ
2710
2711 /*
2712 * wl = dw_i = S * (s'_i - s_i); see (3)
2713 */
977dda7c 2714 wl -= se->load.weight;
cf5f0acf
PZ
2715
2716 /*
2717 * Recursively apply this logic to all parent groups to compute
2718 * the final effective load change on the root group. Since
2719 * only the @tg group gets extra weight, all parent groups can
2720 * only redistribute existing shares. @wl is the shift in shares
2721 * resulting from this level per the above.
2722 */
4be9daaa 2723 wg = 0;
4be9daaa 2724 }
bb3469ac 2725
4be9daaa 2726 return wl;
bb3469ac
PZ
2727}
2728#else
4be9daaa 2729
83378269
PZ
2730static inline unsigned long effective_load(struct task_group *tg, int cpu,
2731 unsigned long wl, unsigned long wg)
4be9daaa 2732{
83378269 2733 return wl;
bb3469ac 2734}
4be9daaa 2735
bb3469ac
PZ
2736#endif
2737
c88d5910 2738static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
098fb9db 2739{
e37b6a7b 2740 s64 this_load, load;
c88d5910 2741 int idx, this_cpu, prev_cpu;
098fb9db 2742 unsigned long tl_per_task;
c88d5910 2743 struct task_group *tg;
83378269 2744 unsigned long weight;
b3137bc8 2745 int balanced;
098fb9db 2746
c88d5910
PZ
2747 idx = sd->wake_idx;
2748 this_cpu = smp_processor_id();
2749 prev_cpu = task_cpu(p);
2750 load = source_load(prev_cpu, idx);
2751 this_load = target_load(this_cpu, idx);
098fb9db 2752
b3137bc8
MG
2753 /*
2754 * If sync wakeup then subtract the (maximum possible)
2755 * effect of the currently running task from the load
2756 * of the current CPU:
2757 */
83378269
PZ
2758 if (sync) {
2759 tg = task_group(current);
2760 weight = current->se.load.weight;
2761
c88d5910 2762 this_load += effective_load(tg, this_cpu, -weight, -weight);
83378269
PZ
2763 load += effective_load(tg, prev_cpu, 0, -weight);
2764 }
b3137bc8 2765
83378269
PZ
2766 tg = task_group(p);
2767 weight = p->se.load.weight;
b3137bc8 2768
71a29aa7
PZ
2769 /*
2770 * In low-load situations, where prev_cpu is idle and this_cpu is idle
c88d5910
PZ
2771 * due to the sync cause above having dropped this_load to 0, we'll
2772 * always have an imbalance, but there's really nothing you can do
2773 * about that, so that's good too.
71a29aa7
PZ
2774 *
2775 * Otherwise check if either cpus are near enough in load to allow this
2776 * task to be woken on this_cpu.
2777 */
e37b6a7b
PT
2778 if (this_load > 0) {
2779 s64 this_eff_load, prev_eff_load;
e51fd5e2
PZ
2780
2781 this_eff_load = 100;
2782 this_eff_load *= power_of(prev_cpu);
2783 this_eff_load *= this_load +
2784 effective_load(tg, this_cpu, weight, weight);
2785
2786 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2787 prev_eff_load *= power_of(this_cpu);
2788 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2789
2790 balanced = this_eff_load <= prev_eff_load;
2791 } else
2792 balanced = true;
b3137bc8 2793
098fb9db 2794 /*
4ae7d5ce
IM
2795 * If the currently running task will sleep within
2796 * a reasonable amount of time then attract this newly
2797 * woken task:
098fb9db 2798 */
2fb7635c
PZ
2799 if (sync && balanced)
2800 return 1;
098fb9db 2801
41acab88 2802 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
098fb9db
IM
2803 tl_per_task = cpu_avg_load_per_task(this_cpu);
2804
c88d5910
PZ
2805 if (balanced ||
2806 (this_load <= load &&
2807 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
098fb9db
IM
2808 /*
2809 * This domain has SD_WAKE_AFFINE and
2810 * p is cache cold in this domain, and
2811 * there is no bad imbalance.
2812 */
c88d5910 2813 schedstat_inc(sd, ttwu_move_affine);
41acab88 2814 schedstat_inc(p, se.statistics.nr_wakeups_affine);
098fb9db
IM
2815
2816 return 1;
2817 }
2818 return 0;
2819}
2820
aaee1203
PZ
2821/*
2822 * find_idlest_group finds and returns the least busy CPU group within the
2823 * domain.
2824 */
2825static struct sched_group *
78e7ed53 2826find_idlest_group(struct sched_domain *sd, struct task_struct *p,
5158f4e4 2827 int this_cpu, int load_idx)
e7693a36 2828{
b3bd3de6 2829 struct sched_group *idlest = NULL, *group = sd->groups;
aaee1203 2830 unsigned long min_load = ULONG_MAX, this_load = 0;
aaee1203 2831 int imbalance = 100 + (sd->imbalance_pct-100)/2;
e7693a36 2832
aaee1203
PZ
2833 do {
2834 unsigned long load, avg_load;
2835 int local_group;
2836 int i;
e7693a36 2837
aaee1203
PZ
2838 /* Skip over this group if it has no CPUs allowed */
2839 if (!cpumask_intersects(sched_group_cpus(group),
fa17b507 2840 tsk_cpus_allowed(p)))
aaee1203
PZ
2841 continue;
2842
2843 local_group = cpumask_test_cpu(this_cpu,
2844 sched_group_cpus(group));
2845
2846 /* Tally up the load of all CPUs in the group */
2847 avg_load = 0;
2848
2849 for_each_cpu(i, sched_group_cpus(group)) {
2850 /* Bias balancing toward cpus of our domain */
2851 if (local_group)
2852 load = source_load(i, load_idx);
2853 else
2854 load = target_load(i, load_idx);
2855
2856 avg_load += load;
2857 }
2858
2859 /* Adjust by relative CPU power of the group */
9c3f75cb 2860 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
aaee1203
PZ
2861
2862 if (local_group) {
2863 this_load = avg_load;
aaee1203
PZ
2864 } else if (avg_load < min_load) {
2865 min_load = avg_load;
2866 idlest = group;
2867 }
2868 } while (group = group->next, group != sd->groups);
2869
2870 if (!idlest || 100*this_load < imbalance*min_load)
2871 return NULL;
2872 return idlest;
2873}
2874
2875/*
2876 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2877 */
2878static int
2879find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2880{
2881 unsigned long load, min_load = ULONG_MAX;
2882 int idlest = -1;
2883 int i;
2884
2885 /* Traverse only the allowed CPUs */
fa17b507 2886 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
aaee1203
PZ
2887 load = weighted_cpuload(i);
2888
2889 if (load < min_load || (load == min_load && i == this_cpu)) {
2890 min_load = load;
2891 idlest = i;
e7693a36
GH
2892 }
2893 }
2894
aaee1203
PZ
2895 return idlest;
2896}
e7693a36 2897
a50bde51
PZ
2898/*
2899 * Try and locate an idle CPU in the sched_domain.
2900 */
99bd5e2f 2901static int select_idle_sibling(struct task_struct *p, int target)
a50bde51
PZ
2902{
2903 int cpu = smp_processor_id();
2904 int prev_cpu = task_cpu(p);
99bd5e2f 2905 struct sched_domain *sd;
37407ea7
LT
2906 struct sched_group *sg;
2907 int i;
a50bde51
PZ
2908
2909 /*
99bd5e2f
SS
2910 * If the task is going to be woken-up on this cpu and if it is
2911 * already idle, then it is the right target.
a50bde51 2912 */
99bd5e2f
SS
2913 if (target == cpu && idle_cpu(cpu))
2914 return cpu;
2915
2916 /*
2917 * If the task is going to be woken-up on the cpu where it previously
2918 * ran and if it is currently idle, then it the right target.
2919 */
2920 if (target == prev_cpu && idle_cpu(prev_cpu))
fe3bcfe1 2921 return prev_cpu;
a50bde51
PZ
2922
2923 /*
37407ea7 2924 * Otherwise, iterate the domains and find an elegible idle cpu.
a50bde51 2925 */
518cd623 2926 sd = rcu_dereference(per_cpu(sd_llc, target));
970e1789 2927 for_each_lower_domain(sd) {
37407ea7
LT
2928 sg = sd->groups;
2929 do {
2930 if (!cpumask_intersects(sched_group_cpus(sg),
2931 tsk_cpus_allowed(p)))
2932 goto next;
2933
2934 for_each_cpu(i, sched_group_cpus(sg)) {
2935 if (!idle_cpu(i))
2936 goto next;
2937 }
970e1789 2938
37407ea7
LT
2939 target = cpumask_first_and(sched_group_cpus(sg),
2940 tsk_cpus_allowed(p));
2941 goto done;
2942next:
2943 sg = sg->next;
2944 } while (sg != sd->groups);
2945 }
2946done:
a50bde51
PZ
2947 return target;
2948}
2949
aaee1203
PZ
2950/*
2951 * sched_balance_self: balance the current task (running on cpu) in domains
2952 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2953 * SD_BALANCE_EXEC.
2954 *
2955 * Balance, ie. select the least loaded group.
2956 *
2957 * Returns the target CPU number, or the same CPU if no balancing is needed.
2958 *
2959 * preempt must be disabled.
2960 */
0017d735 2961static int
7608dec2 2962select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
aaee1203 2963{
29cd8bae 2964 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
c88d5910
PZ
2965 int cpu = smp_processor_id();
2966 int prev_cpu = task_cpu(p);
2967 int new_cpu = cpu;
99bd5e2f 2968 int want_affine = 0;
5158f4e4 2969 int sync = wake_flags & WF_SYNC;
c88d5910 2970
29baa747 2971 if (p->nr_cpus_allowed == 1)
76854c7e
MG
2972 return prev_cpu;
2973
0763a660 2974 if (sd_flag & SD_BALANCE_WAKE) {
fa17b507 2975 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
c88d5910
PZ
2976 want_affine = 1;
2977 new_cpu = prev_cpu;
2978 }
aaee1203 2979
dce840a0 2980 rcu_read_lock();
aaee1203 2981 for_each_domain(cpu, tmp) {
e4f42888
PZ
2982 if (!(tmp->flags & SD_LOAD_BALANCE))
2983 continue;
2984
fe3bcfe1 2985 /*
99bd5e2f
SS
2986 * If both cpu and prev_cpu are part of this domain,
2987 * cpu is a valid SD_WAKE_AFFINE target.
fe3bcfe1 2988 */
99bd5e2f
SS
2989 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2990 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2991 affine_sd = tmp;
29cd8bae 2992 break;
f03542a7 2993 }
29cd8bae 2994
f03542a7 2995 if (tmp->flags & sd_flag)
29cd8bae
PZ
2996 sd = tmp;
2997 }
2998
8b911acd 2999 if (affine_sd) {
f03542a7 3000 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
dce840a0
PZ
3001 prev_cpu = cpu;
3002
3003 new_cpu = select_idle_sibling(p, prev_cpu);
3004 goto unlock;
8b911acd 3005 }
e7693a36 3006
aaee1203 3007 while (sd) {
5158f4e4 3008 int load_idx = sd->forkexec_idx;
aaee1203 3009 struct sched_group *group;
c88d5910 3010 int weight;
098fb9db 3011
0763a660 3012 if (!(sd->flags & sd_flag)) {
aaee1203
PZ
3013 sd = sd->child;
3014 continue;
3015 }
098fb9db 3016
5158f4e4
PZ
3017 if (sd_flag & SD_BALANCE_WAKE)
3018 load_idx = sd->wake_idx;
098fb9db 3019
5158f4e4 3020 group = find_idlest_group(sd, p, cpu, load_idx);
aaee1203
PZ
3021 if (!group) {
3022 sd = sd->child;
3023 continue;
3024 }
4ae7d5ce 3025
d7c33c49 3026 new_cpu = find_idlest_cpu(group, p, cpu);
aaee1203
PZ
3027 if (new_cpu == -1 || new_cpu == cpu) {
3028 /* Now try balancing at a lower domain level of cpu */
3029 sd = sd->child;
3030 continue;
e7693a36 3031 }
aaee1203
PZ
3032
3033 /* Now try balancing at a lower domain level of new_cpu */
3034 cpu = new_cpu;
669c55e9 3035 weight = sd->span_weight;
aaee1203
PZ
3036 sd = NULL;
3037 for_each_domain(cpu, tmp) {
669c55e9 3038 if (weight <= tmp->span_weight)
aaee1203 3039 break;
0763a660 3040 if (tmp->flags & sd_flag)
aaee1203
PZ
3041 sd = tmp;
3042 }
3043 /* while loop will break here if sd == NULL */
e7693a36 3044 }
dce840a0
PZ
3045unlock:
3046 rcu_read_unlock();
e7693a36 3047
c88d5910 3048 return new_cpu;
e7693a36
GH
3049}
3050#endif /* CONFIG_SMP */
3051
e52fb7c0
PZ
3052static unsigned long
3053wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
0bbd3336
PZ
3054{
3055 unsigned long gran = sysctl_sched_wakeup_granularity;
3056
3057 /*
e52fb7c0
PZ
3058 * Since its curr running now, convert the gran from real-time
3059 * to virtual-time in his units.
13814d42
MG
3060 *
3061 * By using 'se' instead of 'curr' we penalize light tasks, so
3062 * they get preempted easier. That is, if 'se' < 'curr' then
3063 * the resulting gran will be larger, therefore penalizing the
3064 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3065 * be smaller, again penalizing the lighter task.
3066 *
3067 * This is especially important for buddies when the leftmost
3068 * task is higher priority than the buddy.
0bbd3336 3069 */
f4ad9bd2 3070 return calc_delta_fair(gran, se);
0bbd3336
PZ
3071}
3072
464b7527
PZ
3073/*
3074 * Should 'se' preempt 'curr'.
3075 *
3076 * |s1
3077 * |s2
3078 * |s3
3079 * g
3080 * |<--->|c
3081 *
3082 * w(c, s1) = -1
3083 * w(c, s2) = 0
3084 * w(c, s3) = 1
3085 *
3086 */
3087static int
3088wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3089{
3090 s64 gran, vdiff = curr->vruntime - se->vruntime;
3091
3092 if (vdiff <= 0)
3093 return -1;
3094
e52fb7c0 3095 gran = wakeup_gran(curr, se);
464b7527
PZ
3096 if (vdiff > gran)
3097 return 1;
3098
3099 return 0;
3100}
3101
02479099
PZ
3102static void set_last_buddy(struct sched_entity *se)
3103{
69c80f3e
VP
3104 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3105 return;
3106
3107 for_each_sched_entity(se)
3108 cfs_rq_of(se)->last = se;
02479099
PZ
3109}
3110
3111static void set_next_buddy(struct sched_entity *se)
3112{
69c80f3e
VP
3113 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3114 return;
3115
3116 for_each_sched_entity(se)
3117 cfs_rq_of(se)->next = se;
02479099
PZ
3118}
3119
ac53db59
RR
3120static void set_skip_buddy(struct sched_entity *se)
3121{
69c80f3e
VP
3122 for_each_sched_entity(se)
3123 cfs_rq_of(se)->skip = se;
ac53db59
RR
3124}
3125
bf0f6f24
IM
3126/*
3127 * Preempt the current task with a newly woken task if needed:
3128 */
5a9b86f6 3129static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
bf0f6f24
IM
3130{
3131 struct task_struct *curr = rq->curr;
8651a86c 3132 struct sched_entity *se = &curr->se, *pse = &p->se;
03e89e45 3133 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
f685ceac 3134 int scale = cfs_rq->nr_running >= sched_nr_latency;
2f36825b 3135 int next_buddy_marked = 0;
bf0f6f24 3136
4ae7d5ce
IM
3137 if (unlikely(se == pse))
3138 return;
3139
5238cdd3 3140 /*
ddcdf6e7 3141 * This is possible from callers such as move_task(), in which we
5238cdd3
PT
3142 * unconditionally check_prempt_curr() after an enqueue (which may have
3143 * lead to a throttle). This both saves work and prevents false
3144 * next-buddy nomination below.
3145 */
3146 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3147 return;
3148
2f36825b 3149 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
3cb63d52 3150 set_next_buddy(pse);
2f36825b
VP
3151 next_buddy_marked = 1;
3152 }
57fdc26d 3153
aec0a514
BR
3154 /*
3155 * We can come here with TIF_NEED_RESCHED already set from new task
3156 * wake up path.
5238cdd3
PT
3157 *
3158 * Note: this also catches the edge-case of curr being in a throttled
3159 * group (e.g. via set_curr_task), since update_curr() (in the
3160 * enqueue of curr) will have resulted in resched being set. This
3161 * prevents us from potentially nominating it as a false LAST_BUDDY
3162 * below.
aec0a514
BR
3163 */
3164 if (test_tsk_need_resched(curr))
3165 return;
3166
a2f5c9ab
DH
3167 /* Idle tasks are by definition preempted by non-idle tasks. */
3168 if (unlikely(curr->policy == SCHED_IDLE) &&
3169 likely(p->policy != SCHED_IDLE))
3170 goto preempt;
3171
91c234b4 3172 /*
a2f5c9ab
DH
3173 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3174 * is driven by the tick):
91c234b4 3175 */
6bc912b7 3176 if (unlikely(p->policy != SCHED_NORMAL))
91c234b4 3177 return;
bf0f6f24 3178
464b7527 3179 find_matching_se(&se, &pse);
9bbd7374 3180 update_curr(cfs_rq_of(se));
002f128b 3181 BUG_ON(!pse);
2f36825b
VP
3182 if (wakeup_preempt_entity(se, pse) == 1) {
3183 /*
3184 * Bias pick_next to pick the sched entity that is
3185 * triggering this preemption.
3186 */
3187 if (!next_buddy_marked)
3188 set_next_buddy(pse);
3a7e73a2 3189 goto preempt;
2f36825b 3190 }
464b7527 3191
3a7e73a2 3192 return;
a65ac745 3193
3a7e73a2
PZ
3194preempt:
3195 resched_task(curr);
3196 /*
3197 * Only set the backward buddy when the current task is still
3198 * on the rq. This can happen when a wakeup gets interleaved
3199 * with schedule on the ->pre_schedule() or idle_balance()
3200 * point, either of which can * drop the rq lock.
3201 *
3202 * Also, during early boot the idle thread is in the fair class,
3203 * for obvious reasons its a bad idea to schedule back to it.
3204 */
3205 if (unlikely(!se->on_rq || curr == rq->idle))
3206 return;
3207
3208 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3209 set_last_buddy(se);
bf0f6f24
IM
3210}
3211
fb8d4724 3212static struct task_struct *pick_next_task_fair(struct rq *rq)
bf0f6f24 3213{
8f4d37ec 3214 struct task_struct *p;
bf0f6f24
IM
3215 struct cfs_rq *cfs_rq = &rq->cfs;
3216 struct sched_entity *se;
3217
36ace27e 3218 if (!cfs_rq->nr_running)
bf0f6f24
IM
3219 return NULL;
3220
3221 do {
9948f4b2 3222 se = pick_next_entity(cfs_rq);
f4b6755f 3223 set_next_entity(cfs_rq, se);
bf0f6f24
IM
3224 cfs_rq = group_cfs_rq(se);
3225 } while (cfs_rq);
3226
8f4d37ec 3227 p = task_of(se);
b39e66ea
MG
3228 if (hrtick_enabled(rq))
3229 hrtick_start_fair(rq, p);
8f4d37ec
PZ
3230
3231 return p;
bf0f6f24
IM
3232}
3233
3234/*
3235 * Account for a descheduled task:
3236 */
31ee529c 3237static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
bf0f6f24
IM
3238{
3239 struct sched_entity *se = &prev->se;
3240 struct cfs_rq *cfs_rq;
3241
3242 for_each_sched_entity(se) {
3243 cfs_rq = cfs_rq_of(se);
ab6cde26 3244 put_prev_entity(cfs_rq, se);
bf0f6f24
IM
3245 }
3246}
3247
ac53db59
RR
3248/*
3249 * sched_yield() is very simple
3250 *
3251 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3252 */
3253static void yield_task_fair(struct rq *rq)
3254{
3255 struct task_struct *curr = rq->curr;
3256 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3257 struct sched_entity *se = &curr->se;
3258
3259 /*
3260 * Are we the only task in the tree?
3261 */
3262 if (unlikely(rq->nr_running == 1))
3263 return;
3264
3265 clear_buddies(cfs_rq, se);
3266
3267 if (curr->policy != SCHED_BATCH) {
3268 update_rq_clock(rq);
3269 /*
3270 * Update run-time statistics of the 'current'.
3271 */
3272 update_curr(cfs_rq);
916671c0
MG
3273 /*
3274 * Tell update_rq_clock() that we've just updated,
3275 * so we don't do microscopic update in schedule()
3276 * and double the fastpath cost.
3277 */
3278 rq->skip_clock_update = 1;
ac53db59
RR
3279 }
3280
3281 set_skip_buddy(se);
3282}
3283
d95f4122
MG
3284static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3285{
3286 struct sched_entity *se = &p->se;
3287
5238cdd3
PT
3288 /* throttled hierarchies are not runnable */
3289 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
d95f4122
MG
3290 return false;
3291
3292 /* Tell the scheduler that we'd really like pse to run next. */
3293 set_next_buddy(se);
3294
d95f4122
MG
3295 yield_task_fair(rq);
3296
3297 return true;
3298}
3299
681f3e68 3300#ifdef CONFIG_SMP
bf0f6f24
IM
3301/**************************************************
3302 * Fair scheduling class load-balancing methods:
3303 */
3304
ed387b78
HS
3305static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3306
ddcdf6e7 3307#define LBF_ALL_PINNED 0x01
367456c7 3308#define LBF_NEED_BREAK 0x02
88b8dac0 3309#define LBF_SOME_PINNED 0x04
ddcdf6e7
PZ
3310
3311struct lb_env {
3312 struct sched_domain *sd;
3313
ddcdf6e7 3314 struct rq *src_rq;
85c1e7da 3315 int src_cpu;
ddcdf6e7
PZ
3316
3317 int dst_cpu;
3318 struct rq *dst_rq;
3319
88b8dac0
SV
3320 struct cpumask *dst_grpmask;
3321 int new_dst_cpu;
ddcdf6e7 3322 enum cpu_idle_type idle;
bd939f45 3323 long imbalance;
b9403130
MW
3324 /* The set of CPUs under consideration for load-balancing */
3325 struct cpumask *cpus;
3326
ddcdf6e7 3327 unsigned int flags;
367456c7
PZ
3328
3329 unsigned int loop;
3330 unsigned int loop_break;
3331 unsigned int loop_max;
ddcdf6e7
PZ
3332};
3333
1e3c88bd 3334/*
ddcdf6e7 3335 * move_task - move a task from one runqueue to another runqueue.
1e3c88bd
PZ
3336 * Both runqueues must be locked.
3337 */
ddcdf6e7 3338static void move_task(struct task_struct *p, struct lb_env *env)
1e3c88bd 3339{
ddcdf6e7
PZ
3340 deactivate_task(env->src_rq, p, 0);
3341 set_task_cpu(p, env->dst_cpu);
3342 activate_task(env->dst_rq, p, 0);
3343 check_preempt_curr(env->dst_rq, p, 0);
1e3c88bd
PZ
3344}
3345
029632fb
PZ
3346/*
3347 * Is this task likely cache-hot:
3348 */
3349static int
3350task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3351{
3352 s64 delta;
3353
3354 if (p->sched_class != &fair_sched_class)
3355 return 0;
3356
3357 if (unlikely(p->policy == SCHED_IDLE))
3358 return 0;
3359
3360 /*
3361 * Buddy candidates are cache hot:
3362 */
3363 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3364 (&p->se == cfs_rq_of(&p->se)->next ||
3365 &p->se == cfs_rq_of(&p->se)->last))
3366 return 1;
3367
3368 if (sysctl_sched_migration_cost == -1)
3369 return 1;
3370 if (sysctl_sched_migration_cost == 0)
3371 return 0;
3372
3373 delta = now - p->se.exec_start;
3374
3375 return delta < (s64)sysctl_sched_migration_cost;
3376}
3377
1e3c88bd
PZ
3378/*
3379 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3380 */
3381static
8e45cb54 3382int can_migrate_task(struct task_struct *p, struct lb_env *env)
1e3c88bd
PZ
3383{
3384 int tsk_cache_hot = 0;
3385 /*
3386 * We do not migrate tasks that are:
3387 * 1) running (obviously), or
3388 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3389 * 3) are cache-hot on their current CPU.
3390 */
ddcdf6e7 3391 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
88b8dac0
SV
3392 int new_dst_cpu;
3393
41acab88 3394 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
88b8dac0
SV
3395
3396 /*
3397 * Remember if this task can be migrated to any other cpu in
3398 * our sched_group. We may want to revisit it if we couldn't
3399 * meet load balance goals by pulling other tasks on src_cpu.
3400 *
3401 * Also avoid computing new_dst_cpu if we have already computed
3402 * one in current iteration.
3403 */
3404 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3405 return 0;
3406
3407 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3408 tsk_cpus_allowed(p));
3409 if (new_dst_cpu < nr_cpu_ids) {
3410 env->flags |= LBF_SOME_PINNED;
3411 env->new_dst_cpu = new_dst_cpu;
3412 }
1e3c88bd
PZ
3413 return 0;
3414 }
88b8dac0
SV
3415
3416 /* Record that we found atleast one task that could run on dst_cpu */
8e45cb54 3417 env->flags &= ~LBF_ALL_PINNED;
1e3c88bd 3418
ddcdf6e7 3419 if (task_running(env->src_rq, p)) {
41acab88 3420 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
1e3c88bd
PZ
3421 return 0;
3422 }
3423
3424 /*
3425 * Aggressive migration if:
3426 * 1) task is cache cold, or
3427 * 2) too many balance attempts have failed.
3428 */
3429
ddcdf6e7 3430 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
1e3c88bd 3431 if (!tsk_cache_hot ||
8e45cb54 3432 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
1e3c88bd
PZ
3433#ifdef CONFIG_SCHEDSTATS
3434 if (tsk_cache_hot) {
8e45cb54 3435 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
41acab88 3436 schedstat_inc(p, se.statistics.nr_forced_migrations);
1e3c88bd
PZ
3437 }
3438#endif
3439 return 1;
3440 }
3441
3442 if (tsk_cache_hot) {
41acab88 3443 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
1e3c88bd
PZ
3444 return 0;
3445 }
3446 return 1;
3447}
3448
897c395f
PZ
3449/*
3450 * move_one_task tries to move exactly one task from busiest to this_rq, as
3451 * part of active balancing operations within "domain".
3452 * Returns 1 if successful and 0 otherwise.
3453 *
3454 * Called with both runqueues locked.
3455 */
8e45cb54 3456static int move_one_task(struct lb_env *env)
897c395f
PZ
3457{
3458 struct task_struct *p, *n;
897c395f 3459
367456c7
PZ
3460 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3461 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3462 continue;
897c395f 3463
367456c7
PZ
3464 if (!can_migrate_task(p, env))
3465 continue;
897c395f 3466
367456c7
PZ
3467 move_task(p, env);
3468 /*
3469 * Right now, this is only the second place move_task()
3470 * is called, so we can safely collect move_task()
3471 * stats here rather than inside move_task().
3472 */
3473 schedstat_inc(env->sd, lb_gained[env->idle]);
3474 return 1;
897c395f 3475 }
897c395f
PZ
3476 return 0;
3477}
3478
367456c7
PZ
3479static unsigned long task_h_load(struct task_struct *p);
3480
eb95308e
PZ
3481static const unsigned int sched_nr_migrate_break = 32;
3482
5d6523eb 3483/*
bd939f45 3484 * move_tasks tries to move up to imbalance weighted load from busiest to
5d6523eb
PZ
3485 * this_rq, as part of a balancing operation within domain "sd".
3486 * Returns 1 if successful and 0 otherwise.
3487 *
3488 * Called with both runqueues locked.
3489 */
3490static int move_tasks(struct lb_env *env)
1e3c88bd 3491{
5d6523eb
PZ
3492 struct list_head *tasks = &env->src_rq->cfs_tasks;
3493 struct task_struct *p;
367456c7
PZ
3494 unsigned long load;
3495 int pulled = 0;
1e3c88bd 3496
bd939f45 3497 if (env->imbalance <= 0)
5d6523eb 3498 return 0;
1e3c88bd 3499
5d6523eb
PZ
3500 while (!list_empty(tasks)) {
3501 p = list_first_entry(tasks, struct task_struct, se.group_node);
1e3c88bd 3502
367456c7
PZ
3503 env->loop++;
3504 /* We've more or less seen every task there is, call it quits */
5d6523eb 3505 if (env->loop > env->loop_max)
367456c7 3506 break;
5d6523eb
PZ
3507
3508 /* take a breather every nr_migrate tasks */
367456c7 3509 if (env->loop > env->loop_break) {
eb95308e 3510 env->loop_break += sched_nr_migrate_break;
8e45cb54 3511 env->flags |= LBF_NEED_BREAK;
ee00e66f 3512 break;
a195f004 3513 }
1e3c88bd 3514
5d6523eb 3515 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
367456c7
PZ
3516 goto next;
3517
3518 load = task_h_load(p);
5d6523eb 3519
eb95308e 3520 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
367456c7
PZ
3521 goto next;
3522
bd939f45 3523 if ((load / 2) > env->imbalance)
367456c7 3524 goto next;
1e3c88bd 3525
367456c7
PZ
3526 if (!can_migrate_task(p, env))
3527 goto next;
1e3c88bd 3528
ddcdf6e7 3529 move_task(p, env);
ee00e66f 3530 pulled++;
bd939f45 3531 env->imbalance -= load;
1e3c88bd
PZ
3532
3533#ifdef CONFIG_PREEMPT
ee00e66f
PZ
3534 /*
3535 * NEWIDLE balancing is a source of latency, so preemptible
3536 * kernels will stop after the first task is pulled to minimize
3537 * the critical section.
3538 */
5d6523eb 3539 if (env->idle == CPU_NEWLY_IDLE)
ee00e66f 3540 break;
1e3c88bd
PZ
3541#endif
3542
ee00e66f
PZ
3543 /*
3544 * We only want to steal up to the prescribed amount of
3545 * weighted load.
3546 */
bd939f45 3547 if (env->imbalance <= 0)
ee00e66f 3548 break;
367456c7
PZ
3549
3550 continue;
3551next:
5d6523eb 3552 list_move_tail(&p->se.group_node, tasks);
1e3c88bd 3553 }
5d6523eb 3554
1e3c88bd 3555 /*
ddcdf6e7
PZ
3556 * Right now, this is one of only two places move_task() is called,
3557 * so we can safely collect move_task() stats here rather than
3558 * inside move_task().
1e3c88bd 3559 */
8e45cb54 3560 schedstat_add(env->sd, lb_gained[env->idle], pulled);
1e3c88bd 3561
5d6523eb 3562 return pulled;
1e3c88bd
PZ
3563}
3564
230059de 3565#ifdef CONFIG_FAIR_GROUP_SCHED
9e3081ca
PZ
3566/*
3567 * update tg->load_weight by folding this cpu's load_avg
3568 */
67e86250 3569static int update_shares_cpu(struct task_group *tg, int cpu)
9e3081ca
PZ
3570{
3571 struct cfs_rq *cfs_rq;
3572 unsigned long flags;
3573 struct rq *rq;
9e3081ca
PZ
3574
3575 if (!tg->se[cpu])
3576 return 0;
3577
3578 rq = cpu_rq(cpu);
3579 cfs_rq = tg->cfs_rq[cpu];
3580
3581 raw_spin_lock_irqsave(&rq->lock, flags);
3582
3583 update_rq_clock(rq);
d6b55918 3584 update_cfs_load(cfs_rq, 1);
9ee474f5 3585 update_cfs_rq_blocked_load(cfs_rq);
9e3081ca
PZ
3586
3587 /*
3588 * We need to update shares after updating tg->load_weight in
3589 * order to adjust the weight of groups with long running tasks.
3590 */
6d5ab293 3591 update_cfs_shares(cfs_rq);
9e3081ca
PZ
3592
3593 raw_spin_unlock_irqrestore(&rq->lock, flags);
3594
3595 return 0;
3596}
3597
3598static void update_shares(int cpu)
3599{
3600 struct cfs_rq *cfs_rq;
3601 struct rq *rq = cpu_rq(cpu);
3602
3603 rcu_read_lock();
9763b67f
PZ
3604 /*
3605 * Iterates the task_group tree in a bottom up fashion, see
3606 * list_add_leaf_cfs_rq() for details.
3607 */
64660c86
PT
3608 for_each_leaf_cfs_rq(rq, cfs_rq) {
3609 /* throttled entities do not contribute to load */
3610 if (throttled_hierarchy(cfs_rq))
3611 continue;
3612
67e86250 3613 update_shares_cpu(cfs_rq->tg, cpu);
64660c86 3614 }
9e3081ca
PZ
3615 rcu_read_unlock();
3616}
3617
9763b67f
PZ
3618/*
3619 * Compute the cpu's hierarchical load factor for each task group.
3620 * This needs to be done in a top-down fashion because the load of a child
3621 * group is a fraction of its parents load.
3622 */
3623static int tg_load_down(struct task_group *tg, void *data)
3624{
3625 unsigned long load;
3626 long cpu = (long)data;
3627
3628 if (!tg->parent) {
3629 load = cpu_rq(cpu)->load.weight;
3630 } else {
3631 load = tg->parent->cfs_rq[cpu]->h_load;
3632 load *= tg->se[cpu]->load.weight;
3633 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3634 }
3635
3636 tg->cfs_rq[cpu]->h_load = load;
3637
3638 return 0;
3639}
3640
3641static void update_h_load(long cpu)
3642{
a35b6466
PZ
3643 struct rq *rq = cpu_rq(cpu);
3644 unsigned long now = jiffies;
3645
3646 if (rq->h_load_throttle == now)
3647 return;
3648
3649 rq->h_load_throttle = now;
3650
367456c7 3651 rcu_read_lock();
9763b67f 3652 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
367456c7 3653 rcu_read_unlock();
9763b67f
PZ
3654}
3655
367456c7 3656static unsigned long task_h_load(struct task_struct *p)
230059de 3657{
367456c7
PZ
3658 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3659 unsigned long load;
230059de 3660
367456c7
PZ
3661 load = p->se.load.weight;
3662 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
230059de 3663
367456c7 3664 return load;
230059de
PZ
3665}
3666#else
9e3081ca
PZ
3667static inline void update_shares(int cpu)
3668{
3669}
3670
367456c7 3671static inline void update_h_load(long cpu)
230059de 3672{
230059de 3673}
230059de 3674
367456c7 3675static unsigned long task_h_load(struct task_struct *p)
1e3c88bd 3676{
367456c7 3677 return p->se.load.weight;
1e3c88bd 3678}
230059de 3679#endif
1e3c88bd 3680
1e3c88bd
PZ
3681/********** Helpers for find_busiest_group ************************/
3682/*
3683 * sd_lb_stats - Structure to store the statistics of a sched_domain
3684 * during load balancing.
3685 */
3686struct sd_lb_stats {
3687 struct sched_group *busiest; /* Busiest group in this sd */
3688 struct sched_group *this; /* Local group in this sd */
3689 unsigned long total_load; /* Total load of all groups in sd */
3690 unsigned long total_pwr; /* Total power of all groups in sd */
3691 unsigned long avg_load; /* Average load across all groups in sd */
3692
3693 /** Statistics of this group */
3694 unsigned long this_load;
3695 unsigned long this_load_per_task;
3696 unsigned long this_nr_running;
fab47622 3697 unsigned long this_has_capacity;
aae6d3dd 3698 unsigned int this_idle_cpus;
1e3c88bd
PZ
3699
3700 /* Statistics of the busiest group */
aae6d3dd 3701 unsigned int busiest_idle_cpus;
1e3c88bd
PZ
3702 unsigned long max_load;
3703 unsigned long busiest_load_per_task;
3704 unsigned long busiest_nr_running;
dd5feea1 3705 unsigned long busiest_group_capacity;
fab47622 3706 unsigned long busiest_has_capacity;
aae6d3dd 3707 unsigned int busiest_group_weight;
1e3c88bd
PZ
3708
3709 int group_imb; /* Is there imbalance in this sd */
1e3c88bd
PZ
3710};
3711
3712/*
3713 * sg_lb_stats - stats of a sched_group required for load_balancing
3714 */
3715struct sg_lb_stats {
3716 unsigned long avg_load; /*Avg load across the CPUs of the group */
3717 unsigned long group_load; /* Total load over the CPUs of the group */
3718 unsigned long sum_nr_running; /* Nr tasks running in the group */
3719 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3720 unsigned long group_capacity;
aae6d3dd
SS
3721 unsigned long idle_cpus;
3722 unsigned long group_weight;
1e3c88bd 3723 int group_imb; /* Is there an imbalance in the group ? */
fab47622 3724 int group_has_capacity; /* Is there extra capacity in the group? */
1e3c88bd
PZ
3725};
3726
1e3c88bd
PZ
3727/**
3728 * get_sd_load_idx - Obtain the load index for a given sched domain.
3729 * @sd: The sched_domain whose load_idx is to be obtained.
3730 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3731 */
3732static inline int get_sd_load_idx(struct sched_domain *sd,
3733 enum cpu_idle_type idle)
3734{
3735 int load_idx;
3736
3737 switch (idle) {
3738 case CPU_NOT_IDLE:
3739 load_idx = sd->busy_idx;
3740 break;
3741
3742 case CPU_NEWLY_IDLE:
3743 load_idx = sd->newidle_idx;
3744 break;
3745 default:
3746 load_idx = sd->idle_idx;
3747 break;
3748 }
3749
3750 return load_idx;
3751}
3752
1e3c88bd
PZ
3753unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3754{
1399fa78 3755 return SCHED_POWER_SCALE;
1e3c88bd
PZ
3756}
3757
3758unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3759{
3760 return default_scale_freq_power(sd, cpu);
3761}
3762
3763unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3764{
669c55e9 3765 unsigned long weight = sd->span_weight;
1e3c88bd
PZ
3766 unsigned long smt_gain = sd->smt_gain;
3767
3768 smt_gain /= weight;
3769
3770 return smt_gain;
3771}
3772
3773unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3774{
3775 return default_scale_smt_power(sd, cpu);
3776}
3777
3778unsigned long scale_rt_power(int cpu)
3779{
3780 struct rq *rq = cpu_rq(cpu);
b654f7de 3781 u64 total, available, age_stamp, avg;
1e3c88bd 3782
b654f7de
PZ
3783 /*
3784 * Since we're reading these variables without serialization make sure
3785 * we read them once before doing sanity checks on them.
3786 */
3787 age_stamp = ACCESS_ONCE(rq->age_stamp);
3788 avg = ACCESS_ONCE(rq->rt_avg);
3789
3790 total = sched_avg_period() + (rq->clock - age_stamp);
aa483808 3791
b654f7de 3792 if (unlikely(total < avg)) {
aa483808
VP
3793 /* Ensures that power won't end up being negative */
3794 available = 0;
3795 } else {
b654f7de 3796 available = total - avg;
aa483808 3797 }
1e3c88bd 3798
1399fa78
NR
3799 if (unlikely((s64)total < SCHED_POWER_SCALE))
3800 total = SCHED_POWER_SCALE;
1e3c88bd 3801
1399fa78 3802 total >>= SCHED_POWER_SHIFT;
1e3c88bd
PZ
3803
3804 return div_u64(available, total);
3805}
3806
3807static void update_cpu_power(struct sched_domain *sd, int cpu)
3808{
669c55e9 3809 unsigned long weight = sd->span_weight;
1399fa78 3810 unsigned long power = SCHED_POWER_SCALE;
1e3c88bd
PZ
3811 struct sched_group *sdg = sd->groups;
3812
1e3c88bd
PZ
3813 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3814 if (sched_feat(ARCH_POWER))
3815 power *= arch_scale_smt_power(sd, cpu);
3816 else
3817 power *= default_scale_smt_power(sd, cpu);
3818
1399fa78 3819 power >>= SCHED_POWER_SHIFT;
1e3c88bd
PZ
3820 }
3821
9c3f75cb 3822 sdg->sgp->power_orig = power;
9d5efe05
SV
3823
3824 if (sched_feat(ARCH_POWER))
3825 power *= arch_scale_freq_power(sd, cpu);
3826 else
3827 power *= default_scale_freq_power(sd, cpu);
3828
1399fa78 3829 power >>= SCHED_POWER_SHIFT;
9d5efe05 3830
1e3c88bd 3831 power *= scale_rt_power(cpu);
1399fa78 3832 power >>= SCHED_POWER_SHIFT;
1e3c88bd
PZ
3833
3834 if (!power)
3835 power = 1;
3836
e51fd5e2 3837 cpu_rq(cpu)->cpu_power = power;
9c3f75cb 3838 sdg->sgp->power = power;
1e3c88bd
PZ
3839}
3840
029632fb 3841void update_group_power(struct sched_domain *sd, int cpu)
1e3c88bd
PZ
3842{
3843 struct sched_domain *child = sd->child;
3844 struct sched_group *group, *sdg = sd->groups;
3845 unsigned long power;
4ec4412e
VG
3846 unsigned long interval;
3847
3848 interval = msecs_to_jiffies(sd->balance_interval);
3849 interval = clamp(interval, 1UL, max_load_balance_interval);
3850 sdg->sgp->next_update = jiffies + interval;
1e3c88bd
PZ
3851
3852 if (!child) {
3853 update_cpu_power(sd, cpu);
3854 return;
3855 }
3856
3857 power = 0;
3858
74a5ce20
PZ
3859 if (child->flags & SD_OVERLAP) {
3860 /*
3861 * SD_OVERLAP domains cannot assume that child groups
3862 * span the current group.
3863 */
3864
3865 for_each_cpu(cpu, sched_group_cpus(sdg))
3866 power += power_of(cpu);
3867 } else {
3868 /*
3869 * !SD_OVERLAP domains can assume that child groups
3870 * span the current group.
3871 */
3872
3873 group = child->groups;
3874 do {
3875 power += group->sgp->power;
3876 group = group->next;
3877 } while (group != child->groups);
3878 }
1e3c88bd 3879
c3decf0d 3880 sdg->sgp->power_orig = sdg->sgp->power = power;
1e3c88bd
PZ
3881}
3882
9d5efe05
SV
3883/*
3884 * Try and fix up capacity for tiny siblings, this is needed when
3885 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3886 * which on its own isn't powerful enough.
3887 *
3888 * See update_sd_pick_busiest() and check_asym_packing().
3889 */
3890static inline int
3891fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3892{
3893 /*
1399fa78 3894 * Only siblings can have significantly less than SCHED_POWER_SCALE
9d5efe05 3895 */
a6c75f2f 3896 if (!(sd->flags & SD_SHARE_CPUPOWER))
9d5efe05
SV
3897 return 0;
3898
3899 /*
3900 * If ~90% of the cpu_power is still there, we're good.
3901 */
9c3f75cb 3902 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
9d5efe05
SV
3903 return 1;
3904
3905 return 0;
3906}
3907
1e3c88bd
PZ
3908/**
3909 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
cd96891d 3910 * @env: The load balancing environment.
1e3c88bd 3911 * @group: sched_group whose statistics are to be updated.
1e3c88bd 3912 * @load_idx: Load index of sched_domain of this_cpu for load calc.
1e3c88bd 3913 * @local_group: Does group contain this_cpu.
1e3c88bd
PZ
3914 * @balance: Should we balance.
3915 * @sgs: variable to hold the statistics for this group.
3916 */
bd939f45
PZ
3917static inline void update_sg_lb_stats(struct lb_env *env,
3918 struct sched_group *group, int load_idx,
b9403130 3919 int local_group, int *balance, struct sg_lb_stats *sgs)
1e3c88bd 3920{
e44bc5c5
PZ
3921 unsigned long nr_running, max_nr_running, min_nr_running;
3922 unsigned long load, max_cpu_load, min_cpu_load;
04f733b4 3923 unsigned int balance_cpu = -1, first_idle_cpu = 0;
dd5feea1 3924 unsigned long avg_load_per_task = 0;
bd939f45 3925 int i;
1e3c88bd 3926
871e35bc 3927 if (local_group)
c1174876 3928 balance_cpu = group_balance_cpu(group);
1e3c88bd
PZ
3929
3930 /* Tally up the load of all CPUs in the group */
1e3c88bd
PZ
3931 max_cpu_load = 0;
3932 min_cpu_load = ~0UL;
2582f0eb 3933 max_nr_running = 0;
e44bc5c5 3934 min_nr_running = ~0UL;
1e3c88bd 3935
b9403130 3936 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
1e3c88bd
PZ
3937 struct rq *rq = cpu_rq(i);
3938
e44bc5c5
PZ
3939 nr_running = rq->nr_running;
3940
1e3c88bd
PZ
3941 /* Bias balancing toward cpus of our domain */
3942 if (local_group) {
c1174876
PZ
3943 if (idle_cpu(i) && !first_idle_cpu &&
3944 cpumask_test_cpu(i, sched_group_mask(group))) {
04f733b4 3945 first_idle_cpu = 1;
1e3c88bd
PZ
3946 balance_cpu = i;
3947 }
04f733b4
PZ
3948
3949 load = target_load(i, load_idx);
1e3c88bd
PZ
3950 } else {
3951 load = source_load(i, load_idx);
e44bc5c5 3952 if (load > max_cpu_load)
1e3c88bd
PZ
3953 max_cpu_load = load;
3954 if (min_cpu_load > load)
3955 min_cpu_load = load;
e44bc5c5
PZ
3956
3957 if (nr_running > max_nr_running)
3958 max_nr_running = nr_running;
3959 if (min_nr_running > nr_running)
3960 min_nr_running = nr_running;
1e3c88bd
PZ
3961 }
3962
3963 sgs->group_load += load;
e44bc5c5 3964 sgs->sum_nr_running += nr_running;
1e3c88bd 3965 sgs->sum_weighted_load += weighted_cpuload(i);
aae6d3dd
SS
3966 if (idle_cpu(i))
3967 sgs->idle_cpus++;
1e3c88bd
PZ
3968 }
3969
3970 /*
3971 * First idle cpu or the first cpu(busiest) in this sched group
3972 * is eligible for doing load balancing at this and above
3973 * domains. In the newly idle case, we will allow all the cpu's
3974 * to do the newly idle load balance.
3975 */
4ec4412e 3976 if (local_group) {
bd939f45 3977 if (env->idle != CPU_NEWLY_IDLE) {
04f733b4 3978 if (balance_cpu != env->dst_cpu) {
4ec4412e
VG
3979 *balance = 0;
3980 return;
3981 }
bd939f45 3982 update_group_power(env->sd, env->dst_cpu);
4ec4412e 3983 } else if (time_after_eq(jiffies, group->sgp->next_update))
bd939f45 3984 update_group_power(env->sd, env->dst_cpu);
1e3c88bd
PZ
3985 }
3986
3987 /* Adjust by relative CPU power of the group */
9c3f75cb 3988 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
1e3c88bd 3989
1e3c88bd
PZ
3990 /*
3991 * Consider the group unbalanced when the imbalance is larger
866ab43e 3992 * than the average weight of a task.
1e3c88bd
PZ
3993 *
3994 * APZ: with cgroup the avg task weight can vary wildly and
3995 * might not be a suitable number - should we keep a
3996 * normalized nr_running number somewhere that negates
3997 * the hierarchy?
3998 */
dd5feea1
SS
3999 if (sgs->sum_nr_running)
4000 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
1e3c88bd 4001
e44bc5c5
PZ
4002 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4003 (max_nr_running - min_nr_running) > 1)
1e3c88bd
PZ
4004 sgs->group_imb = 1;
4005
9c3f75cb 4006 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
1399fa78 4007 SCHED_POWER_SCALE);
9d5efe05 4008 if (!sgs->group_capacity)
bd939f45 4009 sgs->group_capacity = fix_small_capacity(env->sd, group);
aae6d3dd 4010 sgs->group_weight = group->group_weight;
fab47622
NR
4011
4012 if (sgs->group_capacity > sgs->sum_nr_running)
4013 sgs->group_has_capacity = 1;
1e3c88bd
PZ
4014}
4015
532cb4c4
MN
4016/**
4017 * update_sd_pick_busiest - return 1 on busiest group
cd96891d 4018 * @env: The load balancing environment.
532cb4c4
MN
4019 * @sds: sched_domain statistics
4020 * @sg: sched_group candidate to be checked for being the busiest
b6b12294 4021 * @sgs: sched_group statistics
532cb4c4
MN
4022 *
4023 * Determine if @sg is a busier group than the previously selected
4024 * busiest group.
4025 */
bd939f45 4026static bool update_sd_pick_busiest(struct lb_env *env,
532cb4c4
MN
4027 struct sd_lb_stats *sds,
4028 struct sched_group *sg,
bd939f45 4029 struct sg_lb_stats *sgs)
532cb4c4
MN
4030{
4031 if (sgs->avg_load <= sds->max_load)
4032 return false;
4033
4034 if (sgs->sum_nr_running > sgs->group_capacity)
4035 return true;
4036
4037 if (sgs->group_imb)
4038 return true;
4039
4040 /*
4041 * ASYM_PACKING needs to move all the work to the lowest
4042 * numbered CPUs in the group, therefore mark all groups
4043 * higher than ourself as busy.
4044 */
bd939f45
PZ
4045 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4046 env->dst_cpu < group_first_cpu(sg)) {
532cb4c4
MN
4047 if (!sds->busiest)
4048 return true;
4049
4050 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4051 return true;
4052 }
4053
4054 return false;
4055}
4056
1e3c88bd 4057/**
461819ac 4058 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
cd96891d 4059 * @env: The load balancing environment.
1e3c88bd
PZ
4060 * @balance: Should we balance.
4061 * @sds: variable to hold the statistics for this sched_domain.
4062 */
bd939f45 4063static inline void update_sd_lb_stats(struct lb_env *env,
b9403130 4064 int *balance, struct sd_lb_stats *sds)
1e3c88bd 4065{
bd939f45
PZ
4066 struct sched_domain *child = env->sd->child;
4067 struct sched_group *sg = env->sd->groups;
1e3c88bd
PZ
4068 struct sg_lb_stats sgs;
4069 int load_idx, prefer_sibling = 0;
4070
4071 if (child && child->flags & SD_PREFER_SIBLING)
4072 prefer_sibling = 1;
4073
bd939f45 4074 load_idx = get_sd_load_idx(env->sd, env->idle);
1e3c88bd
PZ
4075
4076 do {
4077 int local_group;
4078
bd939f45 4079 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
1e3c88bd 4080 memset(&sgs, 0, sizeof(sgs));
b9403130 4081 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
1e3c88bd 4082
8f190fb3 4083 if (local_group && !(*balance))
1e3c88bd
PZ
4084 return;
4085
4086 sds->total_load += sgs.group_load;
9c3f75cb 4087 sds->total_pwr += sg->sgp->power;
1e3c88bd
PZ
4088
4089 /*
4090 * In case the child domain prefers tasks go to siblings
532cb4c4 4091 * first, lower the sg capacity to one so that we'll try
75dd321d
NR
4092 * and move all the excess tasks away. We lower the capacity
4093 * of a group only if the local group has the capacity to fit
4094 * these excess tasks, i.e. nr_running < group_capacity. The
4095 * extra check prevents the case where you always pull from the
4096 * heaviest group when it is already under-utilized (possible
4097 * with a large weight task outweighs the tasks on the system).
1e3c88bd 4098 */
75dd321d 4099 if (prefer_sibling && !local_group && sds->this_has_capacity)
1e3c88bd
PZ
4100 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4101
4102 if (local_group) {
4103 sds->this_load = sgs.avg_load;
532cb4c4 4104 sds->this = sg;
1e3c88bd
PZ
4105 sds->this_nr_running = sgs.sum_nr_running;
4106 sds->this_load_per_task = sgs.sum_weighted_load;
fab47622 4107 sds->this_has_capacity = sgs.group_has_capacity;
aae6d3dd 4108 sds->this_idle_cpus = sgs.idle_cpus;
bd939f45 4109 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
1e3c88bd 4110 sds->max_load = sgs.avg_load;
532cb4c4 4111 sds->busiest = sg;
1e3c88bd 4112 sds->busiest_nr_running = sgs.sum_nr_running;
aae6d3dd 4113 sds->busiest_idle_cpus = sgs.idle_cpus;
dd5feea1 4114 sds->busiest_group_capacity = sgs.group_capacity;
1e3c88bd 4115 sds->busiest_load_per_task = sgs.sum_weighted_load;
fab47622 4116 sds->busiest_has_capacity = sgs.group_has_capacity;
aae6d3dd 4117 sds->busiest_group_weight = sgs.group_weight;
1e3c88bd
PZ
4118 sds->group_imb = sgs.group_imb;
4119 }
4120
532cb4c4 4121 sg = sg->next;
bd939f45 4122 } while (sg != env->sd->groups);
532cb4c4
MN
4123}
4124
532cb4c4
MN
4125/**
4126 * check_asym_packing - Check to see if the group is packed into the
4127 * sched doman.
4128 *
4129 * This is primarily intended to used at the sibling level. Some
4130 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4131 * case of POWER7, it can move to lower SMT modes only when higher
4132 * threads are idle. When in lower SMT modes, the threads will
4133 * perform better since they share less core resources. Hence when we
4134 * have idle threads, we want them to be the higher ones.
4135 *
4136 * This packing function is run on idle threads. It checks to see if
4137 * the busiest CPU in this domain (core in the P7 case) has a higher
4138 * CPU number than the packing function is being run on. Here we are
4139 * assuming lower CPU number will be equivalent to lower a SMT thread
4140 * number.
4141 *
b6b12294
MN
4142 * Returns 1 when packing is required and a task should be moved to
4143 * this CPU. The amount of the imbalance is returned in *imbalance.
4144 *
cd96891d 4145 * @env: The load balancing environment.
532cb4c4 4146 * @sds: Statistics of the sched_domain which is to be packed
532cb4c4 4147 */
bd939f45 4148static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
532cb4c4
MN
4149{
4150 int busiest_cpu;
4151
bd939f45 4152 if (!(env->sd->flags & SD_ASYM_PACKING))
532cb4c4
MN
4153 return 0;
4154
4155 if (!sds->busiest)
4156 return 0;
4157
4158 busiest_cpu = group_first_cpu(sds->busiest);
bd939f45 4159 if (env->dst_cpu > busiest_cpu)
532cb4c4
MN
4160 return 0;
4161
bd939f45
PZ
4162 env->imbalance = DIV_ROUND_CLOSEST(
4163 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4164
532cb4c4 4165 return 1;
1e3c88bd
PZ
4166}
4167
4168/**
4169 * fix_small_imbalance - Calculate the minor imbalance that exists
4170 * amongst the groups of a sched_domain, during
4171 * load balancing.
cd96891d 4172 * @env: The load balancing environment.
1e3c88bd 4173 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
1e3c88bd 4174 */
bd939f45
PZ
4175static inline
4176void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
1e3c88bd
PZ
4177{
4178 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4179 unsigned int imbn = 2;
dd5feea1 4180 unsigned long scaled_busy_load_per_task;
1e3c88bd
PZ
4181
4182 if (sds->this_nr_running) {
4183 sds->this_load_per_task /= sds->this_nr_running;
4184 if (sds->busiest_load_per_task >
4185 sds->this_load_per_task)
4186 imbn = 1;
bd939f45 4187 } else {
1e3c88bd 4188 sds->this_load_per_task =
bd939f45
PZ
4189 cpu_avg_load_per_task(env->dst_cpu);
4190 }
1e3c88bd 4191
dd5feea1 4192 scaled_busy_load_per_task = sds->busiest_load_per_task
1399fa78 4193 * SCHED_POWER_SCALE;
9c3f75cb 4194 scaled_busy_load_per_task /= sds->busiest->sgp->power;
dd5feea1
SS
4195
4196 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4197 (scaled_busy_load_per_task * imbn)) {
bd939f45 4198 env->imbalance = sds->busiest_load_per_task;
1e3c88bd
PZ
4199 return;
4200 }
4201
4202 /*
4203 * OK, we don't have enough imbalance to justify moving tasks,
4204 * however we may be able to increase total CPU power used by
4205 * moving them.
4206 */
4207
9c3f75cb 4208 pwr_now += sds->busiest->sgp->power *
1e3c88bd 4209 min(sds->busiest_load_per_task, sds->max_load);
9c3f75cb 4210 pwr_now += sds->this->sgp->power *
1e3c88bd 4211 min(sds->this_load_per_task, sds->this_load);
1399fa78 4212 pwr_now /= SCHED_POWER_SCALE;
1e3c88bd
PZ
4213
4214 /* Amount of load we'd subtract */
1399fa78 4215 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
9c3f75cb 4216 sds->busiest->sgp->power;
1e3c88bd 4217 if (sds->max_load > tmp)
9c3f75cb 4218 pwr_move += sds->busiest->sgp->power *
1e3c88bd
PZ
4219 min(sds->busiest_load_per_task, sds->max_load - tmp);
4220
4221 /* Amount of load we'd add */
9c3f75cb 4222 if (sds->max_load * sds->busiest->sgp->power <
1399fa78 4223 sds->busiest_load_per_task * SCHED_POWER_SCALE)
9c3f75cb
PZ
4224 tmp = (sds->max_load * sds->busiest->sgp->power) /
4225 sds->this->sgp->power;
1e3c88bd 4226 else
1399fa78 4227 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
9c3f75cb
PZ
4228 sds->this->sgp->power;
4229 pwr_move += sds->this->sgp->power *
1e3c88bd 4230 min(sds->this_load_per_task, sds->this_load + tmp);
1399fa78 4231 pwr_move /= SCHED_POWER_SCALE;
1e3c88bd
PZ
4232
4233 /* Move if we gain throughput */
4234 if (pwr_move > pwr_now)
bd939f45 4235 env->imbalance = sds->busiest_load_per_task;
1e3c88bd
PZ
4236}
4237
4238/**
4239 * calculate_imbalance - Calculate the amount of imbalance present within the
4240 * groups of a given sched_domain during load balance.
bd939f45 4241 * @env: load balance environment
1e3c88bd 4242 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
1e3c88bd 4243 */
bd939f45 4244static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
1e3c88bd 4245{
dd5feea1
SS
4246 unsigned long max_pull, load_above_capacity = ~0UL;
4247
4248 sds->busiest_load_per_task /= sds->busiest_nr_running;
4249 if (sds->group_imb) {
4250 sds->busiest_load_per_task =
4251 min(sds->busiest_load_per_task, sds->avg_load);
4252 }
4253
1e3c88bd
PZ
4254 /*
4255 * In the presence of smp nice balancing, certain scenarios can have
4256 * max load less than avg load(as we skip the groups at or below
4257 * its cpu_power, while calculating max_load..)
4258 */
4259 if (sds->max_load < sds->avg_load) {
bd939f45
PZ
4260 env->imbalance = 0;
4261 return fix_small_imbalance(env, sds);
1e3c88bd
PZ
4262 }
4263
dd5feea1
SS
4264 if (!sds->group_imb) {
4265 /*
4266 * Don't want to pull so many tasks that a group would go idle.
4267 */
4268 load_above_capacity = (sds->busiest_nr_running -
4269 sds->busiest_group_capacity);
4270
1399fa78 4271 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
dd5feea1 4272
9c3f75cb 4273 load_above_capacity /= sds->busiest->sgp->power;
dd5feea1
SS
4274 }
4275
4276 /*
4277 * We're trying to get all the cpus to the average_load, so we don't
4278 * want to push ourselves above the average load, nor do we wish to
4279 * reduce the max loaded cpu below the average load. At the same time,
4280 * we also don't want to reduce the group load below the group capacity
4281 * (so that we can implement power-savings policies etc). Thus we look
4282 * for the minimum possible imbalance.
4283 * Be careful of negative numbers as they'll appear as very large values
4284 * with unsigned longs.
4285 */
4286 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
1e3c88bd
PZ
4287
4288 /* How much load to actually move to equalise the imbalance */
bd939f45 4289 env->imbalance = min(max_pull * sds->busiest->sgp->power,
9c3f75cb 4290 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
1399fa78 4291 / SCHED_POWER_SCALE;
1e3c88bd
PZ
4292
4293 /*
4294 * if *imbalance is less than the average load per runnable task
25985edc 4295 * there is no guarantee that any tasks will be moved so we'll have
1e3c88bd
PZ
4296 * a think about bumping its value to force at least one task to be
4297 * moved
4298 */
bd939f45
PZ
4299 if (env->imbalance < sds->busiest_load_per_task)
4300 return fix_small_imbalance(env, sds);
1e3c88bd
PZ
4301
4302}
fab47622 4303
1e3c88bd
PZ
4304/******* find_busiest_group() helpers end here *********************/
4305
4306/**
4307 * find_busiest_group - Returns the busiest group within the sched_domain
4308 * if there is an imbalance. If there isn't an imbalance, and
4309 * the user has opted for power-savings, it returns a group whose
4310 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4311 * such a group exists.
4312 *
4313 * Also calculates the amount of weighted load which should be moved
4314 * to restore balance.
4315 *
cd96891d 4316 * @env: The load balancing environment.
1e3c88bd
PZ
4317 * @balance: Pointer to a variable indicating if this_cpu
4318 * is the appropriate cpu to perform load balancing at this_level.
4319 *
4320 * Returns: - the busiest group if imbalance exists.
4321 * - If no imbalance and user has opted for power-savings balance,
4322 * return the least loaded group whose CPUs can be
4323 * put to idle by rebalancing its tasks onto our group.
4324 */
4325static struct sched_group *
b9403130 4326find_busiest_group(struct lb_env *env, int *balance)
1e3c88bd
PZ
4327{
4328 struct sd_lb_stats sds;
4329
4330 memset(&sds, 0, sizeof(sds));
4331
4332 /*
4333 * Compute the various statistics relavent for load balancing at
4334 * this level.
4335 */
b9403130 4336 update_sd_lb_stats(env, balance, &sds);
1e3c88bd 4337
cc57aa8f
PZ
4338 /*
4339 * this_cpu is not the appropriate cpu to perform load balancing at
4340 * this level.
1e3c88bd 4341 */
8f190fb3 4342 if (!(*balance))
1e3c88bd
PZ
4343 goto ret;
4344
bd939f45
PZ
4345 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4346 check_asym_packing(env, &sds))
532cb4c4
MN
4347 return sds.busiest;
4348
cc57aa8f 4349 /* There is no busy sibling group to pull tasks from */
1e3c88bd
PZ
4350 if (!sds.busiest || sds.busiest_nr_running == 0)
4351 goto out_balanced;
4352
1399fa78 4353 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
b0432d8f 4354
866ab43e
PZ
4355 /*
4356 * If the busiest group is imbalanced the below checks don't
4357 * work because they assumes all things are equal, which typically
4358 * isn't true due to cpus_allowed constraints and the like.
4359 */
4360 if (sds.group_imb)
4361 goto force_balance;
4362
cc57aa8f 4363 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
bd939f45 4364 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
fab47622
NR
4365 !sds.busiest_has_capacity)
4366 goto force_balance;
4367
cc57aa8f
PZ
4368 /*
4369 * If the local group is more busy than the selected busiest group
4370 * don't try and pull any tasks.
4371 */
1e3c88bd
PZ
4372 if (sds.this_load >= sds.max_load)
4373 goto out_balanced;
4374
cc57aa8f
PZ
4375 /*
4376 * Don't pull any tasks if this group is already above the domain
4377 * average load.
4378 */
1e3c88bd
PZ
4379 if (sds.this_load >= sds.avg_load)
4380 goto out_balanced;
4381
bd939f45 4382 if (env->idle == CPU_IDLE) {
aae6d3dd
SS
4383 /*
4384 * This cpu is idle. If the busiest group load doesn't
4385 * have more tasks than the number of available cpu's and
4386 * there is no imbalance between this and busiest group
4387 * wrt to idle cpu's, it is balanced.
4388 */
c186fafe 4389 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
aae6d3dd
SS
4390 sds.busiest_nr_running <= sds.busiest_group_weight)
4391 goto out_balanced;
c186fafe
PZ
4392 } else {
4393 /*
4394 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4395 * imbalance_pct to be conservative.
4396 */
bd939f45 4397 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
c186fafe 4398 goto out_balanced;
aae6d3dd 4399 }
1e3c88bd 4400
fab47622 4401force_balance:
1e3c88bd 4402 /* Looks like there is an imbalance. Compute it */
bd939f45 4403 calculate_imbalance(env, &sds);
1e3c88bd
PZ
4404 return sds.busiest;
4405
4406out_balanced:
1e3c88bd 4407ret:
bd939f45 4408 env->imbalance = 0;
1e3c88bd
PZ
4409 return NULL;
4410}
4411
4412/*
4413 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4414 */
bd939f45 4415static struct rq *find_busiest_queue(struct lb_env *env,
b9403130 4416 struct sched_group *group)
1e3c88bd
PZ
4417{
4418 struct rq *busiest = NULL, *rq;
4419 unsigned long max_load = 0;
4420 int i;
4421
4422 for_each_cpu(i, sched_group_cpus(group)) {
4423 unsigned long power = power_of(i);
1399fa78
NR
4424 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4425 SCHED_POWER_SCALE);
1e3c88bd
PZ
4426 unsigned long wl;
4427
9d5efe05 4428 if (!capacity)
bd939f45 4429 capacity = fix_small_capacity(env->sd, group);
9d5efe05 4430
b9403130 4431 if (!cpumask_test_cpu(i, env->cpus))
1e3c88bd
PZ
4432 continue;
4433
4434 rq = cpu_rq(i);
6e40f5bb 4435 wl = weighted_cpuload(i);
1e3c88bd 4436
6e40f5bb
TG
4437 /*
4438 * When comparing with imbalance, use weighted_cpuload()
4439 * which is not scaled with the cpu power.
4440 */
bd939f45 4441 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
1e3c88bd
PZ
4442 continue;
4443
6e40f5bb
TG
4444 /*
4445 * For the load comparisons with the other cpu's, consider
4446 * the weighted_cpuload() scaled with the cpu power, so that
4447 * the load can be moved away from the cpu that is potentially
4448 * running at a lower capacity.
4449 */
1399fa78 4450 wl = (wl * SCHED_POWER_SCALE) / power;
6e40f5bb 4451
1e3c88bd
PZ
4452 if (wl > max_load) {
4453 max_load = wl;
4454 busiest = rq;
4455 }
4456 }
4457
4458 return busiest;
4459}
4460
4461/*
4462 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4463 * so long as it is large enough.
4464 */
4465#define MAX_PINNED_INTERVAL 512
4466
4467/* Working cpumask for load_balance and load_balance_newidle. */
029632fb 4468DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
1e3c88bd 4469
bd939f45 4470static int need_active_balance(struct lb_env *env)
1af3ed3d 4471{
bd939f45
PZ
4472 struct sched_domain *sd = env->sd;
4473
4474 if (env->idle == CPU_NEWLY_IDLE) {
532cb4c4
MN
4475
4476 /*
4477 * ASYM_PACKING needs to force migrate tasks from busy but
4478 * higher numbered CPUs in order to pack all tasks in the
4479 * lowest numbered CPUs.
4480 */
bd939f45 4481 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
532cb4c4 4482 return 1;
1af3ed3d
PZ
4483 }
4484
4485 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4486}
4487
969c7921
TH
4488static int active_load_balance_cpu_stop(void *data);
4489
1e3c88bd
PZ
4490/*
4491 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4492 * tasks if there is an imbalance.
4493 */
4494static int load_balance(int this_cpu, struct rq *this_rq,
4495 struct sched_domain *sd, enum cpu_idle_type idle,
4496 int *balance)
4497{
88b8dac0
SV
4498 int ld_moved, cur_ld_moved, active_balance = 0;
4499 int lb_iterations, max_lb_iterations;
1e3c88bd 4500 struct sched_group *group;
1e3c88bd
PZ
4501 struct rq *busiest;
4502 unsigned long flags;
4503 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4504
8e45cb54
PZ
4505 struct lb_env env = {
4506 .sd = sd,
ddcdf6e7
PZ
4507 .dst_cpu = this_cpu,
4508 .dst_rq = this_rq,
88b8dac0 4509 .dst_grpmask = sched_group_cpus(sd->groups),
8e45cb54 4510 .idle = idle,
eb95308e 4511 .loop_break = sched_nr_migrate_break,
b9403130 4512 .cpus = cpus,
8e45cb54
PZ
4513 };
4514
1e3c88bd 4515 cpumask_copy(cpus, cpu_active_mask);
88b8dac0 4516 max_lb_iterations = cpumask_weight(env.dst_grpmask);
1e3c88bd 4517
1e3c88bd
PZ
4518 schedstat_inc(sd, lb_count[idle]);
4519
4520redo:
b9403130 4521 group = find_busiest_group(&env, balance);
1e3c88bd
PZ
4522
4523 if (*balance == 0)
4524 goto out_balanced;
4525
4526 if (!group) {
4527 schedstat_inc(sd, lb_nobusyg[idle]);
4528 goto out_balanced;
4529 }
4530
b9403130 4531 busiest = find_busiest_queue(&env, group);
1e3c88bd
PZ
4532 if (!busiest) {
4533 schedstat_inc(sd, lb_nobusyq[idle]);
4534 goto out_balanced;
4535 }
4536
78feefc5 4537 BUG_ON(busiest == env.dst_rq);
1e3c88bd 4538
bd939f45 4539 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
1e3c88bd
PZ
4540
4541 ld_moved = 0;
88b8dac0 4542 lb_iterations = 1;
1e3c88bd
PZ
4543 if (busiest->nr_running > 1) {
4544 /*
4545 * Attempt to move tasks. If find_busiest_group has found
4546 * an imbalance but busiest->nr_running <= 1, the group is
4547 * still unbalanced. ld_moved simply stays zero, so it is
4548 * correctly treated as an imbalance.
4549 */
8e45cb54 4550 env.flags |= LBF_ALL_PINNED;
c82513e5
PZ
4551 env.src_cpu = busiest->cpu;
4552 env.src_rq = busiest;
4553 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
8e45cb54 4554
a35b6466 4555 update_h_load(env.src_cpu);
5d6523eb 4556more_balance:
1e3c88bd 4557 local_irq_save(flags);
78feefc5 4558 double_rq_lock(env.dst_rq, busiest);
88b8dac0
SV
4559
4560 /*
4561 * cur_ld_moved - load moved in current iteration
4562 * ld_moved - cumulative load moved across iterations
4563 */
4564 cur_ld_moved = move_tasks(&env);
4565 ld_moved += cur_ld_moved;
78feefc5 4566 double_rq_unlock(env.dst_rq, busiest);
1e3c88bd
PZ
4567 local_irq_restore(flags);
4568
5d6523eb
PZ
4569 if (env.flags & LBF_NEED_BREAK) {
4570 env.flags &= ~LBF_NEED_BREAK;
4571 goto more_balance;
4572 }
4573
1e3c88bd
PZ
4574 /*
4575 * some other cpu did the load balance for us.
4576 */
88b8dac0
SV
4577 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4578 resched_cpu(env.dst_cpu);
4579
4580 /*
4581 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4582 * us and move them to an alternate dst_cpu in our sched_group
4583 * where they can run. The upper limit on how many times we
4584 * iterate on same src_cpu is dependent on number of cpus in our
4585 * sched_group.
4586 *
4587 * This changes load balance semantics a bit on who can move
4588 * load to a given_cpu. In addition to the given_cpu itself
4589 * (or a ilb_cpu acting on its behalf where given_cpu is
4590 * nohz-idle), we now have balance_cpu in a position to move
4591 * load to given_cpu. In rare situations, this may cause
4592 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4593 * _independently_ and at _same_ time to move some load to
4594 * given_cpu) causing exceess load to be moved to given_cpu.
4595 * This however should not happen so much in practice and
4596 * moreover subsequent load balance cycles should correct the
4597 * excess load moved.
4598 */
4599 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4600 lb_iterations++ < max_lb_iterations) {
4601
78feefc5 4602 env.dst_rq = cpu_rq(env.new_dst_cpu);
88b8dac0
SV
4603 env.dst_cpu = env.new_dst_cpu;
4604 env.flags &= ~LBF_SOME_PINNED;
4605 env.loop = 0;
4606 env.loop_break = sched_nr_migrate_break;
4607 /*
4608 * Go back to "more_balance" rather than "redo" since we
4609 * need to continue with same src_cpu.
4610 */
4611 goto more_balance;
4612 }
1e3c88bd
PZ
4613
4614 /* All tasks on this runqueue were pinned by CPU affinity */
8e45cb54 4615 if (unlikely(env.flags & LBF_ALL_PINNED)) {
1e3c88bd 4616 cpumask_clear_cpu(cpu_of(busiest), cpus);
bbf18b19
PN
4617 if (!cpumask_empty(cpus)) {
4618 env.loop = 0;
4619 env.loop_break = sched_nr_migrate_break;
1e3c88bd 4620 goto redo;
bbf18b19 4621 }
1e3c88bd
PZ
4622 goto out_balanced;
4623 }
4624 }
4625
4626 if (!ld_moved) {
4627 schedstat_inc(sd, lb_failed[idle]);
58b26c4c
VP
4628 /*
4629 * Increment the failure counter only on periodic balance.
4630 * We do not want newidle balance, which can be very
4631 * frequent, pollute the failure counter causing
4632 * excessive cache_hot migrations and active balances.
4633 */
4634 if (idle != CPU_NEWLY_IDLE)
4635 sd->nr_balance_failed++;
1e3c88bd 4636
bd939f45 4637 if (need_active_balance(&env)) {
1e3c88bd
PZ
4638 raw_spin_lock_irqsave(&busiest->lock, flags);
4639
969c7921
TH
4640 /* don't kick the active_load_balance_cpu_stop,
4641 * if the curr task on busiest cpu can't be
4642 * moved to this_cpu
1e3c88bd
PZ
4643 */
4644 if (!cpumask_test_cpu(this_cpu,
fa17b507 4645 tsk_cpus_allowed(busiest->curr))) {
1e3c88bd
PZ
4646 raw_spin_unlock_irqrestore(&busiest->lock,
4647 flags);
8e45cb54 4648 env.flags |= LBF_ALL_PINNED;
1e3c88bd
PZ
4649 goto out_one_pinned;
4650 }
4651
969c7921
TH
4652 /*
4653 * ->active_balance synchronizes accesses to
4654 * ->active_balance_work. Once set, it's cleared
4655 * only after active load balance is finished.
4656 */
1e3c88bd
PZ
4657 if (!busiest->active_balance) {
4658 busiest->active_balance = 1;
4659 busiest->push_cpu = this_cpu;
4660 active_balance = 1;
4661 }
4662 raw_spin_unlock_irqrestore(&busiest->lock, flags);
969c7921 4663
bd939f45 4664 if (active_balance) {
969c7921
TH
4665 stop_one_cpu_nowait(cpu_of(busiest),
4666 active_load_balance_cpu_stop, busiest,
4667 &busiest->active_balance_work);
bd939f45 4668 }
1e3c88bd
PZ
4669
4670 /*
4671 * We've kicked active balancing, reset the failure
4672 * counter.
4673 */
4674 sd->nr_balance_failed = sd->cache_nice_tries+1;
4675 }
4676 } else
4677 sd->nr_balance_failed = 0;
4678
4679 if (likely(!active_balance)) {
4680 /* We were unbalanced, so reset the balancing interval */
4681 sd->balance_interval = sd->min_interval;
4682 } else {
4683 /*
4684 * If we've begun active balancing, start to back off. This
4685 * case may not be covered by the all_pinned logic if there
4686 * is only 1 task on the busy runqueue (because we don't call
4687 * move_tasks).
4688 */
4689 if (sd->balance_interval < sd->max_interval)
4690 sd->balance_interval *= 2;
4691 }
4692
1e3c88bd
PZ
4693 goto out;
4694
4695out_balanced:
4696 schedstat_inc(sd, lb_balanced[idle]);
4697
4698 sd->nr_balance_failed = 0;
4699
4700out_one_pinned:
4701 /* tune up the balancing interval */
8e45cb54 4702 if (((env.flags & LBF_ALL_PINNED) &&
5b54b56b 4703 sd->balance_interval < MAX_PINNED_INTERVAL) ||
1e3c88bd
PZ
4704 (sd->balance_interval < sd->max_interval))
4705 sd->balance_interval *= 2;
4706
46e49b38 4707 ld_moved = 0;
1e3c88bd 4708out:
1e3c88bd
PZ
4709 return ld_moved;
4710}
4711
1e3c88bd
PZ
4712/*
4713 * idle_balance is called by schedule() if this_cpu is about to become
4714 * idle. Attempts to pull tasks from other CPUs.
4715 */
029632fb 4716void idle_balance(int this_cpu, struct rq *this_rq)
1e3c88bd
PZ
4717{
4718 struct sched_domain *sd;
4719 int pulled_task = 0;
4720 unsigned long next_balance = jiffies + HZ;
4721
4722 this_rq->idle_stamp = this_rq->clock;
4723
4724 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4725 return;
4726
18bf2805
BS
4727 update_rq_runnable_avg(this_rq, 1);
4728
f492e12e
PZ
4729 /*
4730 * Drop the rq->lock, but keep IRQ/preempt disabled.
4731 */
4732 raw_spin_unlock(&this_rq->lock);
4733
c66eaf61 4734 update_shares(this_cpu);
dce840a0 4735 rcu_read_lock();
1e3c88bd
PZ
4736 for_each_domain(this_cpu, sd) {
4737 unsigned long interval;
f492e12e 4738 int balance = 1;
1e3c88bd
PZ
4739
4740 if (!(sd->flags & SD_LOAD_BALANCE))
4741 continue;
4742
f492e12e 4743 if (sd->flags & SD_BALANCE_NEWIDLE) {
1e3c88bd 4744 /* If we've pulled tasks over stop searching: */
f492e12e
PZ
4745 pulled_task = load_balance(this_cpu, this_rq,
4746 sd, CPU_NEWLY_IDLE, &balance);
4747 }
1e3c88bd
PZ
4748
4749 interval = msecs_to_jiffies(sd->balance_interval);
4750 if (time_after(next_balance, sd->last_balance + interval))
4751 next_balance = sd->last_balance + interval;
d5ad140b
NR
4752 if (pulled_task) {
4753 this_rq->idle_stamp = 0;
1e3c88bd 4754 break;
d5ad140b 4755 }
1e3c88bd 4756 }
dce840a0 4757 rcu_read_unlock();
f492e12e
PZ
4758
4759 raw_spin_lock(&this_rq->lock);
4760
1e3c88bd
PZ
4761 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4762 /*
4763 * We are going idle. next_balance may be set based on
4764 * a busy processor. So reset next_balance.
4765 */
4766 this_rq->next_balance = next_balance;
4767 }
4768}
4769
4770/*
969c7921
TH
4771 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4772 * running tasks off the busiest CPU onto idle CPUs. It requires at
4773 * least 1 task to be running on each physical CPU where possible, and
4774 * avoids physical / logical imbalances.
1e3c88bd 4775 */
969c7921 4776static int active_load_balance_cpu_stop(void *data)
1e3c88bd 4777{
969c7921
TH
4778 struct rq *busiest_rq = data;
4779 int busiest_cpu = cpu_of(busiest_rq);
1e3c88bd 4780 int target_cpu = busiest_rq->push_cpu;
969c7921 4781 struct rq *target_rq = cpu_rq(target_cpu);
1e3c88bd 4782 struct sched_domain *sd;
969c7921
TH
4783
4784 raw_spin_lock_irq(&busiest_rq->lock);
4785
4786 /* make sure the requested cpu hasn't gone down in the meantime */
4787 if (unlikely(busiest_cpu != smp_processor_id() ||
4788 !busiest_rq->active_balance))
4789 goto out_unlock;
1e3c88bd
PZ
4790
4791 /* Is there any task to move? */
4792 if (busiest_rq->nr_running <= 1)
969c7921 4793 goto out_unlock;
1e3c88bd
PZ
4794
4795 /*
4796 * This condition is "impossible", if it occurs
4797 * we need to fix it. Originally reported by
4798 * Bjorn Helgaas on a 128-cpu setup.
4799 */
4800 BUG_ON(busiest_rq == target_rq);
4801
4802 /* move a task from busiest_rq to target_rq */
4803 double_lock_balance(busiest_rq, target_rq);
1e3c88bd
PZ
4804
4805 /* Search for an sd spanning us and the target CPU. */
dce840a0 4806 rcu_read_lock();
1e3c88bd
PZ
4807 for_each_domain(target_cpu, sd) {
4808 if ((sd->flags & SD_LOAD_BALANCE) &&
4809 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4810 break;
4811 }
4812
4813 if (likely(sd)) {
8e45cb54
PZ
4814 struct lb_env env = {
4815 .sd = sd,
ddcdf6e7
PZ
4816 .dst_cpu = target_cpu,
4817 .dst_rq = target_rq,
4818 .src_cpu = busiest_rq->cpu,
4819 .src_rq = busiest_rq,
8e45cb54
PZ
4820 .idle = CPU_IDLE,
4821 };
4822
1e3c88bd
PZ
4823 schedstat_inc(sd, alb_count);
4824
8e45cb54 4825 if (move_one_task(&env))
1e3c88bd
PZ
4826 schedstat_inc(sd, alb_pushed);
4827 else
4828 schedstat_inc(sd, alb_failed);
4829 }
dce840a0 4830 rcu_read_unlock();
1e3c88bd 4831 double_unlock_balance(busiest_rq, target_rq);
969c7921
TH
4832out_unlock:
4833 busiest_rq->active_balance = 0;
4834 raw_spin_unlock_irq(&busiest_rq->lock);
4835 return 0;
1e3c88bd
PZ
4836}
4837
4838#ifdef CONFIG_NO_HZ
83cd4fe2
VP
4839/*
4840 * idle load balancing details
83cd4fe2
VP
4841 * - When one of the busy CPUs notice that there may be an idle rebalancing
4842 * needed, they will kick the idle load balancer, which then does idle
4843 * load balancing for all the idle CPUs.
4844 */
1e3c88bd 4845static struct {
83cd4fe2 4846 cpumask_var_t idle_cpus_mask;
0b005cf5 4847 atomic_t nr_cpus;
83cd4fe2
VP
4848 unsigned long next_balance; /* in jiffy units */
4849} nohz ____cacheline_aligned;
1e3c88bd 4850
8e7fbcbc 4851static inline int find_new_ilb(int call_cpu)
1e3c88bd 4852{
0b005cf5 4853 int ilb = cpumask_first(nohz.idle_cpus_mask);
1e3c88bd 4854
786d6dc7
SS
4855 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4856 return ilb;
4857
4858 return nr_cpu_ids;
1e3c88bd 4859}
1e3c88bd 4860
83cd4fe2
VP
4861/*
4862 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4863 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4864 * CPU (if there is one).
4865 */
4866static void nohz_balancer_kick(int cpu)
4867{
4868 int ilb_cpu;
4869
4870 nohz.next_balance++;
4871
0b005cf5 4872 ilb_cpu = find_new_ilb(cpu);
83cd4fe2 4873
0b005cf5
SS
4874 if (ilb_cpu >= nr_cpu_ids)
4875 return;
83cd4fe2 4876
cd490c5b 4877 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
1c792db7
SS
4878 return;
4879 /*
4880 * Use smp_send_reschedule() instead of resched_cpu().
4881 * This way we generate a sched IPI on the target cpu which
4882 * is idle. And the softirq performing nohz idle load balance
4883 * will be run before returning from the IPI.
4884 */
4885 smp_send_reschedule(ilb_cpu);
83cd4fe2
VP
4886 return;
4887}
4888
c1cc017c 4889static inline void nohz_balance_exit_idle(int cpu)
71325960
SS
4890{
4891 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4892 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4893 atomic_dec(&nohz.nr_cpus);
4894 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4895 }
4896}
4897
69e1e811
SS
4898static inline void set_cpu_sd_state_busy(void)
4899{
4900 struct sched_domain *sd;
4901 int cpu = smp_processor_id();
4902
4903 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4904 return;
4905 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4906
4907 rcu_read_lock();
4908 for_each_domain(cpu, sd)
4909 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4910 rcu_read_unlock();
4911}
4912
4913void set_cpu_sd_state_idle(void)
4914{
4915 struct sched_domain *sd;
4916 int cpu = smp_processor_id();
4917
4918 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4919 return;
4920 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4921
4922 rcu_read_lock();
4923 for_each_domain(cpu, sd)
4924 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4925 rcu_read_unlock();
4926}
4927
1e3c88bd 4928/*
c1cc017c 4929 * This routine will record that the cpu is going idle with tick stopped.
0b005cf5 4930 * This info will be used in performing idle load balancing in the future.
1e3c88bd 4931 */
c1cc017c 4932void nohz_balance_enter_idle(int cpu)
1e3c88bd 4933{
71325960
SS
4934 /*
4935 * If this cpu is going down, then nothing needs to be done.
4936 */
4937 if (!cpu_active(cpu))
4938 return;
4939
c1cc017c
AS
4940 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
4941 return;
1e3c88bd 4942
c1cc017c
AS
4943 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
4944 atomic_inc(&nohz.nr_cpus);
4945 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
1e3c88bd 4946}
71325960
SS
4947
4948static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4949 unsigned long action, void *hcpu)
4950{
4951 switch (action & ~CPU_TASKS_FROZEN) {
4952 case CPU_DYING:
c1cc017c 4953 nohz_balance_exit_idle(smp_processor_id());
71325960
SS
4954 return NOTIFY_OK;
4955 default:
4956 return NOTIFY_DONE;
4957 }
4958}
1e3c88bd
PZ
4959#endif
4960
4961static DEFINE_SPINLOCK(balancing);
4962
49c022e6
PZ
4963/*
4964 * Scale the max load_balance interval with the number of CPUs in the system.
4965 * This trades load-balance latency on larger machines for less cross talk.
4966 */
029632fb 4967void update_max_interval(void)
49c022e6
PZ
4968{
4969 max_load_balance_interval = HZ*num_online_cpus()/10;
4970}
4971
1e3c88bd
PZ
4972/*
4973 * It checks each scheduling domain to see if it is due to be balanced,
4974 * and initiates a balancing operation if so.
4975 *
4976 * Balancing parameters are set up in arch_init_sched_domains.
4977 */
4978static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4979{
4980 int balance = 1;
4981 struct rq *rq = cpu_rq(cpu);
4982 unsigned long interval;
04f733b4 4983 struct sched_domain *sd;
1e3c88bd
PZ
4984 /* Earliest time when we have to do rebalance again */
4985 unsigned long next_balance = jiffies + 60*HZ;
4986 int update_next_balance = 0;
4987 int need_serialize;
4988
2069dd75
PZ
4989 update_shares(cpu);
4990
dce840a0 4991 rcu_read_lock();
1e3c88bd
PZ
4992 for_each_domain(cpu, sd) {
4993 if (!(sd->flags & SD_LOAD_BALANCE))
4994 continue;
4995
4996 interval = sd->balance_interval;
4997 if (idle != CPU_IDLE)
4998 interval *= sd->busy_factor;
4999
5000 /* scale ms to jiffies */
5001 interval = msecs_to_jiffies(interval);
49c022e6 5002 interval = clamp(interval, 1UL, max_load_balance_interval);
1e3c88bd
PZ
5003
5004 need_serialize = sd->flags & SD_SERIALIZE;
5005
5006 if (need_serialize) {
5007 if (!spin_trylock(&balancing))
5008 goto out;
5009 }
5010
5011 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5012 if (load_balance(cpu, rq, sd, idle, &balance)) {
5013 /*
5014 * We've pulled tasks over so either we're no
c186fafe 5015 * longer idle.
1e3c88bd
PZ
5016 */
5017 idle = CPU_NOT_IDLE;
5018 }
5019 sd->last_balance = jiffies;
5020 }
5021 if (need_serialize)
5022 spin_unlock(&balancing);
5023out:
5024 if (time_after(next_balance, sd->last_balance + interval)) {
5025 next_balance = sd->last_balance + interval;
5026 update_next_balance = 1;
5027 }
5028
5029 /*
5030 * Stop the load balance at this level. There is another
5031 * CPU in our sched group which is doing load balancing more
5032 * actively.
5033 */
5034 if (!balance)
5035 break;
5036 }
dce840a0 5037 rcu_read_unlock();
1e3c88bd
PZ
5038
5039 /*
5040 * next_balance will be updated only when there is a need.
5041 * When the cpu is attached to null domain for ex, it will not be
5042 * updated.
5043 */
5044 if (likely(update_next_balance))
5045 rq->next_balance = next_balance;
5046}
5047
83cd4fe2 5048#ifdef CONFIG_NO_HZ
1e3c88bd 5049/*
83cd4fe2 5050 * In CONFIG_NO_HZ case, the idle balance kickee will do the
1e3c88bd
PZ
5051 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5052 */
83cd4fe2
VP
5053static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5054{
5055 struct rq *this_rq = cpu_rq(this_cpu);
5056 struct rq *rq;
5057 int balance_cpu;
5058
1c792db7
SS
5059 if (idle != CPU_IDLE ||
5060 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5061 goto end;
83cd4fe2
VP
5062
5063 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
8a6d42d1 5064 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
83cd4fe2
VP
5065 continue;
5066
5067 /*
5068 * If this cpu gets work to do, stop the load balancing
5069 * work being done for other cpus. Next load
5070 * balancing owner will pick it up.
5071 */
1c792db7 5072 if (need_resched())
83cd4fe2 5073 break;
83cd4fe2 5074
5ed4f1d9
VG
5075 rq = cpu_rq(balance_cpu);
5076
5077 raw_spin_lock_irq(&rq->lock);
5078 update_rq_clock(rq);
5079 update_idle_cpu_load(rq);
5080 raw_spin_unlock_irq(&rq->lock);
83cd4fe2
VP
5081
5082 rebalance_domains(balance_cpu, CPU_IDLE);
5083
83cd4fe2
VP
5084 if (time_after(this_rq->next_balance, rq->next_balance))
5085 this_rq->next_balance = rq->next_balance;
5086 }
5087 nohz.next_balance = this_rq->next_balance;
1c792db7
SS
5088end:
5089 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
83cd4fe2
VP
5090}
5091
5092/*
0b005cf5
SS
5093 * Current heuristic for kicking the idle load balancer in the presence
5094 * of an idle cpu is the system.
5095 * - This rq has more than one task.
5096 * - At any scheduler domain level, this cpu's scheduler group has multiple
5097 * busy cpu's exceeding the group's power.
5098 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5099 * domain span are idle.
83cd4fe2
VP
5100 */
5101static inline int nohz_kick_needed(struct rq *rq, int cpu)
5102{
5103 unsigned long now = jiffies;
0b005cf5 5104 struct sched_domain *sd;
83cd4fe2 5105
1c792db7 5106 if (unlikely(idle_cpu(cpu)))
83cd4fe2
VP
5107 return 0;
5108
1c792db7
SS
5109 /*
5110 * We may be recently in ticked or tickless idle mode. At the first
5111 * busy tick after returning from idle, we will update the busy stats.
5112 */
69e1e811 5113 set_cpu_sd_state_busy();
c1cc017c 5114 nohz_balance_exit_idle(cpu);
0b005cf5
SS
5115
5116 /*
5117 * None are in tickless mode and hence no need for NOHZ idle load
5118 * balancing.
5119 */
5120 if (likely(!atomic_read(&nohz.nr_cpus)))
5121 return 0;
1c792db7
SS
5122
5123 if (time_before(now, nohz.next_balance))
83cd4fe2
VP
5124 return 0;
5125
0b005cf5
SS
5126 if (rq->nr_running >= 2)
5127 goto need_kick;
83cd4fe2 5128
067491b7 5129 rcu_read_lock();
0b005cf5
SS
5130 for_each_domain(cpu, sd) {
5131 struct sched_group *sg = sd->groups;
5132 struct sched_group_power *sgp = sg->sgp;
5133 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
83cd4fe2 5134
0b005cf5 5135 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
067491b7 5136 goto need_kick_unlock;
0b005cf5
SS
5137
5138 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5139 && (cpumask_first_and(nohz.idle_cpus_mask,
5140 sched_domain_span(sd)) < cpu))
067491b7 5141 goto need_kick_unlock;
0b005cf5
SS
5142
5143 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5144 break;
83cd4fe2 5145 }
067491b7 5146 rcu_read_unlock();
83cd4fe2 5147 return 0;
067491b7
PZ
5148
5149need_kick_unlock:
5150 rcu_read_unlock();
0b005cf5
SS
5151need_kick:
5152 return 1;
83cd4fe2
VP
5153}
5154#else
5155static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5156#endif
5157
5158/*
5159 * run_rebalance_domains is triggered when needed from the scheduler tick.
5160 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
5161 */
1e3c88bd
PZ
5162static void run_rebalance_domains(struct softirq_action *h)
5163{
5164 int this_cpu = smp_processor_id();
5165 struct rq *this_rq = cpu_rq(this_cpu);
6eb57e0d 5166 enum cpu_idle_type idle = this_rq->idle_balance ?
1e3c88bd
PZ
5167 CPU_IDLE : CPU_NOT_IDLE;
5168
5169 rebalance_domains(this_cpu, idle);
5170
1e3c88bd 5171 /*
83cd4fe2 5172 * If this cpu has a pending nohz_balance_kick, then do the
1e3c88bd
PZ
5173 * balancing on behalf of the other idle cpus whose ticks are
5174 * stopped.
5175 */
83cd4fe2 5176 nohz_idle_balance(this_cpu, idle);
1e3c88bd
PZ
5177}
5178
5179static inline int on_null_domain(int cpu)
5180{
90a6501f 5181 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
1e3c88bd
PZ
5182}
5183
5184/*
5185 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
1e3c88bd 5186 */
029632fb 5187void trigger_load_balance(struct rq *rq, int cpu)
1e3c88bd 5188{
1e3c88bd
PZ
5189 /* Don't need to rebalance while attached to NULL domain */
5190 if (time_after_eq(jiffies, rq->next_balance) &&
5191 likely(!on_null_domain(cpu)))
5192 raise_softirq(SCHED_SOFTIRQ);
83cd4fe2 5193#ifdef CONFIG_NO_HZ
1c792db7 5194 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
83cd4fe2
VP
5195 nohz_balancer_kick(cpu);
5196#endif
1e3c88bd
PZ
5197}
5198
0bcdcf28
CE
5199static void rq_online_fair(struct rq *rq)
5200{
5201 update_sysctl();
5202}
5203
5204static void rq_offline_fair(struct rq *rq)
5205{
5206 update_sysctl();
a4c96ae3
PB
5207
5208 /* Ensure any throttled groups are reachable by pick_next_task */
5209 unthrottle_offline_cfs_rqs(rq);
0bcdcf28
CE
5210}
5211
55e12e5e 5212#endif /* CONFIG_SMP */
e1d1484f 5213
bf0f6f24
IM
5214/*
5215 * scheduler tick hitting a task of our scheduling class:
5216 */
8f4d37ec 5217static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
bf0f6f24
IM
5218{
5219 struct cfs_rq *cfs_rq;
5220 struct sched_entity *se = &curr->se;
5221
5222 for_each_sched_entity(se) {
5223 cfs_rq = cfs_rq_of(se);
8f4d37ec 5224 entity_tick(cfs_rq, se, queued);
bf0f6f24 5225 }
18bf2805
BS
5226
5227 update_rq_runnable_avg(rq, 1);
bf0f6f24
IM
5228}
5229
5230/*
cd29fe6f
PZ
5231 * called on fork with the child task as argument from the parent's context
5232 * - child not yet on the tasklist
5233 * - preemption disabled
bf0f6f24 5234 */
cd29fe6f 5235static void task_fork_fair(struct task_struct *p)
bf0f6f24 5236{
4fc420c9
DN
5237 struct cfs_rq *cfs_rq;
5238 struct sched_entity *se = &p->se, *curr;
00bf7bfc 5239 int this_cpu = smp_processor_id();
cd29fe6f
PZ
5240 struct rq *rq = this_rq();
5241 unsigned long flags;
5242
05fa785c 5243 raw_spin_lock_irqsave(&rq->lock, flags);
bf0f6f24 5244
861d034e
PZ
5245 update_rq_clock(rq);
5246
4fc420c9
DN
5247 cfs_rq = task_cfs_rq(current);
5248 curr = cfs_rq->curr;
5249
b0a0f667
PM
5250 if (unlikely(task_cpu(p) != this_cpu)) {
5251 rcu_read_lock();
cd29fe6f 5252 __set_task_cpu(p, this_cpu);
b0a0f667
PM
5253 rcu_read_unlock();
5254 }
bf0f6f24 5255
7109c442 5256 update_curr(cfs_rq);
cd29fe6f 5257
b5d9d734
MG
5258 if (curr)
5259 se->vruntime = curr->vruntime;
aeb73b04 5260 place_entity(cfs_rq, se, 1);
4d78e7b6 5261
cd29fe6f 5262 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
87fefa38 5263 /*
edcb60a3
IM
5264 * Upon rescheduling, sched_class::put_prev_task() will place
5265 * 'current' within the tree based on its new key value.
5266 */
4d78e7b6 5267 swap(curr->vruntime, se->vruntime);
aec0a514 5268 resched_task(rq->curr);
4d78e7b6 5269 }
bf0f6f24 5270
88ec22d3
PZ
5271 se->vruntime -= cfs_rq->min_vruntime;
5272
05fa785c 5273 raw_spin_unlock_irqrestore(&rq->lock, flags);
bf0f6f24
IM
5274}
5275
cb469845
SR
5276/*
5277 * Priority of the task has changed. Check to see if we preempt
5278 * the current task.
5279 */
da7a735e
PZ
5280static void
5281prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
cb469845 5282{
da7a735e
PZ
5283 if (!p->se.on_rq)
5284 return;
5285
cb469845
SR
5286 /*
5287 * Reschedule if we are currently running on this runqueue and
5288 * our priority decreased, or if we are not currently running on
5289 * this runqueue and our priority is higher than the current's
5290 */
da7a735e 5291 if (rq->curr == p) {
cb469845
SR
5292 if (p->prio > oldprio)
5293 resched_task(rq->curr);
5294 } else
15afe09b 5295 check_preempt_curr(rq, p, 0);
cb469845
SR
5296}
5297
da7a735e
PZ
5298static void switched_from_fair(struct rq *rq, struct task_struct *p)
5299{
5300 struct sched_entity *se = &p->se;
5301 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5302
5303 /*
5304 * Ensure the task's vruntime is normalized, so that when its
5305 * switched back to the fair class the enqueue_entity(.flags=0) will
5306 * do the right thing.
5307 *
5308 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5309 * have normalized the vruntime, if it was !on_rq, then only when
5310 * the task is sleeping will it still have non-normalized vruntime.
5311 */
5312 if (!se->on_rq && p->state != TASK_RUNNING) {
5313 /*
5314 * Fix up our vruntime so that the current sleep doesn't
5315 * cause 'unlimited' sleep bonus.
5316 */
5317 place_entity(cfs_rq, se, 0);
5318 se->vruntime -= cfs_rq->min_vruntime;
5319 }
9ee474f5
PT
5320
5321#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5322 /*
5323 * Remove our load from contribution when we leave sched_fair
5324 * and ensure we don't carry in an old decay_count if we
5325 * switch back.
5326 */
5327 if (p->se.avg.decay_count) {
5328 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5329 __synchronize_entity_decay(&p->se);
5330 subtract_blocked_load_contrib(cfs_rq,
5331 p->se.avg.load_avg_contrib);
5332 }
5333#endif
da7a735e
PZ
5334}
5335
cb469845
SR
5336/*
5337 * We switched to the sched_fair class.
5338 */
da7a735e 5339static void switched_to_fair(struct rq *rq, struct task_struct *p)
cb469845 5340{
da7a735e
PZ
5341 if (!p->se.on_rq)
5342 return;
5343
cb469845
SR
5344 /*
5345 * We were most likely switched from sched_rt, so
5346 * kick off the schedule if running, otherwise just see
5347 * if we can still preempt the current task.
5348 */
da7a735e 5349 if (rq->curr == p)
cb469845
SR
5350 resched_task(rq->curr);
5351 else
15afe09b 5352 check_preempt_curr(rq, p, 0);
cb469845
SR
5353}
5354
83b699ed
SV
5355/* Account for a task changing its policy or group.
5356 *
5357 * This routine is mostly called to set cfs_rq->curr field when a task
5358 * migrates between groups/classes.
5359 */
5360static void set_curr_task_fair(struct rq *rq)
5361{
5362 struct sched_entity *se = &rq->curr->se;
5363
ec12cb7f
PT
5364 for_each_sched_entity(se) {
5365 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5366
5367 set_next_entity(cfs_rq, se);
5368 /* ensure bandwidth has been allocated on our new cfs_rq */
5369 account_cfs_rq_runtime(cfs_rq, 0);
5370 }
83b699ed
SV
5371}
5372
029632fb
PZ
5373void init_cfs_rq(struct cfs_rq *cfs_rq)
5374{
5375 cfs_rq->tasks_timeline = RB_ROOT;
029632fb
PZ
5376 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5377#ifndef CONFIG_64BIT
5378 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5379#endif
9ee474f5
PT
5380#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5381 atomic64_set(&cfs_rq->decay_counter, 1);
5382#endif
029632fb
PZ
5383}
5384
810b3817 5385#ifdef CONFIG_FAIR_GROUP_SCHED
b2b5ce02 5386static void task_move_group_fair(struct task_struct *p, int on_rq)
810b3817 5387{
b2b5ce02
PZ
5388 /*
5389 * If the task was not on the rq at the time of this cgroup movement
5390 * it must have been asleep, sleeping tasks keep their ->vruntime
5391 * absolute on their old rq until wakeup (needed for the fair sleeper
5392 * bonus in place_entity()).
5393 *
5394 * If it was on the rq, we've just 'preempted' it, which does convert
5395 * ->vruntime to a relative base.
5396 *
5397 * Make sure both cases convert their relative position when migrating
5398 * to another cgroup's rq. This does somewhat interfere with the
5399 * fair sleeper stuff for the first placement, but who cares.
5400 */
7ceff013
DN
5401 /*
5402 * When !on_rq, vruntime of the task has usually NOT been normalized.
5403 * But there are some cases where it has already been normalized:
5404 *
5405 * - Moving a forked child which is waiting for being woken up by
5406 * wake_up_new_task().
62af3783
DN
5407 * - Moving a task which has been woken up by try_to_wake_up() and
5408 * waiting for actually being woken up by sched_ttwu_pending().
7ceff013
DN
5409 *
5410 * To prevent boost or penalty in the new cfs_rq caused by delta
5411 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5412 */
62af3783 5413 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
7ceff013
DN
5414 on_rq = 1;
5415
b2b5ce02
PZ
5416 if (!on_rq)
5417 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5418 set_task_rq(p, task_cpu(p));
88ec22d3 5419 if (!on_rq)
b2b5ce02 5420 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
810b3817 5421}
029632fb
PZ
5422
5423void free_fair_sched_group(struct task_group *tg)
5424{
5425 int i;
5426
5427 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5428
5429 for_each_possible_cpu(i) {
5430 if (tg->cfs_rq)
5431 kfree(tg->cfs_rq[i]);
5432 if (tg->se)
5433 kfree(tg->se[i]);
5434 }
5435
5436 kfree(tg->cfs_rq);
5437 kfree(tg->se);
5438}
5439
5440int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5441{
5442 struct cfs_rq *cfs_rq;
5443 struct sched_entity *se;
5444 int i;
5445
5446 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5447 if (!tg->cfs_rq)
5448 goto err;
5449 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5450 if (!tg->se)
5451 goto err;
5452
5453 tg->shares = NICE_0_LOAD;
5454
5455 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5456
5457 for_each_possible_cpu(i) {
5458 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5459 GFP_KERNEL, cpu_to_node(i));
5460 if (!cfs_rq)
5461 goto err;
5462
5463 se = kzalloc_node(sizeof(struct sched_entity),
5464 GFP_KERNEL, cpu_to_node(i));
5465 if (!se)
5466 goto err_free_rq;
5467
5468 init_cfs_rq(cfs_rq);
5469 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5470 }
5471
5472 return 1;
5473
5474err_free_rq:
5475 kfree(cfs_rq);
5476err:
5477 return 0;
5478}
5479
5480void unregister_fair_sched_group(struct task_group *tg, int cpu)
5481{
5482 struct rq *rq = cpu_rq(cpu);
5483 unsigned long flags;
5484
5485 /*
5486 * Only empty task groups can be destroyed; so we can speculatively
5487 * check on_list without danger of it being re-added.
5488 */
5489 if (!tg->cfs_rq[cpu]->on_list)
5490 return;
5491
5492 raw_spin_lock_irqsave(&rq->lock, flags);
5493 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5494 raw_spin_unlock_irqrestore(&rq->lock, flags);
5495}
5496
5497void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5498 struct sched_entity *se, int cpu,
5499 struct sched_entity *parent)
5500{
5501 struct rq *rq = cpu_rq(cpu);
5502
5503 cfs_rq->tg = tg;
5504 cfs_rq->rq = rq;
5505#ifdef CONFIG_SMP
5506 /* allow initial update_cfs_load() to truncate */
5507 cfs_rq->load_stamp = 1;
810b3817 5508#endif
029632fb
PZ
5509 init_cfs_rq_runtime(cfs_rq);
5510
5511 tg->cfs_rq[cpu] = cfs_rq;
5512 tg->se[cpu] = se;
5513
5514 /* se could be NULL for root_task_group */
5515 if (!se)
5516 return;
5517
5518 if (!parent)
5519 se->cfs_rq = &rq->cfs;
5520 else
5521 se->cfs_rq = parent->my_q;
5522
5523 se->my_q = cfs_rq;
5524 update_load_set(&se->load, 0);
5525 se->parent = parent;
5526}
5527
5528static DEFINE_MUTEX(shares_mutex);
5529
5530int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5531{
5532 int i;
5533 unsigned long flags;
5534
5535 /*
5536 * We can't change the weight of the root cgroup.
5537 */
5538 if (!tg->se[0])
5539 return -EINVAL;
5540
5541 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5542
5543 mutex_lock(&shares_mutex);
5544 if (tg->shares == shares)
5545 goto done;
5546
5547 tg->shares = shares;
5548 for_each_possible_cpu(i) {
5549 struct rq *rq = cpu_rq(i);
5550 struct sched_entity *se;
5551
5552 se = tg->se[i];
5553 /* Propagate contribution to hierarchy */
5554 raw_spin_lock_irqsave(&rq->lock, flags);
5555 for_each_sched_entity(se)
5556 update_cfs_shares(group_cfs_rq(se));
5557 raw_spin_unlock_irqrestore(&rq->lock, flags);
5558 }
5559
5560done:
5561 mutex_unlock(&shares_mutex);
5562 return 0;
5563}
5564#else /* CONFIG_FAIR_GROUP_SCHED */
5565
5566void free_fair_sched_group(struct task_group *tg) { }
5567
5568int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5569{
5570 return 1;
5571}
5572
5573void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5574
5575#endif /* CONFIG_FAIR_GROUP_SCHED */
5576
810b3817 5577
6d686f45 5578static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
0d721cea
PW
5579{
5580 struct sched_entity *se = &task->se;
0d721cea
PW
5581 unsigned int rr_interval = 0;
5582
5583 /*
5584 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5585 * idle runqueue:
5586 */
0d721cea
PW
5587 if (rq->cfs.load.weight)
5588 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
0d721cea
PW
5589
5590 return rr_interval;
5591}
5592
bf0f6f24
IM
5593/*
5594 * All the scheduling class methods:
5595 */
029632fb 5596const struct sched_class fair_sched_class = {
5522d5d5 5597 .next = &idle_sched_class,
bf0f6f24
IM
5598 .enqueue_task = enqueue_task_fair,
5599 .dequeue_task = dequeue_task_fair,
5600 .yield_task = yield_task_fair,
d95f4122 5601 .yield_to_task = yield_to_task_fair,
bf0f6f24 5602
2e09bf55 5603 .check_preempt_curr = check_preempt_wakeup,
bf0f6f24
IM
5604
5605 .pick_next_task = pick_next_task_fair,
5606 .put_prev_task = put_prev_task_fair,
5607
681f3e68 5608#ifdef CONFIG_SMP
4ce72a2c
LZ
5609 .select_task_rq = select_task_rq_fair,
5610
0bcdcf28
CE
5611 .rq_online = rq_online_fair,
5612 .rq_offline = rq_offline_fair,
88ec22d3
PZ
5613
5614 .task_waking = task_waking_fair,
681f3e68 5615#endif
bf0f6f24 5616
83b699ed 5617 .set_curr_task = set_curr_task_fair,
bf0f6f24 5618 .task_tick = task_tick_fair,
cd29fe6f 5619 .task_fork = task_fork_fair,
cb469845
SR
5620
5621 .prio_changed = prio_changed_fair,
da7a735e 5622 .switched_from = switched_from_fair,
cb469845 5623 .switched_to = switched_to_fair,
810b3817 5624
0d721cea
PW
5625 .get_rr_interval = get_rr_interval_fair,
5626
810b3817 5627#ifdef CONFIG_FAIR_GROUP_SCHED
b2b5ce02 5628 .task_move_group = task_move_group_fair,
810b3817 5629#endif
bf0f6f24
IM
5630};
5631
5632#ifdef CONFIG_SCHED_DEBUG
029632fb 5633void print_cfs_stats(struct seq_file *m, int cpu)
bf0f6f24 5634{
bf0f6f24
IM
5635 struct cfs_rq *cfs_rq;
5636
5973e5b9 5637 rcu_read_lock();
c3b64f1e 5638 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
5cef9eca 5639 print_cfs_rq(m, cpu, cfs_rq);
5973e5b9 5640 rcu_read_unlock();
bf0f6f24
IM
5641}
5642#endif
029632fb
PZ
5643
5644__init void init_sched_fair_class(void)
5645{
5646#ifdef CONFIG_SMP
5647 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5648
5649#ifdef CONFIG_NO_HZ
554cecaf 5650 nohz.next_balance = jiffies;
029632fb 5651 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
71325960 5652 cpu_notifier(sched_ilb_notifier, 0);
029632fb
PZ
5653#endif
5654#endif /* SMP */
5655
5656}
This page took 0.894796 seconds and 5 git commands to generate.