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