blob: efe9253bd235da9e68a730f9109925b2aa4b1061 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
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>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800262static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
263{
264 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800265 /*
266 * Ensure we either appear before our parent (if already
267 * enqueued) or force our parent to appear after us when it is
268 * enqueued. The fact that we always enqueue bottom-up
269 * reduces this to two cases.
270 */
271 if (cfs_rq->tg->parent &&
272 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
273 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800274 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800275 } else {
276 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
278 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800279
280 cfs_rq->on_list = 1;
281 }
282}
283
284static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
285{
286 if (cfs_rq->on_list) {
287 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
288 cfs_rq->on_list = 0;
289 }
290}
291
Peter Zijlstrab7581492008-04-19 19:45:00 +0200292/* Iterate thr' all leaf cfs_rq's on a runqueue */
293#define for_each_leaf_cfs_rq(rq, cfs_rq) \
294 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
295
296/* Do the two (enqueued) entities belong to the same group ? */
297static inline int
298is_same_group(struct sched_entity *se, struct sched_entity *pse)
299{
300 if (se->cfs_rq == pse->cfs_rq)
301 return 1;
302
303 return 0;
304}
305
306static inline struct sched_entity *parent_entity(struct sched_entity *se)
307{
308 return se->parent;
309}
310
Peter Zijlstra464b7522008-10-24 11:06:15 +0200311/* return depth at which a sched entity is present in the hierarchy */
312static inline int depth_se(struct sched_entity *se)
313{
314 int depth = 0;
315
316 for_each_sched_entity(se)
317 depth++;
318
319 return depth;
320}
321
322static void
323find_matching_se(struct sched_entity **se, struct sched_entity **pse)
324{
325 int se_depth, pse_depth;
326
327 /*
328 * preemption test can be made between sibling entities who are in the
329 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
330 * both tasks until we find their ancestors who are siblings of common
331 * parent.
332 */
333
334 /* First walk up until both entities are at same depth */
335 se_depth = depth_se(*se);
336 pse_depth = depth_se(*pse);
337
338 while (se_depth > pse_depth) {
339 se_depth--;
340 *se = parent_entity(*se);
341 }
342
343 while (pse_depth > se_depth) {
344 pse_depth--;
345 *pse = parent_entity(*pse);
346 }
347
348 while (!is_same_group(*se, *pse)) {
349 *se = parent_entity(*se);
350 *pse = parent_entity(*pse);
351 }
352}
353
Peter Zijlstra8f488942009-07-24 12:25:30 +0200354#else /* !CONFIG_FAIR_GROUP_SCHED */
355
356static inline struct task_struct *task_of(struct sched_entity *se)
357{
358 return container_of(se, struct task_struct, se);
359}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200360
361static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
362{
363 return container_of(cfs_rq, struct rq, cfs);
364}
365
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200366#define entity_is_task(se) 1
367
Peter Zijlstrab7581492008-04-19 19:45:00 +0200368#define for_each_sched_entity(se) \
369 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200370
Peter Zijlstrab7581492008-04-19 19:45:00 +0200371static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200372{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374}
375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
377{
378 struct task_struct *p = task_of(se);
379 struct rq *rq = task_rq(p);
380
381 return &rq->cfs;
382}
383
384/* runqueue "owned" by this group */
385static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
386{
387 return NULL;
388}
389
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800390static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
391{
392}
393
394static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
395{
396}
397
Peter Zijlstrab7581492008-04-19 19:45:00 +0200398#define for_each_leaf_cfs_rq(rq, cfs_rq) \
399 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
400
401static inline int
402is_same_group(struct sched_entity *se, struct sched_entity *pse)
403{
404 return 1;
405}
406
407static inline struct sched_entity *parent_entity(struct sched_entity *se)
408{
409 return NULL;
410}
411
Peter Zijlstra464b7522008-10-24 11:06:15 +0200412static inline void
413find_matching_se(struct sched_entity **se, struct sched_entity **pse)
414{
415}
416
Peter Zijlstrab7581492008-04-19 19:45:00 +0200417#endif /* CONFIG_FAIR_GROUP_SCHED */
418
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700419static __always_inline
420void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200421
422/**************************************************************
423 * Scheduling class tree data structure manipulation methods:
424 */
425
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200426static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200427{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200428 s64 delta = (s64)(vruntime - min_vruntime);
429 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200430 min_vruntime = vruntime;
431
432 return min_vruntime;
433}
434
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200435static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200436{
437 s64 delta = (s64)(vruntime - min_vruntime);
438 if (delta < 0)
439 min_vruntime = vruntime;
440
441 return min_vruntime;
442}
443
Fabio Checconi54fdc582009-07-16 12:32:27 +0200444static inline int entity_before(struct sched_entity *a,
445 struct sched_entity *b)
446{
447 return (s64)(a->vruntime - b->vruntime) < 0;
448}
449
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200450static void update_min_vruntime(struct cfs_rq *cfs_rq)
451{
452 u64 vruntime = cfs_rq->min_vruntime;
453
454 if (cfs_rq->curr)
455 vruntime = cfs_rq->curr->vruntime;
456
457 if (cfs_rq->rb_leftmost) {
458 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
459 struct sched_entity,
460 run_node);
461
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100462 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200463 vruntime = se->vruntime;
464 else
465 vruntime = min_vruntime(vruntime, se->vruntime);
466 }
467
468 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200469#ifndef CONFIG_64BIT
470 smp_wmb();
471 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
472#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200473}
474
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200475/*
476 * Enqueue an entity into the rb-tree:
477 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200478static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200479{
480 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
481 struct rb_node *parent = NULL;
482 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200483 int leftmost = 1;
484
485 /*
486 * Find the right place in the rbtree:
487 */
488 while (*link) {
489 parent = *link;
490 entry = rb_entry(parent, struct sched_entity, run_node);
491 /*
492 * We dont care about collisions. Nodes with
493 * the same key stay together.
494 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200495 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200496 link = &parent->rb_left;
497 } else {
498 link = &parent->rb_right;
499 leftmost = 0;
500 }
501 }
502
503 /*
504 * Maintain a cache of leftmost tree entries (it is frequently
505 * used):
506 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200507 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200508 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200509
510 rb_link_node(&se->run_node, parent, link);
511 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200512}
513
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200514static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200515{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100516 if (cfs_rq->rb_leftmost == &se->run_node) {
517 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100518
519 next_node = rb_next(&se->run_node);
520 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200522
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200523 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200524}
525
Peter Zijlstra029632f2011-10-25 10:00:11 +0200526struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200527{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100528 struct rb_node *left = cfs_rq->rb_leftmost;
529
530 if (!left)
531 return NULL;
532
533 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200534}
535
Rik van Rielac53db52011-02-01 09:51:03 -0500536static struct sched_entity *__pick_next_entity(struct sched_entity *se)
537{
538 struct rb_node *next = rb_next(&se->run_node);
539
540 if (!next)
541 return NULL;
542
543 return rb_entry(next, struct sched_entity, run_node);
544}
545
546#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200547struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200548{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100549 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200550
Balbir Singh70eee742008-02-22 13:25:53 +0530551 if (!last)
552 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100553
554 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555}
556
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200557/**************************************************************
558 * Scheduling class statistics methods:
559 */
560
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100561int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700562 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100563 loff_t *ppos)
564{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700565 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100567
568 if (ret || !write)
569 return ret;
570
571 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
572 sysctl_sched_min_granularity);
573
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100574#define WRT_SYSCTL(name) \
575 (normalized_sysctl_##name = sysctl_##name / (factor))
576 WRT_SYSCTL(sched_min_granularity);
577 WRT_SYSCTL(sched_latency);
578 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#undef WRT_SYSCTL
580
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100581 return 0;
582}
583#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200584
585/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200586 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200587 */
588static inline unsigned long
589calc_delta_fair(unsigned long delta, struct sched_entity *se)
590{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 if (unlikely(se->load.weight != NICE_0_LOAD))
592 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200593
594 return delta;
595}
596
597/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200598 * The idea is to set a period in which each task runs once.
599 *
600 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
601 * this period because otherwise the slices get too small.
602 *
603 * p = (nr <= nl) ? l : l*nr/nl
604 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200605static u64 __sched_period(unsigned long nr_running)
606{
607 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100608 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200609
610 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100611 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200612 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200613 }
614
615 return period;
616}
617
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200618/*
619 * We calculate the wall-time slice from the period by taking a part
620 * proportional to the weight.
621 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200622 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200624static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200625{
Mike Galbraith0a582442009-01-02 12:16:42 +0100626 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627
Mike Galbraith0a582442009-01-02 12:16:42 +0100628 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100629 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200630 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100631
632 cfs_rq = cfs_rq_of(se);
633 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200634
Mike Galbraith0a582442009-01-02 12:16:42 +0100635 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200636 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100637
638 update_load_add(&lw, se->load.weight);
639 load = &lw;
640 }
641 slice = calc_delta_mine(slice, se->load.weight, load);
642 }
643 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200644}
645
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200646/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200647 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200648 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200649 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200650 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200651static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200652{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200653 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200654}
655
Paul Turnerd6b55912010-11-15 15:47:09 -0800656static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800657static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800658
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200660 * Update the current task's runtime statistics. Skip current tasks that
661 * are not in our scheduling class.
662 */
663static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200664__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
665 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200666{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200667 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200668
Lucas De Marchi41acab82010-03-10 23:37:45 -0300669 schedstat_set(curr->statistics.exec_max,
670 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671
672 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200673 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200674 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100675
Ingo Molnare9acbff2007-10-15 17:00:04 +0200676 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200677 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800678
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100679#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800680 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800681#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200682}
683
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200684static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200685{
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200686 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700687 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200688 unsigned long delta_exec;
689
690 if (unlikely(!curr))
691 return;
692
693 /*
694 * Get the amount of time the current task was running
695 * since the last time we changed load (this cannot
696 * overflow on 32 bits):
697 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200698 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100699 if (!delta_exec)
700 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200701
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200702 __update_curr(cfs_rq, curr, delta_exec);
703 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100704
705 if (entity_is_task(curr)) {
706 struct task_struct *curtask = task_of(curr);
707
Ingo Molnarf977bb42009-09-13 18:15:54 +0200708 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100709 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700710 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100711 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700712
713 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200714}
715
716static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200717update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200718{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300719 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200720}
721
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200722/*
723 * Task is being enqueued - update stats:
724 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200725static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200727 /*
728 * Are we enqueueing a waiting task? (for current tasks
729 * a dequeue/enqueue event is a NOP)
730 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200731 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200732 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200733}
734
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200736update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200737{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300738 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
739 rq_of(cfs_rq)->clock - se->statistics.wait_start));
740 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
741 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
742 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200743#ifdef CONFIG_SCHEDSTATS
744 if (entity_is_task(se)) {
745 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300746 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200747 }
748#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300749 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200750}
751
752static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200753update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200754{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200755 /*
756 * Mark the end of the wait period if dequeueing a
757 * waiting task:
758 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200759 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200760 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200761}
762
763/*
764 * We are picking a new current task - update its stats:
765 */
766static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200767update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200768{
769 /*
770 * We are starting a new run period:
771 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700772 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200773}
774
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200775/**************************************************
776 * Scheduling class queueing methods:
777 */
778
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200779static void
780account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
781{
782 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200783 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200784 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100785#ifdef CONFIG_SMP
786 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200787 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100788#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200789 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200790}
791
792static void
793account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
794{
795 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200796 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200797 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100798 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530799 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200800 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200801}
802
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800803#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700804/* we need this in update_cfs_load and load-balance functions below */
805static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800806# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800807static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
808 int global_update)
809{
810 struct task_group *tg = cfs_rq->tg;
811 long load_avg;
812
813 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
814 load_avg -= cfs_rq->load_contribution;
815
816 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
817 atomic_add(load_avg, &tg->load_weight);
818 cfs_rq->load_contribution += load_avg;
819 }
820}
821
822static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800823{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800824 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800825 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800826 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800827
Paul Turner64660c82011-07-21 09:43:36 -0700828 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800829 return;
830
Paul Turner05ca62c2011-01-21 20:45:02 -0800831 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800832 delta = now - cfs_rq->load_stamp;
833
Paul Turnere33078b2010-11-15 15:47:04 -0800834 /* truncate load history at 4 idle periods */
835 if (cfs_rq->load_stamp > cfs_rq->load_last &&
836 now - cfs_rq->load_last > 4 * period) {
837 cfs_rq->load_period = 0;
838 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800839 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800840 }
841
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800842 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800843 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800844 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800845 if (load) {
846 cfs_rq->load_last = now;
847 cfs_rq->load_avg += delta * load;
848 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800849
Paul Turnerd6b55912010-11-15 15:47:09 -0800850 /* consider updating load contribution on each fold or truncate */
851 if (global_update || cfs_rq->load_period > period
852 || !cfs_rq->load_period)
853 update_cfs_rq_load_contribution(cfs_rq, global_update);
854
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800855 while (cfs_rq->load_period > period) {
856 /*
857 * Inline assembly required to prevent the compiler
858 * optimising this loop into a divmod call.
859 * See __iter_div_u64_rem() for another example of this.
860 */
861 asm("" : "+rm" (cfs_rq->load_period));
862 cfs_rq->load_period /= 2;
863 cfs_rq->load_avg /= 2;
864 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800865
Paul Turnere33078b2010-11-15 15:47:04 -0800866 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
867 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800868}
869
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200870static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
871{
872 long tg_weight;
873
874 /*
875 * Use this CPU's actual weight instead of the last load_contribution
876 * to gain a more accurate current total weight. See
877 * update_cfs_rq_load_contribution().
878 */
879 tg_weight = atomic_read(&tg->load_weight);
880 tg_weight -= cfs_rq->load_contribution;
881 tg_weight += cfs_rq->load.weight;
882
883 return tg_weight;
884}
885
Paul Turner6d5ab292011-01-21 20:45:01 -0800886static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800887{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200888 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800889
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200890 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800891 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800892
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800893 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200894 if (tg_weight)
895 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800896
897 if (shares < MIN_SHARES)
898 shares = MIN_SHARES;
899 if (shares > tg->shares)
900 shares = tg->shares;
901
902 return shares;
903}
904
905static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
906{
907 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
908 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800909 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800910 }
911}
912# else /* CONFIG_SMP */
913static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
914{
915}
916
Paul Turner6d5ab292011-01-21 20:45:01 -0800917static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800918{
919 return tg->shares;
920}
921
922static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
923{
924}
925# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800926static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
927 unsigned long weight)
928{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800929 if (se->on_rq) {
930 /* commit outstanding execution time */
931 if (cfs_rq->curr == se)
932 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800933 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800934 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800935
936 update_load_set(&se->load, weight);
937
938 if (se->on_rq)
939 account_entity_enqueue(cfs_rq, se);
940}
941
Paul Turner6d5ab292011-01-21 20:45:01 -0800942static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800943{
944 struct task_group *tg;
945 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800946 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800947
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800948 tg = cfs_rq->tg;
949 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700950 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800951 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800952#ifndef CONFIG_SMP
953 if (likely(se->load.weight == tg->shares))
954 return;
955#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800956 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800957
958 reweight_entity(cfs_rq_of(se), se, shares);
959}
960#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800961static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800962{
963}
964
Paul Turner6d5ab292011-01-21 20:45:01 -0800965static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800966{
967}
Paul Turner43365bd2010-12-15 19:10:17 -0800968
969static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
970{
971}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800972#endif /* CONFIG_FAIR_GROUP_SCHED */
973
Ingo Molnar2396af62007-08-09 11:16:48 +0200974static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200975{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200976#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +0200977 struct task_struct *tsk = NULL;
978
979 if (entity_is_task(se))
980 tsk = task_of(se);
981
Lucas De Marchi41acab82010-03-10 23:37:45 -0300982 if (se->statistics.sleep_start) {
983 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984
985 if ((s64)delta < 0)
986 delta = 0;
987
Lucas De Marchi41acab82010-03-10 23:37:45 -0300988 if (unlikely(delta > se->statistics.sleep_max))
989 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200990
Peter Zijlstra8c79a042012-01-30 14:51:37 +0100991 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -0300992 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +0100993
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200994 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +0200995 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200996 trace_sched_stat_sleep(tsk, delta);
997 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200998 }
Lucas De Marchi41acab82010-03-10 23:37:45 -0300999 if (se->statistics.block_start) {
1000 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001001
1002 if ((s64)delta < 0)
1003 delta = 0;
1004
Lucas De Marchi41acab82010-03-10 23:37:45 -03001005 if (unlikely(delta > se->statistics.block_max))
1006 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001007
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001008 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001009 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001010
Peter Zijlstrae4143142009-07-23 20:13:26 +02001011 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001012 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001013 se->statistics.iowait_sum += delta;
1014 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001015 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001016 }
1017
Andrew Vaginb781a602011-11-28 12:03:35 +03001018 trace_sched_stat_blocked(tsk, delta);
1019
Peter Zijlstrae4143142009-07-23 20:13:26 +02001020 /*
1021 * Blocking time is in units of nanosecs, so shift by
1022 * 20 to get a milliseconds-range estimation of the
1023 * amount of time that the task spent sleeping:
1024 */
1025 if (unlikely(prof_on == SLEEP_PROFILING)) {
1026 profile_hits(SLEEP_PROFILING,
1027 (void *)get_wchan(tsk),
1028 delta >> 20);
1029 }
1030 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001031 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001032 }
1033#endif
1034}
1035
Peter Zijlstraddc97292007-10-15 17:00:10 +02001036static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1037{
1038#ifdef CONFIG_SCHED_DEBUG
1039 s64 d = se->vruntime - cfs_rq->min_vruntime;
1040
1041 if (d < 0)
1042 d = -d;
1043
1044 if (d > 3*sysctl_sched_latency)
1045 schedstat_inc(cfs_rq, nr_spread_over);
1046#endif
1047}
1048
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001049static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001050place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1051{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001052 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001053
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001054 /*
1055 * The 'current' period is already promised to the current tasks,
1056 * however the extra weight of the new task will slow them down a
1057 * little, place the new task so that it fits in the slot that
1058 * stays open at the end.
1059 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001060 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001061 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001062
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001063 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001064 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001065 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001066
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001067 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001068 * Halve their sleep time's effect, to allow
1069 * for a gentler effect of sleepers:
1070 */
1071 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1072 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001073
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001074 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001075 }
1076
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001077 /* ensure we never gain time by being placed backwards. */
1078 vruntime = max_vruntime(se->vruntime, vruntime);
1079
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001080 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001081}
1082
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001083static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1084
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001085static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001086enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001087{
1088 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001089 * Update the normalized vruntime before updating min_vruntime
1090 * through callig update_curr().
1091 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001092 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001093 se->vruntime += cfs_rq->min_vruntime;
1094
1095 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001096 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001097 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001098 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001099 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001100 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001101 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001102
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001103 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001104 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001105 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001106 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001107
Ingo Molnard2417e52007-08-09 11:16:47 +02001108 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001109 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001110 if (se != cfs_rq->curr)
1111 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001112 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001113
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001114 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001115 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001116 check_enqueue_throttle(cfs_rq);
1117 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001118}
1119
Rik van Riel2c13c9192011-02-01 09:48:37 -05001120static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001121{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001122 for_each_sched_entity(se) {
1123 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1124 if (cfs_rq->last == se)
1125 cfs_rq->last = NULL;
1126 else
1127 break;
1128 }
1129}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001130
Rik van Riel2c13c9192011-02-01 09:48:37 -05001131static void __clear_buddies_next(struct sched_entity *se)
1132{
1133 for_each_sched_entity(se) {
1134 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1135 if (cfs_rq->next == se)
1136 cfs_rq->next = NULL;
1137 else
1138 break;
1139 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001140}
1141
Rik van Rielac53db52011-02-01 09:51:03 -05001142static void __clear_buddies_skip(struct sched_entity *se)
1143{
1144 for_each_sched_entity(se) {
1145 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1146 if (cfs_rq->skip == se)
1147 cfs_rq->skip = NULL;
1148 else
1149 break;
1150 }
1151}
1152
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001153static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1154{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001155 if (cfs_rq->last == se)
1156 __clear_buddies_last(se);
1157
1158 if (cfs_rq->next == se)
1159 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001160
1161 if (cfs_rq->skip == se)
1162 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001163}
1164
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001165static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001166
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001167static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001168dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001169{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001170 /*
1171 * Update run-time statistics of the 'current'.
1172 */
1173 update_curr(cfs_rq);
1174
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001175 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001176 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001177#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001178 if (entity_is_task(se)) {
1179 struct task_struct *tsk = task_of(se);
1180
1181 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001182 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001183 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001184 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001185 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001186#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001187 }
1188
Peter Zijlstra2002c692008-11-11 11:52:33 +01001189 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001190
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001191 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001192 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001193 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001194 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001195 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001196
1197 /*
1198 * Normalize the entity after updating the min_vruntime because the
1199 * update can refer to the ->curr item and we need to reflect this
1200 * movement in our normalized position.
1201 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001202 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001203 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001204
Paul Turnerd8b49862011-07-21 09:43:41 -07001205 /* return excess runtime on last dequeue */
1206 return_cfs_rq_runtime(cfs_rq);
1207
Peter Zijlstra1e876232011-05-17 16:21:10 -07001208 update_min_vruntime(cfs_rq);
1209 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001210}
1211
1212/*
1213 * Preempt the current task with a newly woken task if needed:
1214 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001215static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001216check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001217{
Peter Zijlstra11697832007-09-05 14:32:49 +02001218 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001219 struct sched_entity *se;
1220 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001221
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001222 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001223 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001224 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001225 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001226 /*
1227 * The current task ran long enough, ensure it doesn't get
1228 * re-elected due to buddy favours.
1229 */
1230 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001231 return;
1232 }
1233
1234 /*
1235 * Ensure that a task that missed wakeup preemption by a
1236 * narrow margin doesn't have to wait for a full slice.
1237 * This also mitigates buddy induced latencies under load.
1238 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001239 if (delta_exec < sysctl_sched_min_granularity)
1240 return;
1241
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001242 se = __pick_first_entity(cfs_rq);
1243 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001244
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001245 if (delta < 0)
1246 return;
Mike Galbraithd7d829442011-01-05 05:41:17 +01001247
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001248 if (delta > ideal_runtime)
1249 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001250}
1251
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001252static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001253set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001254{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001255 /* 'current' is not kept within the tree. */
1256 if (se->on_rq) {
1257 /*
1258 * Any task has to be enqueued before it get to execute on
1259 * a CPU. So account for the time it spent waiting on the
1260 * runqueue.
1261 */
1262 update_stats_wait_end(cfs_rq, se);
1263 __dequeue_entity(cfs_rq, se);
1264 }
1265
Ingo Molnar79303e92007-08-09 11:16:47 +02001266 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001267 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001268#ifdef CONFIG_SCHEDSTATS
1269 /*
1270 * Track our maximum slice length, if the CPU's load is at
1271 * least twice that of our own weight (i.e. dont track it
1272 * when there are only lesser-weight tasks around):
1273 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001274 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001275 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001276 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1277 }
1278#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001279 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001280}
1281
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001282static int
1283wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1284
Rik van Rielac53db52011-02-01 09:51:03 -05001285/*
1286 * Pick the next process, keeping these things in mind, in this order:
1287 * 1) keep things fair between processes/task groups
1288 * 2) pick the "next" process, since someone really wants that to run
1289 * 3) pick the "last" process, for cache locality
1290 * 4) do not run the "skip" process, if something else is available
1291 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001292static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001293{
Rik van Rielac53db52011-02-01 09:51:03 -05001294 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001295 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001296
Rik van Rielac53db52011-02-01 09:51:03 -05001297 /*
1298 * Avoid running the skip buddy, if running something else can
1299 * be done without getting too unfair.
1300 */
1301 if (cfs_rq->skip == se) {
1302 struct sched_entity *second = __pick_next_entity(se);
1303 if (second && wakeup_preempt_entity(second, left) < 1)
1304 se = second;
1305 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001306
Mike Galbraithf685cea2009-10-23 23:09:22 +02001307 /*
1308 * Prefer last buddy, try to return the CPU to a preempted task.
1309 */
1310 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1311 se = cfs_rq->last;
1312
Rik van Rielac53db52011-02-01 09:51:03 -05001313 /*
1314 * Someone really wants this to run. If it's not unfair, run it.
1315 */
1316 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1317 se = cfs_rq->next;
1318
Mike Galbraithf685cea2009-10-23 23:09:22 +02001319 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001320
1321 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001322}
1323
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001324static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1325
Ingo Molnarab6cde22007-08-09 11:16:48 +02001326static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001327{
1328 /*
1329 * If still on the runqueue then deactivate_task()
1330 * was not called and update_curr() has to be done:
1331 */
1332 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001333 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001334
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001335 /* throttle cfs_rqs exceeding runtime */
1336 check_cfs_rq_runtime(cfs_rq);
1337
Peter Zijlstraddc97292007-10-15 17:00:10 +02001338 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001339 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001340 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001341 /* Put 'current' back into the tree. */
1342 __enqueue_entity(cfs_rq, prev);
1343 }
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001344 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001345}
1346
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001347static void
1348entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001349{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001350 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001351 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001352 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001353 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001354
Paul Turner43365bd2010-12-15 19:10:17 -08001355 /*
1356 * Update share accounting for long-running entities.
1357 */
1358 update_entity_shares_tick(cfs_rq);
1359
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001360#ifdef CONFIG_SCHED_HRTICK
1361 /*
1362 * queued ticks are scheduled to match the slice, so don't bother
1363 * validating it and just reschedule.
1364 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001365 if (queued) {
1366 resched_task(rq_of(cfs_rq)->curr);
1367 return;
1368 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001369 /*
1370 * don't let the period tick interfere with the hrtick preemption
1371 */
1372 if (!sched_feat(DOUBLE_TICK) &&
1373 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1374 return;
1375#endif
1376
Yong Zhang2c2efae2011-07-29 16:20:33 +08001377 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001378 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001379}
1380
Paul Turnerab84d312011-07-21 09:43:28 -07001381
1382/**************************************************
1383 * CFS bandwidth control machinery
1384 */
1385
1386#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001387
1388#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001389static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001390
1391static inline bool cfs_bandwidth_used(void)
1392{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001393 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001394}
1395
1396void account_cfs_bandwidth_used(int enabled, int was_enabled)
1397{
1398 /* only need to count groups transitioning between enabled/!enabled */
1399 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001400 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001401 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001402 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001403}
1404#else /* HAVE_JUMP_LABEL */
1405static bool cfs_bandwidth_used(void)
1406{
1407 return true;
1408}
1409
1410void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1411#endif /* HAVE_JUMP_LABEL */
1412
Paul Turnerab84d312011-07-21 09:43:28 -07001413/*
1414 * default period for cfs group bandwidth.
1415 * default: 0.1s, units: nanoseconds
1416 */
1417static inline u64 default_cfs_period(void)
1418{
1419 return 100000000ULL;
1420}
Paul Turnerec12cb72011-07-21 09:43:30 -07001421
1422static inline u64 sched_cfs_bandwidth_slice(void)
1423{
1424 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1425}
1426
Paul Turnera9cf55b2011-07-21 09:43:32 -07001427/*
1428 * Replenish runtime according to assigned quota and update expiration time.
1429 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1430 * additional synchronization around rq->lock.
1431 *
1432 * requires cfs_b->lock
1433 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001434void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001435{
1436 u64 now;
1437
1438 if (cfs_b->quota == RUNTIME_INF)
1439 return;
1440
1441 now = sched_clock_cpu(smp_processor_id());
1442 cfs_b->runtime = cfs_b->quota;
1443 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1444}
1445
Peter Zijlstra029632f2011-10-25 10:00:11 +02001446static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1447{
1448 return &tg->cfs_bandwidth;
1449}
1450
Paul Turner85dac902011-07-21 09:43:33 -07001451/* returns 0 on failure to allocate runtime */
1452static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001453{
1454 struct task_group *tg = cfs_rq->tg;
1455 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001456 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001457
1458 /* note: this is a positive sum as runtime_remaining <= 0 */
1459 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1460
1461 raw_spin_lock(&cfs_b->lock);
1462 if (cfs_b->quota == RUNTIME_INF)
1463 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001464 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001465 /*
1466 * If the bandwidth pool has become inactive, then at least one
1467 * period must have elapsed since the last consumption.
1468 * Refresh the global state and ensure bandwidth timer becomes
1469 * active.
1470 */
1471 if (!cfs_b->timer_active) {
1472 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001473 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001474 }
Paul Turner58088ad2011-07-21 09:43:31 -07001475
1476 if (cfs_b->runtime > 0) {
1477 amount = min(cfs_b->runtime, min_amount);
1478 cfs_b->runtime -= amount;
1479 cfs_b->idle = 0;
1480 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001481 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001482 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001483 raw_spin_unlock(&cfs_b->lock);
1484
1485 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001486 /*
1487 * we may have advanced our local expiration to account for allowed
1488 * spread between our sched_clock and the one on which runtime was
1489 * issued.
1490 */
1491 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1492 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001493
1494 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001495}
1496
1497/*
1498 * Note: This depends on the synchronization provided by sched_clock and the
1499 * fact that rq->clock snapshots this value.
1500 */
1501static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1502{
1503 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1504 struct rq *rq = rq_of(cfs_rq);
1505
1506 /* if the deadline is ahead of our clock, nothing to do */
1507 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1508 return;
1509
1510 if (cfs_rq->runtime_remaining < 0)
1511 return;
1512
1513 /*
1514 * If the local deadline has passed we have to consider the
1515 * possibility that our sched_clock is 'fast' and the global deadline
1516 * has not truly expired.
1517 *
1518 * Fortunately we can check determine whether this the case by checking
1519 * whether the global deadline has advanced.
1520 */
1521
1522 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1523 /* extend local deadline, drift is bounded above by 2 ticks */
1524 cfs_rq->runtime_expires += TICK_NSEC;
1525 } else {
1526 /* global deadline is ahead, expiration has passed */
1527 cfs_rq->runtime_remaining = 0;
1528 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001529}
1530
1531static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1532 unsigned long delta_exec)
1533{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001534 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001535 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001536 expire_cfs_rq_runtime(cfs_rq);
1537
1538 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001539 return;
1540
Paul Turner85dac902011-07-21 09:43:33 -07001541 /*
1542 * if we're unable to extend our runtime we resched so that the active
1543 * hierarchy can be throttled
1544 */
1545 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1546 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001547}
1548
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001549static __always_inline
1550void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001551{
Paul Turner56f570e2011-11-07 20:26:33 -08001552 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001553 return;
1554
1555 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1556}
1557
Paul Turner85dac902011-07-21 09:43:33 -07001558static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1559{
Paul Turner56f570e2011-11-07 20:26:33 -08001560 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001561}
1562
Paul Turner64660c82011-07-21 09:43:36 -07001563/* check whether cfs_rq, or any parent, is throttled */
1564static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1565{
Paul Turner56f570e2011-11-07 20:26:33 -08001566 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001567}
1568
1569/*
1570 * Ensure that neither of the group entities corresponding to src_cpu or
1571 * dest_cpu are members of a throttled hierarchy when performing group
1572 * load-balance operations.
1573 */
1574static inline int throttled_lb_pair(struct task_group *tg,
1575 int src_cpu, int dest_cpu)
1576{
1577 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1578
1579 src_cfs_rq = tg->cfs_rq[src_cpu];
1580 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1581
1582 return throttled_hierarchy(src_cfs_rq) ||
1583 throttled_hierarchy(dest_cfs_rq);
1584}
1585
1586/* updated child weight may affect parent so we have to do this bottom up */
1587static int tg_unthrottle_up(struct task_group *tg, void *data)
1588{
1589 struct rq *rq = data;
1590 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1591
1592 cfs_rq->throttle_count--;
1593#ifdef CONFIG_SMP
1594 if (!cfs_rq->throttle_count) {
1595 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1596
1597 /* leaving throttled state, advance shares averaging windows */
1598 cfs_rq->load_stamp += delta;
1599 cfs_rq->load_last += delta;
1600
1601 /* update entity weight now that we are on_rq again */
1602 update_cfs_shares(cfs_rq);
1603 }
1604#endif
1605
1606 return 0;
1607}
1608
1609static int tg_throttle_down(struct task_group *tg, void *data)
1610{
1611 struct rq *rq = data;
1612 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1613
1614 /* group is entering throttled state, record last load */
1615 if (!cfs_rq->throttle_count)
1616 update_cfs_load(cfs_rq, 0);
1617 cfs_rq->throttle_count++;
1618
1619 return 0;
1620}
1621
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001622static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001623{
1624 struct rq *rq = rq_of(cfs_rq);
1625 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1626 struct sched_entity *se;
1627 long task_delta, dequeue = 1;
1628
1629 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1630
1631 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001632 rcu_read_lock();
1633 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1634 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001635
1636 task_delta = cfs_rq->h_nr_running;
1637 for_each_sched_entity(se) {
1638 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1639 /* throttled entity or throttle-on-deactivate */
1640 if (!se->on_rq)
1641 break;
1642
1643 if (dequeue)
1644 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1645 qcfs_rq->h_nr_running -= task_delta;
1646
1647 if (qcfs_rq->load.weight)
1648 dequeue = 0;
1649 }
1650
1651 if (!se)
1652 rq->nr_running -= task_delta;
1653
1654 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001655 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001656 raw_spin_lock(&cfs_b->lock);
1657 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Ben Segalldfb473b2013-10-16 11:16:32 -07001658 if (!cfs_b->timer_active)
1659 __start_cfs_bandwidth(cfs_b);
Paul Turner85dac902011-07-21 09:43:33 -07001660 raw_spin_unlock(&cfs_b->lock);
1661}
1662
Peter Zijlstra029632f2011-10-25 10:00:11 +02001663void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001664{
1665 struct rq *rq = rq_of(cfs_rq);
1666 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1667 struct sched_entity *se;
1668 int enqueue = 1;
1669 long task_delta;
1670
1671 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1672
1673 cfs_rq->throttled = 0;
1674 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001675 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001676 list_del_rcu(&cfs_rq->throttled_list);
1677 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001678 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001679
Paul Turner64660c82011-07-21 09:43:36 -07001680 update_rq_clock(rq);
1681 /* update hierarchical throttle state */
1682 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1683
Paul Turner671fd9d2011-07-21 09:43:34 -07001684 if (!cfs_rq->load.weight)
1685 return;
1686
1687 task_delta = cfs_rq->h_nr_running;
1688 for_each_sched_entity(se) {
1689 if (se->on_rq)
1690 enqueue = 0;
1691
1692 cfs_rq = cfs_rq_of(se);
1693 if (enqueue)
1694 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1695 cfs_rq->h_nr_running += task_delta;
1696
1697 if (cfs_rq_throttled(cfs_rq))
1698 break;
1699 }
1700
1701 if (!se)
1702 rq->nr_running += task_delta;
1703
1704 /* determine whether we need to wake up potentially idle cpu */
1705 if (rq->curr == rq->idle && rq->cfs.nr_running)
1706 resched_task(rq->curr);
1707}
1708
1709static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1710 u64 remaining, u64 expires)
1711{
1712 struct cfs_rq *cfs_rq;
1713 u64 runtime = remaining;
1714
1715 rcu_read_lock();
1716 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1717 throttled_list) {
1718 struct rq *rq = rq_of(cfs_rq);
1719
1720 raw_spin_lock(&rq->lock);
1721 if (!cfs_rq_throttled(cfs_rq))
1722 goto next;
1723
1724 runtime = -cfs_rq->runtime_remaining + 1;
1725 if (runtime > remaining)
1726 runtime = remaining;
1727 remaining -= runtime;
1728
1729 cfs_rq->runtime_remaining += runtime;
1730 cfs_rq->runtime_expires = expires;
1731
1732 /* we check whether we're throttled above */
1733 if (cfs_rq->runtime_remaining > 0)
1734 unthrottle_cfs_rq(cfs_rq);
1735
1736next:
1737 raw_spin_unlock(&rq->lock);
1738
1739 if (!remaining)
1740 break;
1741 }
1742 rcu_read_unlock();
1743
1744 return remaining;
1745}
1746
Paul Turner58088ad2011-07-21 09:43:31 -07001747/*
1748 * Responsible for refilling a task_group's bandwidth and unthrottling its
1749 * cfs_rqs as appropriate. If there has been no activity within the last
1750 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1751 * used to track this state.
1752 */
1753static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1754{
Paul Turner671fd9d2011-07-21 09:43:34 -07001755 u64 runtime, runtime_expires;
1756 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001757
1758 raw_spin_lock(&cfs_b->lock);
1759 /* no need to continue the timer with no bandwidth constraint */
1760 if (cfs_b->quota == RUNTIME_INF)
1761 goto out_unlock;
1762
Paul Turner671fd9d2011-07-21 09:43:34 -07001763 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1764 /* idle depends on !throttled (for the case of a large deficit) */
1765 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001766 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001767
Paul Turnera9cf55b2011-07-21 09:43:32 -07001768 /* if we're going inactive then everything else can be deferred */
1769 if (idle)
1770 goto out_unlock;
1771
1772 __refill_cfs_bandwidth_runtime(cfs_b);
1773
Paul Turner671fd9d2011-07-21 09:43:34 -07001774 if (!throttled) {
1775 /* mark as potentially idle for the upcoming period */
1776 cfs_b->idle = 1;
1777 goto out_unlock;
1778 }
Paul Turner58088ad2011-07-21 09:43:31 -07001779
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001780 /* account preceding periods in which throttling occurred */
1781 cfs_b->nr_throttled += overrun;
1782
Paul Turner671fd9d2011-07-21 09:43:34 -07001783 /*
1784 * There are throttled entities so we must first use the new bandwidth
1785 * to unthrottle them before making it generally available. This
1786 * ensures that all existing debts will be paid before a new cfs_rq is
1787 * allowed to run.
1788 */
1789 runtime = cfs_b->runtime;
1790 runtime_expires = cfs_b->runtime_expires;
1791 cfs_b->runtime = 0;
1792
1793 /*
1794 * This check is repeated as we are holding onto the new bandwidth
1795 * while we unthrottle. This can potentially race with an unthrottled
1796 * group trying to acquire new bandwidth from the global pool.
1797 */
1798 while (throttled && runtime > 0) {
1799 raw_spin_unlock(&cfs_b->lock);
1800 /* we can't nest cfs_b->lock while distributing bandwidth */
1801 runtime = distribute_cfs_runtime(cfs_b, runtime,
1802 runtime_expires);
1803 raw_spin_lock(&cfs_b->lock);
1804
1805 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1806 }
1807
1808 /* return (any) remaining runtime */
1809 cfs_b->runtime = runtime;
1810 /*
1811 * While we are ensured activity in the period following an
1812 * unthrottle, this also covers the case in which the new bandwidth is
1813 * insufficient to cover the existing bandwidth deficit. (Forcing the
1814 * timer to remain active while there are any throttled entities.)
1815 */
1816 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07001817out_unlock:
1818 if (idle)
1819 cfs_b->timer_active = 0;
1820 raw_spin_unlock(&cfs_b->lock);
1821
1822 return idle;
1823}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001824
Paul Turnerd8b49862011-07-21 09:43:41 -07001825/* a cfs_rq won't donate quota below this amount */
1826static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
1827/* minimum remaining period time to redistribute slack quota */
1828static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
1829/* how long we wait to gather additional slack before distributing */
1830static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
1831
1832/* are we near the end of the current quota period? */
1833static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
1834{
1835 struct hrtimer *refresh_timer = &cfs_b->period_timer;
1836 u64 remaining;
1837
1838 /* if the call-back is running a quota refresh is already occurring */
1839 if (hrtimer_callback_running(refresh_timer))
1840 return 1;
1841
1842 /* is a quota refresh about to occur? */
1843 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
1844 if (remaining < min_expire)
1845 return 1;
1846
1847 return 0;
1848}
1849
1850static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
1851{
1852 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
1853
1854 /* if there's a quota refresh soon don't bother with slack */
1855 if (runtime_refresh_within(cfs_b, min_left))
1856 return;
1857
1858 start_bandwidth_timer(&cfs_b->slack_timer,
1859 ns_to_ktime(cfs_bandwidth_slack_period));
1860}
1861
1862/* we know any runtime found here is valid as update_curr() precedes return */
1863static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1864{
1865 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1866 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
1867
1868 if (slack_runtime <= 0)
1869 return;
1870
1871 raw_spin_lock(&cfs_b->lock);
1872 if (cfs_b->quota != RUNTIME_INF &&
1873 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
1874 cfs_b->runtime += slack_runtime;
1875
1876 /* we are under rq->lock, defer unthrottling using a timer */
1877 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
1878 !list_empty(&cfs_b->throttled_cfs_rq))
1879 start_cfs_slack_bandwidth(cfs_b);
1880 }
1881 raw_spin_unlock(&cfs_b->lock);
1882
1883 /* even if it's not valid for return we don't want to try again */
1884 cfs_rq->runtime_remaining -= slack_runtime;
1885}
1886
1887static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1888{
Paul Turner56f570e2011-11-07 20:26:33 -08001889 if (!cfs_bandwidth_used())
1890 return;
1891
Paul Turnerfccfdc62011-11-07 20:26:34 -08001892 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07001893 return;
1894
1895 __return_cfs_rq_runtime(cfs_rq);
1896}
1897
1898/*
1899 * This is done with a timer (instead of inline with bandwidth return) since
1900 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
1901 */
1902static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
1903{
1904 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
1905 u64 expires;
1906
1907 /* confirm we're still not at a refresh boundary */
1908 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
1909 return;
1910
1911 raw_spin_lock(&cfs_b->lock);
1912 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
1913 runtime = cfs_b->runtime;
1914 cfs_b->runtime = 0;
1915 }
1916 expires = cfs_b->runtime_expires;
1917 raw_spin_unlock(&cfs_b->lock);
1918
1919 if (!runtime)
1920 return;
1921
1922 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
1923
1924 raw_spin_lock(&cfs_b->lock);
1925 if (expires == cfs_b->runtime_expires)
1926 cfs_b->runtime = runtime;
1927 raw_spin_unlock(&cfs_b->lock);
1928}
1929
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001930/*
1931 * When a group wakes up we want to make sure that its quota is not already
1932 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
1933 * runtime as update_curr() throttling can not not trigger until it's on-rq.
1934 */
1935static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
1936{
Paul Turner56f570e2011-11-07 20:26:33 -08001937 if (!cfs_bandwidth_used())
1938 return;
1939
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001940 /* an active group must be handled by the update_curr()->put() path */
1941 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
1942 return;
1943
1944 /* ensure the group is not already throttled */
1945 if (cfs_rq_throttled(cfs_rq))
1946 return;
1947
1948 /* update runtime allocation */
1949 account_cfs_rq_runtime(cfs_rq, 0);
1950 if (cfs_rq->runtime_remaining <= 0)
1951 throttle_cfs_rq(cfs_rq);
1952}
1953
1954/* conditionally throttle active cfs_rq's from put_prev_entity() */
1955static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1956{
Paul Turner56f570e2011-11-07 20:26:33 -08001957 if (!cfs_bandwidth_used())
1958 return;
1959
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001960 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
1961 return;
1962
1963 /*
1964 * it's possible for a throttled entity to be forced into a running
1965 * state (e.g. set_curr_task), in this case we're finished.
1966 */
1967 if (cfs_rq_throttled(cfs_rq))
1968 return;
1969
1970 throttle_cfs_rq(cfs_rq);
1971}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001972
1973static inline u64 default_cfs_period(void);
1974static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
1975static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
1976
1977static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
1978{
1979 struct cfs_bandwidth *cfs_b =
1980 container_of(timer, struct cfs_bandwidth, slack_timer);
1981 do_sched_cfs_slack_timer(cfs_b);
1982
1983 return HRTIMER_NORESTART;
1984}
1985
1986static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
1987{
1988 struct cfs_bandwidth *cfs_b =
1989 container_of(timer, struct cfs_bandwidth, period_timer);
1990 ktime_t now;
1991 int overrun;
1992 int idle = 0;
1993
1994 for (;;) {
1995 now = hrtimer_cb_get_time(timer);
1996 overrun = hrtimer_forward(timer, now, cfs_b->period);
1997
1998 if (!overrun)
1999 break;
2000
2001 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2002 }
2003
2004 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2005}
2006
2007void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2008{
2009 raw_spin_lock_init(&cfs_b->lock);
2010 cfs_b->runtime = 0;
2011 cfs_b->quota = RUNTIME_INF;
2012 cfs_b->period = ns_to_ktime(default_cfs_period());
2013
2014 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2015 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2016 cfs_b->period_timer.function = sched_cfs_period_timer;
2017 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2018 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2019}
2020
2021static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2022{
2023 cfs_rq->runtime_enabled = 0;
2024 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2025}
2026
2027/* requires cfs_b->lock, may release to reprogram timer */
2028void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2029{
2030 /*
2031 * The timer may be active because we're trying to set a new bandwidth
2032 * period or because we're racing with the tear-down path
2033 * (timer_active==0 becomes visible before the hrtimer call-back
2034 * terminates). In either case we ensure that it's re-programmed
2035 */
2036 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2037 raw_spin_unlock(&cfs_b->lock);
2038 /* ensure cfs_b->lock is available while we wait */
2039 hrtimer_cancel(&cfs_b->period_timer);
2040
2041 raw_spin_lock(&cfs_b->lock);
2042 /* if someone else restarted the timer then we're done */
2043 if (cfs_b->timer_active)
2044 return;
2045 }
2046
2047 cfs_b->timer_active = 1;
2048 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2049}
2050
2051static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2052{
2053 hrtimer_cancel(&cfs_b->period_timer);
2054 hrtimer_cancel(&cfs_b->slack_timer);
2055}
2056
2057void unthrottle_offline_cfs_rqs(struct rq *rq)
2058{
2059 struct cfs_rq *cfs_rq;
2060
2061 for_each_leaf_cfs_rq(rq, cfs_rq) {
2062 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2063
2064 if (!cfs_rq->runtime_enabled)
2065 continue;
2066
2067 /*
2068 * clock_task is not advancing so we just need to make sure
2069 * there's some valid quota amount
2070 */
2071 cfs_rq->runtime_remaining = cfs_b->quota;
2072 if (cfs_rq_throttled(cfs_rq))
2073 unthrottle_cfs_rq(cfs_rq);
2074 }
2075}
2076
2077#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002078static __always_inline
2079void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002080static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2081static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002082static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002083
2084static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2085{
2086 return 0;
2087}
Paul Turner64660c82011-07-21 09:43:36 -07002088
2089static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2090{
2091 return 0;
2092}
2093
2094static inline int throttled_lb_pair(struct task_group *tg,
2095 int src_cpu, int dest_cpu)
2096{
2097 return 0;
2098}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002099
2100void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2101
2102#ifdef CONFIG_FAIR_GROUP_SCHED
2103static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002104#endif
2105
Peter Zijlstra029632f2011-10-25 10:00:11 +02002106static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2107{
2108 return NULL;
2109}
2110static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2111void unthrottle_offline_cfs_rqs(struct rq *rq) {}
2112
2113#endif /* CONFIG_CFS_BANDWIDTH */
2114
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002115/**************************************************
2116 * CFS operations on tasks:
2117 */
2118
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002119#ifdef CONFIG_SCHED_HRTICK
2120static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2121{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002122 struct sched_entity *se = &p->se;
2123 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2124
2125 WARN_ON(task_rq(p) != rq);
2126
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002127 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002128 u64 slice = sched_slice(cfs_rq, se);
2129 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2130 s64 delta = slice - ran;
2131
2132 if (delta < 0) {
2133 if (rq->curr == p)
2134 resched_task(p);
2135 return;
2136 }
2137
2138 /*
2139 * Don't schedule slices shorter than 10000ns, that just
2140 * doesn't make sense. Rely on vruntime for fairness.
2141 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002142 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002143 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002144
Peter Zijlstra31656512008-07-18 18:01:23 +02002145 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002146 }
2147}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002148
2149/*
2150 * called from enqueue/dequeue and updates the hrtick when the
2151 * current task is from our class and nr_running is low enough
2152 * to matter.
2153 */
2154static void hrtick_update(struct rq *rq)
2155{
2156 struct task_struct *curr = rq->curr;
2157
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002158 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002159 return;
2160
2161 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2162 hrtick_start_fair(rq, curr);
2163}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302164#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002165static inline void
2166hrtick_start_fair(struct rq *rq, struct task_struct *p)
2167{
2168}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002169
2170static inline void hrtick_update(struct rq *rq)
2171{
2172}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002173#endif
2174
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002175/*
2176 * The enqueue_task method is called before nr_running is
2177 * increased. Here we update the fair scheduling stats and
2178 * then put the task into the rbtree:
2179 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002180static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002181enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002182{
2183 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002184 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002185
2186 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002187 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002188 break;
2189 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002190 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002191
2192 /*
2193 * end evaluation on encountering a throttled cfs_rq
2194 *
2195 * note: in the case of encountering a throttled cfs_rq we will
2196 * post the final h_nr_running increment below.
2197 */
2198 if (cfs_rq_throttled(cfs_rq))
2199 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002200 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002201
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002202 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002203 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002204
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002205 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002206 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002207 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002208
Paul Turner85dac902011-07-21 09:43:33 -07002209 if (cfs_rq_throttled(cfs_rq))
2210 break;
2211
Paul Turnerd6b55912010-11-15 15:47:09 -08002212 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002213 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002214 }
2215
Paul Turner85dac902011-07-21 09:43:33 -07002216 if (!se)
2217 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002218 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002219}
2220
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002221static void set_next_buddy(struct sched_entity *se);
2222
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002223/*
2224 * The dequeue_task method is called before nr_running is
2225 * decreased. We remove the task from the rbtree and
2226 * update the fair scheduling stats:
2227 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002228static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002229{
2230 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002231 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002232 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002233
2234 for_each_sched_entity(se) {
2235 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002236 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002237
2238 /*
2239 * end evaluation on encountering a throttled cfs_rq
2240 *
2241 * note: in the case of encountering a throttled cfs_rq we will
2242 * post the final h_nr_running decrement below.
2243 */
2244 if (cfs_rq_throttled(cfs_rq))
2245 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002246 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002247
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002248 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002249 if (cfs_rq->load.weight) {
2250 /*
2251 * Bias pick_next to pick a task from this cfs_rq, as
2252 * p is sleeping when it is within its sched_slice.
2253 */
2254 if (task_sleep && parent_entity(se))
2255 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002256
2257 /* avoid re-evaluating load for this entity */
2258 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002259 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002260 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002261 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002262 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002263
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002264 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002265 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002266 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002267
Paul Turner85dac902011-07-21 09:43:33 -07002268 if (cfs_rq_throttled(cfs_rq))
2269 break;
2270
Paul Turnerd6b55912010-11-15 15:47:09 -08002271 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002272 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002273 }
2274
Paul Turner85dac902011-07-21 09:43:33 -07002275 if (!se)
2276 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002277 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002278}
2279
Gregory Haskinse7693a32008-01-25 21:08:09 +01002280#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002281/* Used instead of source_load when we know the type == 0 */
2282static unsigned long weighted_cpuload(const int cpu)
2283{
2284 return cpu_rq(cpu)->load.weight;
2285}
2286
2287/*
2288 * Return a low guess at the load of a migration-source cpu weighted
2289 * according to the scheduling class and "nice" value.
2290 *
2291 * We want to under-estimate the load of migration sources, to
2292 * balance conservatively.
2293 */
2294static unsigned long source_load(int cpu, int type)
2295{
2296 struct rq *rq = cpu_rq(cpu);
2297 unsigned long total = weighted_cpuload(cpu);
2298
2299 if (type == 0 || !sched_feat(LB_BIAS))
2300 return total;
2301
2302 return min(rq->cpu_load[type-1], total);
2303}
2304
2305/*
2306 * Return a high guess at the load of a migration-target cpu weighted
2307 * according to the scheduling class and "nice" value.
2308 */
2309static unsigned long target_load(int cpu, int type)
2310{
2311 struct rq *rq = cpu_rq(cpu);
2312 unsigned long total = weighted_cpuload(cpu);
2313
2314 if (type == 0 || !sched_feat(LB_BIAS))
2315 return total;
2316
2317 return max(rq->cpu_load[type-1], total);
2318}
2319
2320static unsigned long power_of(int cpu)
2321{
2322 return cpu_rq(cpu)->cpu_power;
2323}
2324
2325static unsigned long cpu_avg_load_per_task(int cpu)
2326{
2327 struct rq *rq = cpu_rq(cpu);
2328 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2329
2330 if (nr_running)
2331 return rq->load.weight / nr_running;
2332
2333 return 0;
2334}
2335
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002336
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002337static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002338{
2339 struct sched_entity *se = &p->se;
2340 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002341 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002342
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002343#ifndef CONFIG_64BIT
2344 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002345
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002346 do {
2347 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2348 smp_rmb();
2349 min_vruntime = cfs_rq->min_vruntime;
2350 } while (min_vruntime != min_vruntime_copy);
2351#else
2352 min_vruntime = cfs_rq->min_vruntime;
2353#endif
2354
2355 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002356}
2357
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002358#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002359/*
2360 * effective_load() calculates the load change as seen from the root_task_group
2361 *
2362 * Adding load to a group doesn't make a group heavier, but can cause movement
2363 * of group shares between cpus. Assuming the shares were perfectly aligned one
2364 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002365 *
2366 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2367 * on this @cpu and results in a total addition (subtraction) of @wg to the
2368 * total group weight.
2369 *
2370 * Given a runqueue weight distribution (rw_i) we can compute a shares
2371 * distribution (s_i) using:
2372 *
2373 * s_i = rw_i / \Sum rw_j (1)
2374 *
2375 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2376 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2377 * shares distribution (s_i):
2378 *
2379 * rw_i = { 2, 4, 1, 0 }
2380 * s_i = { 2/7, 4/7, 1/7, 0 }
2381 *
2382 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2383 * task used to run on and the CPU the waker is running on), we need to
2384 * compute the effect of waking a task on either CPU and, in case of a sync
2385 * wakeup, compute the effect of the current task going to sleep.
2386 *
2387 * So for a change of @wl to the local @cpu with an overall group weight change
2388 * of @wl we can compute the new shares distribution (s'_i) using:
2389 *
2390 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2391 *
2392 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2393 * differences in waking a task to CPU 0. The additional task changes the
2394 * weight and shares distributions like:
2395 *
2396 * rw'_i = { 3, 4, 1, 0 }
2397 * s'_i = { 3/8, 4/8, 1/8, 0 }
2398 *
2399 * We can then compute the difference in effective weight by using:
2400 *
2401 * dw_i = S * (s'_i - s_i) (3)
2402 *
2403 * Where 'S' is the group weight as seen by its parent.
2404 *
2405 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2406 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2407 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002408 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002409static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002410{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002411 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002412
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002413 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002414 return wl;
2415
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002416 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002417 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002418
Paul Turner977dda72011-01-14 17:57:50 -08002419 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002420
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002421 /*
2422 * W = @wg + \Sum rw_j
2423 */
2424 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002425
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002426 /*
2427 * w = rw_i + @wl
2428 */
2429 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002430
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002431 /*
2432 * wl = S * s'_i; see (2)
2433 */
2434 if (W > 0 && w < W)
2435 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002436 else
2437 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002438
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002439 /*
2440 * Per the above, wl is the new se->load.weight value; since
2441 * those are clipped to [MIN_SHARES, ...) do so now. See
2442 * calc_cfs_shares().
2443 */
Paul Turner977dda72011-01-14 17:57:50 -08002444 if (wl < MIN_SHARES)
2445 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002446
2447 /*
2448 * wl = dw_i = S * (s'_i - s_i); see (3)
2449 */
Paul Turner977dda72011-01-14 17:57:50 -08002450 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002451
2452 /*
2453 * Recursively apply this logic to all parent groups to compute
2454 * the final effective load change on the root group. Since
2455 * only the @tg group gets extra weight, all parent groups can
2456 * only redistribute existing shares. @wl is the shift in shares
2457 * resulting from this level per the above.
2458 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002459 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002460 }
2461
2462 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002463}
2464#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002465
Peter Zijlstra83378262008-06-27 13:41:37 +02002466static inline unsigned long effective_load(struct task_group *tg, int cpu,
2467 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002468{
Peter Zijlstra83378262008-06-27 13:41:37 +02002469 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002470}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002471
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002472#endif
2473
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002474static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002475{
Paul Turnere37b6a72011-01-21 20:44:59 -08002476 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002477 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002478 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002479 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002480 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002481 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002482
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002483 idx = sd->wake_idx;
2484 this_cpu = smp_processor_id();
2485 prev_cpu = task_cpu(p);
2486 load = source_load(prev_cpu, idx);
2487 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002488
2489 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002490 * If sync wakeup then subtract the (maximum possible)
2491 * effect of the currently running task from the load
2492 * of the current CPU:
2493 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002494 if (sync) {
2495 tg = task_group(current);
2496 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002497
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002498 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002499 load += effective_load(tg, prev_cpu, 0, -weight);
2500 }
2501
2502 tg = task_group(p);
2503 weight = p->se.load.weight;
2504
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002505 /*
2506 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002507 * due to the sync cause above having dropped this_load to 0, we'll
2508 * always have an imbalance, but there's really nothing you can do
2509 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002510 *
2511 * Otherwise check if either cpus are near enough in load to allow this
2512 * task to be woken on this_cpu.
2513 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002514 if (this_load > 0) {
2515 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002516
2517 this_eff_load = 100;
2518 this_eff_load *= power_of(prev_cpu);
2519 this_eff_load *= this_load +
2520 effective_load(tg, this_cpu, weight, weight);
2521
2522 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2523 prev_eff_load *= power_of(this_cpu);
2524 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2525
2526 balanced = this_eff_load <= prev_eff_load;
2527 } else
2528 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002529
2530 /*
2531 * If the currently running task will sleep within
2532 * a reasonable amount of time then attract this newly
2533 * woken task:
2534 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002535 if (sync && balanced)
2536 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002537
Lucas De Marchi41acab82010-03-10 23:37:45 -03002538 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002539 tl_per_task = cpu_avg_load_per_task(this_cpu);
2540
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002541 if (balanced ||
2542 (this_load <= load &&
2543 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002544 /*
2545 * This domain has SD_WAKE_AFFINE and
2546 * p is cache cold in this domain, and
2547 * there is no bad imbalance.
2548 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002549 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002550 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002551
2552 return 1;
2553 }
2554 return 0;
2555}
2556
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002557/*
2558 * find_idlest_group finds and returns the least busy CPU group within the
2559 * domain.
2560 */
2561static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002562find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002563 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002564{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002565 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002566 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002567 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002568
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002569 do {
2570 unsigned long load, avg_load;
2571 int local_group;
2572 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002573
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002574 /* Skip over this group if it has no CPUs allowed */
2575 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002576 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002577 continue;
2578
2579 local_group = cpumask_test_cpu(this_cpu,
2580 sched_group_cpus(group));
2581
2582 /* Tally up the load of all CPUs in the group */
2583 avg_load = 0;
2584
2585 for_each_cpu(i, sched_group_cpus(group)) {
2586 /* Bias balancing toward cpus of our domain */
2587 if (local_group)
2588 load = source_load(i, load_idx);
2589 else
2590 load = target_load(i, load_idx);
2591
2592 avg_load += load;
2593 }
2594
2595 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002596 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002597
2598 if (local_group) {
2599 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002600 } else if (avg_load < min_load) {
2601 min_load = avg_load;
2602 idlest = group;
2603 }
2604 } while (group = group->next, group != sd->groups);
2605
2606 if (!idlest || 100*this_load < imbalance*min_load)
2607 return NULL;
2608 return idlest;
2609}
2610
2611/*
2612 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2613 */
2614static int
2615find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2616{
2617 unsigned long load, min_load = ULONG_MAX;
2618 int idlest = -1;
2619 int i;
2620
2621 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002622 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002623 load = weighted_cpuload(i);
2624
2625 if (load < min_load || (load == min_load && i == this_cpu)) {
2626 min_load = load;
2627 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002628 }
2629 }
2630
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002631 return idlest;
2632}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002633
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002634/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002635 * Try and locate an idle CPU in the sched_domain.
2636 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002637static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002638{
2639 int cpu = smp_processor_id();
2640 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002641 struct sched_domain *sd;
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002642 struct sched_group *sg;
Suresh Siddha77e81362011-11-17 11:08:23 -08002643 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002644
2645 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002646 * If the task is going to be woken-up on this cpu and if it is
2647 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002648 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002649 if (target == cpu && idle_cpu(cpu))
2650 return cpu;
2651
2652 /*
2653 * If the task is going to be woken-up on the cpu where it previously
2654 * ran and if it is currently idle, then it the right target.
2655 */
2656 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002657 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002658
2659 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002660 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002661 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002662 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002663 for_each_lower_domain(sd) {
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002664 sg = sd->groups;
2665 do {
2666 if (!cpumask_intersects(sched_group_cpus(sg),
2667 tsk_cpus_allowed(p)))
2668 goto next;
2669
2670 for_each_cpu(i, sched_group_cpus(sg)) {
2671 if (!idle_cpu(i))
2672 goto next;
2673 }
2674
2675 target = cpumask_first_and(sched_group_cpus(sg),
2676 tsk_cpus_allowed(p));
2677 goto done;
2678next:
2679 sg = sg->next;
2680 } while (sg != sd->groups);
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002681 }
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002682done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002683 return target;
2684}
2685
2686/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002687 * sched_balance_self: balance the current task (running on cpu) in domains
2688 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2689 * SD_BALANCE_EXEC.
2690 *
2691 * Balance, ie. select the least loaded group.
2692 *
2693 * Returns the target CPU number, or the same CPU if no balancing is needed.
2694 *
2695 * preempt must be disabled.
2696 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002697static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002698select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002699{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002700 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002701 int cpu = smp_processor_id();
2702 int prev_cpu = task_cpu(p);
2703 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002704 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002705 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002706 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002707
Mike Galbraith76854c72011-11-22 15:18:24 +01002708 if (p->rt.nr_cpus_allowed == 1)
2709 return prev_cpu;
2710
Peter Zijlstra0763a662009-09-14 19:37:39 +02002711 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002712 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002713 want_affine = 1;
2714 new_cpu = prev_cpu;
2715 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002716
Peter Zijlstradce840a2011-04-07 14:09:50 +02002717 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002718 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002719 if (!(tmp->flags & SD_LOAD_BALANCE))
2720 continue;
2721
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002722 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02002723 * If power savings logic is enabled for a domain, see if we
2724 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002725 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02002726 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02002727 unsigned long power = 0;
2728 unsigned long nr_running = 0;
2729 unsigned long capacity;
2730 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002731
Peter Zijlstraae154be2009-09-10 14:40:57 +02002732 for_each_cpu(i, sched_domain_span(tmp)) {
2733 power += power_of(i);
2734 nr_running += cpu_rq(i)->cfs.nr_running;
2735 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002736
Nikhil Rao1399fa72011-05-18 10:09:39 -07002737 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002738
Peter Zijlstra59abf022009-09-16 08:28:30 +02002739 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2740 nr_running /= 2;
2741
2742 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002743 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002744 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002745
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002746 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002747 * If both cpu and prev_cpu are part of this domain,
2748 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002749 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002750 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2751 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2752 affine_sd = tmp;
2753 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002754 }
2755
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002756 if (!want_sd && !want_affine)
2757 break;
2758
Peter Zijlstra0763a662009-09-14 19:37:39 +02002759 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002760 continue;
2761
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002762 if (want_sd)
2763 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002764 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002765
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002766 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002767 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002768 prev_cpu = cpu;
2769
2770 new_cpu = select_idle_sibling(p, prev_cpu);
2771 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002772 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002773
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002774 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002775 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002776 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002777 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002778
Peter Zijlstra0763a662009-09-14 19:37:39 +02002779 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002780 sd = sd->child;
2781 continue;
2782 }
2783
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002784 if (sd_flag & SD_BALANCE_WAKE)
2785 load_idx = sd->wake_idx;
2786
2787 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002788 if (!group) {
2789 sd = sd->child;
2790 continue;
2791 }
2792
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002793 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002794 if (new_cpu == -1 || new_cpu == cpu) {
2795 /* Now try balancing at a lower domain level of cpu */
2796 sd = sd->child;
2797 continue;
2798 }
2799
2800 /* Now try balancing at a lower domain level of new_cpu */
2801 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002802 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002803 sd = NULL;
2804 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002805 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002806 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002807 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002808 sd = tmp;
2809 }
2810 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002811 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002812unlock:
2813 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002814
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002815 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002816}
2817#endif /* CONFIG_SMP */
2818
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002819static unsigned long
2820wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002821{
2822 unsigned long gran = sysctl_sched_wakeup_granularity;
2823
2824 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002825 * Since its curr running now, convert the gran from real-time
2826 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002827 *
2828 * By using 'se' instead of 'curr' we penalize light tasks, so
2829 * they get preempted easier. That is, if 'se' < 'curr' then
2830 * the resulting gran will be larger, therefore penalizing the
2831 * lighter, if otoh 'se' > 'curr' then the resulting gran will
2832 * be smaller, again penalizing the lighter task.
2833 *
2834 * This is especially important for buddies when the leftmost
2835 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002836 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08002837 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002838}
2839
2840/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02002841 * Should 'se' preempt 'curr'.
2842 *
2843 * |s1
2844 * |s2
2845 * |s3
2846 * g
2847 * |<--->|c
2848 *
2849 * w(c, s1) = -1
2850 * w(c, s2) = 0
2851 * w(c, s3) = 1
2852 *
2853 */
2854static int
2855wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2856{
2857 s64 gran, vdiff = curr->vruntime - se->vruntime;
2858
2859 if (vdiff <= 0)
2860 return -1;
2861
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002862 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02002863 if (vdiff > gran)
2864 return 1;
2865
2866 return 0;
2867}
2868
Peter Zijlstra02479092008-11-04 21:25:10 +01002869static void set_last_buddy(struct sched_entity *se)
2870{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002871 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2872 return;
2873
2874 for_each_sched_entity(se)
2875 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002876}
2877
2878static void set_next_buddy(struct sched_entity *se)
2879{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002880 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2881 return;
2882
2883 for_each_sched_entity(se)
2884 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002885}
2886
Rik van Rielac53db52011-02-01 09:51:03 -05002887static void set_skip_buddy(struct sched_entity *se)
2888{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002889 for_each_sched_entity(se)
2890 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05002891}
2892
Peter Zijlstra464b7522008-10-24 11:06:15 +02002893/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002894 * Preempt the current task with a newly woken task if needed:
2895 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02002896static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002897{
2898 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02002899 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002900 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002901 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002902 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002903
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002904 if (unlikely(se == pse))
2905 return;
2906
Paul Turner5238cdd2011-07-21 09:43:37 -07002907 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01002908 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07002909 * unconditionally check_prempt_curr() after an enqueue (which may have
2910 * lead to a throttle). This both saves work and prevents false
2911 * next-buddy nomination below.
2912 */
2913 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
2914 return;
2915
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002916 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02002917 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002918 next_buddy_marked = 1;
2919 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02002920
Bharata B Raoaec0a512008-08-28 14:42:49 +05302921 /*
2922 * We can come here with TIF_NEED_RESCHED already set from new task
2923 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07002924 *
2925 * Note: this also catches the edge-case of curr being in a throttled
2926 * group (e.g. via set_curr_task), since update_curr() (in the
2927 * enqueue of curr) will have resulted in resched being set. This
2928 * prevents us from potentially nominating it as a false LAST_BUDDY
2929 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05302930 */
2931 if (test_tsk_need_resched(curr))
2932 return;
2933
Darren Harta2f5c9a2011-02-22 13:04:33 -08002934 /* Idle tasks are by definition preempted by non-idle tasks. */
2935 if (unlikely(curr->policy == SCHED_IDLE) &&
2936 likely(p->policy != SCHED_IDLE))
2937 goto preempt;
2938
Ingo Molnar91c234b2007-10-15 17:00:18 +02002939 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08002940 * Batch and idle tasks do not preempt non-idle tasks (their preemption
2941 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02002942 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01002943 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02002944 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002945
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002946 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07002947 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002948 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002949 if (wakeup_preempt_entity(se, pse) == 1) {
2950 /*
2951 * Bias pick_next to pick the sched entity that is
2952 * triggering this preemption.
2953 */
2954 if (!next_buddy_marked)
2955 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002956 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002957 }
Jupyung Leea65ac742009-11-17 18:51:40 +09002958
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002959 return;
2960
2961preempt:
2962 resched_task(curr);
2963 /*
2964 * Only set the backward buddy when the current task is still
2965 * on the rq. This can happen when a wakeup gets interleaved
2966 * with schedule on the ->pre_schedule() or idle_balance()
2967 * point, either of which can * drop the rq lock.
2968 *
2969 * Also, during early boot the idle thread is in the fair class,
2970 * for obvious reasons its a bad idea to schedule back to it.
2971 */
2972 if (unlikely(!se->on_rq || curr == rq->idle))
2973 return;
2974
2975 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
2976 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002977}
2978
Ingo Molnarfb8d4722007-08-09 11:16:48 +02002979static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002980{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002981 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002982 struct cfs_rq *cfs_rq = &rq->cfs;
2983 struct sched_entity *se;
2984
Tim Blechmann36ace272009-11-24 11:55:45 +01002985 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002986 return NULL;
2987
2988 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02002989 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01002990 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002991 cfs_rq = group_cfs_rq(se);
2992 } while (cfs_rq);
2993
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002994 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002995 if (hrtick_enabled(rq))
2996 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002997
2998 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002999}
3000
3001/*
3002 * Account for a descheduled task:
3003 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003004static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003005{
3006 struct sched_entity *se = &prev->se;
3007 struct cfs_rq *cfs_rq;
3008
3009 for_each_sched_entity(se) {
3010 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003011 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003012 }
3013}
3014
Rik van Rielac53db52011-02-01 09:51:03 -05003015/*
3016 * sched_yield() is very simple
3017 *
3018 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3019 */
3020static void yield_task_fair(struct rq *rq)
3021{
3022 struct task_struct *curr = rq->curr;
3023 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3024 struct sched_entity *se = &curr->se;
3025
3026 /*
3027 * Are we the only task in the tree?
3028 */
3029 if (unlikely(rq->nr_running == 1))
3030 return;
3031
3032 clear_buddies(cfs_rq, se);
3033
3034 if (curr->policy != SCHED_BATCH) {
3035 update_rq_clock(rq);
3036 /*
3037 * Update run-time statistics of the 'current'.
3038 */
3039 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003040 /*
3041 * Tell update_rq_clock() that we've just updated,
3042 * so we don't do microscopic update in schedule()
3043 * and double the fastpath cost.
3044 */
3045 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003046 }
3047
3048 set_skip_buddy(se);
3049}
3050
Mike Galbraithd95f4122011-02-01 09:50:51 -05003051static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3052{
3053 struct sched_entity *se = &p->se;
3054
Paul Turner5238cdd2011-07-21 09:43:37 -07003055 /* throttled hierarchies are not runnable */
3056 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003057 return false;
3058
3059 /* Tell the scheduler that we'd really like pse to run next. */
3060 set_next_buddy(se);
3061
Mike Galbraithd95f4122011-02-01 09:50:51 -05003062 yield_task_fair(rq);
3063
3064 return true;
3065}
3066
Peter Williams681f3e62007-10-24 18:23:51 +02003067#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003068/**************************************************
3069 * Fair scheduling class load-balancing methods:
3070 */
3071
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003072static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3073
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003074#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003075#define LBF_NEED_BREAK 0x02
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003076
3077struct lb_env {
3078 struct sched_domain *sd;
3079
3080 int src_cpu;
3081 struct rq *src_rq;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003082
3083 int dst_cpu;
3084 struct rq *dst_rq;
3085
3086 enum cpu_idle_type idle;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003087 long load_move;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003088 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003089
3090 unsigned int loop;
3091 unsigned int loop_break;
3092 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003093};
3094
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003095/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003096 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003097 * Both runqueues must be locked.
3098 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003099static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003100{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003101 deactivate_task(env->src_rq, p, 0);
3102 set_task_cpu(p, env->dst_cpu);
3103 activate_task(env->dst_rq, p, 0);
3104 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003105}
3106
3107/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003108 * Is this task likely cache-hot:
3109 */
3110static int
3111task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3112{
3113 s64 delta;
3114
3115 if (p->sched_class != &fair_sched_class)
3116 return 0;
3117
3118 if (unlikely(p->policy == SCHED_IDLE))
3119 return 0;
3120
3121 /*
3122 * Buddy candidates are cache hot:
3123 */
3124 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3125 (&p->se == cfs_rq_of(&p->se)->next ||
3126 &p->se == cfs_rq_of(&p->se)->last))
3127 return 1;
3128
3129 if (sysctl_sched_migration_cost == -1)
3130 return 1;
3131 if (sysctl_sched_migration_cost == 0)
3132 return 0;
3133
3134 delta = now - p->se.exec_start;
3135
3136 return delta < (s64)sysctl_sched_migration_cost;
3137}
3138
3139/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003140 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3141 */
3142static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003143int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003144{
3145 int tsk_cache_hot = 0;
3146 /*
3147 * We do not migrate tasks that are:
3148 * 1) running (obviously), or
3149 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3150 * 3) are cache-hot on their current CPU.
3151 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003152 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003153 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003154 return 0;
3155 }
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003156 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003157
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003158 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003159 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003160 return 0;
3161 }
3162
3163 /*
3164 * Aggressive migration if:
3165 * 1) task is cache cold, or
3166 * 2) too many balance attempts have failed.
3167 */
3168
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003169 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003170 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003171 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003172#ifdef CONFIG_SCHEDSTATS
3173 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003174 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003175 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003176 }
3177#endif
3178 return 1;
3179 }
3180
3181 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003182 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003183 return 0;
3184 }
3185 return 1;
3186}
3187
Peter Zijlstra897c3952009-12-17 17:45:42 +01003188/*
3189 * move_one_task tries to move exactly one task from busiest to this_rq, as
3190 * part of active balancing operations within "domain".
3191 * Returns 1 if successful and 0 otherwise.
3192 *
3193 * Called with both runqueues locked.
3194 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003195static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003196{
3197 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003198
Peter Zijlstra367456c2012-02-20 21:49:09 +01003199 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3200 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3201 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003202
Peter Zijlstra367456c2012-02-20 21:49:09 +01003203 if (!can_migrate_task(p, env))
3204 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003205
Peter Zijlstra367456c2012-02-20 21:49:09 +01003206 move_task(p, env);
3207 /*
3208 * Right now, this is only the second place move_task()
3209 * is called, so we can safely collect move_task()
3210 * stats here rather than inside move_task().
3211 */
3212 schedstat_inc(env->sd, lb_gained[env->idle]);
3213 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003214 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003215 return 0;
3216}
3217
Peter Zijlstra367456c2012-02-20 21:49:09 +01003218static unsigned long task_h_load(struct task_struct *p);
3219
Peter Zijlstraeb953082012-04-17 13:38:40 +02003220static const unsigned int sched_nr_migrate_break = 32;
3221
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003222/*
3223 * move_tasks tries to move up to load_move weighted load from busiest to
3224 * this_rq, as part of a balancing operation within domain "sd".
3225 * Returns 1 if successful and 0 otherwise.
3226 *
3227 * Called with both runqueues locked.
3228 */
3229static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003230{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003231 struct list_head *tasks = &env->src_rq->cfs_tasks;
3232 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003233 unsigned long load;
3234 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003235
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003236 if (env->load_move <= 0)
3237 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003238
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003239 while (!list_empty(tasks)) {
3240 p = list_first_entry(tasks, struct task_struct, se.group_node);
3241
Peter Zijlstra367456c2012-02-20 21:49:09 +01003242 env->loop++;
3243 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003244 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003245 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003246
3247 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003248 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003249 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003250 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003251 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003252 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003253
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003254 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003255 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003256
Peter Zijlstra367456c2012-02-20 21:49:09 +01003257 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003258
Peter Zijlstraeb953082012-04-17 13:38:40 +02003259 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003260 goto next;
3261
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003262 if ((load / 2) > env->load_move)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003263 goto next;
3264
3265 if (!can_migrate_task(p, env))
3266 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003267
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003268 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003269 pulled++;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003270 env->load_move -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003271
3272#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003273 /*
3274 * NEWIDLE balancing is a source of latency, so preemptible
3275 * kernels will stop after the first task is pulled to minimize
3276 * the critical section.
3277 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003278 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003279 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003280#endif
3281
Peter Zijlstraee00e662009-12-17 17:25:20 +01003282 /*
3283 * We only want to steal up to the prescribed amount of
3284 * weighted load.
3285 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003286 if (env->load_move <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003287 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003288
Peter Zijlstra367456c2012-02-20 21:49:09 +01003289 continue;
3290next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003291 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003292 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003293
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003294 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003295 * Right now, this is one of only two places move_task() is called,
3296 * so we can safely collect move_task() stats here rather than
3297 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003298 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003299 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003300
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003301 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003302}
3303
Peter Zijlstra230059de2009-12-17 17:47:12 +01003304#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003305/*
3306 * update tg->load_weight by folding this cpu's load_avg
3307 */
Paul Turner67e86252010-11-15 15:47:05 -08003308static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003309{
3310 struct cfs_rq *cfs_rq;
3311 unsigned long flags;
3312 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003313
3314 if (!tg->se[cpu])
3315 return 0;
3316
3317 rq = cpu_rq(cpu);
3318 cfs_rq = tg->cfs_rq[cpu];
3319
3320 raw_spin_lock_irqsave(&rq->lock, flags);
3321
3322 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003323 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003324
3325 /*
3326 * We need to update shares after updating tg->load_weight in
3327 * order to adjust the weight of groups with long running tasks.
3328 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003329 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003330
3331 raw_spin_unlock_irqrestore(&rq->lock, flags);
3332
3333 return 0;
3334}
3335
3336static void update_shares(int cpu)
3337{
3338 struct cfs_rq *cfs_rq;
3339 struct rq *rq = cpu_rq(cpu);
3340
3341 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003342 /*
3343 * Iterates the task_group tree in a bottom up fashion, see
3344 * list_add_leaf_cfs_rq() for details.
3345 */
Paul Turner64660c82011-07-21 09:43:36 -07003346 for_each_leaf_cfs_rq(rq, cfs_rq) {
3347 /* throttled entities do not contribute to load */
3348 if (throttled_hierarchy(cfs_rq))
3349 continue;
3350
Paul Turner67e86252010-11-15 15:47:05 -08003351 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003352 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003353 rcu_read_unlock();
3354}
3355
Peter Zijlstra9763b672011-07-13 13:09:25 +02003356/*
3357 * Compute the cpu's hierarchical load factor for each task group.
3358 * This needs to be done in a top-down fashion because the load of a child
3359 * group is a fraction of its parents load.
3360 */
3361static int tg_load_down(struct task_group *tg, void *data)
3362{
3363 unsigned long load;
3364 long cpu = (long)data;
3365
3366 if (!tg->parent) {
3367 load = cpu_rq(cpu)->load.weight;
3368 } else {
3369 load = tg->parent->cfs_rq[cpu]->h_load;
3370 load *= tg->se[cpu]->load.weight;
3371 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3372 }
3373
3374 tg->cfs_rq[cpu]->h_load = load;
3375
3376 return 0;
3377}
3378
3379static void update_h_load(long cpu)
3380{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003381 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003382 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003383 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003384}
3385
Peter Zijlstra367456c2012-02-20 21:49:09 +01003386static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003387{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003388 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3389 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003390
Peter Zijlstra367456c2012-02-20 21:49:09 +01003391 load = p->se.load.weight;
3392 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003393
Peter Zijlstra367456c2012-02-20 21:49:09 +01003394 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003395}
3396#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003397static inline void update_shares(int cpu)
3398{
3399}
3400
Peter Zijlstra367456c2012-02-20 21:49:09 +01003401static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003402{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003403}
3404
3405static unsigned long task_h_load(struct task_struct *p)
3406{
3407 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003408}
3409#endif
3410
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003411/********** Helpers for find_busiest_group ************************/
3412/*
3413 * sd_lb_stats - Structure to store the statistics of a sched_domain
3414 * during load balancing.
3415 */
3416struct sd_lb_stats {
3417 struct sched_group *busiest; /* Busiest group in this sd */
3418 struct sched_group *this; /* Local group in this sd */
3419 unsigned long total_load; /* Total load of all groups in sd */
3420 unsigned long total_pwr; /* Total power of all groups in sd */
3421 unsigned long avg_load; /* Average load across all groups in sd */
3422
3423 /** Statistics of this group */
3424 unsigned long this_load;
3425 unsigned long this_load_per_task;
3426 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003427 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003428 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003429
3430 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003431 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003432 unsigned long max_load;
3433 unsigned long busiest_load_per_task;
3434 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003435 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003436 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003437 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003438
3439 int group_imb; /* Is there imbalance in this sd */
3440#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3441 int power_savings_balance; /* Is powersave balance needed for this sd */
3442 struct sched_group *group_min; /* Least loaded group in sd */
3443 struct sched_group *group_leader; /* Group which relieves group_min */
3444 unsigned long min_load_per_task; /* load_per_task in group_min */
3445 unsigned long leader_nr_running; /* Nr running of group_leader */
3446 unsigned long min_nr_running; /* Nr running of group_min */
3447#endif
3448};
3449
3450/*
3451 * sg_lb_stats - stats of a sched_group required for load_balancing
3452 */
3453struct sg_lb_stats {
3454 unsigned long avg_load; /*Avg load across the CPUs of the group */
3455 unsigned long group_load; /* Total load over the CPUs of the group */
3456 unsigned long sum_nr_running; /* Nr tasks running in the group */
3457 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3458 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003459 unsigned long idle_cpus;
3460 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003461 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003462 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003463};
3464
3465/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003466 * get_sd_load_idx - Obtain the load index for a given sched domain.
3467 * @sd: The sched_domain whose load_idx is to be obtained.
3468 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3469 */
3470static inline int get_sd_load_idx(struct sched_domain *sd,
3471 enum cpu_idle_type idle)
3472{
3473 int load_idx;
3474
3475 switch (idle) {
3476 case CPU_NOT_IDLE:
3477 load_idx = sd->busy_idx;
3478 break;
3479
3480 case CPU_NEWLY_IDLE:
3481 load_idx = sd->newidle_idx;
3482 break;
3483 default:
3484 load_idx = sd->idle_idx;
3485 break;
3486 }
3487
3488 return load_idx;
3489}
3490
3491
3492#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3493/**
3494 * init_sd_power_savings_stats - Initialize power savings statistics for
3495 * the given sched_domain, during load balancing.
3496 *
3497 * @sd: Sched domain whose power-savings statistics are to be initialized.
3498 * @sds: Variable containing the statistics for sd.
3499 * @idle: Idle status of the CPU at which we're performing load-balancing.
3500 */
3501static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3502 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3503{
3504 /*
3505 * Busy processors will not participate in power savings
3506 * balance.
3507 */
3508 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3509 sds->power_savings_balance = 0;
3510 else {
3511 sds->power_savings_balance = 1;
3512 sds->min_nr_running = ULONG_MAX;
3513 sds->leader_nr_running = 0;
3514 }
3515}
3516
3517/**
3518 * update_sd_power_savings_stats - Update the power saving stats for a
3519 * sched_domain while performing load balancing.
3520 *
3521 * @group: sched_group belonging to the sched_domain under consideration.
3522 * @sds: Variable containing the statistics of the sched_domain
3523 * @local_group: Does group contain the CPU for which we're performing
3524 * load balancing ?
3525 * @sgs: Variable containing the statistics of the group.
3526 */
3527static inline void update_sd_power_savings_stats(struct sched_group *group,
3528 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3529{
3530
3531 if (!sds->power_savings_balance)
3532 return;
3533
3534 /*
3535 * If the local group is idle or completely loaded
3536 * no need to do power savings balance at this domain
3537 */
3538 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3539 !sds->this_nr_running))
3540 sds->power_savings_balance = 0;
3541
3542 /*
3543 * If a group is already running at full capacity or idle,
3544 * don't include that group in power savings calculations
3545 */
3546 if (!sds->power_savings_balance ||
3547 sgs->sum_nr_running >= sgs->group_capacity ||
3548 !sgs->sum_nr_running)
3549 return;
3550
3551 /*
3552 * Calculate the group which has the least non-idle load.
3553 * This is the group from where we need to pick up the load
3554 * for saving power
3555 */
3556 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3557 (sgs->sum_nr_running == sds->min_nr_running &&
3558 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3559 sds->group_min = group;
3560 sds->min_nr_running = sgs->sum_nr_running;
3561 sds->min_load_per_task = sgs->sum_weighted_load /
3562 sgs->sum_nr_running;
3563 }
3564
3565 /*
3566 * Calculate the group which is almost near its
3567 * capacity but still has some space to pick up some load
3568 * from other group and save more power
3569 */
3570 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3571 return;
3572
3573 if (sgs->sum_nr_running > sds->leader_nr_running ||
3574 (sgs->sum_nr_running == sds->leader_nr_running &&
3575 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3576 sds->group_leader = group;
3577 sds->leader_nr_running = sgs->sum_nr_running;
3578 }
3579}
3580
3581/**
3582 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3583 * @sds: Variable containing the statistics of the sched_domain
3584 * under consideration.
3585 * @this_cpu: Cpu at which we're currently performing load-balancing.
3586 * @imbalance: Variable to store the imbalance.
3587 *
3588 * Description:
3589 * Check if we have potential to perform some power-savings balance.
3590 * If yes, set the busiest group to be the least loaded group in the
3591 * sched_domain, so that it's CPUs can be put to idle.
3592 *
3593 * Returns 1 if there is potential to perform power-savings balance.
3594 * Else returns 0.
3595 */
3596static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3597 int this_cpu, unsigned long *imbalance)
3598{
3599 if (!sds->power_savings_balance)
3600 return 0;
3601
3602 if (sds->this != sds->group_leader ||
3603 sds->group_leader == sds->group_min)
3604 return 0;
3605
3606 *imbalance = sds->min_load_per_task;
3607 sds->busiest = sds->group_min;
3608
3609 return 1;
3610
3611}
3612#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3613static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3614 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3615{
3616 return;
3617}
3618
3619static inline void update_sd_power_savings_stats(struct sched_group *group,
3620 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3621{
3622 return;
3623}
3624
3625static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3626 int this_cpu, unsigned long *imbalance)
3627{
3628 return 0;
3629}
3630#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3631
3632
3633unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3634{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003635 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003636}
3637
3638unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3639{
3640 return default_scale_freq_power(sd, cpu);
3641}
3642
3643unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3644{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003645 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003646 unsigned long smt_gain = sd->smt_gain;
3647
3648 smt_gain /= weight;
3649
3650 return smt_gain;
3651}
3652
3653unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3654{
3655 return default_scale_smt_power(sd, cpu);
3656}
3657
3658unsigned long scale_rt_power(int cpu)
3659{
3660 struct rq *rq = cpu_rq(cpu);
3661 u64 total, available;
3662
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003663 total = sched_avg_period() + (rq->clock - rq->age_stamp);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003664
3665 if (unlikely(total < rq->rt_avg)) {
3666 /* Ensures that power won't end up being negative */
3667 available = 0;
3668 } else {
3669 available = total - rq->rt_avg;
3670 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003671
Nikhil Rao1399fa72011-05-18 10:09:39 -07003672 if (unlikely((s64)total < SCHED_POWER_SCALE))
3673 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003674
Nikhil Rao1399fa72011-05-18 10:09:39 -07003675 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003676
3677 return div_u64(available, total);
3678}
3679
3680static void update_cpu_power(struct sched_domain *sd, int cpu)
3681{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003682 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003683 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003684 struct sched_group *sdg = sd->groups;
3685
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003686 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3687 if (sched_feat(ARCH_POWER))
3688 power *= arch_scale_smt_power(sd, cpu);
3689 else
3690 power *= default_scale_smt_power(sd, cpu);
3691
Nikhil Rao1399fa72011-05-18 10:09:39 -07003692 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003693 }
3694
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003695 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003696
3697 if (sched_feat(ARCH_POWER))
3698 power *= arch_scale_freq_power(sd, cpu);
3699 else
3700 power *= default_scale_freq_power(sd, cpu);
3701
Nikhil Rao1399fa72011-05-18 10:09:39 -07003702 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003703
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003704 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003705 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003706
3707 if (!power)
3708 power = 1;
3709
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003710 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003711 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003712}
3713
Peter Zijlstra029632f2011-10-25 10:00:11 +02003714void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003715{
3716 struct sched_domain *child = sd->child;
3717 struct sched_group *group, *sdg = sd->groups;
3718 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003719 unsigned long interval;
3720
3721 interval = msecs_to_jiffies(sd->balance_interval);
3722 interval = clamp(interval, 1UL, max_load_balance_interval);
3723 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003724
3725 if (!child) {
3726 update_cpu_power(sd, cpu);
3727 return;
3728 }
3729
3730 power = 0;
3731
3732 group = child->groups;
3733 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003734 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003735 group = group->next;
3736 } while (group != child->groups);
3737
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003738 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003739}
3740
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003741/*
3742 * Try and fix up capacity for tiny siblings, this is needed when
3743 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3744 * which on its own isn't powerful enough.
3745 *
3746 * See update_sd_pick_busiest() and check_asym_packing().
3747 */
3748static inline int
3749fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3750{
3751 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003752 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003753 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003754 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003755 return 0;
3756
3757 /*
3758 * If ~90% of the cpu_power is still there, we're good.
3759 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003760 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003761 return 1;
3762
3763 return 0;
3764}
3765
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003766/**
3767 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3768 * @sd: The sched_domain whose statistics are to be updated.
3769 * @group: sched_group whose statistics are to be updated.
3770 * @this_cpu: Cpu for which load balance is currently performed.
3771 * @idle: Idle status of this_cpu
3772 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003773 * @local_group: Does group contain this_cpu.
3774 * @cpus: Set of cpus considered for load balancing.
3775 * @balance: Should we balance.
3776 * @sgs: variable to hold the statistics for this group.
3777 */
3778static inline void update_sg_lb_stats(struct sched_domain *sd,
3779 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003780 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003781 int local_group, const struct cpumask *cpus,
3782 int *balance, struct sg_lb_stats *sgs)
3783{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003784 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003785 int i;
3786 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003787 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003788
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003789 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003790 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003791
3792 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003793 max_cpu_load = 0;
3794 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003795 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003796
3797 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3798 struct rq *rq = cpu_rq(i);
3799
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003800 /* Bias balancing toward cpus of our domain */
3801 if (local_group) {
3802 if (idle_cpu(i) && !first_idle_cpu) {
3803 first_idle_cpu = 1;
3804 balance_cpu = i;
3805 }
3806
3807 load = target_load(i, load_idx);
3808 } else {
3809 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003810 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003811 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003812 max_nr_running = rq->nr_running;
3813 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003814 if (min_cpu_load > load)
3815 min_cpu_load = load;
3816 }
3817
3818 sgs->group_load += load;
3819 sgs->sum_nr_running += rq->nr_running;
3820 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003821 if (idle_cpu(i))
3822 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003823 }
3824
3825 /*
3826 * First idle cpu or the first cpu(busiest) in this sched group
3827 * is eligible for doing load balancing at this and above
3828 * domains. In the newly idle case, we will allow all the cpu's
3829 * to do the newly idle load balance.
3830 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003831 if (local_group) {
3832 if (idle != CPU_NEWLY_IDLE) {
3833 if (balance_cpu != this_cpu) {
3834 *balance = 0;
3835 return;
3836 }
3837 update_group_power(sd, this_cpu);
3838 } else if (time_after_eq(jiffies, group->sgp->next_update))
3839 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003840 }
3841
3842 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003843 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003844
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003845 /*
3846 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003847 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003848 *
3849 * APZ: with cgroup the avg task weight can vary wildly and
3850 * might not be a suitable number - should we keep a
3851 * normalized nr_running number somewhere that negates
3852 * the hierarchy?
3853 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003854 if (sgs->sum_nr_running)
3855 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003856
Peter Zijlstra866ab432011-02-21 18:56:47 +01003857 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003858 sgs->group_imb = 1;
3859
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003860 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003861 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003862 if (!sgs->group_capacity)
3863 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003864 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003865
3866 if (sgs->group_capacity > sgs->sum_nr_running)
3867 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003868}
3869
3870/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003871 * update_sd_pick_busiest - return 1 on busiest group
3872 * @sd: sched_domain whose statistics are to be checked
3873 * @sds: sched_domain statistics
3874 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003875 * @sgs: sched_group statistics
3876 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10003877 *
3878 * Determine if @sg is a busier group than the previously selected
3879 * busiest group.
3880 */
3881static bool update_sd_pick_busiest(struct sched_domain *sd,
3882 struct sd_lb_stats *sds,
3883 struct sched_group *sg,
3884 struct sg_lb_stats *sgs,
3885 int this_cpu)
3886{
3887 if (sgs->avg_load <= sds->max_load)
3888 return false;
3889
3890 if (sgs->sum_nr_running > sgs->group_capacity)
3891 return true;
3892
3893 if (sgs->group_imb)
3894 return true;
3895
3896 /*
3897 * ASYM_PACKING needs to move all the work to the lowest
3898 * numbered CPUs in the group, therefore mark all groups
3899 * higher than ourself as busy.
3900 */
3901 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3902 this_cpu < group_first_cpu(sg)) {
3903 if (!sds->busiest)
3904 return true;
3905
3906 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3907 return true;
3908 }
3909
3910 return false;
3911}
3912
3913/**
Hui Kang461819a2011-10-11 23:00:59 -04003914 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003915 * @sd: sched_domain whose statistics are to be updated.
3916 * @this_cpu: Cpu for which load balance is currently performed.
3917 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003918 * @cpus: Set of cpus considered for load balancing.
3919 * @balance: Should we balance.
3920 * @sds: variable to hold the statistics for this sched_domain.
3921 */
3922static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003923 enum cpu_idle_type idle, const struct cpumask *cpus,
3924 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003925{
3926 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003927 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003928 struct sg_lb_stats sgs;
3929 int load_idx, prefer_sibling = 0;
3930
3931 if (child && child->flags & SD_PREFER_SIBLING)
3932 prefer_sibling = 1;
3933
3934 init_sd_power_savings_stats(sd, sds, idle);
3935 load_idx = get_sd_load_idx(sd, idle);
3936
3937 do {
3938 int local_group;
3939
Michael Neuling532cb4c2010-06-08 14:57:02 +10003940 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003941 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003942 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943 local_group, cpus, balance, &sgs);
3944
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003945 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003946 return;
3947
3948 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003949 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003950
3951 /*
3952 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10003953 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07003954 * and move all the excess tasks away. We lower the capacity
3955 * of a group only if the local group has the capacity to fit
3956 * these excess tasks, i.e. nr_running < group_capacity. The
3957 * extra check prevents the case where you always pull from the
3958 * heaviest group when it is already under-utilized (possible
3959 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003960 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07003961 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003962 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3963
3964 if (local_group) {
3965 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003966 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003967 sds->this_nr_running = sgs.sum_nr_running;
3968 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003969 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003970 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003971 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003972 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003973 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003974 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003975 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003976 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003977 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003978 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003979 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003980 sds->group_imb = sgs.group_imb;
3981 }
3982
Michael Neuling532cb4c2010-06-08 14:57:02 +10003983 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
3984 sg = sg->next;
3985 } while (sg != sd->groups);
3986}
3987
Michael Neuling532cb4c2010-06-08 14:57:02 +10003988/**
3989 * check_asym_packing - Check to see if the group is packed into the
3990 * sched doman.
3991 *
3992 * This is primarily intended to used at the sibling level. Some
3993 * cores like POWER7 prefer to use lower numbered SMT threads. In the
3994 * case of POWER7, it can move to lower SMT modes only when higher
3995 * threads are idle. When in lower SMT modes, the threads will
3996 * perform better since they share less core resources. Hence when we
3997 * have idle threads, we want them to be the higher ones.
3998 *
3999 * This packing function is run on idle threads. It checks to see if
4000 * the busiest CPU in this domain (core in the P7 case) has a higher
4001 * CPU number than the packing function is being run on. Here we are
4002 * assuming lower CPU number will be equivalent to lower a SMT thread
4003 * number.
4004 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004005 * Returns 1 when packing is required and a task should be moved to
4006 * this CPU. The amount of the imbalance is returned in *imbalance.
4007 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10004008 * @sd: The sched_domain whose packing is to be checked.
4009 * @sds: Statistics of the sched_domain which is to be packed
4010 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4011 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004012 */
4013static int check_asym_packing(struct sched_domain *sd,
4014 struct sd_lb_stats *sds,
4015 int this_cpu, unsigned long *imbalance)
4016{
4017 int busiest_cpu;
4018
4019 if (!(sd->flags & SD_ASYM_PACKING))
4020 return 0;
4021
4022 if (!sds->busiest)
4023 return 0;
4024
4025 busiest_cpu = group_first_cpu(sds->busiest);
4026 if (this_cpu > busiest_cpu)
4027 return 0;
4028
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004029 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004030 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004031 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004032}
4033
4034/**
4035 * fix_small_imbalance - Calculate the minor imbalance that exists
4036 * amongst the groups of a sched_domain, during
4037 * load balancing.
4038 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4039 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4040 * @imbalance: Variable to store the imbalance.
4041 */
4042static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4043 int this_cpu, unsigned long *imbalance)
4044{
4045 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4046 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004047 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004048
4049 if (sds->this_nr_running) {
4050 sds->this_load_per_task /= sds->this_nr_running;
4051 if (sds->busiest_load_per_task >
4052 sds->this_load_per_task)
4053 imbn = 1;
4054 } else
4055 sds->this_load_per_task =
4056 cpu_avg_load_per_task(this_cpu);
4057
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004058 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004059 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004060 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004061
4062 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4063 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004064 *imbalance = sds->busiest_load_per_task;
4065 return;
4066 }
4067
4068 /*
4069 * OK, we don't have enough imbalance to justify moving tasks,
4070 * however we may be able to increase total CPU power used by
4071 * moving them.
4072 */
4073
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004074 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004075 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004076 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004077 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004078 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004079
4080 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004081 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004082 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004083 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004084 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004085 min(sds->busiest_load_per_task, sds->max_load - tmp);
4086
4087 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004088 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004089 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004090 tmp = (sds->max_load * sds->busiest->sgp->power) /
4091 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004092 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004093 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004094 sds->this->sgp->power;
4095 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004096 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004097 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098
4099 /* Move if we gain throughput */
4100 if (pwr_move > pwr_now)
4101 *imbalance = sds->busiest_load_per_task;
4102}
4103
4104/**
4105 * calculate_imbalance - Calculate the amount of imbalance present within the
4106 * groups of a given sched_domain during load balance.
4107 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4108 * @this_cpu: Cpu for which currently load balance is being performed.
4109 * @imbalance: The variable to store the imbalance.
4110 */
4111static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4112 unsigned long *imbalance)
4113{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004114 unsigned long max_pull, load_above_capacity = ~0UL;
4115
4116 sds->busiest_load_per_task /= sds->busiest_nr_running;
4117 if (sds->group_imb) {
4118 sds->busiest_load_per_task =
4119 min(sds->busiest_load_per_task, sds->avg_load);
4120 }
4121
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004122 /*
4123 * In the presence of smp nice balancing, certain scenarios can have
4124 * max load less than avg load(as we skip the groups at or below
4125 * its cpu_power, while calculating max_load..)
4126 */
4127 if (sds->max_load < sds->avg_load) {
4128 *imbalance = 0;
4129 return fix_small_imbalance(sds, this_cpu, imbalance);
4130 }
4131
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004132 if (!sds->group_imb) {
4133 /*
4134 * Don't want to pull so many tasks that a group would go idle.
4135 */
4136 load_above_capacity = (sds->busiest_nr_running -
4137 sds->busiest_group_capacity);
4138
Nikhil Rao1399fa72011-05-18 10:09:39 -07004139 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004140
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004141 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004142 }
4143
4144 /*
4145 * We're trying to get all the cpus to the average_load, so we don't
4146 * want to push ourselves above the average load, nor do we wish to
4147 * reduce the max loaded cpu below the average load. At the same time,
4148 * we also don't want to reduce the group load below the group capacity
4149 * (so that we can implement power-savings policies etc). Thus we look
4150 * for the minimum possible imbalance.
4151 * Be careful of negative numbers as they'll appear as very large values
4152 * with unsigned longs.
4153 */
4154 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004155
4156 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004157 *imbalance = min(max_pull * sds->busiest->sgp->power,
4158 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004159 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004160
4161 /*
4162 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004163 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004164 * a think about bumping its value to force at least one task to be
4165 * moved
4166 */
4167 if (*imbalance < sds->busiest_load_per_task)
4168 return fix_small_imbalance(sds, this_cpu, imbalance);
4169
4170}
Nikhil Raofab47622010-10-15 13:12:29 -07004171
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004172/******* find_busiest_group() helpers end here *********************/
4173
4174/**
4175 * find_busiest_group - Returns the busiest group within the sched_domain
4176 * if there is an imbalance. If there isn't an imbalance, and
4177 * the user has opted for power-savings, it returns a group whose
4178 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4179 * such a group exists.
4180 *
4181 * Also calculates the amount of weighted load which should be moved
4182 * to restore balance.
4183 *
4184 * @sd: The sched_domain whose busiest group is to be returned.
4185 * @this_cpu: The cpu for which load balancing is currently being performed.
4186 * @imbalance: Variable which stores amount of weighted load which should
4187 * be moved to restore balance/put a group to idle.
4188 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004189 * @cpus: The set of CPUs under consideration for load-balancing.
4190 * @balance: Pointer to a variable indicating if this_cpu
4191 * is the appropriate cpu to perform load balancing at this_level.
4192 *
4193 * Returns: - the busiest group if imbalance exists.
4194 * - If no imbalance and user has opted for power-savings balance,
4195 * return the least loaded group whose CPUs can be
4196 * put to idle by rebalancing its tasks onto our group.
4197 */
4198static struct sched_group *
4199find_busiest_group(struct sched_domain *sd, int this_cpu,
4200 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004201 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004202{
4203 struct sd_lb_stats sds;
4204
4205 memset(&sds, 0, sizeof(sds));
4206
4207 /*
4208 * Compute the various statistics relavent for load balancing at
4209 * this level.
4210 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004211 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004212
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004213 /*
4214 * this_cpu is not the appropriate cpu to perform load balancing at
4215 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004216 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004217 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004218 goto ret;
4219
Michael Neuling532cb4c2010-06-08 14:57:02 +10004220 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
4221 check_asym_packing(sd, &sds, this_cpu, imbalance))
4222 return sds.busiest;
4223
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004224 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004225 if (!sds.busiest || sds.busiest_nr_running == 0)
4226 goto out_balanced;
4227
Nikhil Rao1399fa72011-05-18 10:09:39 -07004228 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004229
Peter Zijlstra866ab432011-02-21 18:56:47 +01004230 /*
4231 * If the busiest group is imbalanced the below checks don't
4232 * work because they assumes all things are equal, which typically
4233 * isn't true due to cpus_allowed constraints and the like.
4234 */
4235 if (sds.group_imb)
4236 goto force_balance;
4237
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004238 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07004239 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4240 !sds.busiest_has_capacity)
4241 goto force_balance;
4242
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004243 /*
4244 * If the local group is more busy than the selected busiest group
4245 * don't try and pull any tasks.
4246 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004247 if (sds.this_load >= sds.max_load)
4248 goto out_balanced;
4249
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004250 /*
4251 * Don't pull any tasks if this group is already above the domain
4252 * average load.
4253 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004254 if (sds.this_load >= sds.avg_load)
4255 goto out_balanced;
4256
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004257 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004258 /*
4259 * This cpu is idle. If the busiest group load doesn't
4260 * have more tasks than the number of available cpu's and
4261 * there is no imbalance between this and busiest group
4262 * wrt to idle cpu's, it is balanced.
4263 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004264 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004265 sds.busiest_nr_running <= sds.busiest_group_weight)
4266 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004267 } else {
4268 /*
4269 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4270 * imbalance_pct to be conservative.
4271 */
4272 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4273 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004274 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004275
Nikhil Raofab47622010-10-15 13:12:29 -07004276force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004277 /* Looks like there is an imbalance. Compute it */
4278 calculate_imbalance(&sds, this_cpu, imbalance);
4279 return sds.busiest;
4280
4281out_balanced:
4282 /*
4283 * There is no obvious imbalance. But check if we can do some balancing
4284 * to save power.
4285 */
4286 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4287 return sds.busiest;
4288ret:
4289 *imbalance = 0;
4290 return NULL;
4291}
4292
4293/*
4294 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4295 */
4296static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004297find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4298 enum cpu_idle_type idle, unsigned long imbalance,
4299 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004300{
4301 struct rq *busiest = NULL, *rq;
4302 unsigned long max_load = 0;
4303 int i;
4304
4305 for_each_cpu(i, sched_group_cpus(group)) {
4306 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004307 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4308 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004309 unsigned long wl;
4310
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004311 if (!capacity)
4312 capacity = fix_small_capacity(sd, group);
4313
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004314 if (!cpumask_test_cpu(i, cpus))
4315 continue;
4316
4317 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004318 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004319
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004320 /*
4321 * When comparing with imbalance, use weighted_cpuload()
4322 * which is not scaled with the cpu power.
4323 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004324 if (capacity && rq->nr_running == 1 && wl > imbalance)
4325 continue;
4326
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004327 /*
4328 * For the load comparisons with the other cpu's, consider
4329 * the weighted_cpuload() scaled with the cpu power, so that
4330 * the load can be moved away from the cpu that is potentially
4331 * running at a lower capacity.
4332 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004333 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004334
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004335 if (wl > max_load) {
4336 max_load = wl;
4337 busiest = rq;
4338 }
4339 }
4340
4341 return busiest;
4342}
4343
4344/*
4345 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4346 * so long as it is large enough.
4347 */
4348#define MAX_PINNED_INTERVAL 512
4349
4350/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004351DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004352
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004353static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004354 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004355{
4356 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004357
4358 /*
4359 * ASYM_PACKING needs to force migrate tasks from busy but
4360 * higher numbered CPUs in order to pack all tasks in the
4361 * lowest numbered CPUs.
4362 */
4363 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
4364 return 1;
4365
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004366 /*
4367 * The only task running in a non-idle cpu can be moved to this
4368 * cpu in an attempt to completely freeup the other CPU
4369 * package.
4370 *
4371 * The package power saving logic comes from
4372 * find_busiest_group(). If there are no imbalance, then
4373 * f_b_g() will return NULL. However when sched_mc={1,2} then
4374 * f_b_g() will select a group from which a running task may be
4375 * pulled to this cpu in order to make the other package idle.
4376 * If there is no opportunity to make a package idle and if
4377 * there are no imbalance, then f_b_g() will return NULL and no
4378 * action will be taken in load_balance_newidle().
4379 *
4380 * Under normal task pull operation due to imbalance, there
4381 * will be more than one task in the source run queue and
4382 * move_tasks() will succeed. ld_moved will be true and this
4383 * active balance code will not be triggered.
4384 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004385 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4386 return 0;
4387 }
4388
4389 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4390}
4391
Tejun Heo969c7922010-05-06 18:49:21 +02004392static int active_load_balance_cpu_stop(void *data);
4393
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004394/*
4395 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4396 * tasks if there is an imbalance.
4397 */
4398static int load_balance(int this_cpu, struct rq *this_rq,
4399 struct sched_domain *sd, enum cpu_idle_type idle,
4400 int *balance)
4401{
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004402 int ld_moved, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004403 struct sched_group *group;
4404 unsigned long imbalance;
4405 struct rq *busiest;
4406 unsigned long flags;
4407 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4408
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004409 struct lb_env env = {
4410 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004411 .dst_cpu = this_cpu,
4412 .dst_rq = this_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004413 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004414 .loop_break = sched_nr_migrate_break,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004415 };
4416
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004417 cpumask_copy(cpus, cpu_active_mask);
4418
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004419 schedstat_inc(sd, lb_count[idle]);
4420
4421redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004422 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004423 cpus, balance);
4424
4425 if (*balance == 0)
4426 goto out_balanced;
4427
4428 if (!group) {
4429 schedstat_inc(sd, lb_nobusyg[idle]);
4430 goto out_balanced;
4431 }
4432
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004433 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004434 if (!busiest) {
4435 schedstat_inc(sd, lb_nobusyq[idle]);
4436 goto out_balanced;
4437 }
4438
4439 BUG_ON(busiest == this_rq);
4440
4441 schedstat_add(sd, lb_imbalance[idle], imbalance);
4442
4443 ld_moved = 0;
4444 if (busiest->nr_running > 1) {
4445 /*
4446 * Attempt to move tasks. If find_busiest_group has found
4447 * an imbalance but busiest->nr_running <= 1, the group is
4448 * still unbalanced. ld_moved simply stays zero, so it is
4449 * correctly treated as an imbalance.
4450 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004451 env.flags |= LBF_ALL_PINNED;
Peter Zijlstraeb953082012-04-17 13:38:40 +02004452 env.load_move = imbalance;
4453 env.src_cpu = busiest->cpu;
4454 env.src_rq = busiest;
4455 env.loop_max = min_t(unsigned long, sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004456
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004457more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004458 local_irq_save(flags);
4459 double_rq_lock(this_rq, busiest);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004460 if (!env.loop)
4461 update_h_load(env.src_cpu);
4462 ld_moved += move_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004463 double_rq_unlock(this_rq, busiest);
4464 local_irq_restore(flags);
4465
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004466 if (env.flags & LBF_NEED_BREAK) {
4467 env.flags &= ~LBF_NEED_BREAK;
4468 goto more_balance;
4469 }
4470
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004471 /*
4472 * some other cpu did the load balance for us.
4473 */
4474 if (ld_moved && this_cpu != smp_processor_id())
4475 resched_cpu(this_cpu);
4476
4477 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004478 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004479 cpumask_clear_cpu(cpu_of(busiest), cpus);
4480 if (!cpumask_empty(cpus))
4481 goto redo;
4482 goto out_balanced;
4483 }
4484 }
4485
4486 if (!ld_moved) {
4487 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004488 /*
4489 * Increment the failure counter only on periodic balance.
4490 * We do not want newidle balance, which can be very
4491 * frequent, pollute the failure counter causing
4492 * excessive cache_hot migrations and active balances.
4493 */
4494 if (idle != CPU_NEWLY_IDLE)
4495 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004496
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004497 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004498 raw_spin_lock_irqsave(&busiest->lock, flags);
4499
Tejun Heo969c7922010-05-06 18:49:21 +02004500 /* don't kick the active_load_balance_cpu_stop,
4501 * if the curr task on busiest cpu can't be
4502 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004503 */
4504 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004505 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004506 raw_spin_unlock_irqrestore(&busiest->lock,
4507 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004508 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004509 goto out_one_pinned;
4510 }
4511
Tejun Heo969c7922010-05-06 18:49:21 +02004512 /*
4513 * ->active_balance synchronizes accesses to
4514 * ->active_balance_work. Once set, it's cleared
4515 * only after active load balance is finished.
4516 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004517 if (!busiest->active_balance) {
4518 busiest->active_balance = 1;
4519 busiest->push_cpu = this_cpu;
4520 active_balance = 1;
4521 }
4522 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004523
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004524 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02004525 stop_one_cpu_nowait(cpu_of(busiest),
4526 active_load_balance_cpu_stop, busiest,
4527 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004528
4529 /*
4530 * We've kicked active balancing, reset the failure
4531 * counter.
4532 */
4533 sd->nr_balance_failed = sd->cache_nice_tries+1;
4534 }
4535 } else
4536 sd->nr_balance_failed = 0;
4537
4538 if (likely(!active_balance)) {
4539 /* We were unbalanced, so reset the balancing interval */
4540 sd->balance_interval = sd->min_interval;
4541 } else {
4542 /*
4543 * If we've begun active balancing, start to back off. This
4544 * case may not be covered by the all_pinned logic if there
4545 * is only 1 task on the busy runqueue (because we don't call
4546 * move_tasks).
4547 */
4548 if (sd->balance_interval < sd->max_interval)
4549 sd->balance_interval *= 2;
4550 }
4551
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004552 goto out;
4553
4554out_balanced:
4555 schedstat_inc(sd, lb_balanced[idle]);
4556
4557 sd->nr_balance_failed = 0;
4558
4559out_one_pinned:
4560 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004561 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004562 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004563 (sd->balance_interval < sd->max_interval))
4564 sd->balance_interval *= 2;
4565
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004566 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004567out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004568 return ld_moved;
4569}
4570
4571/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004572 * idle_balance is called by schedule() if this_cpu is about to become
4573 * idle. Attempts to pull tasks from other CPUs.
4574 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004575void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004576{
4577 struct sched_domain *sd;
4578 int pulled_task = 0;
4579 unsigned long next_balance = jiffies + HZ;
4580
4581 this_rq->idle_stamp = this_rq->clock;
4582
4583 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4584 return;
4585
Peter Zijlstraf492e122009-12-23 15:29:42 +01004586 /*
4587 * Drop the rq->lock, but keep IRQ/preempt disabled.
4588 */
4589 raw_spin_unlock(&this_rq->lock);
4590
Paul Turnerc66eaf62010-11-15 15:47:07 -08004591 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004592 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004593 for_each_domain(this_cpu, sd) {
4594 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004595 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004596
4597 if (!(sd->flags & SD_LOAD_BALANCE))
4598 continue;
4599
Peter Zijlstraf492e122009-12-23 15:29:42 +01004600 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004601 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004602 pulled_task = load_balance(this_cpu, this_rq,
4603 sd, CPU_NEWLY_IDLE, &balance);
4604 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004605
4606 interval = msecs_to_jiffies(sd->balance_interval);
4607 if (time_after(next_balance, sd->last_balance + interval))
4608 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004609 if (pulled_task) {
4610 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004611 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004612 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004613 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004614 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004615
4616 raw_spin_lock(&this_rq->lock);
4617
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004618 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4619 /*
4620 * We are going idle. next_balance may be set based on
4621 * a busy processor. So reset next_balance.
4622 */
4623 this_rq->next_balance = next_balance;
4624 }
4625}
4626
4627/*
Tejun Heo969c7922010-05-06 18:49:21 +02004628 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4629 * running tasks off the busiest CPU onto idle CPUs. It requires at
4630 * least 1 task to be running on each physical CPU where possible, and
4631 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004632 */
Tejun Heo969c7922010-05-06 18:49:21 +02004633static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004634{
Tejun Heo969c7922010-05-06 18:49:21 +02004635 struct rq *busiest_rq = data;
4636 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004637 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004638 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004639 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004640
4641 raw_spin_lock_irq(&busiest_rq->lock);
4642
4643 /* make sure the requested cpu hasn't gone down in the meantime */
4644 if (unlikely(busiest_cpu != smp_processor_id() ||
4645 !busiest_rq->active_balance))
4646 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647
4648 /* Is there any task to move? */
4649 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004650 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004651
4652 /*
4653 * This condition is "impossible", if it occurs
4654 * we need to fix it. Originally reported by
4655 * Bjorn Helgaas on a 128-cpu setup.
4656 */
4657 BUG_ON(busiest_rq == target_rq);
4658
4659 /* move a task from busiest_rq to target_rq */
4660 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004661
4662 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004663 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004664 for_each_domain(target_cpu, sd) {
4665 if ((sd->flags & SD_LOAD_BALANCE) &&
4666 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4667 break;
4668 }
4669
4670 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004671 struct lb_env env = {
4672 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004673 .dst_cpu = target_cpu,
4674 .dst_rq = target_rq,
4675 .src_cpu = busiest_rq->cpu,
4676 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004677 .idle = CPU_IDLE,
4678 };
4679
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004680 schedstat_inc(sd, alb_count);
4681
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004682 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004683 schedstat_inc(sd, alb_pushed);
4684 else
4685 schedstat_inc(sd, alb_failed);
4686 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004687 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004688 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004689out_unlock:
4690 busiest_rq->active_balance = 0;
4691 raw_spin_unlock_irq(&busiest_rq->lock);
4692 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004693}
4694
4695#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004696/*
4697 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004698 * - When one of the busy CPUs notice that there may be an idle rebalancing
4699 * needed, they will kick the idle load balancer, which then does idle
4700 * load balancing for all the idle CPUs.
4701 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004702static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004703 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004704 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004705 unsigned long next_balance; /* in jiffy units */
4706} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004707
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004708#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4709/**
4710 * lowest_flag_domain - Return lowest sched_domain containing flag.
4711 * @cpu: The cpu whose lowest level of sched domain is to
4712 * be returned.
4713 * @flag: The flag to check for the lowest sched_domain
4714 * for the given cpu.
4715 *
4716 * Returns the lowest sched_domain of a cpu which contains the given flag.
4717 */
4718static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4719{
4720 struct sched_domain *sd;
4721
4722 for_each_domain(cpu, sd)
Hillf Danton08354712011-06-16 21:55:19 -04004723 if (sd->flags & flag)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004724 break;
4725
4726 return sd;
4727}
4728
4729/**
4730 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4731 * @cpu: The cpu whose domains we're iterating over.
4732 * @sd: variable holding the value of the power_savings_sd
4733 * for cpu.
4734 * @flag: The flag to filter the sched_domains to be iterated.
4735 *
4736 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4737 * set, starting from the lowest sched_domain to the highest.
4738 */
4739#define for_each_flag_domain(cpu, sd, flag) \
4740 for (sd = lowest_flag_domain(cpu, flag); \
4741 (sd && (sd->flags & flag)); sd = sd->parent)
4742
4743/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004744 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4745 * @cpu: The cpu which is nominating a new idle_load_balancer.
4746 *
4747 * Returns: Returns the id of the idle load balancer if it exists,
4748 * Else, returns >= nr_cpu_ids.
4749 *
4750 * This algorithm picks the idle load balancer such that it belongs to a
4751 * semi-idle powersavings sched_domain. The idea is to try and avoid
4752 * completely idle packages/cores just for the purpose of idle load balancing
4753 * when there are other idle cpu's which are better suited for that job.
4754 */
4755static int find_new_ilb(int cpu)
4756{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004757 int ilb = cpumask_first(nohz.idle_cpus_mask);
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004758 struct sched_group *ilbg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004759 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004760
4761 /*
4762 * Have idle load balancer selection from semi-idle packages only
4763 * when power-aware load balancing is enabled
4764 */
4765 if (!(sched_smt_power_savings || sched_mc_power_savings))
4766 goto out_done;
4767
4768 /*
4769 * Optimize for the case when we have no idle CPUs or only one
4770 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4771 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004772 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004773 goto out_done;
4774
Peter Zijlstradce840a2011-04-07 14:09:50 +02004775 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004776 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004777 ilbg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004778
4779 do {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004780 if (ilbg->group_weight !=
4781 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4782 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4783 sched_group_cpus(ilbg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02004784 goto unlock;
4785 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004786
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004787 ilbg = ilbg->next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004788
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004789 } while (ilbg != sd->groups);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004790 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004791unlock:
4792 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004793
4794out_done:
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004795 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4796 return ilb;
4797
4798 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004799}
4800#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4801static inline int find_new_ilb(int call_cpu)
4802{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004803 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004804}
4805#endif
4806
4807/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004808 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4809 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4810 * CPU (if there is one).
4811 */
4812static void nohz_balancer_kick(int cpu)
4813{
4814 int ilb_cpu;
4815
4816 nohz.next_balance++;
4817
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004818 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004819
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004820 if (ilb_cpu >= nr_cpu_ids)
4821 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004822
Suresh Siddhacd490c52011-12-06 11:26:34 -08004823 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004824 return;
4825 /*
4826 * Use smp_send_reschedule() instead of resched_cpu().
4827 * This way we generate a sched IPI on the target cpu which
4828 * is idle. And the softirq performing nohz idle load balance
4829 * will be run before returning from the IPI.
4830 */
4831 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004832 return;
4833}
4834
Suresh Siddha71325962012-01-19 18:28:57 -08004835static inline void clear_nohz_tick_stopped(int cpu)
4836{
4837 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4838 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4839 atomic_dec(&nohz.nr_cpus);
4840 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4841 }
4842}
4843
Suresh Siddha69e1e812011-12-01 17:07:33 -08004844static inline void set_cpu_sd_state_busy(void)
4845{
4846 struct sched_domain *sd;
4847 int cpu = smp_processor_id();
4848
4849 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4850 return;
4851 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4852
4853 rcu_read_lock();
4854 for_each_domain(cpu, sd)
4855 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4856 rcu_read_unlock();
4857}
4858
4859void set_cpu_sd_state_idle(void)
4860{
4861 struct sched_domain *sd;
4862 int cpu = smp_processor_id();
4863
4864 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4865 return;
4866 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4867
4868 rcu_read_lock();
4869 for_each_domain(cpu, sd)
4870 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4871 rcu_read_unlock();
4872}
4873
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004874/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004875 * This routine will record that this cpu is going idle with tick stopped.
4876 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004877 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004878void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004879{
4880 int cpu = smp_processor_id();
4881
Suresh Siddha71325962012-01-19 18:28:57 -08004882 /*
4883 * If this cpu is going down, then nothing needs to be done.
4884 */
4885 if (!cpu_active(cpu))
4886 return;
4887
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004888 if (stop_tick) {
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004889 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004890 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004891
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004892 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004893 atomic_inc(&nohz.nr_cpus);
Suresh Siddha1c792db2011-12-01 17:07:32 -08004894 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004895 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004896 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004897}
Suresh Siddha71325962012-01-19 18:28:57 -08004898
4899static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4900 unsigned long action, void *hcpu)
4901{
4902 switch (action & ~CPU_TASKS_FROZEN) {
4903 case CPU_DYING:
4904 clear_nohz_tick_stopped(smp_processor_id());
4905 return NOTIFY_OK;
4906 default:
4907 return NOTIFY_DONE;
4908 }
4909}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004910#endif
4911
4912static DEFINE_SPINLOCK(balancing);
4913
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004914/*
4915 * Scale the max load_balance interval with the number of CPUs in the system.
4916 * This trades load-balance latency on larger machines for less cross talk.
4917 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004918void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004919{
4920 max_load_balance_interval = HZ*num_online_cpus()/10;
4921}
4922
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004923/*
4924 * It checks each scheduling domain to see if it is due to be balanced,
4925 * and initiates a balancing operation if so.
4926 *
4927 * Balancing parameters are set up in arch_init_sched_domains.
4928 */
4929static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4930{
4931 int balance = 1;
4932 struct rq *rq = cpu_rq(cpu);
4933 unsigned long interval;
4934 struct sched_domain *sd;
4935 /* Earliest time when we have to do rebalance again */
4936 unsigned long next_balance = jiffies + 60*HZ;
4937 int update_next_balance = 0;
4938 int need_serialize;
4939
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004940 update_shares(cpu);
4941
Peter Zijlstradce840a2011-04-07 14:09:50 +02004942 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004943 for_each_domain(cpu, sd) {
4944 if (!(sd->flags & SD_LOAD_BALANCE))
4945 continue;
4946
4947 interval = sd->balance_interval;
4948 if (idle != CPU_IDLE)
4949 interval *= sd->busy_factor;
4950
4951 /* scale ms to jiffies */
4952 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004953 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004954
4955 need_serialize = sd->flags & SD_SERIALIZE;
4956
4957 if (need_serialize) {
4958 if (!spin_trylock(&balancing))
4959 goto out;
4960 }
4961
4962 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4963 if (load_balance(cpu, rq, sd, idle, &balance)) {
4964 /*
4965 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004966 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004967 */
4968 idle = CPU_NOT_IDLE;
4969 }
4970 sd->last_balance = jiffies;
4971 }
4972 if (need_serialize)
4973 spin_unlock(&balancing);
4974out:
4975 if (time_after(next_balance, sd->last_balance + interval)) {
4976 next_balance = sd->last_balance + interval;
4977 update_next_balance = 1;
4978 }
4979
4980 /*
4981 * Stop the load balance at this level. There is another
4982 * CPU in our sched group which is doing load balancing more
4983 * actively.
4984 */
4985 if (!balance)
4986 break;
4987 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004988 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004989
4990 /*
4991 * next_balance will be updated only when there is a need.
4992 * When the cpu is attached to null domain for ex, it will not be
4993 * updated.
4994 */
4995 if (likely(update_next_balance))
4996 rq->next_balance = next_balance;
4997}
4998
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004999#ifdef CONFIG_NO_HZ
5000/*
5001 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5002 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5003 */
5004static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5005{
5006 struct rq *this_rq = cpu_rq(this_cpu);
5007 struct rq *rq;
5008 int balance_cpu;
5009
Suresh Siddha1c792db2011-12-01 17:07:32 -08005010 if (idle != CPU_IDLE ||
5011 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5012 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005013
5014 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005015 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005016 continue;
5017
5018 /*
5019 * If this cpu gets work to do, stop the load balancing
5020 * work being done for other cpus. Next load
5021 * balancing owner will pick it up.
5022 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005023 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005024 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005025
5026 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02005027 update_rq_clock(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005028 update_cpu_load(this_rq);
5029 raw_spin_unlock_irq(&this_rq->lock);
5030
5031 rebalance_domains(balance_cpu, CPU_IDLE);
5032
5033 rq = cpu_rq(balance_cpu);
5034 if (time_after(this_rq->next_balance, rq->next_balance))
5035 this_rq->next_balance = rq->next_balance;
5036 }
5037 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005038end:
5039 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005040}
5041
5042/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005043 * Current heuristic for kicking the idle load balancer in the presence
5044 * of an idle cpu is the system.
5045 * - This rq has more than one task.
5046 * - At any scheduler domain level, this cpu's scheduler group has multiple
5047 * busy cpu's exceeding the group's power.
5048 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5049 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005050 */
5051static inline int nohz_kick_needed(struct rq *rq, int cpu)
5052{
5053 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005054 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005055
Suresh Siddha1c792db2011-12-01 17:07:32 -08005056 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005057 return 0;
5058
Suresh Siddha1c792db2011-12-01 17:07:32 -08005059 /*
5060 * We may be recently in ticked or tickless idle mode. At the first
5061 * busy tick after returning from idle, we will update the busy stats.
5062 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005063 set_cpu_sd_state_busy();
Suresh Siddha71325962012-01-19 18:28:57 -08005064 clear_nohz_tick_stopped(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005065
5066 /*
5067 * None are in tickless mode and hence no need for NOHZ idle load
5068 * balancing.
5069 */
5070 if (likely(!atomic_read(&nohz.nr_cpus)))
5071 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005072
5073 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005074 return 0;
5075
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005076 if (rq->nr_running >= 2)
5077 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005078
Peter Zijlstra067491b2011-12-07 14:32:08 +01005079 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005080 for_each_domain(cpu, sd) {
5081 struct sched_group *sg = sd->groups;
5082 struct sched_group_power *sgp = sg->sgp;
5083 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005085 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005086 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005087
5088 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5089 && (cpumask_first_and(nohz.idle_cpus_mask,
5090 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005091 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005092
5093 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5094 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005095 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005096 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005097 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005098
5099need_kick_unlock:
5100 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005101need_kick:
5102 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005103}
5104#else
5105static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5106#endif
5107
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005108/*
5109 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005110 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005111 */
5112static void run_rebalance_domains(struct softirq_action *h)
5113{
5114 int this_cpu = smp_processor_id();
5115 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005116 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005117 CPU_IDLE : CPU_NOT_IDLE;
5118
5119 rebalance_domains(this_cpu, idle);
5120
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005121 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005122 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005123 * balancing on behalf of the other idle cpus whose ticks are
5124 * stopped.
5125 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005126 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005127}
5128
5129static inline int on_null_domain(int cpu)
5130{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005131 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005132}
5133
5134/*
5135 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005136 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005137void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005138{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005139 /* Don't need to rebalance while attached to NULL domain */
5140 if (time_after_eq(jiffies, rq->next_balance) &&
5141 likely(!on_null_domain(cpu)))
5142 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005143#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005144 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005145 nohz_balancer_kick(cpu);
5146#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005147}
5148
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005149static void rq_online_fair(struct rq *rq)
5150{
5151 update_sysctl();
5152}
5153
5154static void rq_offline_fair(struct rq *rq)
5155{
5156 update_sysctl();
5157}
5158
Dhaval Giani55e12e52008-06-24 23:39:43 +05305159#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005160
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005161/*
5162 * scheduler tick hitting a task of our scheduling class:
5163 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005164static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005165{
5166 struct cfs_rq *cfs_rq;
5167 struct sched_entity *se = &curr->se;
5168
5169 for_each_sched_entity(se) {
5170 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005171 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005172 }
5173}
5174
5175/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005176 * called on fork with the child task as argument from the parent's context
5177 * - child not yet on the tasklist
5178 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005179 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005180static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005181{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005182 struct cfs_rq *cfs_rq;
5183 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005184 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005185 struct rq *rq = this_rq();
5186 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005187
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005188 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005189
Peter Zijlstra861d0342010-08-19 13:31:43 +02005190 update_rq_clock(rq);
5191
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005192 cfs_rq = task_cfs_rq(current);
5193 curr = cfs_rq->curr;
5194
Daisuke Nishimura628753322013-09-10 18:16:36 +09005195 /*
5196 * Not only the cpu but also the task_group of the parent might have
5197 * been changed after parent->se.parent,cfs_rq were copied to
5198 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
5199 * of child point to valid ones.
5200 */
5201 rcu_read_lock();
5202 __set_task_cpu(p, this_cpu);
5203 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005204
Ting Yang7109c442007-08-28 12:53:24 +02005205 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005206
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005207 if (curr)
5208 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005209 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005210
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005211 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005212 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005213 * Upon rescheduling, sched_class::put_prev_task() will place
5214 * 'current' within the tree based on its new key value.
5215 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005216 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305217 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005218 }
5219
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005220 se->vruntime -= cfs_rq->min_vruntime;
5221
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005222 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005223}
5224
Steven Rostedtcb469842008-01-25 21:08:22 +01005225/*
5226 * Priority of the task has changed. Check to see if we preempt
5227 * the current task.
5228 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005229static void
5230prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005231{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005232 if (!p->se.on_rq)
5233 return;
5234
Steven Rostedtcb469842008-01-25 21:08:22 +01005235 /*
5236 * Reschedule if we are currently running on this runqueue and
5237 * our priority decreased, or if we are not currently running on
5238 * this runqueue and our priority is higher than the current's
5239 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005240 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005241 if (p->prio > oldprio)
5242 resched_task(rq->curr);
5243 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005244 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005245}
5246
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005247static void switched_from_fair(struct rq *rq, struct task_struct *p)
5248{
5249 struct sched_entity *se = &p->se;
5250 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5251
5252 /*
5253 * Ensure the task's vruntime is normalized, so that when its
5254 * switched back to the fair class the enqueue_entity(.flags=0) will
5255 * do the right thing.
5256 *
5257 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5258 * have normalized the vruntime, if it was !on_rq, then only when
5259 * the task is sleeping will it still have non-normalized vruntime.
5260 */
5261 if (!se->on_rq && p->state != TASK_RUNNING) {
5262 /*
5263 * Fix up our vruntime so that the current sleep doesn't
5264 * cause 'unlimited' sleep bonus.
5265 */
5266 place_entity(cfs_rq, se, 0);
5267 se->vruntime -= cfs_rq->min_vruntime;
5268 }
5269}
5270
Steven Rostedtcb469842008-01-25 21:08:22 +01005271/*
5272 * We switched to the sched_fair class.
5273 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005274static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005275{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005276 if (!p->se.on_rq)
5277 return;
5278
Steven Rostedtcb469842008-01-25 21:08:22 +01005279 /*
5280 * We were most likely switched from sched_rt, so
5281 * kick off the schedule if running, otherwise just see
5282 * if we can still preempt the current task.
5283 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005284 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005285 resched_task(rq->curr);
5286 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005287 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005288}
5289
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005290/* Account for a task changing its policy or group.
5291 *
5292 * This routine is mostly called to set cfs_rq->curr field when a task
5293 * migrates between groups/classes.
5294 */
5295static void set_curr_task_fair(struct rq *rq)
5296{
5297 struct sched_entity *se = &rq->curr->se;
5298
Paul Turnerec12cb72011-07-21 09:43:30 -07005299 for_each_sched_entity(se) {
5300 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5301
5302 set_next_entity(cfs_rq, se);
5303 /* ensure bandwidth has been allocated on our new cfs_rq */
5304 account_cfs_rq_runtime(cfs_rq, 0);
5305 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005306}
5307
Peter Zijlstra029632f2011-10-25 10:00:11 +02005308void init_cfs_rq(struct cfs_rq *cfs_rq)
5309{
5310 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005311 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5312#ifndef CONFIG_64BIT
5313 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5314#endif
5315}
5316
Peter Zijlstra810b3812008-02-29 15:21:01 -05005317#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005318static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005319{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005320 /*
5321 * If the task was not on the rq at the time of this cgroup movement
5322 * it must have been asleep, sleeping tasks keep their ->vruntime
5323 * absolute on their old rq until wakeup (needed for the fair sleeper
5324 * bonus in place_entity()).
5325 *
5326 * If it was on the rq, we've just 'preempted' it, which does convert
5327 * ->vruntime to a relative base.
5328 *
5329 * Make sure both cases convert their relative position when migrating
5330 * to another cgroup's rq. This does somewhat interfere with the
5331 * fair sleeper stuff for the first placement, but who cares.
5332 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005333 /*
5334 * When !on_rq, vruntime of the task has usually NOT been normalized.
5335 * But there are some cases where it has already been normalized:
5336 *
5337 * - Moving a forked child which is waiting for being woken up by
5338 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005339 * - Moving a task which has been woken up by try_to_wake_up() and
5340 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005341 *
5342 * To prevent boost or penalty in the new cfs_rq caused by delta
5343 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5344 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005345 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005346 on_rq = 1;
5347
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005348 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005349 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5350 set_task_rq(p, task_cpu(p));
5351 if (!on_rq)
5352 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005353}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005354
5355void free_fair_sched_group(struct task_group *tg)
5356{
5357 int i;
5358
5359 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5360
5361 for_each_possible_cpu(i) {
5362 if (tg->cfs_rq)
5363 kfree(tg->cfs_rq[i]);
5364 if (tg->se)
5365 kfree(tg->se[i]);
5366 }
5367
5368 kfree(tg->cfs_rq);
5369 kfree(tg->se);
5370}
5371
5372int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5373{
5374 struct cfs_rq *cfs_rq;
5375 struct sched_entity *se;
5376 int i;
5377
5378 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5379 if (!tg->cfs_rq)
5380 goto err;
5381 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5382 if (!tg->se)
5383 goto err;
5384
5385 tg->shares = NICE_0_LOAD;
5386
5387 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5388
5389 for_each_possible_cpu(i) {
5390 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5391 GFP_KERNEL, cpu_to_node(i));
5392 if (!cfs_rq)
5393 goto err;
5394
5395 se = kzalloc_node(sizeof(struct sched_entity),
5396 GFP_KERNEL, cpu_to_node(i));
5397 if (!se)
5398 goto err_free_rq;
5399
5400 init_cfs_rq(cfs_rq);
5401 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5402 }
5403
5404 return 1;
5405
5406err_free_rq:
5407 kfree(cfs_rq);
5408err:
5409 return 0;
5410}
5411
5412void unregister_fair_sched_group(struct task_group *tg, int cpu)
5413{
5414 struct rq *rq = cpu_rq(cpu);
5415 unsigned long flags;
5416
5417 /*
5418 * Only empty task groups can be destroyed; so we can speculatively
5419 * check on_list without danger of it being re-added.
5420 */
5421 if (!tg->cfs_rq[cpu]->on_list)
5422 return;
5423
5424 raw_spin_lock_irqsave(&rq->lock, flags);
5425 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5426 raw_spin_unlock_irqrestore(&rq->lock, flags);
5427}
5428
5429void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5430 struct sched_entity *se, int cpu,
5431 struct sched_entity *parent)
5432{
5433 struct rq *rq = cpu_rq(cpu);
5434
5435 cfs_rq->tg = tg;
5436 cfs_rq->rq = rq;
5437#ifdef CONFIG_SMP
5438 /* allow initial update_cfs_load() to truncate */
5439 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005440#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005441 init_cfs_rq_runtime(cfs_rq);
5442
5443 tg->cfs_rq[cpu] = cfs_rq;
5444 tg->se[cpu] = se;
5445
5446 /* se could be NULL for root_task_group */
5447 if (!se)
5448 return;
5449
5450 if (!parent)
5451 se->cfs_rq = &rq->cfs;
5452 else
5453 se->cfs_rq = parent->my_q;
5454
5455 se->my_q = cfs_rq;
5456 update_load_set(&se->load, 0);
5457 se->parent = parent;
5458}
5459
5460static DEFINE_MUTEX(shares_mutex);
5461
5462int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5463{
5464 int i;
5465 unsigned long flags;
5466
5467 /*
5468 * We can't change the weight of the root cgroup.
5469 */
5470 if (!tg->se[0])
5471 return -EINVAL;
5472
5473 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5474
5475 mutex_lock(&shares_mutex);
5476 if (tg->shares == shares)
5477 goto done;
5478
5479 tg->shares = shares;
5480 for_each_possible_cpu(i) {
5481 struct rq *rq = cpu_rq(i);
5482 struct sched_entity *se;
5483
5484 se = tg->se[i];
5485 /* Propagate contribution to hierarchy */
5486 raw_spin_lock_irqsave(&rq->lock, flags);
5487 for_each_sched_entity(se)
5488 update_cfs_shares(group_cfs_rq(se));
5489 raw_spin_unlock_irqrestore(&rq->lock, flags);
5490 }
5491
5492done:
5493 mutex_unlock(&shares_mutex);
5494 return 0;
5495}
5496#else /* CONFIG_FAIR_GROUP_SCHED */
5497
5498void free_fair_sched_group(struct task_group *tg) { }
5499
5500int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5501{
5502 return 1;
5503}
5504
5505void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5506
5507#endif /* CONFIG_FAIR_GROUP_SCHED */
5508
Peter Zijlstra810b3812008-02-29 15:21:01 -05005509
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005510static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005511{
5512 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005513 unsigned int rr_interval = 0;
5514
5515 /*
5516 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5517 * idle runqueue:
5518 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005519 if (rq->cfs.load.weight)
Zhu Yanhai1836cd12013-01-08 12:56:52 +08005520 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005521
5522 return rr_interval;
5523}
5524
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005525/*
5526 * All the scheduling class methods:
5527 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005528const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005529 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005530 .enqueue_task = enqueue_task_fair,
5531 .dequeue_task = dequeue_task_fair,
5532 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005533 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005534
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005535 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005536
5537 .pick_next_task = pick_next_task_fair,
5538 .put_prev_task = put_prev_task_fair,
5539
Peter Williams681f3e62007-10-24 18:23:51 +02005540#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005541 .select_task_rq = select_task_rq_fair,
5542
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005543 .rq_online = rq_online_fair,
5544 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005545
5546 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005547#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005548
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005549 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005550 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005551 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005552
5553 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005554 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005555 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005556
Peter Williams0d721ce2009-09-21 01:31:53 +00005557 .get_rr_interval = get_rr_interval_fair,
5558
Peter Zijlstra810b3812008-02-29 15:21:01 -05005559#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005560 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005561#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005562};
5563
5564#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005565void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005566{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005567 struct cfs_rq *cfs_rq;
5568
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005569 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005570 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005571 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005572 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005573}
5574#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005575
5576__init void init_sched_fair_class(void)
5577{
5578#ifdef CONFIG_SMP
5579 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5580
5581#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005582 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005583 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005584 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005585#endif
5586#endif /* SMP */
5587
5588}