blob: b17507303060ef06452722a5c889ec7e2bceb372 [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/*
Steve Muckled448aba2012-10-24 15:00:20 -070080 * Controls whether, when SD_SHARE_PKG_RESOURCES is on, if all
81 * tasks go to idle CPUs when woken. If this is off, note that the
82 * per-task flag PF_WAKE_ON_IDLE can still cause a task to go to an
83 * idle CPU upon being woken.
84 */
85unsigned int __read_mostly sysctl_sched_wake_to_idle;
86
87/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020088 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020089 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020090 *
91 * This option delays the preemption effects of decoupled workloads
92 * and reduces their over-scheduling. Synchronous workloads will still
93 * have immediate wakeup/sleep latencies.
94 */
Mike Galbraith172e0822009-09-09 15:41:37 +020095unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010096unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020097
Ingo Molnarda84d962007-10-15 17:00:18 +020098const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
99
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800100/*
101 * The exponential sliding window over which load is averaged for shares
102 * distribution.
103 * (default: 10msec)
104 */
105unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
106
Paul Turnerec12cb72011-07-21 09:43:30 -0700107#ifdef CONFIG_CFS_BANDWIDTH
108/*
109 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
110 * each time a cfs_rq requests quota.
111 *
112 * Note: in the case that the slice exceeds the runtime remaining (either due
113 * to consumption or the quota being specified to be smaller than the slice)
114 * we will always only issue the remaining available time.
115 *
116 * default: 5 msec, units: microseconds
117 */
118unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
119#endif
120
Peter Zijlstra029632f2011-10-25 10:00:11 +0200121/*
122 * Increase the granularity value when there are more CPUs,
123 * because with more CPUs the 'effective latency' as visible
124 * to users decreases. But the relationship is not linear,
125 * so pick a second-best guess by going with the log2 of the
126 * number of CPUs.
127 *
128 * This idea comes from the SD scheduler of Con Kolivas:
129 */
130static int get_update_sysctl_factor(void)
131{
132 unsigned int cpus = min_t(int, num_online_cpus(), 8);
133 unsigned int factor;
134
135 switch (sysctl_sched_tunable_scaling) {
136 case SCHED_TUNABLESCALING_NONE:
137 factor = 1;
138 break;
139 case SCHED_TUNABLESCALING_LINEAR:
140 factor = cpus;
141 break;
142 case SCHED_TUNABLESCALING_LOG:
143 default:
144 factor = 1 + ilog2(cpus);
145 break;
146 }
147
148 return factor;
149}
150
151static void update_sysctl(void)
152{
153 unsigned int factor = get_update_sysctl_factor();
154
155#define SET_SYSCTL(name) \
156 (sysctl_##name = (factor) * normalized_sysctl_##name)
157 SET_SYSCTL(sched_min_granularity);
158 SET_SYSCTL(sched_latency);
159 SET_SYSCTL(sched_wakeup_granularity);
160#undef SET_SYSCTL
161}
162
163void sched_init_granularity(void)
164{
165 update_sysctl();
166}
167
168#if BITS_PER_LONG == 32
169# define WMULT_CONST (~0UL)
170#else
171# define WMULT_CONST (1UL << 32)
172#endif
173
174#define WMULT_SHIFT 32
175
176/*
177 * Shift right and round:
178 */
179#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
180
181/*
182 * delta *= weight / lw
183 */
184static unsigned long
185calc_delta_mine(unsigned long delta_exec, unsigned long weight,
186 struct load_weight *lw)
187{
188 u64 tmp;
189
190 /*
191 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
192 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
193 * 2^SCHED_LOAD_RESOLUTION.
194 */
195 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
196 tmp = (u64)delta_exec * scale_load_down(weight);
197 else
198 tmp = (u64)delta_exec;
199
200 if (!lw->inv_weight) {
201 unsigned long w = scale_load_down(lw->weight);
202
203 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
204 lw->inv_weight = 1;
205 else if (unlikely(!w))
206 lw->inv_weight = WMULT_CONST;
207 else
208 lw->inv_weight = WMULT_CONST / w;
209 }
210
211 /*
212 * Check whether we'd overflow the 64-bit multiplication:
213 */
214 if (unlikely(tmp > WMULT_CONST))
215 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
216 WMULT_SHIFT/2);
217 else
218 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
219
220 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
221}
222
223
224const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200225
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200226/**************************************************************
227 * CFS operations on generic schedulable entities:
228 */
229
230#ifdef CONFIG_FAIR_GROUP_SCHED
231
232/* cpu runqueue to which this cfs_rq is attached */
233static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
234{
235 return cfs_rq->rq;
236}
237
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200238/* An entity is a task if it doesn't "own" a runqueue */
239#define entity_is_task(se) (!se->my_q)
240
Peter Zijlstra8f488942009-07-24 12:25:30 +0200241static inline struct task_struct *task_of(struct sched_entity *se)
242{
243#ifdef CONFIG_SCHED_DEBUG
244 WARN_ON_ONCE(!entity_is_task(se));
245#endif
246 return container_of(se, struct task_struct, se);
247}
248
Peter Zijlstrab7581492008-04-19 19:45:00 +0200249/* Walk up scheduling entities hierarchy */
250#define for_each_sched_entity(se) \
251 for (; se; se = se->parent)
252
253static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
254{
255 return p->se.cfs_rq;
256}
257
258/* runqueue on which this entity is (to be) queued */
259static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
260{
261 return se->cfs_rq;
262}
263
264/* runqueue "owned" by this group */
265static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
266{
267 return grp->my_q;
268}
269
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800270static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
271{
272 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800273 /*
274 * Ensure we either appear before our parent (if already
275 * enqueued) or force our parent to appear after us when it is
276 * enqueued. The fact that we always enqueue bottom-up
277 * reduces this to two cases.
278 */
279 if (cfs_rq->tg->parent &&
280 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
281 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800283 } else {
284 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
285 &rq_of(cfs_rq)->leaf_cfs_rq_list);
286 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800287
288 cfs_rq->on_list = 1;
289 }
290}
291
292static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
293{
294 if (cfs_rq->on_list) {
295 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
296 cfs_rq->on_list = 0;
297 }
298}
299
Peter Zijlstrab7581492008-04-19 19:45:00 +0200300/* Iterate thr' all leaf cfs_rq's on a runqueue */
301#define for_each_leaf_cfs_rq(rq, cfs_rq) \
302 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
303
304/* Do the two (enqueued) entities belong to the same group ? */
305static inline int
306is_same_group(struct sched_entity *se, struct sched_entity *pse)
307{
308 if (se->cfs_rq == pse->cfs_rq)
309 return 1;
310
311 return 0;
312}
313
314static inline struct sched_entity *parent_entity(struct sched_entity *se)
315{
316 return se->parent;
317}
318
Peter Zijlstra464b7522008-10-24 11:06:15 +0200319/* return depth at which a sched entity is present in the hierarchy */
320static inline int depth_se(struct sched_entity *se)
321{
322 int depth = 0;
323
324 for_each_sched_entity(se)
325 depth++;
326
327 return depth;
328}
329
330static void
331find_matching_se(struct sched_entity **se, struct sched_entity **pse)
332{
333 int se_depth, pse_depth;
334
335 /*
336 * preemption test can be made between sibling entities who are in the
337 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
338 * both tasks until we find their ancestors who are siblings of common
339 * parent.
340 */
341
342 /* First walk up until both entities are at same depth */
343 se_depth = depth_se(*se);
344 pse_depth = depth_se(*pse);
345
346 while (se_depth > pse_depth) {
347 se_depth--;
348 *se = parent_entity(*se);
349 }
350
351 while (pse_depth > se_depth) {
352 pse_depth--;
353 *pse = parent_entity(*pse);
354 }
355
356 while (!is_same_group(*se, *pse)) {
357 *se = parent_entity(*se);
358 *pse = parent_entity(*pse);
359 }
360}
361
Peter Zijlstra8f488942009-07-24 12:25:30 +0200362#else /* !CONFIG_FAIR_GROUP_SCHED */
363
364static inline struct task_struct *task_of(struct sched_entity *se)
365{
366 return container_of(se, struct task_struct, se);
367}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200368
369static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
370{
371 return container_of(cfs_rq, struct rq, cfs);
372}
373
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374#define entity_is_task(se) 1
375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376#define for_each_sched_entity(se) \
377 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200378
Peter Zijlstrab7581492008-04-19 19:45:00 +0200379static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200380{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200382}
383
Peter Zijlstrab7581492008-04-19 19:45:00 +0200384static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
385{
386 struct task_struct *p = task_of(se);
387 struct rq *rq = task_rq(p);
388
389 return &rq->cfs;
390}
391
392/* runqueue "owned" by this group */
393static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
394{
395 return NULL;
396}
397
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800398static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
399{
400}
401
402static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
403{
404}
405
Peter Zijlstrab7581492008-04-19 19:45:00 +0200406#define for_each_leaf_cfs_rq(rq, cfs_rq) \
407 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
408
409static inline int
410is_same_group(struct sched_entity *se, struct sched_entity *pse)
411{
412 return 1;
413}
414
415static inline struct sched_entity *parent_entity(struct sched_entity *se)
416{
417 return NULL;
418}
419
Peter Zijlstra464b7522008-10-24 11:06:15 +0200420static inline void
421find_matching_se(struct sched_entity **se, struct sched_entity **pse)
422{
423}
424
Peter Zijlstrab7581492008-04-19 19:45:00 +0200425#endif /* CONFIG_FAIR_GROUP_SCHED */
426
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700427static __always_inline
428void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200429
430/**************************************************************
431 * Scheduling class tree data structure manipulation methods:
432 */
433
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200434static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200436 s64 delta = (s64)(vruntime - min_vruntime);
437 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200438 min_vruntime = vruntime;
439
440 return min_vruntime;
441}
442
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200443static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200444{
445 s64 delta = (s64)(vruntime - min_vruntime);
446 if (delta < 0)
447 min_vruntime = vruntime;
448
449 return min_vruntime;
450}
451
Fabio Checconi54fdc582009-07-16 12:32:27 +0200452static inline int entity_before(struct sched_entity *a,
453 struct sched_entity *b)
454{
455 return (s64)(a->vruntime - b->vruntime) < 0;
456}
457
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200458static void update_min_vruntime(struct cfs_rq *cfs_rq)
459{
460 u64 vruntime = cfs_rq->min_vruntime;
461
462 if (cfs_rq->curr)
463 vruntime = cfs_rq->curr->vruntime;
464
465 if (cfs_rq->rb_leftmost) {
466 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
467 struct sched_entity,
468 run_node);
469
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100470 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200471 vruntime = se->vruntime;
472 else
473 vruntime = min_vruntime(vruntime, se->vruntime);
474 }
475
476 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200477#ifndef CONFIG_64BIT
478 smp_wmb();
479 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
480#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200481}
482
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200483/*
484 * Enqueue an entity into the rb-tree:
485 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200486static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200487{
488 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
489 struct rb_node *parent = NULL;
490 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200491 int leftmost = 1;
492
493 /*
494 * Find the right place in the rbtree:
495 */
496 while (*link) {
497 parent = *link;
498 entry = rb_entry(parent, struct sched_entity, run_node);
499 /*
500 * We dont care about collisions. Nodes with
501 * the same key stay together.
502 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200503 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200504 link = &parent->rb_left;
505 } else {
506 link = &parent->rb_right;
507 leftmost = 0;
508 }
509 }
510
511 /*
512 * Maintain a cache of leftmost tree entries (it is frequently
513 * used):
514 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200515 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200516 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517
518 rb_link_node(&se->run_node, parent, link);
519 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520}
521
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200522static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200523{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100524 if (cfs_rq->rb_leftmost == &se->run_node) {
525 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526
527 next_node = rb_next(&se->run_node);
528 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100529 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200530
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532}
533
Peter Zijlstra029632f2011-10-25 10:00:11 +0200534struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200535{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100536 struct rb_node *left = cfs_rq->rb_leftmost;
537
538 if (!left)
539 return NULL;
540
541 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542}
543
Rik van Rielac53db52011-02-01 09:51:03 -0500544static struct sched_entity *__pick_next_entity(struct sched_entity *se)
545{
546 struct rb_node *next = rb_next(&se->run_node);
547
548 if (!next)
549 return NULL;
550
551 return rb_entry(next, struct sched_entity, run_node);
552}
553
554#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200555struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200556{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100557 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200558
Balbir Singh70eee742008-02-22 13:25:53 +0530559 if (!last)
560 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100561
562 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200563}
564
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200565/**************************************************************
566 * Scheduling class statistics methods:
567 */
568
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100569int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100571 loff_t *ppos)
572{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700573 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100574 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100575
576 if (ret || !write)
577 return ret;
578
579 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
580 sysctl_sched_min_granularity);
581
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100582#define WRT_SYSCTL(name) \
583 (normalized_sysctl_##name = sysctl_##name / (factor))
584 WRT_SYSCTL(sched_min_granularity);
585 WRT_SYSCTL(sched_latency);
586 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100587#undef WRT_SYSCTL
588
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100589 return 0;
590}
591#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200592
593/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200594 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200595 */
596static inline unsigned long
597calc_delta_fair(unsigned long delta, struct sched_entity *se)
598{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200599 if (unlikely(se->load.weight != NICE_0_LOAD))
600 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200601
602 return delta;
603}
604
605/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * The idea is to set a period in which each task runs once.
607 *
608 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
609 * this period because otherwise the slices get too small.
610 *
611 * p = (nr <= nl) ? l : l*nr/nl
612 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200613static u64 __sched_period(unsigned long nr_running)
614{
615 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100616 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617
618 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100619 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200620 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200621 }
622
623 return period;
624}
625
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200626/*
627 * We calculate the wall-time slice from the period by taking a part
628 * proportional to the weight.
629 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200630 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200631 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200632static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200633{
Mike Galbraith0a582442009-01-02 12:16:42 +0100634 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200635
Mike Galbraith0a582442009-01-02 12:16:42 +0100636 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100637 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200638 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100639
640 cfs_rq = cfs_rq_of(se);
641 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200642
Mike Galbraith0a582442009-01-02 12:16:42 +0100643 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200644 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100645
646 update_load_add(&lw, se->load.weight);
647 load = &lw;
648 }
649 slice = calc_delta_mine(slice, se->load.weight, load);
650 }
651 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652}
653
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200654/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200655 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200656 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200657 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200658 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200659static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200660{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200661 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200662}
663
Paul Turnerd6b55912010-11-15 15:47:09 -0800664static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800665static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800666
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200667/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200668 * Update the current task's runtime statistics. Skip current tasks that
669 * are not in our scheduling class.
670 */
671static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200672__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
673 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200674{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200675 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200676
Lucas De Marchi41acab82010-03-10 23:37:45 -0300677 schedstat_set(curr->statistics.exec_max,
678 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200679
680 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200681 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200682 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100683
Ingo Molnare9acbff2007-10-15 17:00:04 +0200684 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200685 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800686
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100687#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800688 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800689#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690}
691
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200692static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693{
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200694 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700695 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200696 unsigned long delta_exec;
697
698 if (unlikely(!curr))
699 return;
700
701 /*
702 * Get the amount of time the current task was running
703 * since the last time we changed load (this cannot
704 * overflow on 32 bits):
705 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200706 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100707 if (!delta_exec)
708 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200709
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200710 __update_curr(cfs_rq, curr, delta_exec);
711 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100712
713 if (entity_is_task(curr)) {
714 struct task_struct *curtask = task_of(curr);
715
Ingo Molnarf977bb42009-09-13 18:15:54 +0200716 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100717 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700718 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100719 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700720
721 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200722}
723
724static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200725update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300727 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200728}
729
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200730/*
731 * Task is being enqueued - update stats:
732 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200733static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200734{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735 /*
736 * Are we enqueueing a waiting task? (for current tasks
737 * a dequeue/enqueue event is a NOP)
738 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200739 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200740 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200741}
742
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200743static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200744update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200745{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300746 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
747 rq_of(cfs_rq)->clock - se->statistics.wait_start));
748 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
749 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
750 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200751#ifdef CONFIG_SCHEDSTATS
752 if (entity_is_task(se)) {
753 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300754 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200755 }
756#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300757 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200758}
759
760static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200761update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200762{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200763 /*
764 * Mark the end of the wait period if dequeueing a
765 * waiting task:
766 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200767 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200768 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200769}
770
771/*
772 * We are picking a new current task - update its stats:
773 */
774static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200775update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200776{
777 /*
778 * We are starting a new run period:
779 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700780 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200781}
782
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200783/**************************************************
784 * Scheduling class queueing methods:
785 */
786
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200787static void
788account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
789{
790 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200791 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200792 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100793#ifdef CONFIG_SMP
794 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200795 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100796#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200797 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200798}
799
800static void
801account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
802{
803 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200804 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200805 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100806 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530807 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200808 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200809}
810
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800811#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700812/* we need this in update_cfs_load and load-balance functions below */
813static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800814# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800815static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
816 int global_update)
817{
818 struct task_group *tg = cfs_rq->tg;
819 long load_avg;
820
821 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
822 load_avg -= cfs_rq->load_contribution;
823
824 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
825 atomic_add(load_avg, &tg->load_weight);
826 cfs_rq->load_contribution += load_avg;
827 }
828}
829
830static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800831{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800832 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800833 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800834 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800835
Paul Turner64660c82011-07-21 09:43:36 -0700836 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800837 return;
838
Paul Turner05ca62c2011-01-21 20:45:02 -0800839 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800840 delta = now - cfs_rq->load_stamp;
841
Paul Turnere33078b2010-11-15 15:47:04 -0800842 /* truncate load history at 4 idle periods */
843 if (cfs_rq->load_stamp > cfs_rq->load_last &&
844 now - cfs_rq->load_last > 4 * period) {
845 cfs_rq->load_period = 0;
846 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800847 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800848 }
849
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800850 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800851 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800852 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800853 if (load) {
854 cfs_rq->load_last = now;
855 cfs_rq->load_avg += delta * load;
856 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800857
Paul Turnerd6b55912010-11-15 15:47:09 -0800858 /* consider updating load contribution on each fold or truncate */
859 if (global_update || cfs_rq->load_period > period
860 || !cfs_rq->load_period)
861 update_cfs_rq_load_contribution(cfs_rq, global_update);
862
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800863 while (cfs_rq->load_period > period) {
864 /*
865 * Inline assembly required to prevent the compiler
866 * optimising this loop into a divmod call.
867 * See __iter_div_u64_rem() for another example of this.
868 */
869 asm("" : "+rm" (cfs_rq->load_period));
870 cfs_rq->load_period /= 2;
871 cfs_rq->load_avg /= 2;
872 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800873
Paul Turnere33078b2010-11-15 15:47:04 -0800874 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
875 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800876}
877
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200878static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
879{
880 long tg_weight;
881
882 /*
883 * Use this CPU's actual weight instead of the last load_contribution
884 * to gain a more accurate current total weight. See
885 * update_cfs_rq_load_contribution().
886 */
887 tg_weight = atomic_read(&tg->load_weight);
888 tg_weight -= cfs_rq->load_contribution;
889 tg_weight += cfs_rq->load.weight;
890
891 return tg_weight;
892}
893
Paul Turner6d5ab292011-01-21 20:45:01 -0800894static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800895{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200896 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200898 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800899 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800900
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800901 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200902 if (tg_weight)
903 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800904
905 if (shares < MIN_SHARES)
906 shares = MIN_SHARES;
907 if (shares > tg->shares)
908 shares = tg->shares;
909
910 return shares;
911}
912
913static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
914{
915 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
916 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800917 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800918 }
919}
920# else /* CONFIG_SMP */
921static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
922{
923}
924
Paul Turner6d5ab292011-01-21 20:45:01 -0800925static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800926{
927 return tg->shares;
928}
929
930static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
931{
932}
933# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800934static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
935 unsigned long weight)
936{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800937 if (se->on_rq) {
938 /* commit outstanding execution time */
939 if (cfs_rq->curr == se)
940 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800941 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800942 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800943
944 update_load_set(&se->load, weight);
945
946 if (se->on_rq)
947 account_entity_enqueue(cfs_rq, se);
948}
949
Paul Turner6d5ab292011-01-21 20:45:01 -0800950static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800951{
952 struct task_group *tg;
953 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800954 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800955
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800956 tg = cfs_rq->tg;
957 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700958 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800959 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800960#ifndef CONFIG_SMP
961 if (likely(se->load.weight == tg->shares))
962 return;
963#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800964 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800965
966 reweight_entity(cfs_rq_of(se), se, shares);
967}
968#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800969static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800970{
971}
972
Paul Turner6d5ab292011-01-21 20:45:01 -0800973static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800974{
975}
Paul Turner43365bd2010-12-15 19:10:17 -0800976
977static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
978{
979}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800980#endif /* CONFIG_FAIR_GROUP_SCHED */
981
Ingo Molnar2396af62007-08-09 11:16:48 +0200982static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200983{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +0200985 struct task_struct *tsk = NULL;
986
987 if (entity_is_task(se))
988 tsk = task_of(se);
989
Lucas De Marchi41acab82010-03-10 23:37:45 -0300990 if (se->statistics.sleep_start) {
991 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200992
993 if ((s64)delta < 0)
994 delta = 0;
995
Lucas De Marchi41acab82010-03-10 23:37:45 -0300996 if (unlikely(delta > se->statistics.sleep_max))
997 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200998
Peter Zijlstra8c79a042012-01-30 14:51:37 +0100999 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001000 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001001
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001002 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001003 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001004 trace_sched_stat_sleep(tsk, delta);
1005 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001006 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001007 if (se->statistics.block_start) {
1008 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001009
1010 if ((s64)delta < 0)
1011 delta = 0;
1012
Lucas De Marchi41acab82010-03-10 23:37:45 -03001013 if (unlikely(delta > se->statistics.block_max))
1014 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001015
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001016 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001017 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001018
Peter Zijlstrae4143142009-07-23 20:13:26 +02001019 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001020 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001021 se->statistics.iowait_sum += delta;
1022 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001023 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001024 }
1025
Andrew Vaginb781a602011-11-28 12:03:35 +03001026 trace_sched_stat_blocked(tsk, delta);
1027
Peter Zijlstrae4143142009-07-23 20:13:26 +02001028 /*
1029 * Blocking time is in units of nanosecs, so shift by
1030 * 20 to get a milliseconds-range estimation of the
1031 * amount of time that the task spent sleeping:
1032 */
1033 if (unlikely(prof_on == SLEEP_PROFILING)) {
1034 profile_hits(SLEEP_PROFILING,
1035 (void *)get_wchan(tsk),
1036 delta >> 20);
1037 }
1038 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001039 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040 }
1041#endif
1042}
1043
Peter Zijlstraddc97292007-10-15 17:00:10 +02001044static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1045{
1046#ifdef CONFIG_SCHED_DEBUG
1047 s64 d = se->vruntime - cfs_rq->min_vruntime;
1048
1049 if (d < 0)
1050 d = -d;
1051
1052 if (d > 3*sysctl_sched_latency)
1053 schedstat_inc(cfs_rq, nr_spread_over);
1054#endif
1055}
1056
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001057static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001058place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1059{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001060 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001061
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001062 /*
1063 * The 'current' period is already promised to the current tasks,
1064 * however the extra weight of the new task will slow them down a
1065 * little, place the new task so that it fits in the slot that
1066 * stays open at the end.
1067 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001068 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001069 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001070
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001071 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001072 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001073 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001074
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001075 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001076 * Halve their sleep time's effect, to allow
1077 * for a gentler effect of sleepers:
1078 */
1079 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1080 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001081
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001082 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001083 }
1084
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001085 /* ensure we never gain time by being placed backwards. */
1086 vruntime = max_vruntime(se->vruntime, vruntime);
1087
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001088 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001089}
1090
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001091static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1092
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001093static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001094enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001095{
1096 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001097 * Update the normalized vruntime before updating min_vruntime
1098 * through callig update_curr().
1099 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001100 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001101 se->vruntime += cfs_rq->min_vruntime;
1102
1103 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001104 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001105 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001106 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001107 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001108 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001109 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001110
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001111 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001112 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001113 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001114 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001115
Ingo Molnard2417e52007-08-09 11:16:47 +02001116 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001117 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001118 if (se != cfs_rq->curr)
1119 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001120 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001121
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001122 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001123 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001124 check_enqueue_throttle(cfs_rq);
1125 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001126}
1127
Rik van Riel2c13c9192011-02-01 09:48:37 -05001128static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001129{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001130 for_each_sched_entity(se) {
1131 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1132 if (cfs_rq->last == se)
1133 cfs_rq->last = NULL;
1134 else
1135 break;
1136 }
1137}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001138
Rik van Riel2c13c9192011-02-01 09:48:37 -05001139static void __clear_buddies_next(struct sched_entity *se)
1140{
1141 for_each_sched_entity(se) {
1142 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1143 if (cfs_rq->next == se)
1144 cfs_rq->next = NULL;
1145 else
1146 break;
1147 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001148}
1149
Rik van Rielac53db52011-02-01 09:51:03 -05001150static void __clear_buddies_skip(struct sched_entity *se)
1151{
1152 for_each_sched_entity(se) {
1153 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1154 if (cfs_rq->skip == se)
1155 cfs_rq->skip = NULL;
1156 else
1157 break;
1158 }
1159}
1160
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001161static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1162{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001163 if (cfs_rq->last == se)
1164 __clear_buddies_last(se);
1165
1166 if (cfs_rq->next == se)
1167 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001168
1169 if (cfs_rq->skip == se)
1170 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001171}
1172
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001173static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001174
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001175static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001176dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001177{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001178 /*
1179 * Update run-time statistics of the 'current'.
1180 */
1181 update_curr(cfs_rq);
1182
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001183 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001184 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001185#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001186 if (entity_is_task(se)) {
1187 struct task_struct *tsk = task_of(se);
1188
1189 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001190 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001191 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001192 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001193 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001194#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001195 }
1196
Peter Zijlstra2002c692008-11-11 11:52:33 +01001197 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001198
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001199 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001200 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001201 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001202 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001203 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001204
1205 /*
1206 * Normalize the entity after updating the min_vruntime because the
1207 * update can refer to the ->curr item and we need to reflect this
1208 * movement in our normalized position.
1209 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001210 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001211 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001212
Paul Turnerd8b49862011-07-21 09:43:41 -07001213 /* return excess runtime on last dequeue */
1214 return_cfs_rq_runtime(cfs_rq);
1215
Peter Zijlstra1e876232011-05-17 16:21:10 -07001216 update_min_vruntime(cfs_rq);
1217 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001218}
1219
1220/*
1221 * Preempt the current task with a newly woken task if needed:
1222 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001223static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001224check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001225{
Peter Zijlstra11697832007-09-05 14:32:49 +02001226 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001227 struct sched_entity *se;
1228 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001229
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001230 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001231 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001232 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001233 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001234 /*
1235 * The current task ran long enough, ensure it doesn't get
1236 * re-elected due to buddy favours.
1237 */
1238 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001239 return;
1240 }
1241
1242 /*
1243 * Ensure that a task that missed wakeup preemption by a
1244 * narrow margin doesn't have to wait for a full slice.
1245 * This also mitigates buddy induced latencies under load.
1246 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001247 if (delta_exec < sysctl_sched_min_granularity)
1248 return;
1249
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001250 se = __pick_first_entity(cfs_rq);
1251 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001252
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001253 if (delta < 0)
1254 return;
Mike Galbraithd7d829442011-01-05 05:41:17 +01001255
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001256 if (delta > ideal_runtime)
1257 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001258}
1259
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001260static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001261set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001262{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001263 /* 'current' is not kept within the tree. */
1264 if (se->on_rq) {
1265 /*
1266 * Any task has to be enqueued before it get to execute on
1267 * a CPU. So account for the time it spent waiting on the
1268 * runqueue.
1269 */
1270 update_stats_wait_end(cfs_rq, se);
1271 __dequeue_entity(cfs_rq, se);
1272 }
1273
Ingo Molnar79303e92007-08-09 11:16:47 +02001274 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001275 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001276#ifdef CONFIG_SCHEDSTATS
1277 /*
1278 * Track our maximum slice length, if the CPU's load is at
1279 * least twice that of our own weight (i.e. dont track it
1280 * when there are only lesser-weight tasks around):
1281 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001282 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001283 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001284 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1285 }
1286#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001287 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001288}
1289
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001290static int
1291wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1292
Rik van Rielac53db52011-02-01 09:51:03 -05001293/*
1294 * Pick the next process, keeping these things in mind, in this order:
1295 * 1) keep things fair between processes/task groups
1296 * 2) pick the "next" process, since someone really wants that to run
1297 * 3) pick the "last" process, for cache locality
1298 * 4) do not run the "skip" process, if something else is available
1299 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001300static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001301{
Rik van Rielac53db52011-02-01 09:51:03 -05001302 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001303 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001304
Rik van Rielac53db52011-02-01 09:51:03 -05001305 /*
1306 * Avoid running the skip buddy, if running something else can
1307 * be done without getting too unfair.
1308 */
1309 if (cfs_rq->skip == se) {
1310 struct sched_entity *second = __pick_next_entity(se);
1311 if (second && wakeup_preempt_entity(second, left) < 1)
1312 se = second;
1313 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001314
Mike Galbraithf685cea2009-10-23 23:09:22 +02001315 /*
1316 * Prefer last buddy, try to return the CPU to a preempted task.
1317 */
1318 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1319 se = cfs_rq->last;
1320
Rik van Rielac53db52011-02-01 09:51:03 -05001321 /*
1322 * Someone really wants this to run. If it's not unfair, run it.
1323 */
1324 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1325 se = cfs_rq->next;
1326
Mike Galbraithf685cea2009-10-23 23:09:22 +02001327 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001328
1329 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001330}
1331
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001332static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1333
Ingo Molnarab6cde22007-08-09 11:16:48 +02001334static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001335{
1336 /*
1337 * If still on the runqueue then deactivate_task()
1338 * was not called and update_curr() has to be done:
1339 */
1340 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001341 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001342
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001343 /* throttle cfs_rqs exceeding runtime */
1344 check_cfs_rq_runtime(cfs_rq);
1345
Peter Zijlstraddc97292007-10-15 17:00:10 +02001346 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001347 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001348 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001349 /* Put 'current' back into the tree. */
1350 __enqueue_entity(cfs_rq, prev);
1351 }
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001352 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001353}
1354
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001355static void
1356entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001357{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001358 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001359 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001360 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001361 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001362
Paul Turner43365bd2010-12-15 19:10:17 -08001363 /*
1364 * Update share accounting for long-running entities.
1365 */
1366 update_entity_shares_tick(cfs_rq);
1367
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001368#ifdef CONFIG_SCHED_HRTICK
1369 /*
1370 * queued ticks are scheduled to match the slice, so don't bother
1371 * validating it and just reschedule.
1372 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001373 if (queued) {
1374 resched_task(rq_of(cfs_rq)->curr);
1375 return;
1376 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001377 /*
1378 * don't let the period tick interfere with the hrtick preemption
1379 */
1380 if (!sched_feat(DOUBLE_TICK) &&
1381 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1382 return;
1383#endif
1384
Yong Zhang2c2efae2011-07-29 16:20:33 +08001385 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001386 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001387}
1388
Paul Turnerab84d312011-07-21 09:43:28 -07001389
1390/**************************************************
1391 * CFS bandwidth control machinery
1392 */
1393
1394#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001395
1396#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001397static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001398
1399static inline bool cfs_bandwidth_used(void)
1400{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001401 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001402}
1403
1404void account_cfs_bandwidth_used(int enabled, int was_enabled)
1405{
1406 /* only need to count groups transitioning between enabled/!enabled */
1407 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001408 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001409 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001410 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001411}
1412#else /* HAVE_JUMP_LABEL */
1413static bool cfs_bandwidth_used(void)
1414{
1415 return true;
1416}
1417
1418void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1419#endif /* HAVE_JUMP_LABEL */
1420
Paul Turnerab84d312011-07-21 09:43:28 -07001421/*
1422 * default period for cfs group bandwidth.
1423 * default: 0.1s, units: nanoseconds
1424 */
1425static inline u64 default_cfs_period(void)
1426{
1427 return 100000000ULL;
1428}
Paul Turnerec12cb72011-07-21 09:43:30 -07001429
1430static inline u64 sched_cfs_bandwidth_slice(void)
1431{
1432 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1433}
1434
Paul Turnera9cf55b2011-07-21 09:43:32 -07001435/*
1436 * Replenish runtime according to assigned quota and update expiration time.
1437 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1438 * additional synchronization around rq->lock.
1439 *
1440 * requires cfs_b->lock
1441 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001442void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001443{
1444 u64 now;
1445
1446 if (cfs_b->quota == RUNTIME_INF)
1447 return;
1448
1449 now = sched_clock_cpu(smp_processor_id());
1450 cfs_b->runtime = cfs_b->quota;
1451 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1452}
1453
Peter Zijlstra029632f2011-10-25 10:00:11 +02001454static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1455{
1456 return &tg->cfs_bandwidth;
1457}
1458
Paul Turner85dac902011-07-21 09:43:33 -07001459/* returns 0 on failure to allocate runtime */
1460static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001461{
1462 struct task_group *tg = cfs_rq->tg;
1463 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001464 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001465
1466 /* note: this is a positive sum as runtime_remaining <= 0 */
1467 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1468
1469 raw_spin_lock(&cfs_b->lock);
1470 if (cfs_b->quota == RUNTIME_INF)
1471 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001472 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001473 /*
1474 * If the bandwidth pool has become inactive, then at least one
1475 * period must have elapsed since the last consumption.
1476 * Refresh the global state and ensure bandwidth timer becomes
1477 * active.
1478 */
1479 if (!cfs_b->timer_active) {
1480 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001481 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001482 }
Paul Turner58088ad2011-07-21 09:43:31 -07001483
1484 if (cfs_b->runtime > 0) {
1485 amount = min(cfs_b->runtime, min_amount);
1486 cfs_b->runtime -= amount;
1487 cfs_b->idle = 0;
1488 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001489 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001490 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001491 raw_spin_unlock(&cfs_b->lock);
1492
1493 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001494 /*
1495 * we may have advanced our local expiration to account for allowed
1496 * spread between our sched_clock and the one on which runtime was
1497 * issued.
1498 */
1499 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1500 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001501
1502 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001503}
1504
1505/*
1506 * Note: This depends on the synchronization provided by sched_clock and the
1507 * fact that rq->clock snapshots this value.
1508 */
1509static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1510{
1511 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1512 struct rq *rq = rq_of(cfs_rq);
1513
1514 /* if the deadline is ahead of our clock, nothing to do */
1515 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1516 return;
1517
1518 if (cfs_rq->runtime_remaining < 0)
1519 return;
1520
1521 /*
1522 * If the local deadline has passed we have to consider the
1523 * possibility that our sched_clock is 'fast' and the global deadline
1524 * has not truly expired.
1525 *
1526 * Fortunately we can check determine whether this the case by checking
1527 * whether the global deadline has advanced.
1528 */
1529
1530 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1531 /* extend local deadline, drift is bounded above by 2 ticks */
1532 cfs_rq->runtime_expires += TICK_NSEC;
1533 } else {
1534 /* global deadline is ahead, expiration has passed */
1535 cfs_rq->runtime_remaining = 0;
1536 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001537}
1538
1539static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1540 unsigned long delta_exec)
1541{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001542 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001543 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001544 expire_cfs_rq_runtime(cfs_rq);
1545
1546 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001547 return;
1548
Paul Turner85dac902011-07-21 09:43:33 -07001549 /*
1550 * if we're unable to extend our runtime we resched so that the active
1551 * hierarchy can be throttled
1552 */
1553 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1554 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001555}
1556
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001557static __always_inline
1558void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001559{
Paul Turner56f570e2011-11-07 20:26:33 -08001560 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001561 return;
1562
1563 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1564}
1565
Paul Turner85dac902011-07-21 09:43:33 -07001566static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1567{
Paul Turner56f570e2011-11-07 20:26:33 -08001568 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001569}
1570
Paul Turner64660c82011-07-21 09:43:36 -07001571/* check whether cfs_rq, or any parent, is throttled */
1572static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1573{
Paul Turner56f570e2011-11-07 20:26:33 -08001574 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001575}
1576
1577/*
1578 * Ensure that neither of the group entities corresponding to src_cpu or
1579 * dest_cpu are members of a throttled hierarchy when performing group
1580 * load-balance operations.
1581 */
1582static inline int throttled_lb_pair(struct task_group *tg,
1583 int src_cpu, int dest_cpu)
1584{
1585 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1586
1587 src_cfs_rq = tg->cfs_rq[src_cpu];
1588 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1589
1590 return throttled_hierarchy(src_cfs_rq) ||
1591 throttled_hierarchy(dest_cfs_rq);
1592}
1593
1594/* updated child weight may affect parent so we have to do this bottom up */
1595static int tg_unthrottle_up(struct task_group *tg, void *data)
1596{
1597 struct rq *rq = data;
1598 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1599
1600 cfs_rq->throttle_count--;
1601#ifdef CONFIG_SMP
1602 if (!cfs_rq->throttle_count) {
1603 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1604
1605 /* leaving throttled state, advance shares averaging windows */
1606 cfs_rq->load_stamp += delta;
1607 cfs_rq->load_last += delta;
1608
1609 /* update entity weight now that we are on_rq again */
1610 update_cfs_shares(cfs_rq);
1611 }
1612#endif
1613
1614 return 0;
1615}
1616
1617static int tg_throttle_down(struct task_group *tg, void *data)
1618{
1619 struct rq *rq = data;
1620 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1621
1622 /* group is entering throttled state, record last load */
1623 if (!cfs_rq->throttle_count)
1624 update_cfs_load(cfs_rq, 0);
1625 cfs_rq->throttle_count++;
1626
1627 return 0;
1628}
1629
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001630static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001631{
1632 struct rq *rq = rq_of(cfs_rq);
1633 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1634 struct sched_entity *se;
1635 long task_delta, dequeue = 1;
1636
1637 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1638
1639 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001640 rcu_read_lock();
1641 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1642 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001643
1644 task_delta = cfs_rq->h_nr_running;
1645 for_each_sched_entity(se) {
1646 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1647 /* throttled entity or throttle-on-deactivate */
1648 if (!se->on_rq)
1649 break;
1650
1651 if (dequeue)
1652 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1653 qcfs_rq->h_nr_running -= task_delta;
1654
1655 if (qcfs_rq->load.weight)
1656 dequeue = 0;
1657 }
1658
1659 if (!se)
1660 rq->nr_running -= task_delta;
1661
1662 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001663 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001664 raw_spin_lock(&cfs_b->lock);
1665 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1666 raw_spin_unlock(&cfs_b->lock);
1667}
1668
Peter Zijlstra029632f2011-10-25 10:00:11 +02001669void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001670{
1671 struct rq *rq = rq_of(cfs_rq);
1672 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1673 struct sched_entity *se;
1674 int enqueue = 1;
1675 long task_delta;
1676
1677 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1678
1679 cfs_rq->throttled = 0;
1680 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001681 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001682 list_del_rcu(&cfs_rq->throttled_list);
1683 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001684 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001685
Paul Turner64660c82011-07-21 09:43:36 -07001686 update_rq_clock(rq);
1687 /* update hierarchical throttle state */
1688 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1689
Paul Turner671fd9d2011-07-21 09:43:34 -07001690 if (!cfs_rq->load.weight)
1691 return;
1692
1693 task_delta = cfs_rq->h_nr_running;
1694 for_each_sched_entity(se) {
1695 if (se->on_rq)
1696 enqueue = 0;
1697
1698 cfs_rq = cfs_rq_of(se);
1699 if (enqueue)
1700 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1701 cfs_rq->h_nr_running += task_delta;
1702
1703 if (cfs_rq_throttled(cfs_rq))
1704 break;
1705 }
1706
1707 if (!se)
1708 rq->nr_running += task_delta;
1709
1710 /* determine whether we need to wake up potentially idle cpu */
1711 if (rq->curr == rq->idle && rq->cfs.nr_running)
1712 resched_task(rq->curr);
1713}
1714
1715static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1716 u64 remaining, u64 expires)
1717{
1718 struct cfs_rq *cfs_rq;
1719 u64 runtime = remaining;
1720
1721 rcu_read_lock();
1722 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1723 throttled_list) {
1724 struct rq *rq = rq_of(cfs_rq);
1725
1726 raw_spin_lock(&rq->lock);
1727 if (!cfs_rq_throttled(cfs_rq))
1728 goto next;
1729
1730 runtime = -cfs_rq->runtime_remaining + 1;
1731 if (runtime > remaining)
1732 runtime = remaining;
1733 remaining -= runtime;
1734
1735 cfs_rq->runtime_remaining += runtime;
1736 cfs_rq->runtime_expires = expires;
1737
1738 /* we check whether we're throttled above */
1739 if (cfs_rq->runtime_remaining > 0)
1740 unthrottle_cfs_rq(cfs_rq);
1741
1742next:
1743 raw_spin_unlock(&rq->lock);
1744
1745 if (!remaining)
1746 break;
1747 }
1748 rcu_read_unlock();
1749
1750 return remaining;
1751}
1752
Paul Turner58088ad2011-07-21 09:43:31 -07001753/*
1754 * Responsible for refilling a task_group's bandwidth and unthrottling its
1755 * cfs_rqs as appropriate. If there has been no activity within the last
1756 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1757 * used to track this state.
1758 */
1759static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1760{
Paul Turner671fd9d2011-07-21 09:43:34 -07001761 u64 runtime, runtime_expires;
1762 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001763
1764 raw_spin_lock(&cfs_b->lock);
1765 /* no need to continue the timer with no bandwidth constraint */
1766 if (cfs_b->quota == RUNTIME_INF)
1767 goto out_unlock;
1768
Paul Turner671fd9d2011-07-21 09:43:34 -07001769 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1770 /* idle depends on !throttled (for the case of a large deficit) */
1771 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001772 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001773
Paul Turnera9cf55b2011-07-21 09:43:32 -07001774 /* if we're going inactive then everything else can be deferred */
1775 if (idle)
1776 goto out_unlock;
1777
1778 __refill_cfs_bandwidth_runtime(cfs_b);
1779
Paul Turner671fd9d2011-07-21 09:43:34 -07001780 if (!throttled) {
1781 /* mark as potentially idle for the upcoming period */
1782 cfs_b->idle = 1;
1783 goto out_unlock;
1784 }
Paul Turner58088ad2011-07-21 09:43:31 -07001785
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001786 /* account preceding periods in which throttling occurred */
1787 cfs_b->nr_throttled += overrun;
1788
Paul Turner671fd9d2011-07-21 09:43:34 -07001789 /*
1790 * There are throttled entities so we must first use the new bandwidth
1791 * to unthrottle them before making it generally available. This
1792 * ensures that all existing debts will be paid before a new cfs_rq is
1793 * allowed to run.
1794 */
1795 runtime = cfs_b->runtime;
1796 runtime_expires = cfs_b->runtime_expires;
1797 cfs_b->runtime = 0;
1798
1799 /*
1800 * This check is repeated as we are holding onto the new bandwidth
1801 * while we unthrottle. This can potentially race with an unthrottled
1802 * group trying to acquire new bandwidth from the global pool.
1803 */
1804 while (throttled && runtime > 0) {
1805 raw_spin_unlock(&cfs_b->lock);
1806 /* we can't nest cfs_b->lock while distributing bandwidth */
1807 runtime = distribute_cfs_runtime(cfs_b, runtime,
1808 runtime_expires);
1809 raw_spin_lock(&cfs_b->lock);
1810
1811 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1812 }
1813
1814 /* return (any) remaining runtime */
1815 cfs_b->runtime = runtime;
1816 /*
1817 * While we are ensured activity in the period following an
1818 * unthrottle, this also covers the case in which the new bandwidth is
1819 * insufficient to cover the existing bandwidth deficit. (Forcing the
1820 * timer to remain active while there are any throttled entities.)
1821 */
1822 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07001823out_unlock:
1824 if (idle)
1825 cfs_b->timer_active = 0;
1826 raw_spin_unlock(&cfs_b->lock);
1827
1828 return idle;
1829}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001830
Paul Turnerd8b49862011-07-21 09:43:41 -07001831/* a cfs_rq won't donate quota below this amount */
1832static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
1833/* minimum remaining period time to redistribute slack quota */
1834static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
1835/* how long we wait to gather additional slack before distributing */
1836static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
1837
1838/* are we near the end of the current quota period? */
1839static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
1840{
1841 struct hrtimer *refresh_timer = &cfs_b->period_timer;
1842 u64 remaining;
1843
1844 /* if the call-back is running a quota refresh is already occurring */
1845 if (hrtimer_callback_running(refresh_timer))
1846 return 1;
1847
1848 /* is a quota refresh about to occur? */
1849 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
1850 if (remaining < min_expire)
1851 return 1;
1852
1853 return 0;
1854}
1855
1856static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
1857{
1858 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
1859
1860 /* if there's a quota refresh soon don't bother with slack */
1861 if (runtime_refresh_within(cfs_b, min_left))
1862 return;
1863
1864 start_bandwidth_timer(&cfs_b->slack_timer,
1865 ns_to_ktime(cfs_bandwidth_slack_period));
1866}
1867
1868/* we know any runtime found here is valid as update_curr() precedes return */
1869static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1870{
1871 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1872 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
1873
1874 if (slack_runtime <= 0)
1875 return;
1876
1877 raw_spin_lock(&cfs_b->lock);
1878 if (cfs_b->quota != RUNTIME_INF &&
1879 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
1880 cfs_b->runtime += slack_runtime;
1881
1882 /* we are under rq->lock, defer unthrottling using a timer */
1883 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
1884 !list_empty(&cfs_b->throttled_cfs_rq))
1885 start_cfs_slack_bandwidth(cfs_b);
1886 }
1887 raw_spin_unlock(&cfs_b->lock);
1888
1889 /* even if it's not valid for return we don't want to try again */
1890 cfs_rq->runtime_remaining -= slack_runtime;
1891}
1892
1893static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1894{
Paul Turner56f570e2011-11-07 20:26:33 -08001895 if (!cfs_bandwidth_used())
1896 return;
1897
Paul Turnerfccfdc62011-11-07 20:26:34 -08001898 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07001899 return;
1900
1901 __return_cfs_rq_runtime(cfs_rq);
1902}
1903
1904/*
1905 * This is done with a timer (instead of inline with bandwidth return) since
1906 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
1907 */
1908static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
1909{
1910 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
1911 u64 expires;
1912
1913 /* confirm we're still not at a refresh boundary */
1914 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
1915 return;
1916
1917 raw_spin_lock(&cfs_b->lock);
1918 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
1919 runtime = cfs_b->runtime;
1920 cfs_b->runtime = 0;
1921 }
1922 expires = cfs_b->runtime_expires;
1923 raw_spin_unlock(&cfs_b->lock);
1924
1925 if (!runtime)
1926 return;
1927
1928 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
1929
1930 raw_spin_lock(&cfs_b->lock);
1931 if (expires == cfs_b->runtime_expires)
1932 cfs_b->runtime = runtime;
1933 raw_spin_unlock(&cfs_b->lock);
1934}
1935
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001936/*
1937 * When a group wakes up we want to make sure that its quota is not already
1938 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
1939 * runtime as update_curr() throttling can not not trigger until it's on-rq.
1940 */
1941static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
1942{
Paul Turner56f570e2011-11-07 20:26:33 -08001943 if (!cfs_bandwidth_used())
1944 return;
1945
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001946 /* an active group must be handled by the update_curr()->put() path */
1947 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
1948 return;
1949
1950 /* ensure the group is not already throttled */
1951 if (cfs_rq_throttled(cfs_rq))
1952 return;
1953
1954 /* update runtime allocation */
1955 account_cfs_rq_runtime(cfs_rq, 0);
1956 if (cfs_rq->runtime_remaining <= 0)
1957 throttle_cfs_rq(cfs_rq);
1958}
1959
1960/* conditionally throttle active cfs_rq's from put_prev_entity() */
1961static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1962{
Paul Turner56f570e2011-11-07 20:26:33 -08001963 if (!cfs_bandwidth_used())
1964 return;
1965
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001966 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
1967 return;
1968
1969 /*
1970 * it's possible for a throttled entity to be forced into a running
1971 * state (e.g. set_curr_task), in this case we're finished.
1972 */
1973 if (cfs_rq_throttled(cfs_rq))
1974 return;
1975
1976 throttle_cfs_rq(cfs_rq);
1977}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001978
1979static inline u64 default_cfs_period(void);
1980static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
1981static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
1982
1983static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
1984{
1985 struct cfs_bandwidth *cfs_b =
1986 container_of(timer, struct cfs_bandwidth, slack_timer);
1987 do_sched_cfs_slack_timer(cfs_b);
1988
1989 return HRTIMER_NORESTART;
1990}
1991
1992static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
1993{
1994 struct cfs_bandwidth *cfs_b =
1995 container_of(timer, struct cfs_bandwidth, period_timer);
1996 ktime_t now;
1997 int overrun;
1998 int idle = 0;
1999
2000 for (;;) {
2001 now = hrtimer_cb_get_time(timer);
2002 overrun = hrtimer_forward(timer, now, cfs_b->period);
2003
2004 if (!overrun)
2005 break;
2006
2007 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2008 }
2009
2010 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2011}
2012
2013void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2014{
2015 raw_spin_lock_init(&cfs_b->lock);
2016 cfs_b->runtime = 0;
2017 cfs_b->quota = RUNTIME_INF;
2018 cfs_b->period = ns_to_ktime(default_cfs_period());
2019
2020 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2021 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2022 cfs_b->period_timer.function = sched_cfs_period_timer;
2023 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2024 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2025}
2026
2027static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2028{
2029 cfs_rq->runtime_enabled = 0;
2030 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2031}
2032
2033/* requires cfs_b->lock, may release to reprogram timer */
2034void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2035{
2036 /*
2037 * The timer may be active because we're trying to set a new bandwidth
2038 * period or because we're racing with the tear-down path
2039 * (timer_active==0 becomes visible before the hrtimer call-back
2040 * terminates). In either case we ensure that it's re-programmed
2041 */
2042 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2043 raw_spin_unlock(&cfs_b->lock);
2044 /* ensure cfs_b->lock is available while we wait */
2045 hrtimer_cancel(&cfs_b->period_timer);
2046
2047 raw_spin_lock(&cfs_b->lock);
2048 /* if someone else restarted the timer then we're done */
2049 if (cfs_b->timer_active)
2050 return;
2051 }
2052
2053 cfs_b->timer_active = 1;
2054 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2055}
2056
2057static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2058{
2059 hrtimer_cancel(&cfs_b->period_timer);
2060 hrtimer_cancel(&cfs_b->slack_timer);
2061}
2062
2063void unthrottle_offline_cfs_rqs(struct rq *rq)
2064{
2065 struct cfs_rq *cfs_rq;
2066
2067 for_each_leaf_cfs_rq(rq, cfs_rq) {
2068 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2069
2070 if (!cfs_rq->runtime_enabled)
2071 continue;
2072
2073 /*
2074 * clock_task is not advancing so we just need to make sure
2075 * there's some valid quota amount
2076 */
2077 cfs_rq->runtime_remaining = cfs_b->quota;
2078 if (cfs_rq_throttled(cfs_rq))
2079 unthrottle_cfs_rq(cfs_rq);
2080 }
2081}
2082
2083#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002084static __always_inline
2085void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002086static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2087static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002088static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002089
2090static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2091{
2092 return 0;
2093}
Paul Turner64660c82011-07-21 09:43:36 -07002094
2095static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2096{
2097 return 0;
2098}
2099
2100static inline int throttled_lb_pair(struct task_group *tg,
2101 int src_cpu, int dest_cpu)
2102{
2103 return 0;
2104}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002105
2106void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2107
2108#ifdef CONFIG_FAIR_GROUP_SCHED
2109static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002110#endif
2111
Peter Zijlstra029632f2011-10-25 10:00:11 +02002112static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2113{
2114 return NULL;
2115}
2116static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2117void unthrottle_offline_cfs_rqs(struct rq *rq) {}
2118
2119#endif /* CONFIG_CFS_BANDWIDTH */
2120
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002121/**************************************************
2122 * CFS operations on tasks:
2123 */
2124
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002125#ifdef CONFIG_SCHED_HRTICK
2126static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2127{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002128 struct sched_entity *se = &p->se;
2129 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2130
2131 WARN_ON(task_rq(p) != rq);
2132
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002133 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002134 u64 slice = sched_slice(cfs_rq, se);
2135 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2136 s64 delta = slice - ran;
2137
2138 if (delta < 0) {
2139 if (rq->curr == p)
2140 resched_task(p);
2141 return;
2142 }
2143
2144 /*
2145 * Don't schedule slices shorter than 10000ns, that just
2146 * doesn't make sense. Rely on vruntime for fairness.
2147 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002148 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002149 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002150
Peter Zijlstra31656512008-07-18 18:01:23 +02002151 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002152 }
2153}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002154
2155/*
2156 * called from enqueue/dequeue and updates the hrtick when the
2157 * current task is from our class and nr_running is low enough
2158 * to matter.
2159 */
2160static void hrtick_update(struct rq *rq)
2161{
2162 struct task_struct *curr = rq->curr;
2163
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002164 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002165 return;
2166
2167 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2168 hrtick_start_fair(rq, curr);
2169}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302170#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002171static inline void
2172hrtick_start_fair(struct rq *rq, struct task_struct *p)
2173{
2174}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002175
2176static inline void hrtick_update(struct rq *rq)
2177{
2178}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002179#endif
2180
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002181/*
2182 * The enqueue_task method is called before nr_running is
2183 * increased. Here we update the fair scheduling stats and
2184 * then put the task into the rbtree:
2185 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002186static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002187enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002188{
2189 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002190 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002191
2192 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002193 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002194 break;
2195 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002196 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002197
2198 /*
2199 * end evaluation on encountering a throttled cfs_rq
2200 *
2201 * note: in the case of encountering a throttled cfs_rq we will
2202 * post the final h_nr_running increment below.
2203 */
2204 if (cfs_rq_throttled(cfs_rq))
2205 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002206 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002207
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002208 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002209 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002210
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002211 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002212 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002213 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002214
Paul Turner85dac902011-07-21 09:43:33 -07002215 if (cfs_rq_throttled(cfs_rq))
2216 break;
2217
Paul Turnerd6b55912010-11-15 15:47:09 -08002218 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002219 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002220 }
2221
Paul Turner85dac902011-07-21 09:43:33 -07002222 if (!se)
2223 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002224 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002225}
2226
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002227static void set_next_buddy(struct sched_entity *se);
2228
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002229/*
2230 * The dequeue_task method is called before nr_running is
2231 * decreased. We remove the task from the rbtree and
2232 * update the fair scheduling stats:
2233 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002234static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002235{
2236 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002237 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002238 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002239
2240 for_each_sched_entity(se) {
2241 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002242 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002243
2244 /*
2245 * end evaluation on encountering a throttled cfs_rq
2246 *
2247 * note: in the case of encountering a throttled cfs_rq we will
2248 * post the final h_nr_running decrement below.
2249 */
2250 if (cfs_rq_throttled(cfs_rq))
2251 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002252 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002253
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002254 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002255 if (cfs_rq->load.weight) {
2256 /*
2257 * Bias pick_next to pick a task from this cfs_rq, as
2258 * p is sleeping when it is within its sched_slice.
2259 */
2260 if (task_sleep && parent_entity(se))
2261 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002262
2263 /* avoid re-evaluating load for this entity */
2264 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002265 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002266 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002267 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002268 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002269
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002270 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002271 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002272 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002273
Paul Turner85dac902011-07-21 09:43:33 -07002274 if (cfs_rq_throttled(cfs_rq))
2275 break;
2276
Paul Turnerd6b55912010-11-15 15:47:09 -08002277 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002278 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002279 }
2280
Paul Turner85dac902011-07-21 09:43:33 -07002281 if (!se)
2282 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002283 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002284}
2285
Gregory Haskinse7693a32008-01-25 21:08:09 +01002286#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002287/* Used instead of source_load when we know the type == 0 */
2288static unsigned long weighted_cpuload(const int cpu)
2289{
2290 return cpu_rq(cpu)->load.weight;
2291}
2292
2293/*
2294 * Return a low guess at the load of a migration-source cpu weighted
2295 * according to the scheduling class and "nice" value.
2296 *
2297 * We want to under-estimate the load of migration sources, to
2298 * balance conservatively.
2299 */
2300static unsigned long source_load(int cpu, int type)
2301{
2302 struct rq *rq = cpu_rq(cpu);
2303 unsigned long total = weighted_cpuload(cpu);
2304
2305 if (type == 0 || !sched_feat(LB_BIAS))
2306 return total;
2307
2308 return min(rq->cpu_load[type-1], total);
2309}
2310
2311/*
2312 * Return a high guess at the load of a migration-target cpu weighted
2313 * according to the scheduling class and "nice" value.
2314 */
2315static unsigned long target_load(int cpu, int type)
2316{
2317 struct rq *rq = cpu_rq(cpu);
2318 unsigned long total = weighted_cpuload(cpu);
2319
2320 if (type == 0 || !sched_feat(LB_BIAS))
2321 return total;
2322
2323 return max(rq->cpu_load[type-1], total);
2324}
2325
2326static unsigned long power_of(int cpu)
2327{
2328 return cpu_rq(cpu)->cpu_power;
2329}
2330
2331static unsigned long cpu_avg_load_per_task(int cpu)
2332{
2333 struct rq *rq = cpu_rq(cpu);
2334 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2335
2336 if (nr_running)
2337 return rq->load.weight / nr_running;
2338
2339 return 0;
2340}
2341
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002342
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002343static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002344{
2345 struct sched_entity *se = &p->se;
2346 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002347 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002348
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002349#ifndef CONFIG_64BIT
2350 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002351
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002352 do {
2353 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2354 smp_rmb();
2355 min_vruntime = cfs_rq->min_vruntime;
2356 } while (min_vruntime != min_vruntime_copy);
2357#else
2358 min_vruntime = cfs_rq->min_vruntime;
2359#endif
2360
2361 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002362}
2363
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002364#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002365/*
2366 * effective_load() calculates the load change as seen from the root_task_group
2367 *
2368 * Adding load to a group doesn't make a group heavier, but can cause movement
2369 * of group shares between cpus. Assuming the shares were perfectly aligned one
2370 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002371 *
2372 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2373 * on this @cpu and results in a total addition (subtraction) of @wg to the
2374 * total group weight.
2375 *
2376 * Given a runqueue weight distribution (rw_i) we can compute a shares
2377 * distribution (s_i) using:
2378 *
2379 * s_i = rw_i / \Sum rw_j (1)
2380 *
2381 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2382 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2383 * shares distribution (s_i):
2384 *
2385 * rw_i = { 2, 4, 1, 0 }
2386 * s_i = { 2/7, 4/7, 1/7, 0 }
2387 *
2388 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2389 * task used to run on and the CPU the waker is running on), we need to
2390 * compute the effect of waking a task on either CPU and, in case of a sync
2391 * wakeup, compute the effect of the current task going to sleep.
2392 *
2393 * So for a change of @wl to the local @cpu with an overall group weight change
2394 * of @wl we can compute the new shares distribution (s'_i) using:
2395 *
2396 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2397 *
2398 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2399 * differences in waking a task to CPU 0. The additional task changes the
2400 * weight and shares distributions like:
2401 *
2402 * rw'_i = { 3, 4, 1, 0 }
2403 * s'_i = { 3/8, 4/8, 1/8, 0 }
2404 *
2405 * We can then compute the difference in effective weight by using:
2406 *
2407 * dw_i = S * (s'_i - s_i) (3)
2408 *
2409 * Where 'S' is the group weight as seen by its parent.
2410 *
2411 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2412 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2413 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002414 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002415static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002416{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002417 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002418
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002419 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002420 return wl;
2421
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002422 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002423 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002424
Paul Turner977dda72011-01-14 17:57:50 -08002425 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002426
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002427 /*
2428 * W = @wg + \Sum rw_j
2429 */
2430 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002431
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002432 /*
2433 * w = rw_i + @wl
2434 */
2435 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002436
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002437 /*
2438 * wl = S * s'_i; see (2)
2439 */
2440 if (W > 0 && w < W)
2441 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002442 else
2443 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002444
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002445 /*
2446 * Per the above, wl is the new se->load.weight value; since
2447 * those are clipped to [MIN_SHARES, ...) do so now. See
2448 * calc_cfs_shares().
2449 */
Paul Turner977dda72011-01-14 17:57:50 -08002450 if (wl < MIN_SHARES)
2451 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002452
2453 /*
2454 * wl = dw_i = S * (s'_i - s_i); see (3)
2455 */
Paul Turner977dda72011-01-14 17:57:50 -08002456 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002457
2458 /*
2459 * Recursively apply this logic to all parent groups to compute
2460 * the final effective load change on the root group. Since
2461 * only the @tg group gets extra weight, all parent groups can
2462 * only redistribute existing shares. @wl is the shift in shares
2463 * resulting from this level per the above.
2464 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002465 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002466 }
2467
2468 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002469}
2470#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002471
Peter Zijlstra83378262008-06-27 13:41:37 +02002472static inline unsigned long effective_load(struct task_group *tg, int cpu,
2473 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002474{
Peter Zijlstra83378262008-06-27 13:41:37 +02002475 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002476}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002477
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002478#endif
2479
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002480static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002481{
Paul Turnere37b6a72011-01-21 20:44:59 -08002482 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002483 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002484 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002485 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002486 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002487 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002488
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002489 idx = sd->wake_idx;
2490 this_cpu = smp_processor_id();
2491 prev_cpu = task_cpu(p);
2492 load = source_load(prev_cpu, idx);
2493 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002494
2495 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002496 * If sync wakeup then subtract the (maximum possible)
2497 * effect of the currently running task from the load
2498 * of the current CPU:
2499 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002500 if (sync) {
2501 tg = task_group(current);
2502 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002503
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002504 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002505 load += effective_load(tg, prev_cpu, 0, -weight);
2506 }
2507
2508 tg = task_group(p);
2509 weight = p->se.load.weight;
2510
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002511 /*
2512 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002513 * due to the sync cause above having dropped this_load to 0, we'll
2514 * always have an imbalance, but there's really nothing you can do
2515 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002516 *
2517 * Otherwise check if either cpus are near enough in load to allow this
2518 * task to be woken on this_cpu.
2519 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002520 if (this_load > 0) {
2521 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002522
2523 this_eff_load = 100;
2524 this_eff_load *= power_of(prev_cpu);
2525 this_eff_load *= this_load +
2526 effective_load(tg, this_cpu, weight, weight);
2527
2528 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2529 prev_eff_load *= power_of(this_cpu);
2530 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2531
2532 balanced = this_eff_load <= prev_eff_load;
2533 } else
2534 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002535
2536 /*
2537 * If the currently running task will sleep within
2538 * a reasonable amount of time then attract this newly
2539 * woken task:
2540 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002541 if (sync && balanced)
2542 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002543
Lucas De Marchi41acab82010-03-10 23:37:45 -03002544 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002545 tl_per_task = cpu_avg_load_per_task(this_cpu);
2546
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002547 if (balanced ||
2548 (this_load <= load &&
2549 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002550 /*
2551 * This domain has SD_WAKE_AFFINE and
2552 * p is cache cold in this domain, and
2553 * there is no bad imbalance.
2554 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002555 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002556 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002557
2558 return 1;
2559 }
2560 return 0;
2561}
2562
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002563/*
2564 * find_idlest_group finds and returns the least busy CPU group within the
2565 * domain.
2566 */
2567static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002568find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002569 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002570{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002571 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002572 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002573 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002574
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002575 do {
2576 unsigned long load, avg_load;
2577 int local_group;
2578 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002579
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002580 /* Skip over this group if it has no CPUs allowed */
2581 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002582 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002583 continue;
2584
2585 local_group = cpumask_test_cpu(this_cpu,
2586 sched_group_cpus(group));
2587
2588 /* Tally up the load of all CPUs in the group */
2589 avg_load = 0;
2590
2591 for_each_cpu(i, sched_group_cpus(group)) {
2592 /* Bias balancing toward cpus of our domain */
2593 if (local_group)
2594 load = source_load(i, load_idx);
2595 else
2596 load = target_load(i, load_idx);
2597
2598 avg_load += load;
2599 }
2600
2601 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002602 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002603
2604 if (local_group) {
2605 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002606 } else if (avg_load < min_load) {
2607 min_load = avg_load;
2608 idlest = group;
2609 }
2610 } while (group = group->next, group != sd->groups);
2611
2612 if (!idlest || 100*this_load < imbalance*min_load)
2613 return NULL;
2614 return idlest;
2615}
2616
2617/*
2618 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2619 */
2620static int
2621find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2622{
2623 unsigned long load, min_load = ULONG_MAX;
2624 int idlest = -1;
2625 int i;
2626
2627 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002628 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002629 load = weighted_cpuload(i);
2630
2631 if (load < min_load || (load == min_load && i == this_cpu)) {
2632 min_load = load;
2633 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002634 }
2635 }
2636
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002637 return idlest;
2638}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002639
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002640/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002641 * Try and locate an idle CPU in the sched_domain.
2642 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002643static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002644{
2645 int cpu = smp_processor_id();
2646 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002647 struct sched_domain *sd;
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002648 struct sched_group *sg;
Suresh Siddha77e81362011-11-17 11:08:23 -08002649 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002650
2651 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002652 * If the task is going to be woken-up on this cpu and if it is
2653 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002654 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002655 if (target == cpu && idle_cpu(cpu))
2656 return cpu;
2657
2658 /*
2659 * If the task is going to be woken-up on the cpu where it previously
2660 * ran and if it is currently idle, then it the right target.
2661 */
2662 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002663 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002664
Steve Muckled448aba2012-10-24 15:00:20 -07002665 if (!sysctl_sched_wake_to_idle &&
2666 !(current->flags & PF_WAKE_UP_IDLE) &&
Steve Muckleff96cd22012-09-10 11:45:02 -07002667 !(p->flags & PF_WAKE_UP_IDLE))
2668 return target;
2669
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002670 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002671 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002672 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002673 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002674 for_each_lower_domain(sd) {
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002675 sg = sd->groups;
2676 do {
2677 if (!cpumask_intersects(sched_group_cpus(sg),
2678 tsk_cpus_allowed(p)))
2679 goto next;
2680
2681 for_each_cpu(i, sched_group_cpus(sg)) {
2682 if (!idle_cpu(i))
2683 goto next;
2684 }
2685
2686 target = cpumask_first_and(sched_group_cpus(sg),
2687 tsk_cpus_allowed(p));
2688 goto done;
2689next:
2690 sg = sg->next;
2691 } while (sg != sd->groups);
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002692 }
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002693done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002694 return target;
2695}
2696
2697/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002698 * sched_balance_self: balance the current task (running on cpu) in domains
2699 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2700 * SD_BALANCE_EXEC.
2701 *
2702 * Balance, ie. select the least loaded group.
2703 *
2704 * Returns the target CPU number, or the same CPU if no balancing is needed.
2705 *
2706 * preempt must be disabled.
2707 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002708static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002709select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002710{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002711 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002712 int cpu = smp_processor_id();
2713 int prev_cpu = task_cpu(p);
2714 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002715 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002716 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002717 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002718
Mike Galbraith76854c72011-11-22 15:18:24 +01002719 if (p->rt.nr_cpus_allowed == 1)
2720 return prev_cpu;
2721
Peter Zijlstra0763a662009-09-14 19:37:39 +02002722 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002723 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002724 want_affine = 1;
2725 new_cpu = prev_cpu;
2726 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002727
Peter Zijlstradce840a2011-04-07 14:09:50 +02002728 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002729 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002730 if (!(tmp->flags & SD_LOAD_BALANCE))
2731 continue;
2732
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002733 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02002734 * If power savings logic is enabled for a domain, see if we
2735 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002736 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02002737 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02002738 unsigned long power = 0;
2739 unsigned long nr_running = 0;
2740 unsigned long capacity;
2741 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002742
Peter Zijlstraae154be2009-09-10 14:40:57 +02002743 for_each_cpu(i, sched_domain_span(tmp)) {
2744 power += power_of(i);
2745 nr_running += cpu_rq(i)->cfs.nr_running;
2746 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002747
Nikhil Rao1399fa72011-05-18 10:09:39 -07002748 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002749
Peter Zijlstra59abf022009-09-16 08:28:30 +02002750 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2751 nr_running /= 2;
2752
2753 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002754 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002755 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002756
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002757 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002758 * If both cpu and prev_cpu are part of this domain,
2759 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002760 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002761 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2762 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2763 affine_sd = tmp;
2764 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002765 }
2766
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002767 if (!want_sd && !want_affine)
2768 break;
2769
Peter Zijlstra0763a662009-09-14 19:37:39 +02002770 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002771 continue;
2772
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002773 if (want_sd)
2774 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002775 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002776
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002777 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002778 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002779 prev_cpu = cpu;
2780
2781 new_cpu = select_idle_sibling(p, prev_cpu);
2782 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002783 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002784
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002785 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002786 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002787 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002788 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002789
Peter Zijlstra0763a662009-09-14 19:37:39 +02002790 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002791 sd = sd->child;
2792 continue;
2793 }
2794
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002795 if (sd_flag & SD_BALANCE_WAKE)
2796 load_idx = sd->wake_idx;
2797
2798 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002799 if (!group) {
2800 sd = sd->child;
2801 continue;
2802 }
2803
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002804 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002805 if (new_cpu == -1 || new_cpu == cpu) {
2806 /* Now try balancing at a lower domain level of cpu */
2807 sd = sd->child;
2808 continue;
2809 }
2810
2811 /* Now try balancing at a lower domain level of new_cpu */
2812 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002813 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002814 sd = NULL;
2815 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002816 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002817 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002818 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002819 sd = tmp;
2820 }
2821 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002822 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002823unlock:
2824 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002825
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002826 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002827}
2828#endif /* CONFIG_SMP */
2829
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002830static unsigned long
2831wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002832{
2833 unsigned long gran = sysctl_sched_wakeup_granularity;
2834
2835 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002836 * Since its curr running now, convert the gran from real-time
2837 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002838 *
2839 * By using 'se' instead of 'curr' we penalize light tasks, so
2840 * they get preempted easier. That is, if 'se' < 'curr' then
2841 * the resulting gran will be larger, therefore penalizing the
2842 * lighter, if otoh 'se' > 'curr' then the resulting gran will
2843 * be smaller, again penalizing the lighter task.
2844 *
2845 * This is especially important for buddies when the leftmost
2846 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002847 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08002848 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002849}
2850
2851/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02002852 * Should 'se' preempt 'curr'.
2853 *
2854 * |s1
2855 * |s2
2856 * |s3
2857 * g
2858 * |<--->|c
2859 *
2860 * w(c, s1) = -1
2861 * w(c, s2) = 0
2862 * w(c, s3) = 1
2863 *
2864 */
2865static int
2866wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2867{
2868 s64 gran, vdiff = curr->vruntime - se->vruntime;
2869
2870 if (vdiff <= 0)
2871 return -1;
2872
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002873 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02002874 if (vdiff > gran)
2875 return 1;
2876
2877 return 0;
2878}
2879
Peter Zijlstra02479092008-11-04 21:25:10 +01002880static void set_last_buddy(struct sched_entity *se)
2881{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002882 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2883 return;
2884
2885 for_each_sched_entity(se)
2886 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002887}
2888
2889static void set_next_buddy(struct sched_entity *se)
2890{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002891 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2892 return;
2893
2894 for_each_sched_entity(se)
2895 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002896}
2897
Rik van Rielac53db52011-02-01 09:51:03 -05002898static void set_skip_buddy(struct sched_entity *se)
2899{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002900 for_each_sched_entity(se)
2901 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05002902}
2903
Peter Zijlstra464b7522008-10-24 11:06:15 +02002904/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002905 * Preempt the current task with a newly woken task if needed:
2906 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02002907static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002908{
2909 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02002910 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002911 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002912 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002913 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002914
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002915 if (unlikely(se == pse))
2916 return;
2917
Paul Turner5238cdd2011-07-21 09:43:37 -07002918 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01002919 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07002920 * unconditionally check_prempt_curr() after an enqueue (which may have
2921 * lead to a throttle). This both saves work and prevents false
2922 * next-buddy nomination below.
2923 */
2924 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
2925 return;
2926
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002927 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02002928 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002929 next_buddy_marked = 1;
2930 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02002931
Bharata B Raoaec0a512008-08-28 14:42:49 +05302932 /*
2933 * We can come here with TIF_NEED_RESCHED already set from new task
2934 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07002935 *
2936 * Note: this also catches the edge-case of curr being in a throttled
2937 * group (e.g. via set_curr_task), since update_curr() (in the
2938 * enqueue of curr) will have resulted in resched being set. This
2939 * prevents us from potentially nominating it as a false LAST_BUDDY
2940 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05302941 */
2942 if (test_tsk_need_resched(curr))
2943 return;
2944
Darren Harta2f5c9a2011-02-22 13:04:33 -08002945 /* Idle tasks are by definition preempted by non-idle tasks. */
2946 if (unlikely(curr->policy == SCHED_IDLE) &&
2947 likely(p->policy != SCHED_IDLE))
2948 goto preempt;
2949
Ingo Molnar91c234b2007-10-15 17:00:18 +02002950 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08002951 * Batch and idle tasks do not preempt non-idle tasks (their preemption
2952 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02002953 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01002954 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02002955 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002956
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002957 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07002958 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002959 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002960 if (wakeup_preempt_entity(se, pse) == 1) {
2961 /*
2962 * Bias pick_next to pick the sched entity that is
2963 * triggering this preemption.
2964 */
2965 if (!next_buddy_marked)
2966 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002967 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002968 }
Jupyung Leea65ac742009-11-17 18:51:40 +09002969
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002970 return;
2971
2972preempt:
2973 resched_task(curr);
2974 /*
2975 * Only set the backward buddy when the current task is still
2976 * on the rq. This can happen when a wakeup gets interleaved
2977 * with schedule on the ->pre_schedule() or idle_balance()
2978 * point, either of which can * drop the rq lock.
2979 *
2980 * Also, during early boot the idle thread is in the fair class,
2981 * for obvious reasons its a bad idea to schedule back to it.
2982 */
2983 if (unlikely(!se->on_rq || curr == rq->idle))
2984 return;
2985
2986 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
2987 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002988}
2989
Ingo Molnarfb8d4722007-08-09 11:16:48 +02002990static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002991{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002992 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002993 struct cfs_rq *cfs_rq = &rq->cfs;
2994 struct sched_entity *se;
2995
Tim Blechmann36ace272009-11-24 11:55:45 +01002996 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002997 return NULL;
2998
2999 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003000 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003001 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003002 cfs_rq = group_cfs_rq(se);
3003 } while (cfs_rq);
3004
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003005 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003006 if (hrtick_enabled(rq))
3007 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003008
3009 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003010}
3011
3012/*
3013 * Account for a descheduled task:
3014 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003015static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003016{
3017 struct sched_entity *se = &prev->se;
3018 struct cfs_rq *cfs_rq;
3019
3020 for_each_sched_entity(se) {
3021 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003022 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003023 }
3024}
3025
Rik van Rielac53db52011-02-01 09:51:03 -05003026/*
3027 * sched_yield() is very simple
3028 *
3029 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3030 */
3031static void yield_task_fair(struct rq *rq)
3032{
3033 struct task_struct *curr = rq->curr;
3034 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3035 struct sched_entity *se = &curr->se;
3036
3037 /*
3038 * Are we the only task in the tree?
3039 */
3040 if (unlikely(rq->nr_running == 1))
3041 return;
3042
3043 clear_buddies(cfs_rq, se);
3044
3045 if (curr->policy != SCHED_BATCH) {
3046 update_rq_clock(rq);
3047 /*
3048 * Update run-time statistics of the 'current'.
3049 */
3050 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003051 /*
3052 * Tell update_rq_clock() that we've just updated,
3053 * so we don't do microscopic update in schedule()
3054 * and double the fastpath cost.
3055 */
3056 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003057 }
3058
3059 set_skip_buddy(se);
3060}
3061
Mike Galbraithd95f4122011-02-01 09:50:51 -05003062static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3063{
3064 struct sched_entity *se = &p->se;
3065
Paul Turner5238cdd2011-07-21 09:43:37 -07003066 /* throttled hierarchies are not runnable */
3067 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003068 return false;
3069
3070 /* Tell the scheduler that we'd really like pse to run next. */
3071 set_next_buddy(se);
3072
Mike Galbraithd95f4122011-02-01 09:50:51 -05003073 yield_task_fair(rq);
3074
3075 return true;
3076}
3077
Peter Williams681f3e62007-10-24 18:23:51 +02003078#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003079/**************************************************
3080 * Fair scheduling class load-balancing methods:
3081 */
3082
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003083static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3084
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003085#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003086#define LBF_NEED_BREAK 0x02
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003087
3088struct lb_env {
3089 struct sched_domain *sd;
3090
3091 int src_cpu;
3092 struct rq *src_rq;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003093
3094 int dst_cpu;
3095 struct rq *dst_rq;
3096
3097 enum cpu_idle_type idle;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003098 long load_move;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003099 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003100
3101 unsigned int loop;
3102 unsigned int loop_break;
3103 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003104};
3105
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003106/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003107 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003108 * Both runqueues must be locked.
3109 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003110static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003111{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003112 deactivate_task(env->src_rq, p, 0);
3113 set_task_cpu(p, env->dst_cpu);
3114 activate_task(env->dst_rq, p, 0);
3115 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003116}
3117
3118/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003119 * Is this task likely cache-hot:
3120 */
3121static int
3122task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3123{
3124 s64 delta;
3125
3126 if (p->sched_class != &fair_sched_class)
3127 return 0;
3128
3129 if (unlikely(p->policy == SCHED_IDLE))
3130 return 0;
3131
3132 /*
3133 * Buddy candidates are cache hot:
3134 */
3135 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3136 (&p->se == cfs_rq_of(&p->se)->next ||
3137 &p->se == cfs_rq_of(&p->se)->last))
3138 return 1;
3139
3140 if (sysctl_sched_migration_cost == -1)
3141 return 1;
3142 if (sysctl_sched_migration_cost == 0)
3143 return 0;
3144
3145 delta = now - p->se.exec_start;
3146
3147 return delta < (s64)sysctl_sched_migration_cost;
3148}
3149
3150/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003151 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3152 */
3153static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003154int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003155{
3156 int tsk_cache_hot = 0;
3157 /*
3158 * We do not migrate tasks that are:
3159 * 1) running (obviously), or
3160 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3161 * 3) are cache-hot on their current CPU.
3162 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003163 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003164 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003165 return 0;
3166 }
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003167 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003168
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003169 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003170 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003171 return 0;
3172 }
3173
3174 /*
3175 * Aggressive migration if:
3176 * 1) task is cache cold, or
3177 * 2) too many balance attempts have failed.
3178 */
3179
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003180 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003181 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003182 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003183#ifdef CONFIG_SCHEDSTATS
3184 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003185 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003186 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003187 }
3188#endif
3189 return 1;
3190 }
3191
3192 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003193 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003194 return 0;
3195 }
3196 return 1;
3197}
3198
Peter Zijlstra897c3952009-12-17 17:45:42 +01003199/*
3200 * move_one_task tries to move exactly one task from busiest to this_rq, as
3201 * part of active balancing operations within "domain".
3202 * Returns 1 if successful and 0 otherwise.
3203 *
3204 * Called with both runqueues locked.
3205 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003206static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003207{
3208 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003209
Peter Zijlstra367456c2012-02-20 21:49:09 +01003210 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3211 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3212 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003213
Peter Zijlstra367456c2012-02-20 21:49:09 +01003214 if (!can_migrate_task(p, env))
3215 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003216
Peter Zijlstra367456c2012-02-20 21:49:09 +01003217 move_task(p, env);
3218 /*
3219 * Right now, this is only the second place move_task()
3220 * is called, so we can safely collect move_task()
3221 * stats here rather than inside move_task().
3222 */
3223 schedstat_inc(env->sd, lb_gained[env->idle]);
3224 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003225 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003226 return 0;
3227}
3228
Peter Zijlstra367456c2012-02-20 21:49:09 +01003229static unsigned long task_h_load(struct task_struct *p);
3230
Peter Zijlstraeb953082012-04-17 13:38:40 +02003231static const unsigned int sched_nr_migrate_break = 32;
3232
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003233/*
3234 * move_tasks tries to move up to load_move weighted load from busiest to
3235 * this_rq, as part of a balancing operation within domain "sd".
3236 * Returns 1 if successful and 0 otherwise.
3237 *
3238 * Called with both runqueues locked.
3239 */
3240static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003241{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003242 struct list_head *tasks = &env->src_rq->cfs_tasks;
3243 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003244 unsigned long load;
3245 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003246
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003247 if (env->load_move <= 0)
3248 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003249
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003250 while (!list_empty(tasks)) {
3251 p = list_first_entry(tasks, struct task_struct, se.group_node);
3252
Peter Zijlstra367456c2012-02-20 21:49:09 +01003253 env->loop++;
3254 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003255 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003256 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003257
3258 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003259 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003260 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003261 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003262 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003263 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003264
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003265 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003266 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003267
Peter Zijlstra367456c2012-02-20 21:49:09 +01003268 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003269
Peter Zijlstraeb953082012-04-17 13:38:40 +02003270 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003271 goto next;
3272
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003273 if ((load / 2) > env->load_move)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003274 goto next;
3275
3276 if (!can_migrate_task(p, env))
3277 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003278
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003279 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003280 pulled++;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003281 env->load_move -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003282
3283#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003284 /*
3285 * NEWIDLE balancing is a source of latency, so preemptible
3286 * kernels will stop after the first task is pulled to minimize
3287 * the critical section.
3288 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003289 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003290 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003291#endif
3292
Peter Zijlstraee00e662009-12-17 17:25:20 +01003293 /*
3294 * We only want to steal up to the prescribed amount of
3295 * weighted load.
3296 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003297 if (env->load_move <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003298 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003299
Peter Zijlstra367456c2012-02-20 21:49:09 +01003300 continue;
3301next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003302 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003303 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003304
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003305 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003306 * Right now, this is one of only two places move_task() is called,
3307 * so we can safely collect move_task() stats here rather than
3308 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003309 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003310 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003311
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003312 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003313}
3314
Peter Zijlstra230059de2009-12-17 17:47:12 +01003315#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003316/*
3317 * update tg->load_weight by folding this cpu's load_avg
3318 */
Paul Turner67e86252010-11-15 15:47:05 -08003319static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003320{
3321 struct cfs_rq *cfs_rq;
3322 unsigned long flags;
3323 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003324
3325 if (!tg->se[cpu])
3326 return 0;
3327
3328 rq = cpu_rq(cpu);
3329 cfs_rq = tg->cfs_rq[cpu];
3330
3331 raw_spin_lock_irqsave(&rq->lock, flags);
3332
3333 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003334 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003335
3336 /*
3337 * We need to update shares after updating tg->load_weight in
3338 * order to adjust the weight of groups with long running tasks.
3339 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003340 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003341
3342 raw_spin_unlock_irqrestore(&rq->lock, flags);
3343
3344 return 0;
3345}
3346
3347static void update_shares(int cpu)
3348{
3349 struct cfs_rq *cfs_rq;
3350 struct rq *rq = cpu_rq(cpu);
3351
3352 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003353 /*
3354 * Iterates the task_group tree in a bottom up fashion, see
3355 * list_add_leaf_cfs_rq() for details.
3356 */
Paul Turner64660c82011-07-21 09:43:36 -07003357 for_each_leaf_cfs_rq(rq, cfs_rq) {
3358 /* throttled entities do not contribute to load */
3359 if (throttled_hierarchy(cfs_rq))
3360 continue;
3361
Paul Turner67e86252010-11-15 15:47:05 -08003362 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003363 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003364 rcu_read_unlock();
3365}
3366
Peter Zijlstra9763b672011-07-13 13:09:25 +02003367/*
3368 * Compute the cpu's hierarchical load factor for each task group.
3369 * This needs to be done in a top-down fashion because the load of a child
3370 * group is a fraction of its parents load.
3371 */
3372static int tg_load_down(struct task_group *tg, void *data)
3373{
3374 unsigned long load;
3375 long cpu = (long)data;
3376
3377 if (!tg->parent) {
3378 load = cpu_rq(cpu)->load.weight;
3379 } else {
3380 load = tg->parent->cfs_rq[cpu]->h_load;
3381 load *= tg->se[cpu]->load.weight;
3382 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3383 }
3384
3385 tg->cfs_rq[cpu]->h_load = load;
3386
3387 return 0;
3388}
3389
3390static void update_h_load(long cpu)
3391{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003392 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003393 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003394 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003395}
3396
Peter Zijlstra367456c2012-02-20 21:49:09 +01003397static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003398{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003399 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3400 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003401
Peter Zijlstra367456c2012-02-20 21:49:09 +01003402 load = p->se.load.weight;
3403 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003404
Peter Zijlstra367456c2012-02-20 21:49:09 +01003405 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003406}
3407#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003408static inline void update_shares(int cpu)
3409{
3410}
3411
Peter Zijlstra367456c2012-02-20 21:49:09 +01003412static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003413{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003414}
3415
3416static unsigned long task_h_load(struct task_struct *p)
3417{
3418 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003419}
3420#endif
3421
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003422/********** Helpers for find_busiest_group ************************/
3423/*
3424 * sd_lb_stats - Structure to store the statistics of a sched_domain
3425 * during load balancing.
3426 */
3427struct sd_lb_stats {
3428 struct sched_group *busiest; /* Busiest group in this sd */
3429 struct sched_group *this; /* Local group in this sd */
3430 unsigned long total_load; /* Total load of all groups in sd */
3431 unsigned long total_pwr; /* Total power of all groups in sd */
3432 unsigned long avg_load; /* Average load across all groups in sd */
3433
3434 /** Statistics of this group */
3435 unsigned long this_load;
3436 unsigned long this_load_per_task;
3437 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003438 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003439 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003440
3441 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003442 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003443 unsigned long max_load;
3444 unsigned long busiest_load_per_task;
3445 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003446 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003447 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003448 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003449
3450 int group_imb; /* Is there imbalance in this sd */
3451#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3452 int power_savings_balance; /* Is powersave balance needed for this sd */
3453 struct sched_group *group_min; /* Least loaded group in sd */
3454 struct sched_group *group_leader; /* Group which relieves group_min */
3455 unsigned long min_load_per_task; /* load_per_task in group_min */
3456 unsigned long leader_nr_running; /* Nr running of group_leader */
3457 unsigned long min_nr_running; /* Nr running of group_min */
3458#endif
3459};
3460
3461/*
3462 * sg_lb_stats - stats of a sched_group required for load_balancing
3463 */
3464struct sg_lb_stats {
3465 unsigned long avg_load; /*Avg load across the CPUs of the group */
3466 unsigned long group_load; /* Total load over the CPUs of the group */
3467 unsigned long sum_nr_running; /* Nr tasks running in the group */
3468 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3469 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003470 unsigned long idle_cpus;
3471 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003472 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003473 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003474};
3475
3476/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003477 * get_sd_load_idx - Obtain the load index for a given sched domain.
3478 * @sd: The sched_domain whose load_idx is to be obtained.
3479 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3480 */
3481static inline int get_sd_load_idx(struct sched_domain *sd,
3482 enum cpu_idle_type idle)
3483{
3484 int load_idx;
3485
3486 switch (idle) {
3487 case CPU_NOT_IDLE:
3488 load_idx = sd->busy_idx;
3489 break;
3490
3491 case CPU_NEWLY_IDLE:
3492 load_idx = sd->newidle_idx;
3493 break;
3494 default:
3495 load_idx = sd->idle_idx;
3496 break;
3497 }
3498
3499 return load_idx;
3500}
3501
3502
3503#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3504/**
3505 * init_sd_power_savings_stats - Initialize power savings statistics for
3506 * the given sched_domain, during load balancing.
3507 *
3508 * @sd: Sched domain whose power-savings statistics are to be initialized.
3509 * @sds: Variable containing the statistics for sd.
3510 * @idle: Idle status of the CPU at which we're performing load-balancing.
3511 */
3512static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3513 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3514{
3515 /*
3516 * Busy processors will not participate in power savings
3517 * balance.
3518 */
3519 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3520 sds->power_savings_balance = 0;
3521 else {
3522 sds->power_savings_balance = 1;
3523 sds->min_nr_running = ULONG_MAX;
3524 sds->leader_nr_running = 0;
3525 }
3526}
3527
3528/**
3529 * update_sd_power_savings_stats - Update the power saving stats for a
3530 * sched_domain while performing load balancing.
3531 *
3532 * @group: sched_group belonging to the sched_domain under consideration.
3533 * @sds: Variable containing the statistics of the sched_domain
3534 * @local_group: Does group contain the CPU for which we're performing
3535 * load balancing ?
3536 * @sgs: Variable containing the statistics of the group.
3537 */
3538static inline void update_sd_power_savings_stats(struct sched_group *group,
3539 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3540{
3541
3542 if (!sds->power_savings_balance)
3543 return;
3544
3545 /*
3546 * If the local group is idle or completely loaded
3547 * no need to do power savings balance at this domain
3548 */
3549 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3550 !sds->this_nr_running))
3551 sds->power_savings_balance = 0;
3552
3553 /*
3554 * If a group is already running at full capacity or idle,
3555 * don't include that group in power savings calculations
3556 */
3557 if (!sds->power_savings_balance ||
3558 sgs->sum_nr_running >= sgs->group_capacity ||
3559 !sgs->sum_nr_running)
3560 return;
3561
3562 /*
3563 * Calculate the group which has the least non-idle load.
3564 * This is the group from where we need to pick up the load
3565 * for saving power
3566 */
3567 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3568 (sgs->sum_nr_running == sds->min_nr_running &&
3569 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3570 sds->group_min = group;
3571 sds->min_nr_running = sgs->sum_nr_running;
3572 sds->min_load_per_task = sgs->sum_weighted_load /
3573 sgs->sum_nr_running;
3574 }
3575
3576 /*
3577 * Calculate the group which is almost near its
3578 * capacity but still has some space to pick up some load
3579 * from other group and save more power
3580 */
3581 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3582 return;
3583
3584 if (sgs->sum_nr_running > sds->leader_nr_running ||
3585 (sgs->sum_nr_running == sds->leader_nr_running &&
3586 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3587 sds->group_leader = group;
3588 sds->leader_nr_running = sgs->sum_nr_running;
3589 }
3590}
3591
3592/**
3593 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3594 * @sds: Variable containing the statistics of the sched_domain
3595 * under consideration.
3596 * @this_cpu: Cpu at which we're currently performing load-balancing.
3597 * @imbalance: Variable to store the imbalance.
3598 *
3599 * Description:
3600 * Check if we have potential to perform some power-savings balance.
3601 * If yes, set the busiest group to be the least loaded group in the
3602 * sched_domain, so that it's CPUs can be put to idle.
3603 *
3604 * Returns 1 if there is potential to perform power-savings balance.
3605 * Else returns 0.
3606 */
3607static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3608 int this_cpu, unsigned long *imbalance)
3609{
3610 if (!sds->power_savings_balance)
3611 return 0;
3612
3613 if (sds->this != sds->group_leader ||
3614 sds->group_leader == sds->group_min)
3615 return 0;
3616
3617 *imbalance = sds->min_load_per_task;
3618 sds->busiest = sds->group_min;
3619
3620 return 1;
3621
3622}
3623#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3624static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3625 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3626{
3627 return;
3628}
3629
3630static inline void update_sd_power_savings_stats(struct sched_group *group,
3631 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3632{
3633 return;
3634}
3635
3636static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3637 int this_cpu, unsigned long *imbalance)
3638{
3639 return 0;
3640}
3641#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3642
3643
3644unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3645{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003646 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003647}
3648
3649unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3650{
3651 return default_scale_freq_power(sd, cpu);
3652}
3653
3654unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3655{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003656 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003657 unsigned long smt_gain = sd->smt_gain;
3658
3659 smt_gain /= weight;
3660
3661 return smt_gain;
3662}
3663
3664unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3665{
3666 return default_scale_smt_power(sd, cpu);
3667}
3668
3669unsigned long scale_rt_power(int cpu)
3670{
3671 struct rq *rq = cpu_rq(cpu);
3672 u64 total, available;
3673
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003674 total = sched_avg_period() + (rq->clock - rq->age_stamp);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003675
3676 if (unlikely(total < rq->rt_avg)) {
3677 /* Ensures that power won't end up being negative */
3678 available = 0;
3679 } else {
3680 available = total - rq->rt_avg;
3681 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003682
Nikhil Rao1399fa72011-05-18 10:09:39 -07003683 if (unlikely((s64)total < SCHED_POWER_SCALE))
3684 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003685
Nikhil Rao1399fa72011-05-18 10:09:39 -07003686 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003687
3688 return div_u64(available, total);
3689}
3690
3691static void update_cpu_power(struct sched_domain *sd, int cpu)
3692{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003693 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003694 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003695 struct sched_group *sdg = sd->groups;
3696
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003697 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3698 if (sched_feat(ARCH_POWER))
3699 power *= arch_scale_smt_power(sd, cpu);
3700 else
3701 power *= default_scale_smt_power(sd, cpu);
3702
Nikhil Rao1399fa72011-05-18 10:09:39 -07003703 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003704 }
3705
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003706 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003707
3708 if (sched_feat(ARCH_POWER))
3709 power *= arch_scale_freq_power(sd, cpu);
3710 else
3711 power *= default_scale_freq_power(sd, cpu);
3712
Nikhil Rao1399fa72011-05-18 10:09:39 -07003713 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003714
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003715 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003716 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003717
3718 if (!power)
3719 power = 1;
3720
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003721 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003722 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003723}
3724
Peter Zijlstra029632f2011-10-25 10:00:11 +02003725void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003726{
3727 struct sched_domain *child = sd->child;
3728 struct sched_group *group, *sdg = sd->groups;
3729 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003730 unsigned long interval;
3731
3732 interval = msecs_to_jiffies(sd->balance_interval);
3733 interval = clamp(interval, 1UL, max_load_balance_interval);
3734 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003735
3736 if (!child) {
3737 update_cpu_power(sd, cpu);
3738 return;
3739 }
3740
3741 power = 0;
3742
3743 group = child->groups;
3744 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003745 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003746 group = group->next;
3747 } while (group != child->groups);
3748
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003749 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003750}
3751
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003752/*
3753 * Try and fix up capacity for tiny siblings, this is needed when
3754 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3755 * which on its own isn't powerful enough.
3756 *
3757 * See update_sd_pick_busiest() and check_asym_packing().
3758 */
3759static inline int
3760fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3761{
3762 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003763 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003764 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003765 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003766 return 0;
3767
3768 /*
3769 * If ~90% of the cpu_power is still there, we're good.
3770 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003771 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003772 return 1;
3773
3774 return 0;
3775}
3776
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003777/**
3778 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3779 * @sd: The sched_domain whose statistics are to be updated.
3780 * @group: sched_group whose statistics are to be updated.
3781 * @this_cpu: Cpu for which load balance is currently performed.
3782 * @idle: Idle status of this_cpu
3783 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003784 * @local_group: Does group contain this_cpu.
3785 * @cpus: Set of cpus considered for load balancing.
3786 * @balance: Should we balance.
3787 * @sgs: variable to hold the statistics for this group.
3788 */
3789static inline void update_sg_lb_stats(struct sched_domain *sd,
3790 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003791 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003792 int local_group, const struct cpumask *cpus,
3793 int *balance, struct sg_lb_stats *sgs)
3794{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003795 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003796 int i;
3797 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003798 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003799
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003800 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003801 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003802
3803 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003804 max_cpu_load = 0;
3805 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003806 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003807
3808 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3809 struct rq *rq = cpu_rq(i);
3810
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003811 /* Bias balancing toward cpus of our domain */
3812 if (local_group) {
3813 if (idle_cpu(i) && !first_idle_cpu) {
3814 first_idle_cpu = 1;
3815 balance_cpu = i;
3816 }
3817
3818 load = target_load(i, load_idx);
3819 } else {
3820 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003821 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003822 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003823 max_nr_running = rq->nr_running;
3824 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003825 if (min_cpu_load > load)
3826 min_cpu_load = load;
3827 }
3828
3829 sgs->group_load += load;
3830 sgs->sum_nr_running += rq->nr_running;
3831 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003832 if (idle_cpu(i))
3833 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003834 }
3835
3836 /*
3837 * First idle cpu or the first cpu(busiest) in this sched group
3838 * is eligible for doing load balancing at this and above
3839 * domains. In the newly idle case, we will allow all the cpu's
3840 * to do the newly idle load balance.
3841 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003842 if (local_group) {
3843 if (idle != CPU_NEWLY_IDLE) {
3844 if (balance_cpu != this_cpu) {
3845 *balance = 0;
3846 return;
3847 }
3848 update_group_power(sd, this_cpu);
3849 } else if (time_after_eq(jiffies, group->sgp->next_update))
3850 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003851 }
3852
3853 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003854 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003855
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003856 /*
3857 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003858 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003859 *
3860 * APZ: with cgroup the avg task weight can vary wildly and
3861 * might not be a suitable number - should we keep a
3862 * normalized nr_running number somewhere that negates
3863 * the hierarchy?
3864 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003865 if (sgs->sum_nr_running)
3866 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003867
Peter Zijlstra866ab432011-02-21 18:56:47 +01003868 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003869 sgs->group_imb = 1;
3870
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003871 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003872 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003873 if (!sgs->group_capacity)
3874 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003875 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003876
3877 if (sgs->group_capacity > sgs->sum_nr_running)
3878 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003879}
3880
3881/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003882 * update_sd_pick_busiest - return 1 on busiest group
3883 * @sd: sched_domain whose statistics are to be checked
3884 * @sds: sched_domain statistics
3885 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003886 * @sgs: sched_group statistics
3887 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10003888 *
3889 * Determine if @sg is a busier group than the previously selected
3890 * busiest group.
3891 */
3892static bool update_sd_pick_busiest(struct sched_domain *sd,
3893 struct sd_lb_stats *sds,
3894 struct sched_group *sg,
3895 struct sg_lb_stats *sgs,
3896 int this_cpu)
3897{
3898 if (sgs->avg_load <= sds->max_load)
3899 return false;
3900
3901 if (sgs->sum_nr_running > sgs->group_capacity)
3902 return true;
3903
3904 if (sgs->group_imb)
3905 return true;
3906
3907 /*
3908 * ASYM_PACKING needs to move all the work to the lowest
3909 * numbered CPUs in the group, therefore mark all groups
3910 * higher than ourself as busy.
3911 */
3912 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3913 this_cpu < group_first_cpu(sg)) {
3914 if (!sds->busiest)
3915 return true;
3916
3917 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3918 return true;
3919 }
3920
3921 return false;
3922}
3923
3924/**
Hui Kang461819a2011-10-11 23:00:59 -04003925 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003926 * @sd: sched_domain whose statistics are to be updated.
3927 * @this_cpu: Cpu for which load balance is currently performed.
3928 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003929 * @cpus: Set of cpus considered for load balancing.
3930 * @balance: Should we balance.
3931 * @sds: variable to hold the statistics for this sched_domain.
3932 */
3933static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003934 enum cpu_idle_type idle, const struct cpumask *cpus,
3935 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003936{
3937 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003938 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003939 struct sg_lb_stats sgs;
3940 int load_idx, prefer_sibling = 0;
3941
3942 if (child && child->flags & SD_PREFER_SIBLING)
3943 prefer_sibling = 1;
3944
3945 init_sd_power_savings_stats(sd, sds, idle);
3946 load_idx = get_sd_load_idx(sd, idle);
3947
3948 do {
3949 int local_group;
3950
Michael Neuling532cb4c2010-06-08 14:57:02 +10003951 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003952 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003953 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003954 local_group, cpus, balance, &sgs);
3955
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003956 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003957 return;
3958
3959 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003960 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003961
3962 /*
3963 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10003964 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07003965 * and move all the excess tasks away. We lower the capacity
3966 * of a group only if the local group has the capacity to fit
3967 * these excess tasks, i.e. nr_running < group_capacity. The
3968 * extra check prevents the case where you always pull from the
3969 * heaviest group when it is already under-utilized (possible
3970 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003971 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07003972 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003973 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3974
3975 if (local_group) {
3976 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003977 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003978 sds->this_nr_running = sgs.sum_nr_running;
3979 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003980 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003981 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003982 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003983 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003984 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003985 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003986 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003987 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003988 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003989 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003990 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003991 sds->group_imb = sgs.group_imb;
3992 }
3993
Michael Neuling532cb4c2010-06-08 14:57:02 +10003994 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
3995 sg = sg->next;
3996 } while (sg != sd->groups);
3997}
3998
Michael Neuling532cb4c2010-06-08 14:57:02 +10003999/**
4000 * check_asym_packing - Check to see if the group is packed into the
4001 * sched doman.
4002 *
4003 * This is primarily intended to used at the sibling level. Some
4004 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4005 * case of POWER7, it can move to lower SMT modes only when higher
4006 * threads are idle. When in lower SMT modes, the threads will
4007 * perform better since they share less core resources. Hence when we
4008 * have idle threads, we want them to be the higher ones.
4009 *
4010 * This packing function is run on idle threads. It checks to see if
4011 * the busiest CPU in this domain (core in the P7 case) has a higher
4012 * CPU number than the packing function is being run on. Here we are
4013 * assuming lower CPU number will be equivalent to lower a SMT thread
4014 * number.
4015 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004016 * Returns 1 when packing is required and a task should be moved to
4017 * this CPU. The amount of the imbalance is returned in *imbalance.
4018 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10004019 * @sd: The sched_domain whose packing is to be checked.
4020 * @sds: Statistics of the sched_domain which is to be packed
4021 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4022 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004023 */
4024static int check_asym_packing(struct sched_domain *sd,
4025 struct sd_lb_stats *sds,
4026 int this_cpu, unsigned long *imbalance)
4027{
4028 int busiest_cpu;
4029
4030 if (!(sd->flags & SD_ASYM_PACKING))
4031 return 0;
4032
4033 if (!sds->busiest)
4034 return 0;
4035
4036 busiest_cpu = group_first_cpu(sds->busiest);
4037 if (this_cpu > busiest_cpu)
4038 return 0;
4039
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004040 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004041 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004042 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004043}
4044
4045/**
4046 * fix_small_imbalance - Calculate the minor imbalance that exists
4047 * amongst the groups of a sched_domain, during
4048 * load balancing.
4049 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4050 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4051 * @imbalance: Variable to store the imbalance.
4052 */
4053static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4054 int this_cpu, unsigned long *imbalance)
4055{
4056 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4057 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004058 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004059
4060 if (sds->this_nr_running) {
4061 sds->this_load_per_task /= sds->this_nr_running;
4062 if (sds->busiest_load_per_task >
4063 sds->this_load_per_task)
4064 imbn = 1;
4065 } else
4066 sds->this_load_per_task =
4067 cpu_avg_load_per_task(this_cpu);
4068
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004069 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004070 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004071 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004072
4073 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4074 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004075 *imbalance = sds->busiest_load_per_task;
4076 return;
4077 }
4078
4079 /*
4080 * OK, we don't have enough imbalance to justify moving tasks,
4081 * however we may be able to increase total CPU power used by
4082 * moving them.
4083 */
4084
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004085 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004086 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004087 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004088 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004089 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004090
4091 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004092 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004093 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004095 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004096 min(sds->busiest_load_per_task, sds->max_load - tmp);
4097
4098 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004099 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004100 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004101 tmp = (sds->max_load * sds->busiest->sgp->power) /
4102 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004103 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004104 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004105 sds->this->sgp->power;
4106 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004108 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004109
4110 /* Move if we gain throughput */
4111 if (pwr_move > pwr_now)
4112 *imbalance = sds->busiest_load_per_task;
4113}
4114
4115/**
4116 * calculate_imbalance - Calculate the amount of imbalance present within the
4117 * groups of a given sched_domain during load balance.
4118 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4119 * @this_cpu: Cpu for which currently load balance is being performed.
4120 * @imbalance: The variable to store the imbalance.
4121 */
4122static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4123 unsigned long *imbalance)
4124{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004125 unsigned long max_pull, load_above_capacity = ~0UL;
4126
4127 sds->busiest_load_per_task /= sds->busiest_nr_running;
4128 if (sds->group_imb) {
4129 sds->busiest_load_per_task =
4130 min(sds->busiest_load_per_task, sds->avg_load);
4131 }
4132
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004133 /*
4134 * In the presence of smp nice balancing, certain scenarios can have
4135 * max load less than avg load(as we skip the groups at or below
4136 * its cpu_power, while calculating max_load..)
4137 */
4138 if (sds->max_load < sds->avg_load) {
4139 *imbalance = 0;
4140 return fix_small_imbalance(sds, this_cpu, imbalance);
4141 }
4142
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004143 if (!sds->group_imb) {
4144 /*
4145 * Don't want to pull so many tasks that a group would go idle.
4146 */
4147 load_above_capacity = (sds->busiest_nr_running -
4148 sds->busiest_group_capacity);
4149
Nikhil Rao1399fa72011-05-18 10:09:39 -07004150 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004151
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004152 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004153 }
4154
4155 /*
4156 * We're trying to get all the cpus to the average_load, so we don't
4157 * want to push ourselves above the average load, nor do we wish to
4158 * reduce the max loaded cpu below the average load. At the same time,
4159 * we also don't want to reduce the group load below the group capacity
4160 * (so that we can implement power-savings policies etc). Thus we look
4161 * for the minimum possible imbalance.
4162 * Be careful of negative numbers as they'll appear as very large values
4163 * with unsigned longs.
4164 */
4165 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004166
4167 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004168 *imbalance = min(max_pull * sds->busiest->sgp->power,
4169 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004170 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004171
4172 /*
4173 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004174 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004175 * a think about bumping its value to force at least one task to be
4176 * moved
4177 */
4178 if (*imbalance < sds->busiest_load_per_task)
4179 return fix_small_imbalance(sds, this_cpu, imbalance);
4180
4181}
Nikhil Raofab47622010-10-15 13:12:29 -07004182
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004183/******* find_busiest_group() helpers end here *********************/
4184
4185/**
4186 * find_busiest_group - Returns the busiest group within the sched_domain
4187 * if there is an imbalance. If there isn't an imbalance, and
4188 * the user has opted for power-savings, it returns a group whose
4189 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4190 * such a group exists.
4191 *
4192 * Also calculates the amount of weighted load which should be moved
4193 * to restore balance.
4194 *
4195 * @sd: The sched_domain whose busiest group is to be returned.
4196 * @this_cpu: The cpu for which load balancing is currently being performed.
4197 * @imbalance: Variable which stores amount of weighted load which should
4198 * be moved to restore balance/put a group to idle.
4199 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004200 * @cpus: The set of CPUs under consideration for load-balancing.
4201 * @balance: Pointer to a variable indicating if this_cpu
4202 * is the appropriate cpu to perform load balancing at this_level.
4203 *
4204 * Returns: - the busiest group if imbalance exists.
4205 * - If no imbalance and user has opted for power-savings balance,
4206 * return the least loaded group whose CPUs can be
4207 * put to idle by rebalancing its tasks onto our group.
4208 */
4209static struct sched_group *
4210find_busiest_group(struct sched_domain *sd, int this_cpu,
4211 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004212 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004213{
4214 struct sd_lb_stats sds;
4215
4216 memset(&sds, 0, sizeof(sds));
4217
4218 /*
4219 * Compute the various statistics relavent for load balancing at
4220 * this level.
4221 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004222 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004223
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004224 /*
4225 * this_cpu is not the appropriate cpu to perform load balancing at
4226 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004227 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004228 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004229 goto ret;
4230
Michael Neuling532cb4c2010-06-08 14:57:02 +10004231 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
4232 check_asym_packing(sd, &sds, this_cpu, imbalance))
4233 return sds.busiest;
4234
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004235 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004236 if (!sds.busiest || sds.busiest_nr_running == 0)
4237 goto out_balanced;
4238
Nikhil Rao1399fa72011-05-18 10:09:39 -07004239 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004240
Peter Zijlstra866ab432011-02-21 18:56:47 +01004241 /*
4242 * If the busiest group is imbalanced the below checks don't
4243 * work because they assumes all things are equal, which typically
4244 * isn't true due to cpus_allowed constraints and the like.
4245 */
4246 if (sds.group_imb)
4247 goto force_balance;
4248
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004249 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07004250 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4251 !sds.busiest_has_capacity)
4252 goto force_balance;
4253
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004254 /*
4255 * If the local group is more busy than the selected busiest group
4256 * don't try and pull any tasks.
4257 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004258 if (sds.this_load >= sds.max_load)
4259 goto out_balanced;
4260
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004261 /*
4262 * Don't pull any tasks if this group is already above the domain
4263 * average load.
4264 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004265 if (sds.this_load >= sds.avg_load)
4266 goto out_balanced;
4267
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004268 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004269 /*
4270 * This cpu is idle. If the busiest group load doesn't
4271 * have more tasks than the number of available cpu's and
4272 * there is no imbalance between this and busiest group
4273 * wrt to idle cpu's, it is balanced.
4274 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004275 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004276 sds.busiest_nr_running <= sds.busiest_group_weight)
4277 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004278 } else {
4279 /*
4280 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4281 * imbalance_pct to be conservative.
4282 */
4283 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4284 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004285 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004286
Nikhil Raofab47622010-10-15 13:12:29 -07004287force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004288 /* Looks like there is an imbalance. Compute it */
4289 calculate_imbalance(&sds, this_cpu, imbalance);
4290 return sds.busiest;
4291
4292out_balanced:
4293 /*
4294 * There is no obvious imbalance. But check if we can do some balancing
4295 * to save power.
4296 */
4297 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4298 return sds.busiest;
4299ret:
4300 *imbalance = 0;
4301 return NULL;
4302}
4303
4304/*
4305 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4306 */
4307static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004308find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4309 enum cpu_idle_type idle, unsigned long imbalance,
4310 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004311{
4312 struct rq *busiest = NULL, *rq;
4313 unsigned long max_load = 0;
4314 int i;
4315
4316 for_each_cpu(i, sched_group_cpus(group)) {
4317 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004318 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4319 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004320 unsigned long wl;
4321
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004322 if (!capacity)
4323 capacity = fix_small_capacity(sd, group);
4324
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004325 if (!cpumask_test_cpu(i, cpus))
4326 continue;
4327
4328 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004329 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004330
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004331 /*
4332 * When comparing with imbalance, use weighted_cpuload()
4333 * which is not scaled with the cpu power.
4334 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004335 if (capacity && rq->nr_running == 1 && wl > imbalance)
4336 continue;
4337
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004338 /*
4339 * For the load comparisons with the other cpu's, consider
4340 * the weighted_cpuload() scaled with the cpu power, so that
4341 * the load can be moved away from the cpu that is potentially
4342 * running at a lower capacity.
4343 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004344 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004345
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004346 if (wl > max_load) {
4347 max_load = wl;
4348 busiest = rq;
4349 }
4350 }
4351
4352 return busiest;
4353}
4354
4355/*
4356 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4357 * so long as it is large enough.
4358 */
4359#define MAX_PINNED_INTERVAL 512
4360
4361/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004362DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004363
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004364static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004365 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004366{
4367 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004368
4369 /*
4370 * ASYM_PACKING needs to force migrate tasks from busy but
4371 * higher numbered CPUs in order to pack all tasks in the
4372 * lowest numbered CPUs.
4373 */
4374 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
4375 return 1;
4376
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004377 /*
4378 * The only task running in a non-idle cpu can be moved to this
4379 * cpu in an attempt to completely freeup the other CPU
4380 * package.
4381 *
4382 * The package power saving logic comes from
4383 * find_busiest_group(). If there are no imbalance, then
4384 * f_b_g() will return NULL. However when sched_mc={1,2} then
4385 * f_b_g() will select a group from which a running task may be
4386 * pulled to this cpu in order to make the other package idle.
4387 * If there is no opportunity to make a package idle and if
4388 * there are no imbalance, then f_b_g() will return NULL and no
4389 * action will be taken in load_balance_newidle().
4390 *
4391 * Under normal task pull operation due to imbalance, there
4392 * will be more than one task in the source run queue and
4393 * move_tasks() will succeed. ld_moved will be true and this
4394 * active balance code will not be triggered.
4395 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004396 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4397 return 0;
4398 }
4399
4400 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4401}
4402
Tejun Heo969c7922010-05-06 18:49:21 +02004403static int active_load_balance_cpu_stop(void *data);
4404
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004405/*
4406 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4407 * tasks if there is an imbalance.
4408 */
4409static int load_balance(int this_cpu, struct rq *this_rq,
4410 struct sched_domain *sd, enum cpu_idle_type idle,
4411 int *balance)
4412{
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004413 int ld_moved, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004414 struct sched_group *group;
4415 unsigned long imbalance;
4416 struct rq *busiest;
4417 unsigned long flags;
4418 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4419
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004420 struct lb_env env = {
4421 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004422 .dst_cpu = this_cpu,
4423 .dst_rq = this_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004424 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004425 .loop_break = sched_nr_migrate_break,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004426 };
4427
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004428 cpumask_copy(cpus, cpu_active_mask);
4429
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004430 schedstat_inc(sd, lb_count[idle]);
4431
4432redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004433 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004434 cpus, balance);
4435
4436 if (*balance == 0)
4437 goto out_balanced;
4438
4439 if (!group) {
4440 schedstat_inc(sd, lb_nobusyg[idle]);
4441 goto out_balanced;
4442 }
4443
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004444 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004445 if (!busiest) {
4446 schedstat_inc(sd, lb_nobusyq[idle]);
4447 goto out_balanced;
4448 }
4449
4450 BUG_ON(busiest == this_rq);
4451
4452 schedstat_add(sd, lb_imbalance[idle], imbalance);
4453
4454 ld_moved = 0;
4455 if (busiest->nr_running > 1) {
4456 /*
4457 * Attempt to move tasks. If find_busiest_group has found
4458 * an imbalance but busiest->nr_running <= 1, the group is
4459 * still unbalanced. ld_moved simply stays zero, so it is
4460 * correctly treated as an imbalance.
4461 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004462 env.flags |= LBF_ALL_PINNED;
Peter Zijlstraeb953082012-04-17 13:38:40 +02004463 env.load_move = imbalance;
4464 env.src_cpu = busiest->cpu;
4465 env.src_rq = busiest;
4466 env.loop_max = min_t(unsigned long, sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004467
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004468more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004469 local_irq_save(flags);
4470 double_rq_lock(this_rq, busiest);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004471 if (!env.loop)
4472 update_h_load(env.src_cpu);
4473 ld_moved += move_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004474 double_rq_unlock(this_rq, busiest);
4475 local_irq_restore(flags);
4476
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004477 if (env.flags & LBF_NEED_BREAK) {
4478 env.flags &= ~LBF_NEED_BREAK;
4479 goto more_balance;
4480 }
4481
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004482 /*
4483 * some other cpu did the load balance for us.
4484 */
4485 if (ld_moved && this_cpu != smp_processor_id())
4486 resched_cpu(this_cpu);
4487
4488 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004489 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004490 cpumask_clear_cpu(cpu_of(busiest), cpus);
4491 if (!cpumask_empty(cpus))
4492 goto redo;
4493 goto out_balanced;
4494 }
4495 }
4496
4497 if (!ld_moved) {
4498 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004499 /*
4500 * Increment the failure counter only on periodic balance.
4501 * We do not want newidle balance, which can be very
4502 * frequent, pollute the failure counter causing
4503 * excessive cache_hot migrations and active balances.
4504 */
4505 if (idle != CPU_NEWLY_IDLE)
4506 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004507
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004508 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004509 raw_spin_lock_irqsave(&busiest->lock, flags);
4510
Tejun Heo969c7922010-05-06 18:49:21 +02004511 /* don't kick the active_load_balance_cpu_stop,
4512 * if the curr task on busiest cpu can't be
4513 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004514 */
4515 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004516 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004517 raw_spin_unlock_irqrestore(&busiest->lock,
4518 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004519 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004520 goto out_one_pinned;
4521 }
4522
Tejun Heo969c7922010-05-06 18:49:21 +02004523 /*
4524 * ->active_balance synchronizes accesses to
4525 * ->active_balance_work. Once set, it's cleared
4526 * only after active load balance is finished.
4527 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004528 if (!busiest->active_balance) {
4529 busiest->active_balance = 1;
4530 busiest->push_cpu = this_cpu;
4531 active_balance = 1;
4532 }
4533 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004534
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004535 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02004536 stop_one_cpu_nowait(cpu_of(busiest),
4537 active_load_balance_cpu_stop, busiest,
4538 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004539
4540 /*
4541 * We've kicked active balancing, reset the failure
4542 * counter.
4543 */
4544 sd->nr_balance_failed = sd->cache_nice_tries+1;
4545 }
4546 } else
4547 sd->nr_balance_failed = 0;
4548
4549 if (likely(!active_balance)) {
4550 /* We were unbalanced, so reset the balancing interval */
4551 sd->balance_interval = sd->min_interval;
4552 } else {
4553 /*
4554 * If we've begun active balancing, start to back off. This
4555 * case may not be covered by the all_pinned logic if there
4556 * is only 1 task on the busy runqueue (because we don't call
4557 * move_tasks).
4558 */
4559 if (sd->balance_interval < sd->max_interval)
4560 sd->balance_interval *= 2;
4561 }
4562
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004563 goto out;
4564
4565out_balanced:
4566 schedstat_inc(sd, lb_balanced[idle]);
4567
4568 sd->nr_balance_failed = 0;
4569
4570out_one_pinned:
4571 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004572 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004573 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004574 (sd->balance_interval < sd->max_interval))
4575 sd->balance_interval *= 2;
4576
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004577 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004578out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004579 return ld_moved;
4580}
4581
4582/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004583 * idle_balance is called by schedule() if this_cpu is about to become
4584 * idle. Attempts to pull tasks from other CPUs.
4585 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004586void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004587{
4588 struct sched_domain *sd;
4589 int pulled_task = 0;
4590 unsigned long next_balance = jiffies + HZ;
4591
4592 this_rq->idle_stamp = this_rq->clock;
4593
4594 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4595 return;
4596
Peter Zijlstraf492e122009-12-23 15:29:42 +01004597 /*
4598 * Drop the rq->lock, but keep IRQ/preempt disabled.
4599 */
4600 raw_spin_unlock(&this_rq->lock);
4601
Paul Turnerc66eaf62010-11-15 15:47:07 -08004602 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004603 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004604 for_each_domain(this_cpu, sd) {
4605 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004606 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004607
4608 if (!(sd->flags & SD_LOAD_BALANCE))
4609 continue;
4610
Peter Zijlstraf492e122009-12-23 15:29:42 +01004611 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004612 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004613 pulled_task = load_balance(this_cpu, this_rq,
4614 sd, CPU_NEWLY_IDLE, &balance);
4615 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004616
4617 interval = msecs_to_jiffies(sd->balance_interval);
4618 if (time_after(next_balance, sd->last_balance + interval))
4619 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004620 if (pulled_task) {
4621 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004622 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004623 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004624 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004625 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004626
4627 raw_spin_lock(&this_rq->lock);
4628
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004629 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4630 /*
4631 * We are going idle. next_balance may be set based on
4632 * a busy processor. So reset next_balance.
4633 */
4634 this_rq->next_balance = next_balance;
4635 }
4636}
4637
4638/*
Tejun Heo969c7922010-05-06 18:49:21 +02004639 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4640 * running tasks off the busiest CPU onto idle CPUs. It requires at
4641 * least 1 task to be running on each physical CPU where possible, and
4642 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004643 */
Tejun Heo969c7922010-05-06 18:49:21 +02004644static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004645{
Tejun Heo969c7922010-05-06 18:49:21 +02004646 struct rq *busiest_rq = data;
4647 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004648 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004649 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004650 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004651
4652 raw_spin_lock_irq(&busiest_rq->lock);
4653
4654 /* make sure the requested cpu hasn't gone down in the meantime */
4655 if (unlikely(busiest_cpu != smp_processor_id() ||
4656 !busiest_rq->active_balance))
4657 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004658
4659 /* Is there any task to move? */
4660 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004661 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004662
4663 /*
4664 * This condition is "impossible", if it occurs
4665 * we need to fix it. Originally reported by
4666 * Bjorn Helgaas on a 128-cpu setup.
4667 */
4668 BUG_ON(busiest_rq == target_rq);
4669
4670 /* move a task from busiest_rq to target_rq */
4671 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004672
4673 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004674 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004675 for_each_domain(target_cpu, sd) {
4676 if ((sd->flags & SD_LOAD_BALANCE) &&
4677 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4678 break;
4679 }
4680
4681 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004682 struct lb_env env = {
4683 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004684 .dst_cpu = target_cpu,
4685 .dst_rq = target_rq,
4686 .src_cpu = busiest_rq->cpu,
4687 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004688 .idle = CPU_IDLE,
4689 };
4690
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004691 schedstat_inc(sd, alb_count);
4692
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004693 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004694 schedstat_inc(sd, alb_pushed);
4695 else
4696 schedstat_inc(sd, alb_failed);
4697 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004698 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004699 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004700out_unlock:
4701 busiest_rq->active_balance = 0;
4702 raw_spin_unlock_irq(&busiest_rq->lock);
4703 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004704}
4705
4706#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004707/*
4708 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004709 * - When one of the busy CPUs notice that there may be an idle rebalancing
4710 * needed, they will kick the idle load balancer, which then does idle
4711 * load balancing for all the idle CPUs.
4712 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004713static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004714 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004715 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004716 unsigned long next_balance; /* in jiffy units */
4717} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004718
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004719#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4720/**
4721 * lowest_flag_domain - Return lowest sched_domain containing flag.
4722 * @cpu: The cpu whose lowest level of sched domain is to
4723 * be returned.
4724 * @flag: The flag to check for the lowest sched_domain
4725 * for the given cpu.
4726 *
4727 * Returns the lowest sched_domain of a cpu which contains the given flag.
4728 */
4729static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4730{
4731 struct sched_domain *sd;
4732
4733 for_each_domain(cpu, sd)
Hillf Danton08354712011-06-16 21:55:19 -04004734 if (sd->flags & flag)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004735 break;
4736
4737 return sd;
4738}
4739
4740/**
4741 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4742 * @cpu: The cpu whose domains we're iterating over.
4743 * @sd: variable holding the value of the power_savings_sd
4744 * for cpu.
4745 * @flag: The flag to filter the sched_domains to be iterated.
4746 *
4747 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4748 * set, starting from the lowest sched_domain to the highest.
4749 */
4750#define for_each_flag_domain(cpu, sd, flag) \
4751 for (sd = lowest_flag_domain(cpu, flag); \
4752 (sd && (sd->flags & flag)); sd = sd->parent)
4753
4754/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004755 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4756 * @cpu: The cpu which is nominating a new idle_load_balancer.
4757 *
4758 * Returns: Returns the id of the idle load balancer if it exists,
4759 * Else, returns >= nr_cpu_ids.
4760 *
4761 * This algorithm picks the idle load balancer such that it belongs to a
4762 * semi-idle powersavings sched_domain. The idea is to try and avoid
4763 * completely idle packages/cores just for the purpose of idle load balancing
4764 * when there are other idle cpu's which are better suited for that job.
4765 */
4766static int find_new_ilb(int cpu)
4767{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004768 int ilb = cpumask_first(nohz.idle_cpus_mask);
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004769 struct sched_group *ilbg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004770 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004771
4772 /*
4773 * Have idle load balancer selection from semi-idle packages only
4774 * when power-aware load balancing is enabled
4775 */
4776 if (!(sched_smt_power_savings || sched_mc_power_savings))
4777 goto out_done;
4778
4779 /*
4780 * Optimize for the case when we have no idle CPUs or only one
4781 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4782 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004783 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004784 goto out_done;
4785
Peter Zijlstradce840a2011-04-07 14:09:50 +02004786 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004787 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004788 ilbg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004789
4790 do {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004791 if (ilbg->group_weight !=
4792 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4793 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4794 sched_group_cpus(ilbg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02004795 goto unlock;
4796 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004797
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004798 ilbg = ilbg->next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004799
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004800 } while (ilbg != sd->groups);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004801 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004802unlock:
4803 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004804
4805out_done:
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004806 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4807 return ilb;
4808
4809 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004810}
4811#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4812static inline int find_new_ilb(int call_cpu)
4813{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004814 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004815}
4816#endif
4817
4818/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004819 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4820 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4821 * CPU (if there is one).
4822 */
4823static void nohz_balancer_kick(int cpu)
4824{
4825 int ilb_cpu;
4826
4827 nohz.next_balance++;
4828
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004829 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004830
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004831 if (ilb_cpu >= nr_cpu_ids)
4832 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004833
Suresh Siddhacd490c52011-12-06 11:26:34 -08004834 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004835 return;
4836 /*
4837 * Use smp_send_reschedule() instead of resched_cpu().
4838 * This way we generate a sched IPI on the target cpu which
4839 * is idle. And the softirq performing nohz idle load balance
4840 * will be run before returning from the IPI.
4841 */
4842 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004843 return;
4844}
4845
Suresh Siddha71325962012-01-19 18:28:57 -08004846static inline void clear_nohz_tick_stopped(int cpu)
4847{
4848 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4849 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4850 atomic_dec(&nohz.nr_cpus);
4851 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4852 }
4853}
4854
Suresh Siddha69e1e812011-12-01 17:07:33 -08004855static inline void set_cpu_sd_state_busy(void)
4856{
4857 struct sched_domain *sd;
4858 int cpu = smp_processor_id();
4859
4860 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4861 return;
4862 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4863
4864 rcu_read_lock();
4865 for_each_domain(cpu, sd)
4866 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4867 rcu_read_unlock();
4868}
4869
4870void set_cpu_sd_state_idle(void)
4871{
4872 struct sched_domain *sd;
4873 int cpu = smp_processor_id();
4874
4875 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4876 return;
4877 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4878
4879 rcu_read_lock();
4880 for_each_domain(cpu, sd)
4881 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4882 rcu_read_unlock();
4883}
4884
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004885/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004886 * This routine will record that this cpu is going idle with tick stopped.
4887 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004888 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004889void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004890{
4891 int cpu = smp_processor_id();
4892
Suresh Siddha71325962012-01-19 18:28:57 -08004893 /*
4894 * If this cpu is going down, then nothing needs to be done.
4895 */
4896 if (!cpu_active(cpu))
4897 return;
4898
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004899 if (stop_tick) {
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004900 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004901 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004902
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004903 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004904 atomic_inc(&nohz.nr_cpus);
Suresh Siddha1c792db2011-12-01 17:07:32 -08004905 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004906 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004907 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004908}
Suresh Siddha71325962012-01-19 18:28:57 -08004909
4910static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4911 unsigned long action, void *hcpu)
4912{
4913 switch (action & ~CPU_TASKS_FROZEN) {
4914 case CPU_DYING:
4915 clear_nohz_tick_stopped(smp_processor_id());
4916 return NOTIFY_OK;
4917 default:
4918 return NOTIFY_DONE;
4919 }
4920}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004921#endif
4922
4923static DEFINE_SPINLOCK(balancing);
4924
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004925/*
4926 * Scale the max load_balance interval with the number of CPUs in the system.
4927 * This trades load-balance latency on larger machines for less cross talk.
4928 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004929void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004930{
4931 max_load_balance_interval = HZ*num_online_cpus()/10;
4932}
4933
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004934/*
4935 * It checks each scheduling domain to see if it is due to be balanced,
4936 * and initiates a balancing operation if so.
4937 *
4938 * Balancing parameters are set up in arch_init_sched_domains.
4939 */
4940static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4941{
4942 int balance = 1;
4943 struct rq *rq = cpu_rq(cpu);
4944 unsigned long interval;
4945 struct sched_domain *sd;
4946 /* Earliest time when we have to do rebalance again */
4947 unsigned long next_balance = jiffies + 60*HZ;
4948 int update_next_balance = 0;
4949 int need_serialize;
4950
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004951 update_shares(cpu);
4952
Peter Zijlstradce840a2011-04-07 14:09:50 +02004953 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004954 for_each_domain(cpu, sd) {
4955 if (!(sd->flags & SD_LOAD_BALANCE))
4956 continue;
4957
4958 interval = sd->balance_interval;
4959 if (idle != CPU_IDLE)
4960 interval *= sd->busy_factor;
4961
4962 /* scale ms to jiffies */
4963 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004964 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004965
4966 need_serialize = sd->flags & SD_SERIALIZE;
4967
4968 if (need_serialize) {
4969 if (!spin_trylock(&balancing))
4970 goto out;
4971 }
4972
4973 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4974 if (load_balance(cpu, rq, sd, idle, &balance)) {
4975 /*
4976 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004977 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004978 */
4979 idle = CPU_NOT_IDLE;
4980 }
4981 sd->last_balance = jiffies;
4982 }
4983 if (need_serialize)
4984 spin_unlock(&balancing);
4985out:
4986 if (time_after(next_balance, sd->last_balance + interval)) {
4987 next_balance = sd->last_balance + interval;
4988 update_next_balance = 1;
4989 }
4990
4991 /*
4992 * Stop the load balance at this level. There is another
4993 * CPU in our sched group which is doing load balancing more
4994 * actively.
4995 */
4996 if (!balance)
4997 break;
4998 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004999 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005000
5001 /*
5002 * next_balance will be updated only when there is a need.
5003 * When the cpu is attached to null domain for ex, it will not be
5004 * updated.
5005 */
5006 if (likely(update_next_balance))
5007 rq->next_balance = next_balance;
5008}
5009
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005010#ifdef CONFIG_NO_HZ
5011/*
5012 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5013 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5014 */
5015static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5016{
5017 struct rq *this_rq = cpu_rq(this_cpu);
5018 struct rq *rq;
5019 int balance_cpu;
5020
Suresh Siddha1c792db2011-12-01 17:07:32 -08005021 if (idle != CPU_IDLE ||
5022 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5023 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005024
5025 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005026 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005027 continue;
5028
5029 /*
5030 * If this cpu gets work to do, stop the load balancing
5031 * work being done for other cpus. Next load
5032 * balancing owner will pick it up.
5033 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005034 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005035 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005036
5037 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02005038 update_rq_clock(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005039 update_cpu_load(this_rq);
5040 raw_spin_unlock_irq(&this_rq->lock);
5041
5042 rebalance_domains(balance_cpu, CPU_IDLE);
5043
5044 rq = cpu_rq(balance_cpu);
5045 if (time_after(this_rq->next_balance, rq->next_balance))
5046 this_rq->next_balance = rq->next_balance;
5047 }
5048 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005049end:
5050 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005051}
5052
5053/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005054 * Current heuristic for kicking the idle load balancer in the presence
5055 * of an idle cpu is the system.
5056 * - This rq has more than one task.
5057 * - At any scheduler domain level, this cpu's scheduler group has multiple
5058 * busy cpu's exceeding the group's power.
5059 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5060 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005061 */
5062static inline int nohz_kick_needed(struct rq *rq, int cpu)
5063{
5064 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005065 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005066
Suresh Siddha1c792db2011-12-01 17:07:32 -08005067 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005068 return 0;
5069
Suresh Siddha1c792db2011-12-01 17:07:32 -08005070 /*
5071 * We may be recently in ticked or tickless idle mode. At the first
5072 * busy tick after returning from idle, we will update the busy stats.
5073 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005074 set_cpu_sd_state_busy();
Suresh Siddha71325962012-01-19 18:28:57 -08005075 clear_nohz_tick_stopped(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005076
5077 /*
5078 * None are in tickless mode and hence no need for NOHZ idle load
5079 * balancing.
5080 */
5081 if (likely(!atomic_read(&nohz.nr_cpus)))
5082 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005083
5084 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005085 return 0;
5086
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005087 if (rq->nr_running >= 2)
5088 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005089
Peter Zijlstra067491b2011-12-07 14:32:08 +01005090 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005091 for_each_domain(cpu, sd) {
5092 struct sched_group *sg = sd->groups;
5093 struct sched_group_power *sgp = sg->sgp;
5094 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005095
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005096 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005097 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005098
5099 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5100 && (cpumask_first_and(nohz.idle_cpus_mask,
5101 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005102 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005103
5104 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5105 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005106 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005107 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005108 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005109
5110need_kick_unlock:
5111 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005112need_kick:
5113 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005114}
5115#else
5116static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5117#endif
5118
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005119/*
5120 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005121 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005122 */
5123static void run_rebalance_domains(struct softirq_action *h)
5124{
5125 int this_cpu = smp_processor_id();
5126 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005127 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005128 CPU_IDLE : CPU_NOT_IDLE;
5129
5130 rebalance_domains(this_cpu, idle);
5131
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005132 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005133 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005134 * balancing on behalf of the other idle cpus whose ticks are
5135 * stopped.
5136 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005137 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005138}
5139
5140static inline int on_null_domain(int cpu)
5141{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005142 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005143}
5144
5145/*
5146 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005147 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005148void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005149{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005150 /* Don't need to rebalance while attached to NULL domain */
5151 if (time_after_eq(jiffies, rq->next_balance) &&
5152 likely(!on_null_domain(cpu)))
5153 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005154#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005155 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005156 nohz_balancer_kick(cpu);
5157#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005158}
5159
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005160static void rq_online_fair(struct rq *rq)
5161{
5162 update_sysctl();
5163}
5164
5165static void rq_offline_fair(struct rq *rq)
5166{
5167 update_sysctl();
5168}
5169
Dhaval Giani55e12e52008-06-24 23:39:43 +05305170#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005171
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005172/*
5173 * scheduler tick hitting a task of our scheduling class:
5174 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005175static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005176{
5177 struct cfs_rq *cfs_rq;
5178 struct sched_entity *se = &curr->se;
5179
5180 for_each_sched_entity(se) {
5181 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005182 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005183 }
5184}
5185
5186/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005187 * called on fork with the child task as argument from the parent's context
5188 * - child not yet on the tasklist
5189 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005190 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005191static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005192{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005193 struct cfs_rq *cfs_rq;
5194 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005195 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005196 struct rq *rq = this_rq();
5197 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005198
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005199 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005200
Peter Zijlstra861d0342010-08-19 13:31:43 +02005201 update_rq_clock(rq);
5202
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005203 cfs_rq = task_cfs_rq(current);
5204 curr = cfs_rq->curr;
5205
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005206 if (unlikely(task_cpu(p) != this_cpu)) {
5207 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005208 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005209 rcu_read_unlock();
5210 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005211
Ting Yang7109c442007-08-28 12:53:24 +02005212 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005213
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005214 if (curr)
5215 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005216 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005217
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005218 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005219 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005220 * Upon rescheduling, sched_class::put_prev_task() will place
5221 * 'current' within the tree based on its new key value.
5222 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005223 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305224 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005225 }
5226
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005227 se->vruntime -= cfs_rq->min_vruntime;
5228
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005229 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005230}
5231
Steven Rostedtcb469842008-01-25 21:08:22 +01005232/*
5233 * Priority of the task has changed. Check to see if we preempt
5234 * the current task.
5235 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005236static void
5237prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005238{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005239 if (!p->se.on_rq)
5240 return;
5241
Steven Rostedtcb469842008-01-25 21:08:22 +01005242 /*
5243 * Reschedule if we are currently running on this runqueue and
5244 * our priority decreased, or if we are not currently running on
5245 * this runqueue and our priority is higher than the current's
5246 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005247 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005248 if (p->prio > oldprio)
5249 resched_task(rq->curr);
5250 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005251 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005252}
5253
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005254static void switched_from_fair(struct rq *rq, struct task_struct *p)
5255{
5256 struct sched_entity *se = &p->se;
5257 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5258
5259 /*
5260 * Ensure the task's vruntime is normalized, so that when its
5261 * switched back to the fair class the enqueue_entity(.flags=0) will
5262 * do the right thing.
5263 *
5264 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5265 * have normalized the vruntime, if it was !on_rq, then only when
5266 * the task is sleeping will it still have non-normalized vruntime.
5267 */
5268 if (!se->on_rq && p->state != TASK_RUNNING) {
5269 /*
5270 * Fix up our vruntime so that the current sleep doesn't
5271 * cause 'unlimited' sleep bonus.
5272 */
5273 place_entity(cfs_rq, se, 0);
5274 se->vruntime -= cfs_rq->min_vruntime;
5275 }
5276}
5277
Steven Rostedtcb469842008-01-25 21:08:22 +01005278/*
5279 * We switched to the sched_fair class.
5280 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005281static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005282{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005283 if (!p->se.on_rq)
5284 return;
5285
Steven Rostedtcb469842008-01-25 21:08:22 +01005286 /*
5287 * We were most likely switched from sched_rt, so
5288 * kick off the schedule if running, otherwise just see
5289 * if we can still preempt the current task.
5290 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005291 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005292 resched_task(rq->curr);
5293 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005294 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005295}
5296
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005297/* Account for a task changing its policy or group.
5298 *
5299 * This routine is mostly called to set cfs_rq->curr field when a task
5300 * migrates between groups/classes.
5301 */
5302static void set_curr_task_fair(struct rq *rq)
5303{
5304 struct sched_entity *se = &rq->curr->se;
5305
Paul Turnerec12cb72011-07-21 09:43:30 -07005306 for_each_sched_entity(se) {
5307 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5308
5309 set_next_entity(cfs_rq, se);
5310 /* ensure bandwidth has been allocated on our new cfs_rq */
5311 account_cfs_rq_runtime(cfs_rq, 0);
5312 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005313}
5314
Peter Zijlstra029632f2011-10-25 10:00:11 +02005315void init_cfs_rq(struct cfs_rq *cfs_rq)
5316{
5317 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005318 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5319#ifndef CONFIG_64BIT
5320 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5321#endif
5322}
5323
Peter Zijlstra810b3812008-02-29 15:21:01 -05005324#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005325static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005326{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005327 /*
5328 * If the task was not on the rq at the time of this cgroup movement
5329 * it must have been asleep, sleeping tasks keep their ->vruntime
5330 * absolute on their old rq until wakeup (needed for the fair sleeper
5331 * bonus in place_entity()).
5332 *
5333 * If it was on the rq, we've just 'preempted' it, which does convert
5334 * ->vruntime to a relative base.
5335 *
5336 * Make sure both cases convert their relative position when migrating
5337 * to another cgroup's rq. This does somewhat interfere with the
5338 * fair sleeper stuff for the first placement, but who cares.
5339 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005340 /*
5341 * When !on_rq, vruntime of the task has usually NOT been normalized.
5342 * But there are some cases where it has already been normalized:
5343 *
5344 * - Moving a forked child which is waiting for being woken up by
5345 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005346 * - Moving a task which has been woken up by try_to_wake_up() and
5347 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005348 *
5349 * To prevent boost or penalty in the new cfs_rq caused by delta
5350 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5351 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005352 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005353 on_rq = 1;
5354
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005355 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005356 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5357 set_task_rq(p, task_cpu(p));
5358 if (!on_rq)
5359 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005360}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005361
5362void free_fair_sched_group(struct task_group *tg)
5363{
5364 int i;
5365
5366 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5367
5368 for_each_possible_cpu(i) {
5369 if (tg->cfs_rq)
5370 kfree(tg->cfs_rq[i]);
5371 if (tg->se)
5372 kfree(tg->se[i]);
5373 }
5374
5375 kfree(tg->cfs_rq);
5376 kfree(tg->se);
5377}
5378
5379int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5380{
5381 struct cfs_rq *cfs_rq;
5382 struct sched_entity *se;
5383 int i;
5384
5385 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5386 if (!tg->cfs_rq)
5387 goto err;
5388 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5389 if (!tg->se)
5390 goto err;
5391
5392 tg->shares = NICE_0_LOAD;
5393
5394 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5395
5396 for_each_possible_cpu(i) {
5397 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5398 GFP_KERNEL, cpu_to_node(i));
5399 if (!cfs_rq)
5400 goto err;
5401
5402 se = kzalloc_node(sizeof(struct sched_entity),
5403 GFP_KERNEL, cpu_to_node(i));
5404 if (!se)
5405 goto err_free_rq;
5406
5407 init_cfs_rq(cfs_rq);
5408 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5409 }
5410
5411 return 1;
5412
5413err_free_rq:
5414 kfree(cfs_rq);
5415err:
5416 return 0;
5417}
5418
5419void unregister_fair_sched_group(struct task_group *tg, int cpu)
5420{
5421 struct rq *rq = cpu_rq(cpu);
5422 unsigned long flags;
5423
5424 /*
5425 * Only empty task groups can be destroyed; so we can speculatively
5426 * check on_list without danger of it being re-added.
5427 */
5428 if (!tg->cfs_rq[cpu]->on_list)
5429 return;
5430
5431 raw_spin_lock_irqsave(&rq->lock, flags);
5432 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5433 raw_spin_unlock_irqrestore(&rq->lock, flags);
5434}
5435
5436void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5437 struct sched_entity *se, int cpu,
5438 struct sched_entity *parent)
5439{
5440 struct rq *rq = cpu_rq(cpu);
5441
5442 cfs_rq->tg = tg;
5443 cfs_rq->rq = rq;
5444#ifdef CONFIG_SMP
5445 /* allow initial update_cfs_load() to truncate */
5446 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005447#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005448 init_cfs_rq_runtime(cfs_rq);
5449
5450 tg->cfs_rq[cpu] = cfs_rq;
5451 tg->se[cpu] = se;
5452
5453 /* se could be NULL for root_task_group */
5454 if (!se)
5455 return;
5456
5457 if (!parent)
5458 se->cfs_rq = &rq->cfs;
5459 else
5460 se->cfs_rq = parent->my_q;
5461
5462 se->my_q = cfs_rq;
5463 update_load_set(&se->load, 0);
5464 se->parent = parent;
5465}
5466
5467static DEFINE_MUTEX(shares_mutex);
5468
5469int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5470{
5471 int i;
5472 unsigned long flags;
5473
5474 /*
5475 * We can't change the weight of the root cgroup.
5476 */
5477 if (!tg->se[0])
5478 return -EINVAL;
5479
5480 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5481
5482 mutex_lock(&shares_mutex);
5483 if (tg->shares == shares)
5484 goto done;
5485
5486 tg->shares = shares;
5487 for_each_possible_cpu(i) {
5488 struct rq *rq = cpu_rq(i);
5489 struct sched_entity *se;
5490
5491 se = tg->se[i];
5492 /* Propagate contribution to hierarchy */
5493 raw_spin_lock_irqsave(&rq->lock, flags);
5494 for_each_sched_entity(se)
5495 update_cfs_shares(group_cfs_rq(se));
5496 raw_spin_unlock_irqrestore(&rq->lock, flags);
5497 }
5498
5499done:
5500 mutex_unlock(&shares_mutex);
5501 return 0;
5502}
5503#else /* CONFIG_FAIR_GROUP_SCHED */
5504
5505void free_fair_sched_group(struct task_group *tg) { }
5506
5507int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5508{
5509 return 1;
5510}
5511
5512void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5513
5514#endif /* CONFIG_FAIR_GROUP_SCHED */
5515
Peter Zijlstra810b3812008-02-29 15:21:01 -05005516
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005517static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005518{
5519 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005520 unsigned int rr_interval = 0;
5521
5522 /*
5523 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5524 * idle runqueue:
5525 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005526 if (rq->cfs.load.weight)
5527 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005528
5529 return rr_interval;
5530}
5531
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005532/*
5533 * All the scheduling class methods:
5534 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005535const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005536 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005537 .enqueue_task = enqueue_task_fair,
5538 .dequeue_task = dequeue_task_fair,
5539 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005540 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005541
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005542 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005543
5544 .pick_next_task = pick_next_task_fair,
5545 .put_prev_task = put_prev_task_fair,
5546
Peter Williams681f3e62007-10-24 18:23:51 +02005547#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005548 .select_task_rq = select_task_rq_fair,
5549
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005550 .rq_online = rq_online_fair,
5551 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005552
5553 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005554#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005555
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005556 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005557 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005558 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005559
5560 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005561 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005562 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005563
Peter Williams0d721ce2009-09-21 01:31:53 +00005564 .get_rr_interval = get_rr_interval_fair,
5565
Peter Zijlstra810b3812008-02-29 15:21:01 -05005566#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005567 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005568#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005569};
5570
5571#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005572void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005573{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005574 struct cfs_rq *cfs_rq;
5575
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005576 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005577 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005578 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005579 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005580}
5581#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005582
5583__init void init_sched_fair_class(void)
5584{
5585#ifdef CONFIG_SMP
5586 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5587
5588#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005589 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005590 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005591 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005592#endif
5593#endif /* SMP */
5594
5595}