blob: 4f1bc4b72fe2d6e33435d095b3c5ccc41c595589 [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);
Ben Segalldfb473b2013-10-16 11:16:32 -07001666 if (!cfs_b->timer_active)
1667 __start_cfs_bandwidth(cfs_b);
Paul Turner85dac902011-07-21 09:43:33 -07001668 raw_spin_unlock(&cfs_b->lock);
1669}
1670
Peter Zijlstra029632f2011-10-25 10:00:11 +02001671void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001672{
1673 struct rq *rq = rq_of(cfs_rq);
1674 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1675 struct sched_entity *se;
1676 int enqueue = 1;
1677 long task_delta;
1678
1679 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1680
1681 cfs_rq->throttled = 0;
1682 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001683 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001684 list_del_rcu(&cfs_rq->throttled_list);
1685 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001686 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001687
Paul Turner64660c82011-07-21 09:43:36 -07001688 update_rq_clock(rq);
1689 /* update hierarchical throttle state */
1690 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1691
Paul Turner671fd9d2011-07-21 09:43:34 -07001692 if (!cfs_rq->load.weight)
1693 return;
1694
1695 task_delta = cfs_rq->h_nr_running;
1696 for_each_sched_entity(se) {
1697 if (se->on_rq)
1698 enqueue = 0;
1699
1700 cfs_rq = cfs_rq_of(se);
1701 if (enqueue)
1702 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1703 cfs_rq->h_nr_running += task_delta;
1704
1705 if (cfs_rq_throttled(cfs_rq))
1706 break;
1707 }
1708
1709 if (!se)
1710 rq->nr_running += task_delta;
1711
1712 /* determine whether we need to wake up potentially idle cpu */
1713 if (rq->curr == rq->idle && rq->cfs.nr_running)
1714 resched_task(rq->curr);
1715}
1716
1717static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1718 u64 remaining, u64 expires)
1719{
1720 struct cfs_rq *cfs_rq;
1721 u64 runtime = remaining;
1722
1723 rcu_read_lock();
1724 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1725 throttled_list) {
1726 struct rq *rq = rq_of(cfs_rq);
1727
1728 raw_spin_lock(&rq->lock);
1729 if (!cfs_rq_throttled(cfs_rq))
1730 goto next;
1731
1732 runtime = -cfs_rq->runtime_remaining + 1;
1733 if (runtime > remaining)
1734 runtime = remaining;
1735 remaining -= runtime;
1736
1737 cfs_rq->runtime_remaining += runtime;
1738 cfs_rq->runtime_expires = expires;
1739
1740 /* we check whether we're throttled above */
1741 if (cfs_rq->runtime_remaining > 0)
1742 unthrottle_cfs_rq(cfs_rq);
1743
1744next:
1745 raw_spin_unlock(&rq->lock);
1746
1747 if (!remaining)
1748 break;
1749 }
1750 rcu_read_unlock();
1751
1752 return remaining;
1753}
1754
Paul Turner58088ad2011-07-21 09:43:31 -07001755/*
1756 * Responsible for refilling a task_group's bandwidth and unthrottling its
1757 * cfs_rqs as appropriate. If there has been no activity within the last
1758 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1759 * used to track this state.
1760 */
1761static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1762{
Paul Turner671fd9d2011-07-21 09:43:34 -07001763 u64 runtime, runtime_expires;
1764 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001765
1766 raw_spin_lock(&cfs_b->lock);
1767 /* no need to continue the timer with no bandwidth constraint */
1768 if (cfs_b->quota == RUNTIME_INF)
1769 goto out_unlock;
1770
Paul Turner671fd9d2011-07-21 09:43:34 -07001771 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1772 /* idle depends on !throttled (for the case of a large deficit) */
1773 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001774 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001775
Paul Turnera9cf55b2011-07-21 09:43:32 -07001776 /* if we're going inactive then everything else can be deferred */
1777 if (idle)
1778 goto out_unlock;
1779
1780 __refill_cfs_bandwidth_runtime(cfs_b);
1781
Paul Turner671fd9d2011-07-21 09:43:34 -07001782 if (!throttled) {
1783 /* mark as potentially idle for the upcoming period */
1784 cfs_b->idle = 1;
1785 goto out_unlock;
1786 }
Paul Turner58088ad2011-07-21 09:43:31 -07001787
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001788 /* account preceding periods in which throttling occurred */
1789 cfs_b->nr_throttled += overrun;
1790
Paul Turner671fd9d2011-07-21 09:43:34 -07001791 /*
1792 * There are throttled entities so we must first use the new bandwidth
1793 * to unthrottle them before making it generally available. This
1794 * ensures that all existing debts will be paid before a new cfs_rq is
1795 * allowed to run.
1796 */
1797 runtime = cfs_b->runtime;
1798 runtime_expires = cfs_b->runtime_expires;
1799 cfs_b->runtime = 0;
1800
1801 /*
1802 * This check is repeated as we are holding onto the new bandwidth
1803 * while we unthrottle. This can potentially race with an unthrottled
1804 * group trying to acquire new bandwidth from the global pool.
1805 */
1806 while (throttled && runtime > 0) {
1807 raw_spin_unlock(&cfs_b->lock);
1808 /* we can't nest cfs_b->lock while distributing bandwidth */
1809 runtime = distribute_cfs_runtime(cfs_b, runtime,
1810 runtime_expires);
1811 raw_spin_lock(&cfs_b->lock);
1812
1813 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1814 }
1815
1816 /* return (any) remaining runtime */
1817 cfs_b->runtime = runtime;
1818 /*
1819 * While we are ensured activity in the period following an
1820 * unthrottle, this also covers the case in which the new bandwidth is
1821 * insufficient to cover the existing bandwidth deficit. (Forcing the
1822 * timer to remain active while there are any throttled entities.)
1823 */
1824 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07001825out_unlock:
1826 if (idle)
1827 cfs_b->timer_active = 0;
1828 raw_spin_unlock(&cfs_b->lock);
1829
1830 return idle;
1831}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001832
Paul Turnerd8b49862011-07-21 09:43:41 -07001833/* a cfs_rq won't donate quota below this amount */
1834static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
1835/* minimum remaining period time to redistribute slack quota */
1836static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
1837/* how long we wait to gather additional slack before distributing */
1838static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
1839
1840/* are we near the end of the current quota period? */
1841static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
1842{
1843 struct hrtimer *refresh_timer = &cfs_b->period_timer;
1844 u64 remaining;
1845
1846 /* if the call-back is running a quota refresh is already occurring */
1847 if (hrtimer_callback_running(refresh_timer))
1848 return 1;
1849
1850 /* is a quota refresh about to occur? */
1851 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
1852 if (remaining < min_expire)
1853 return 1;
1854
1855 return 0;
1856}
1857
1858static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
1859{
1860 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
1861
1862 /* if there's a quota refresh soon don't bother with slack */
1863 if (runtime_refresh_within(cfs_b, min_left))
1864 return;
1865
1866 start_bandwidth_timer(&cfs_b->slack_timer,
1867 ns_to_ktime(cfs_bandwidth_slack_period));
1868}
1869
1870/* we know any runtime found here is valid as update_curr() precedes return */
1871static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1872{
1873 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1874 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
1875
1876 if (slack_runtime <= 0)
1877 return;
1878
1879 raw_spin_lock(&cfs_b->lock);
1880 if (cfs_b->quota != RUNTIME_INF &&
1881 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
1882 cfs_b->runtime += slack_runtime;
1883
1884 /* we are under rq->lock, defer unthrottling using a timer */
1885 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
1886 !list_empty(&cfs_b->throttled_cfs_rq))
1887 start_cfs_slack_bandwidth(cfs_b);
1888 }
1889 raw_spin_unlock(&cfs_b->lock);
1890
1891 /* even if it's not valid for return we don't want to try again */
1892 cfs_rq->runtime_remaining -= slack_runtime;
1893}
1894
1895static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1896{
Paul Turner56f570e2011-11-07 20:26:33 -08001897 if (!cfs_bandwidth_used())
1898 return;
1899
Paul Turnerfccfdc62011-11-07 20:26:34 -08001900 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07001901 return;
1902
1903 __return_cfs_rq_runtime(cfs_rq);
1904}
1905
1906/*
1907 * This is done with a timer (instead of inline with bandwidth return) since
1908 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
1909 */
1910static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
1911{
1912 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
1913 u64 expires;
1914
1915 /* confirm we're still not at a refresh boundary */
1916 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
1917 return;
1918
1919 raw_spin_lock(&cfs_b->lock);
1920 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
1921 runtime = cfs_b->runtime;
1922 cfs_b->runtime = 0;
1923 }
1924 expires = cfs_b->runtime_expires;
1925 raw_spin_unlock(&cfs_b->lock);
1926
1927 if (!runtime)
1928 return;
1929
1930 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
1931
1932 raw_spin_lock(&cfs_b->lock);
1933 if (expires == cfs_b->runtime_expires)
1934 cfs_b->runtime = runtime;
1935 raw_spin_unlock(&cfs_b->lock);
1936}
1937
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001938/*
1939 * When a group wakes up we want to make sure that its quota is not already
1940 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
1941 * runtime as update_curr() throttling can not not trigger until it's on-rq.
1942 */
1943static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
1944{
Paul Turner56f570e2011-11-07 20:26:33 -08001945 if (!cfs_bandwidth_used())
1946 return;
1947
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001948 /* an active group must be handled by the update_curr()->put() path */
1949 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
1950 return;
1951
1952 /* ensure the group is not already throttled */
1953 if (cfs_rq_throttled(cfs_rq))
1954 return;
1955
1956 /* update runtime allocation */
1957 account_cfs_rq_runtime(cfs_rq, 0);
1958 if (cfs_rq->runtime_remaining <= 0)
1959 throttle_cfs_rq(cfs_rq);
1960}
1961
1962/* conditionally throttle active cfs_rq's from put_prev_entity() */
1963static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1964{
Paul Turner56f570e2011-11-07 20:26:33 -08001965 if (!cfs_bandwidth_used())
1966 return;
1967
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001968 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
1969 return;
1970
1971 /*
1972 * it's possible for a throttled entity to be forced into a running
1973 * state (e.g. set_curr_task), in this case we're finished.
1974 */
1975 if (cfs_rq_throttled(cfs_rq))
1976 return;
1977
1978 throttle_cfs_rq(cfs_rq);
1979}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001980
1981static inline u64 default_cfs_period(void);
1982static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
1983static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
1984
1985static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
1986{
1987 struct cfs_bandwidth *cfs_b =
1988 container_of(timer, struct cfs_bandwidth, slack_timer);
1989 do_sched_cfs_slack_timer(cfs_b);
1990
1991 return HRTIMER_NORESTART;
1992}
1993
1994static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
1995{
1996 struct cfs_bandwidth *cfs_b =
1997 container_of(timer, struct cfs_bandwidth, period_timer);
1998 ktime_t now;
1999 int overrun;
2000 int idle = 0;
2001
2002 for (;;) {
2003 now = hrtimer_cb_get_time(timer);
2004 overrun = hrtimer_forward(timer, now, cfs_b->period);
2005
2006 if (!overrun)
2007 break;
2008
2009 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2010 }
2011
2012 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2013}
2014
2015void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2016{
2017 raw_spin_lock_init(&cfs_b->lock);
2018 cfs_b->runtime = 0;
2019 cfs_b->quota = RUNTIME_INF;
2020 cfs_b->period = ns_to_ktime(default_cfs_period());
2021
2022 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2023 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2024 cfs_b->period_timer.function = sched_cfs_period_timer;
2025 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2026 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2027}
2028
2029static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2030{
2031 cfs_rq->runtime_enabled = 0;
2032 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2033}
2034
2035/* requires cfs_b->lock, may release to reprogram timer */
2036void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2037{
2038 /*
2039 * The timer may be active because we're trying to set a new bandwidth
2040 * period or because we're racing with the tear-down path
2041 * (timer_active==0 becomes visible before the hrtimer call-back
2042 * terminates). In either case we ensure that it's re-programmed
2043 */
2044 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2045 raw_spin_unlock(&cfs_b->lock);
2046 /* ensure cfs_b->lock is available while we wait */
2047 hrtimer_cancel(&cfs_b->period_timer);
2048
2049 raw_spin_lock(&cfs_b->lock);
2050 /* if someone else restarted the timer then we're done */
2051 if (cfs_b->timer_active)
2052 return;
2053 }
2054
2055 cfs_b->timer_active = 1;
2056 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2057}
2058
2059static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2060{
2061 hrtimer_cancel(&cfs_b->period_timer);
2062 hrtimer_cancel(&cfs_b->slack_timer);
2063}
2064
2065void unthrottle_offline_cfs_rqs(struct rq *rq)
2066{
2067 struct cfs_rq *cfs_rq;
2068
2069 for_each_leaf_cfs_rq(rq, cfs_rq) {
2070 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2071
2072 if (!cfs_rq->runtime_enabled)
2073 continue;
2074
2075 /*
2076 * clock_task is not advancing so we just need to make sure
2077 * there's some valid quota amount
2078 */
2079 cfs_rq->runtime_remaining = cfs_b->quota;
2080 if (cfs_rq_throttled(cfs_rq))
2081 unthrottle_cfs_rq(cfs_rq);
2082 }
2083}
2084
2085#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002086static __always_inline
2087void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002088static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2089static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002090static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002091
2092static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2093{
2094 return 0;
2095}
Paul Turner64660c82011-07-21 09:43:36 -07002096
2097static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2098{
2099 return 0;
2100}
2101
2102static inline int throttled_lb_pair(struct task_group *tg,
2103 int src_cpu, int dest_cpu)
2104{
2105 return 0;
2106}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002107
2108void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2109
2110#ifdef CONFIG_FAIR_GROUP_SCHED
2111static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002112#endif
2113
Peter Zijlstra029632f2011-10-25 10:00:11 +02002114static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2115{
2116 return NULL;
2117}
2118static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2119void unthrottle_offline_cfs_rqs(struct rq *rq) {}
2120
2121#endif /* CONFIG_CFS_BANDWIDTH */
2122
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002123/**************************************************
2124 * CFS operations on tasks:
2125 */
2126
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002127#ifdef CONFIG_SCHED_HRTICK
2128static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2129{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002130 struct sched_entity *se = &p->se;
2131 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2132
2133 WARN_ON(task_rq(p) != rq);
2134
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002135 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002136 u64 slice = sched_slice(cfs_rq, se);
2137 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2138 s64 delta = slice - ran;
2139
2140 if (delta < 0) {
2141 if (rq->curr == p)
2142 resched_task(p);
2143 return;
2144 }
2145
2146 /*
2147 * Don't schedule slices shorter than 10000ns, that just
2148 * doesn't make sense. Rely on vruntime for fairness.
2149 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002150 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002151 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002152
Peter Zijlstra31656512008-07-18 18:01:23 +02002153 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002154 }
2155}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002156
2157/*
2158 * called from enqueue/dequeue and updates the hrtick when the
2159 * current task is from our class and nr_running is low enough
2160 * to matter.
2161 */
2162static void hrtick_update(struct rq *rq)
2163{
2164 struct task_struct *curr = rq->curr;
2165
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002166 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002167 return;
2168
2169 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2170 hrtick_start_fair(rq, curr);
2171}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302172#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002173static inline void
2174hrtick_start_fair(struct rq *rq, struct task_struct *p)
2175{
2176}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002177
2178static inline void hrtick_update(struct rq *rq)
2179{
2180}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002181#endif
2182
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002183/*
2184 * The enqueue_task method is called before nr_running is
2185 * increased. Here we update the fair scheduling stats and
2186 * then put the task into the rbtree:
2187 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002188static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002189enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002190{
2191 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002192 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002193
2194 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002195 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002196 break;
2197 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002198 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002199
2200 /*
2201 * end evaluation on encountering a throttled cfs_rq
2202 *
2203 * note: in the case of encountering a throttled cfs_rq we will
2204 * post the final h_nr_running increment below.
2205 */
2206 if (cfs_rq_throttled(cfs_rq))
2207 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002208 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002209
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002210 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002211 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002212
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002213 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002214 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002215 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002216
Paul Turner85dac902011-07-21 09:43:33 -07002217 if (cfs_rq_throttled(cfs_rq))
2218 break;
2219
Paul Turnerd6b55912010-11-15 15:47:09 -08002220 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002221 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002222 }
2223
Paul Turner85dac902011-07-21 09:43:33 -07002224 if (!se)
2225 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002226 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002227}
2228
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002229static void set_next_buddy(struct sched_entity *se);
2230
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002231/*
2232 * The dequeue_task method is called before nr_running is
2233 * decreased. We remove the task from the rbtree and
2234 * update the fair scheduling stats:
2235 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002236static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002237{
2238 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002239 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002240 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002241
2242 for_each_sched_entity(se) {
2243 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002244 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002245
2246 /*
2247 * end evaluation on encountering a throttled cfs_rq
2248 *
2249 * note: in the case of encountering a throttled cfs_rq we will
2250 * post the final h_nr_running decrement below.
2251 */
2252 if (cfs_rq_throttled(cfs_rq))
2253 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002254 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002255
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002256 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002257 if (cfs_rq->load.weight) {
2258 /*
2259 * Bias pick_next to pick a task from this cfs_rq, as
2260 * p is sleeping when it is within its sched_slice.
2261 */
2262 if (task_sleep && parent_entity(se))
2263 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002264
2265 /* avoid re-evaluating load for this entity */
2266 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002267 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002268 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002269 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002270 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002271
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002272 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002273 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002274 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002275
Paul Turner85dac902011-07-21 09:43:33 -07002276 if (cfs_rq_throttled(cfs_rq))
2277 break;
2278
Paul Turnerd6b55912010-11-15 15:47:09 -08002279 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002280 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002281 }
2282
Paul Turner85dac902011-07-21 09:43:33 -07002283 if (!se)
2284 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002285 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002286}
2287
Gregory Haskinse7693a32008-01-25 21:08:09 +01002288#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002289/* Used instead of source_load when we know the type == 0 */
2290static unsigned long weighted_cpuload(const int cpu)
2291{
2292 return cpu_rq(cpu)->load.weight;
2293}
2294
2295/*
2296 * Return a low guess at the load of a migration-source cpu weighted
2297 * according to the scheduling class and "nice" value.
2298 *
2299 * We want to under-estimate the load of migration sources, to
2300 * balance conservatively.
2301 */
2302static unsigned long source_load(int cpu, int type)
2303{
2304 struct rq *rq = cpu_rq(cpu);
2305 unsigned long total = weighted_cpuload(cpu);
2306
2307 if (type == 0 || !sched_feat(LB_BIAS))
2308 return total;
2309
2310 return min(rq->cpu_load[type-1], total);
2311}
2312
2313/*
2314 * Return a high guess at the load of a migration-target cpu weighted
2315 * according to the scheduling class and "nice" value.
2316 */
2317static unsigned long target_load(int cpu, int type)
2318{
2319 struct rq *rq = cpu_rq(cpu);
2320 unsigned long total = weighted_cpuload(cpu);
2321
2322 if (type == 0 || !sched_feat(LB_BIAS))
2323 return total;
2324
2325 return max(rq->cpu_load[type-1], total);
2326}
2327
2328static unsigned long power_of(int cpu)
2329{
2330 return cpu_rq(cpu)->cpu_power;
2331}
2332
2333static unsigned long cpu_avg_load_per_task(int cpu)
2334{
2335 struct rq *rq = cpu_rq(cpu);
2336 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2337
2338 if (nr_running)
2339 return rq->load.weight / nr_running;
2340
2341 return 0;
2342}
2343
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002344
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002345static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002346{
2347 struct sched_entity *se = &p->se;
2348 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002349 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002350
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002351#ifndef CONFIG_64BIT
2352 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002353
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002354 do {
2355 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2356 smp_rmb();
2357 min_vruntime = cfs_rq->min_vruntime;
2358 } while (min_vruntime != min_vruntime_copy);
2359#else
2360 min_vruntime = cfs_rq->min_vruntime;
2361#endif
2362
2363 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002364}
2365
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002366#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002367/*
2368 * effective_load() calculates the load change as seen from the root_task_group
2369 *
2370 * Adding load to a group doesn't make a group heavier, but can cause movement
2371 * of group shares between cpus. Assuming the shares were perfectly aligned one
2372 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002373 *
2374 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2375 * on this @cpu and results in a total addition (subtraction) of @wg to the
2376 * total group weight.
2377 *
2378 * Given a runqueue weight distribution (rw_i) we can compute a shares
2379 * distribution (s_i) using:
2380 *
2381 * s_i = rw_i / \Sum rw_j (1)
2382 *
2383 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2384 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2385 * shares distribution (s_i):
2386 *
2387 * rw_i = { 2, 4, 1, 0 }
2388 * s_i = { 2/7, 4/7, 1/7, 0 }
2389 *
2390 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2391 * task used to run on and the CPU the waker is running on), we need to
2392 * compute the effect of waking a task on either CPU and, in case of a sync
2393 * wakeup, compute the effect of the current task going to sleep.
2394 *
2395 * So for a change of @wl to the local @cpu with an overall group weight change
2396 * of @wl we can compute the new shares distribution (s'_i) using:
2397 *
2398 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2399 *
2400 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2401 * differences in waking a task to CPU 0. The additional task changes the
2402 * weight and shares distributions like:
2403 *
2404 * rw'_i = { 3, 4, 1, 0 }
2405 * s'_i = { 3/8, 4/8, 1/8, 0 }
2406 *
2407 * We can then compute the difference in effective weight by using:
2408 *
2409 * dw_i = S * (s'_i - s_i) (3)
2410 *
2411 * Where 'S' is the group weight as seen by its parent.
2412 *
2413 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2414 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2415 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002416 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002417static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002418{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002419 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002420
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002421 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002422 return wl;
2423
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002424 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002425 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002426
Paul Turner977dda72011-01-14 17:57:50 -08002427 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002428
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002429 /*
2430 * W = @wg + \Sum rw_j
2431 */
2432 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002433
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002434 /*
2435 * w = rw_i + @wl
2436 */
2437 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002438
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002439 /*
2440 * wl = S * s'_i; see (2)
2441 */
2442 if (W > 0 && w < W)
2443 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002444 else
2445 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002446
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002447 /*
2448 * Per the above, wl is the new se->load.weight value; since
2449 * those are clipped to [MIN_SHARES, ...) do so now. See
2450 * calc_cfs_shares().
2451 */
Paul Turner977dda72011-01-14 17:57:50 -08002452 if (wl < MIN_SHARES)
2453 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002454
2455 /*
2456 * wl = dw_i = S * (s'_i - s_i); see (3)
2457 */
Paul Turner977dda72011-01-14 17:57:50 -08002458 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002459
2460 /*
2461 * Recursively apply this logic to all parent groups to compute
2462 * the final effective load change on the root group. Since
2463 * only the @tg group gets extra weight, all parent groups can
2464 * only redistribute existing shares. @wl is the shift in shares
2465 * resulting from this level per the above.
2466 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002467 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002468 }
2469
2470 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002471}
2472#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002473
Peter Zijlstra83378262008-06-27 13:41:37 +02002474static inline unsigned long effective_load(struct task_group *tg, int cpu,
2475 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002476{
Peter Zijlstra83378262008-06-27 13:41:37 +02002477 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002478}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002479
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002480#endif
2481
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002482static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002483{
Paul Turnere37b6a72011-01-21 20:44:59 -08002484 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002485 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002486 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002487 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002488 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002489 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002490
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002491 idx = sd->wake_idx;
2492 this_cpu = smp_processor_id();
2493 prev_cpu = task_cpu(p);
2494 load = source_load(prev_cpu, idx);
2495 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002496
2497 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002498 * If sync wakeup then subtract the (maximum possible)
2499 * effect of the currently running task from the load
2500 * of the current CPU:
2501 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002502 if (sync) {
2503 tg = task_group(current);
2504 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002505
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002506 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002507 load += effective_load(tg, prev_cpu, 0, -weight);
2508 }
2509
2510 tg = task_group(p);
2511 weight = p->se.load.weight;
2512
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002513 /*
2514 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002515 * due to the sync cause above having dropped this_load to 0, we'll
2516 * always have an imbalance, but there's really nothing you can do
2517 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002518 *
2519 * Otherwise check if either cpus are near enough in load to allow this
2520 * task to be woken on this_cpu.
2521 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002522 if (this_load > 0) {
2523 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002524
2525 this_eff_load = 100;
2526 this_eff_load *= power_of(prev_cpu);
2527 this_eff_load *= this_load +
2528 effective_load(tg, this_cpu, weight, weight);
2529
2530 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2531 prev_eff_load *= power_of(this_cpu);
2532 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2533
2534 balanced = this_eff_load <= prev_eff_load;
2535 } else
2536 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002537
2538 /*
2539 * If the currently running task will sleep within
2540 * a reasonable amount of time then attract this newly
2541 * woken task:
2542 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002543 if (sync && balanced)
2544 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002545
Lucas De Marchi41acab82010-03-10 23:37:45 -03002546 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002547 tl_per_task = cpu_avg_load_per_task(this_cpu);
2548
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002549 if (balanced ||
2550 (this_load <= load &&
2551 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002552 /*
2553 * This domain has SD_WAKE_AFFINE and
2554 * p is cache cold in this domain, and
2555 * there is no bad imbalance.
2556 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002557 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002558 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002559
2560 return 1;
2561 }
2562 return 0;
2563}
2564
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002565/*
2566 * find_idlest_group finds and returns the least busy CPU group within the
2567 * domain.
2568 */
2569static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002570find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002571 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002572{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002573 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002574 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002575 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002576
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002577 do {
2578 unsigned long load, avg_load;
2579 int local_group;
2580 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002581
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002582 /* Skip over this group if it has no CPUs allowed */
2583 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002584 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002585 continue;
2586
2587 local_group = cpumask_test_cpu(this_cpu,
2588 sched_group_cpus(group));
2589
2590 /* Tally up the load of all CPUs in the group */
2591 avg_load = 0;
2592
2593 for_each_cpu(i, sched_group_cpus(group)) {
2594 /* Bias balancing toward cpus of our domain */
2595 if (local_group)
2596 load = source_load(i, load_idx);
2597 else
2598 load = target_load(i, load_idx);
2599
2600 avg_load += load;
2601 }
2602
2603 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002604 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002605
2606 if (local_group) {
2607 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002608 } else if (avg_load < min_load) {
2609 min_load = avg_load;
2610 idlest = group;
2611 }
2612 } while (group = group->next, group != sd->groups);
2613
2614 if (!idlest || 100*this_load < imbalance*min_load)
2615 return NULL;
2616 return idlest;
2617}
2618
2619/*
2620 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2621 */
2622static int
2623find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2624{
2625 unsigned long load, min_load = ULONG_MAX;
2626 int idlest = -1;
2627 int i;
2628
2629 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002630 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002631 load = weighted_cpuload(i);
2632
2633 if (load < min_load || (load == min_load && i == this_cpu)) {
2634 min_load = load;
2635 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002636 }
2637 }
2638
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002639 return idlest;
2640}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002641
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002642/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002643 * Try and locate an idle CPU in the sched_domain.
2644 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002645static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002646{
2647 int cpu = smp_processor_id();
2648 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002649 struct sched_domain *sd;
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002650 struct sched_group *sg;
Suresh Siddha77e81362011-11-17 11:08:23 -08002651 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002652
2653 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002654 * If the task is going to be woken-up on this cpu and if it is
2655 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002656 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002657 if (target == cpu && idle_cpu(cpu))
2658 return cpu;
2659
2660 /*
2661 * If the task is going to be woken-up on the cpu where it previously
2662 * ran and if it is currently idle, then it the right target.
2663 */
2664 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002665 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002666
Steve Muckled448aba2012-10-24 15:00:20 -07002667 if (!sysctl_sched_wake_to_idle &&
2668 !(current->flags & PF_WAKE_UP_IDLE) &&
Steve Muckleff96cd22012-09-10 11:45:02 -07002669 !(p->flags & PF_WAKE_UP_IDLE))
2670 return target;
2671
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002672 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002673 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002674 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002675 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002676 for_each_lower_domain(sd) {
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002677 sg = sd->groups;
2678 do {
2679 if (!cpumask_intersects(sched_group_cpus(sg),
2680 tsk_cpus_allowed(p)))
2681 goto next;
2682
2683 for_each_cpu(i, sched_group_cpus(sg)) {
2684 if (!idle_cpu(i))
2685 goto next;
2686 }
2687
2688 target = cpumask_first_and(sched_group_cpus(sg),
2689 tsk_cpus_allowed(p));
2690 goto done;
2691next:
2692 sg = sg->next;
2693 } while (sg != sd->groups);
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002694 }
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002695done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002696 return target;
2697}
2698
2699/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002700 * sched_balance_self: balance the current task (running on cpu) in domains
2701 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2702 * SD_BALANCE_EXEC.
2703 *
2704 * Balance, ie. select the least loaded group.
2705 *
2706 * Returns the target CPU number, or the same CPU if no balancing is needed.
2707 *
2708 * preempt must be disabled.
2709 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002710static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002711select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002712{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002713 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002714 int cpu = smp_processor_id();
2715 int prev_cpu = task_cpu(p);
2716 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002717 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002718 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002719 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002720
Mike Galbraith76854c72011-11-22 15:18:24 +01002721 if (p->rt.nr_cpus_allowed == 1)
2722 return prev_cpu;
2723
Peter Zijlstra0763a662009-09-14 19:37:39 +02002724 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002725 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002726 want_affine = 1;
2727 new_cpu = prev_cpu;
2728 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002729
Peter Zijlstradce840a2011-04-07 14:09:50 +02002730 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002731 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002732 if (!(tmp->flags & SD_LOAD_BALANCE))
2733 continue;
2734
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002735 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02002736 * If power savings logic is enabled for a domain, see if we
2737 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002738 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02002739 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02002740 unsigned long power = 0;
2741 unsigned long nr_running = 0;
2742 unsigned long capacity;
2743 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002744
Peter Zijlstraae154be2009-09-10 14:40:57 +02002745 for_each_cpu(i, sched_domain_span(tmp)) {
2746 power += power_of(i);
2747 nr_running += cpu_rq(i)->cfs.nr_running;
2748 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002749
Nikhil Rao1399fa72011-05-18 10:09:39 -07002750 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002751
Peter Zijlstra59abf022009-09-16 08:28:30 +02002752 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2753 nr_running /= 2;
2754
2755 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002756 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002757 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002758
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002759 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002760 * If both cpu and prev_cpu are part of this domain,
2761 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002762 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002763 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2764 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2765 affine_sd = tmp;
2766 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002767 }
2768
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002769 if (!want_sd && !want_affine)
2770 break;
2771
Peter Zijlstra0763a662009-09-14 19:37:39 +02002772 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002773 continue;
2774
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002775 if (want_sd)
2776 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002777 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002778
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002779 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002780 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002781 prev_cpu = cpu;
2782
2783 new_cpu = select_idle_sibling(p, prev_cpu);
2784 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002785 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002786
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002787 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002788 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002789 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002790 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002791
Peter Zijlstra0763a662009-09-14 19:37:39 +02002792 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002793 sd = sd->child;
2794 continue;
2795 }
2796
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002797 if (sd_flag & SD_BALANCE_WAKE)
2798 load_idx = sd->wake_idx;
2799
2800 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002801 if (!group) {
2802 sd = sd->child;
2803 continue;
2804 }
2805
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002806 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002807 if (new_cpu == -1 || new_cpu == cpu) {
2808 /* Now try balancing at a lower domain level of cpu */
2809 sd = sd->child;
2810 continue;
2811 }
2812
2813 /* Now try balancing at a lower domain level of new_cpu */
2814 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002815 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002816 sd = NULL;
2817 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002818 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002819 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002820 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002821 sd = tmp;
2822 }
2823 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002824 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002825unlock:
2826 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002827
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002828 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002829}
2830#endif /* CONFIG_SMP */
2831
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002832static unsigned long
2833wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002834{
2835 unsigned long gran = sysctl_sched_wakeup_granularity;
2836
2837 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002838 * Since its curr running now, convert the gran from real-time
2839 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002840 *
2841 * By using 'se' instead of 'curr' we penalize light tasks, so
2842 * they get preempted easier. That is, if 'se' < 'curr' then
2843 * the resulting gran will be larger, therefore penalizing the
2844 * lighter, if otoh 'se' > 'curr' then the resulting gran will
2845 * be smaller, again penalizing the lighter task.
2846 *
2847 * This is especially important for buddies when the leftmost
2848 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002849 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08002850 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002851}
2852
2853/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02002854 * Should 'se' preempt 'curr'.
2855 *
2856 * |s1
2857 * |s2
2858 * |s3
2859 * g
2860 * |<--->|c
2861 *
2862 * w(c, s1) = -1
2863 * w(c, s2) = 0
2864 * w(c, s3) = 1
2865 *
2866 */
2867static int
2868wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2869{
2870 s64 gran, vdiff = curr->vruntime - se->vruntime;
2871
2872 if (vdiff <= 0)
2873 return -1;
2874
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002875 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02002876 if (vdiff > gran)
2877 return 1;
2878
2879 return 0;
2880}
2881
Peter Zijlstra02479092008-11-04 21:25:10 +01002882static void set_last_buddy(struct sched_entity *se)
2883{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002884 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2885 return;
2886
2887 for_each_sched_entity(se)
2888 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002889}
2890
2891static void set_next_buddy(struct sched_entity *se)
2892{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002893 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2894 return;
2895
2896 for_each_sched_entity(se)
2897 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002898}
2899
Rik van Rielac53db52011-02-01 09:51:03 -05002900static void set_skip_buddy(struct sched_entity *se)
2901{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002902 for_each_sched_entity(se)
2903 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05002904}
2905
Peter Zijlstra464b7522008-10-24 11:06:15 +02002906/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002907 * Preempt the current task with a newly woken task if needed:
2908 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02002909static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002910{
2911 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02002912 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002913 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002914 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002915 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002916
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002917 if (unlikely(se == pse))
2918 return;
2919
Paul Turner5238cdd2011-07-21 09:43:37 -07002920 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01002921 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07002922 * unconditionally check_prempt_curr() after an enqueue (which may have
2923 * lead to a throttle). This both saves work and prevents false
2924 * next-buddy nomination below.
2925 */
2926 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
2927 return;
2928
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002929 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02002930 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002931 next_buddy_marked = 1;
2932 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02002933
Bharata B Raoaec0a512008-08-28 14:42:49 +05302934 /*
2935 * We can come here with TIF_NEED_RESCHED already set from new task
2936 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07002937 *
2938 * Note: this also catches the edge-case of curr being in a throttled
2939 * group (e.g. via set_curr_task), since update_curr() (in the
2940 * enqueue of curr) will have resulted in resched being set. This
2941 * prevents us from potentially nominating it as a false LAST_BUDDY
2942 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05302943 */
2944 if (test_tsk_need_resched(curr))
2945 return;
2946
Darren Harta2f5c9a2011-02-22 13:04:33 -08002947 /* Idle tasks are by definition preempted by non-idle tasks. */
2948 if (unlikely(curr->policy == SCHED_IDLE) &&
2949 likely(p->policy != SCHED_IDLE))
2950 goto preempt;
2951
Ingo Molnar91c234b2007-10-15 17:00:18 +02002952 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08002953 * Batch and idle tasks do not preempt non-idle tasks (their preemption
2954 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02002955 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01002956 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02002957 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002958
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002959 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07002960 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002961 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002962 if (wakeup_preempt_entity(se, pse) == 1) {
2963 /*
2964 * Bias pick_next to pick the sched entity that is
2965 * triggering this preemption.
2966 */
2967 if (!next_buddy_marked)
2968 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002969 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002970 }
Jupyung Leea65ac742009-11-17 18:51:40 +09002971
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002972 return;
2973
2974preempt:
2975 resched_task(curr);
2976 /*
2977 * Only set the backward buddy when the current task is still
2978 * on the rq. This can happen when a wakeup gets interleaved
2979 * with schedule on the ->pre_schedule() or idle_balance()
2980 * point, either of which can * drop the rq lock.
2981 *
2982 * Also, during early boot the idle thread is in the fair class,
2983 * for obvious reasons its a bad idea to schedule back to it.
2984 */
2985 if (unlikely(!se->on_rq || curr == rq->idle))
2986 return;
2987
2988 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
2989 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002990}
2991
Ingo Molnarfb8d4722007-08-09 11:16:48 +02002992static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002993{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002994 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002995 struct cfs_rq *cfs_rq = &rq->cfs;
2996 struct sched_entity *se;
2997
Tim Blechmann36ace272009-11-24 11:55:45 +01002998 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002999 return NULL;
3000
3001 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003002 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003003 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003004 cfs_rq = group_cfs_rq(se);
3005 } while (cfs_rq);
3006
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003007 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003008 if (hrtick_enabled(rq))
3009 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003010
3011 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003012}
3013
3014/*
3015 * Account for a descheduled task:
3016 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003017static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003018{
3019 struct sched_entity *se = &prev->se;
3020 struct cfs_rq *cfs_rq;
3021
3022 for_each_sched_entity(se) {
3023 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003024 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003025 }
3026}
3027
Rik van Rielac53db52011-02-01 09:51:03 -05003028/*
3029 * sched_yield() is very simple
3030 *
3031 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3032 */
3033static void yield_task_fair(struct rq *rq)
3034{
3035 struct task_struct *curr = rq->curr;
3036 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3037 struct sched_entity *se = &curr->se;
3038
3039 /*
3040 * Are we the only task in the tree?
3041 */
3042 if (unlikely(rq->nr_running == 1))
3043 return;
3044
3045 clear_buddies(cfs_rq, se);
3046
3047 if (curr->policy != SCHED_BATCH) {
3048 update_rq_clock(rq);
3049 /*
3050 * Update run-time statistics of the 'current'.
3051 */
3052 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003053 /*
3054 * Tell update_rq_clock() that we've just updated,
3055 * so we don't do microscopic update in schedule()
3056 * and double the fastpath cost.
3057 */
3058 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003059 }
3060
3061 set_skip_buddy(se);
3062}
3063
Mike Galbraithd95f4122011-02-01 09:50:51 -05003064static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3065{
3066 struct sched_entity *se = &p->se;
3067
Paul Turner5238cdd2011-07-21 09:43:37 -07003068 /* throttled hierarchies are not runnable */
3069 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003070 return false;
3071
3072 /* Tell the scheduler that we'd really like pse to run next. */
3073 set_next_buddy(se);
3074
Mike Galbraithd95f4122011-02-01 09:50:51 -05003075 yield_task_fair(rq);
3076
3077 return true;
3078}
3079
Peter Williams681f3e62007-10-24 18:23:51 +02003080#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003081/**************************************************
3082 * Fair scheduling class load-balancing methods:
3083 */
3084
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003085static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3086
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003087#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003088#define LBF_NEED_BREAK 0x02
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003089
3090struct lb_env {
3091 struct sched_domain *sd;
3092
3093 int src_cpu;
3094 struct rq *src_rq;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003095
3096 int dst_cpu;
3097 struct rq *dst_rq;
3098
3099 enum cpu_idle_type idle;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003100 long load_move;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003101 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003102
3103 unsigned int loop;
3104 unsigned int loop_break;
3105 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003106};
3107
Steve Muckle8f77c282013-03-11 16:33:42 -07003108static DEFINE_PER_CPU(bool, dbs_boost_needed);
3109
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003110/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003111 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003112 * Both runqueues must be locked.
3113 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003114static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003115{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003116 deactivate_task(env->src_rq, p, 0);
3117 set_task_cpu(p, env->dst_cpu);
3118 activate_task(env->dst_rq, p, 0);
3119 check_preempt_curr(env->dst_rq, p, 0);
Steve Muckle8f77c282013-03-11 16:33:42 -07003120 if (task_notify_on_migrate(p))
3121 per_cpu(dbs_boost_needed, env->dst_cpu) = true;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003122}
3123
3124/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003125 * Is this task likely cache-hot:
3126 */
3127static int
3128task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3129{
3130 s64 delta;
3131
3132 if (p->sched_class != &fair_sched_class)
3133 return 0;
3134
3135 if (unlikely(p->policy == SCHED_IDLE))
3136 return 0;
3137
3138 /*
3139 * Buddy candidates are cache hot:
3140 */
3141 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3142 (&p->se == cfs_rq_of(&p->se)->next ||
3143 &p->se == cfs_rq_of(&p->se)->last))
3144 return 1;
3145
3146 if (sysctl_sched_migration_cost == -1)
3147 return 1;
3148 if (sysctl_sched_migration_cost == 0)
3149 return 0;
3150
3151 delta = now - p->se.exec_start;
3152
3153 return delta < (s64)sysctl_sched_migration_cost;
3154}
3155
3156/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003157 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3158 */
3159static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003160int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003161{
3162 int tsk_cache_hot = 0;
3163 /*
3164 * We do not migrate tasks that are:
3165 * 1) running (obviously), or
3166 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3167 * 3) are cache-hot on their current CPU.
3168 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003169 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003170 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003171 return 0;
3172 }
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003173 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003174
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003175 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003176 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003177 return 0;
3178 }
3179
3180 /*
3181 * Aggressive migration if:
3182 * 1) task is cache cold, or
3183 * 2) too many balance attempts have failed.
3184 */
3185
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003186 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003187 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003188 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003189#ifdef CONFIG_SCHEDSTATS
3190 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003191 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003192 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003193 }
3194#endif
3195 return 1;
3196 }
3197
3198 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003199 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003200 return 0;
3201 }
3202 return 1;
3203}
3204
Peter Zijlstra897c3952009-12-17 17:45:42 +01003205/*
3206 * move_one_task tries to move exactly one task from busiest to this_rq, as
3207 * part of active balancing operations within "domain".
3208 * Returns 1 if successful and 0 otherwise.
3209 *
3210 * Called with both runqueues locked.
3211 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003212static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003213{
3214 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003215
Peter Zijlstra367456c2012-02-20 21:49:09 +01003216 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3217 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3218 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003219
Peter Zijlstra367456c2012-02-20 21:49:09 +01003220 if (!can_migrate_task(p, env))
3221 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003222
Peter Zijlstra367456c2012-02-20 21:49:09 +01003223 move_task(p, env);
3224 /*
3225 * Right now, this is only the second place move_task()
3226 * is called, so we can safely collect move_task()
3227 * stats here rather than inside move_task().
3228 */
3229 schedstat_inc(env->sd, lb_gained[env->idle]);
3230 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003231 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003232 return 0;
3233}
3234
Peter Zijlstra367456c2012-02-20 21:49:09 +01003235static unsigned long task_h_load(struct task_struct *p);
3236
Peter Zijlstraeb953082012-04-17 13:38:40 +02003237static const unsigned int sched_nr_migrate_break = 32;
3238
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003239/*
3240 * move_tasks tries to move up to load_move weighted load from busiest to
3241 * this_rq, as part of a balancing operation within domain "sd".
3242 * Returns 1 if successful and 0 otherwise.
3243 *
3244 * Called with both runqueues locked.
3245 */
3246static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003247{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003248 struct list_head *tasks = &env->src_rq->cfs_tasks;
3249 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003250 unsigned long load;
3251 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003252
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003253 if (env->load_move <= 0)
3254 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003255
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003256 while (!list_empty(tasks)) {
3257 p = list_first_entry(tasks, struct task_struct, se.group_node);
3258
Peter Zijlstra367456c2012-02-20 21:49:09 +01003259 env->loop++;
3260 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003261 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003262 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003263
3264 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003265 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003266 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003267 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003268 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003269 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003270
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003271 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003272 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003273
Peter Zijlstra367456c2012-02-20 21:49:09 +01003274 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003275
Peter Zijlstraeb953082012-04-17 13:38:40 +02003276 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003277 goto next;
3278
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003279 if ((load / 2) > env->load_move)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003280 goto next;
3281
3282 if (!can_migrate_task(p, env))
3283 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003284
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003285 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003286 pulled++;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003287 env->load_move -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003288
3289#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003290 /*
3291 * NEWIDLE balancing is a source of latency, so preemptible
3292 * kernels will stop after the first task is pulled to minimize
3293 * the critical section.
3294 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003295 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003296 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003297#endif
3298
Peter Zijlstraee00e662009-12-17 17:25:20 +01003299 /*
3300 * We only want to steal up to the prescribed amount of
3301 * weighted load.
3302 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003303 if (env->load_move <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003304 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003305
Peter Zijlstra367456c2012-02-20 21:49:09 +01003306 continue;
3307next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003308 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003309 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003310
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003311 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003312 * Right now, this is one of only two places move_task() is called,
3313 * so we can safely collect move_task() stats here rather than
3314 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003315 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003316 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003317
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003318 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003319}
3320
Peter Zijlstra230059de2009-12-17 17:47:12 +01003321#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003322/*
3323 * update tg->load_weight by folding this cpu's load_avg
3324 */
Paul Turner67e86252010-11-15 15:47:05 -08003325static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003326{
3327 struct cfs_rq *cfs_rq;
3328 unsigned long flags;
3329 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003330
3331 if (!tg->se[cpu])
3332 return 0;
3333
3334 rq = cpu_rq(cpu);
3335 cfs_rq = tg->cfs_rq[cpu];
3336
3337 raw_spin_lock_irqsave(&rq->lock, flags);
3338
3339 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003340 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003341
3342 /*
3343 * We need to update shares after updating tg->load_weight in
3344 * order to adjust the weight of groups with long running tasks.
3345 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003346 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003347
3348 raw_spin_unlock_irqrestore(&rq->lock, flags);
3349
3350 return 0;
3351}
3352
3353static void update_shares(int cpu)
3354{
3355 struct cfs_rq *cfs_rq;
3356 struct rq *rq = cpu_rq(cpu);
3357
3358 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003359 /*
3360 * Iterates the task_group tree in a bottom up fashion, see
3361 * list_add_leaf_cfs_rq() for details.
3362 */
Paul Turner64660c82011-07-21 09:43:36 -07003363 for_each_leaf_cfs_rq(rq, cfs_rq) {
3364 /* throttled entities do not contribute to load */
3365 if (throttled_hierarchy(cfs_rq))
3366 continue;
3367
Paul Turner67e86252010-11-15 15:47:05 -08003368 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003369 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003370 rcu_read_unlock();
3371}
3372
Peter Zijlstra9763b672011-07-13 13:09:25 +02003373/*
3374 * Compute the cpu's hierarchical load factor for each task group.
3375 * This needs to be done in a top-down fashion because the load of a child
3376 * group is a fraction of its parents load.
3377 */
3378static int tg_load_down(struct task_group *tg, void *data)
3379{
3380 unsigned long load;
3381 long cpu = (long)data;
3382
3383 if (!tg->parent) {
3384 load = cpu_rq(cpu)->load.weight;
3385 } else {
3386 load = tg->parent->cfs_rq[cpu]->h_load;
3387 load *= tg->se[cpu]->load.weight;
3388 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3389 }
3390
3391 tg->cfs_rq[cpu]->h_load = load;
3392
3393 return 0;
3394}
3395
3396static void update_h_load(long cpu)
3397{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003398 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003399 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003400 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003401}
3402
Peter Zijlstra367456c2012-02-20 21:49:09 +01003403static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003404{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003405 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3406 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003407
Peter Zijlstra367456c2012-02-20 21:49:09 +01003408 load = p->se.load.weight;
3409 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003410
Peter Zijlstra367456c2012-02-20 21:49:09 +01003411 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003412}
3413#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003414static inline void update_shares(int cpu)
3415{
3416}
3417
Peter Zijlstra367456c2012-02-20 21:49:09 +01003418static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003419{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003420}
3421
3422static unsigned long task_h_load(struct task_struct *p)
3423{
3424 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003425}
3426#endif
3427
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003428/********** Helpers for find_busiest_group ************************/
3429/*
3430 * sd_lb_stats - Structure to store the statistics of a sched_domain
3431 * during load balancing.
3432 */
3433struct sd_lb_stats {
3434 struct sched_group *busiest; /* Busiest group in this sd */
3435 struct sched_group *this; /* Local group in this sd */
3436 unsigned long total_load; /* Total load of all groups in sd */
3437 unsigned long total_pwr; /* Total power of all groups in sd */
3438 unsigned long avg_load; /* Average load across all groups in sd */
3439
3440 /** Statistics of this group */
3441 unsigned long this_load;
3442 unsigned long this_load_per_task;
3443 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003444 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003445 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003446
3447 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003448 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003449 unsigned long max_load;
3450 unsigned long busiest_load_per_task;
3451 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003452 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003453 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003454 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003455
3456 int group_imb; /* Is there imbalance in this sd */
3457#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3458 int power_savings_balance; /* Is powersave balance needed for this sd */
3459 struct sched_group *group_min; /* Least loaded group in sd */
3460 struct sched_group *group_leader; /* Group which relieves group_min */
3461 unsigned long min_load_per_task; /* load_per_task in group_min */
3462 unsigned long leader_nr_running; /* Nr running of group_leader */
3463 unsigned long min_nr_running; /* Nr running of group_min */
3464#endif
3465};
3466
3467/*
3468 * sg_lb_stats - stats of a sched_group required for load_balancing
3469 */
3470struct sg_lb_stats {
3471 unsigned long avg_load; /*Avg load across the CPUs of the group */
3472 unsigned long group_load; /* Total load over the CPUs of the group */
3473 unsigned long sum_nr_running; /* Nr tasks running in the group */
3474 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3475 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003476 unsigned long idle_cpus;
3477 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003478 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003479 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003480};
3481
3482/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003483 * get_sd_load_idx - Obtain the load index for a given sched domain.
3484 * @sd: The sched_domain whose load_idx is to be obtained.
3485 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3486 */
3487static inline int get_sd_load_idx(struct sched_domain *sd,
3488 enum cpu_idle_type idle)
3489{
3490 int load_idx;
3491
3492 switch (idle) {
3493 case CPU_NOT_IDLE:
3494 load_idx = sd->busy_idx;
3495 break;
3496
3497 case CPU_NEWLY_IDLE:
3498 load_idx = sd->newidle_idx;
3499 break;
3500 default:
3501 load_idx = sd->idle_idx;
3502 break;
3503 }
3504
3505 return load_idx;
3506}
3507
3508
3509#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3510/**
3511 * init_sd_power_savings_stats - Initialize power savings statistics for
3512 * the given sched_domain, during load balancing.
3513 *
3514 * @sd: Sched domain whose power-savings statistics are to be initialized.
3515 * @sds: Variable containing the statistics for sd.
3516 * @idle: Idle status of the CPU at which we're performing load-balancing.
3517 */
3518static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3519 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3520{
3521 /*
3522 * Busy processors will not participate in power savings
3523 * balance.
3524 */
3525 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3526 sds->power_savings_balance = 0;
3527 else {
3528 sds->power_savings_balance = 1;
3529 sds->min_nr_running = ULONG_MAX;
3530 sds->leader_nr_running = 0;
3531 }
3532}
3533
3534/**
3535 * update_sd_power_savings_stats - Update the power saving stats for a
3536 * sched_domain while performing load balancing.
3537 *
3538 * @group: sched_group belonging to the sched_domain under consideration.
3539 * @sds: Variable containing the statistics of the sched_domain
3540 * @local_group: Does group contain the CPU for which we're performing
3541 * load balancing ?
3542 * @sgs: Variable containing the statistics of the group.
3543 */
3544static inline void update_sd_power_savings_stats(struct sched_group *group,
3545 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3546{
3547
3548 if (!sds->power_savings_balance)
3549 return;
3550
3551 /*
3552 * If the local group is idle or completely loaded
3553 * no need to do power savings balance at this domain
3554 */
3555 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3556 !sds->this_nr_running))
3557 sds->power_savings_balance = 0;
3558
3559 /*
3560 * If a group is already running at full capacity or idle,
3561 * don't include that group in power savings calculations
3562 */
3563 if (!sds->power_savings_balance ||
3564 sgs->sum_nr_running >= sgs->group_capacity ||
3565 !sgs->sum_nr_running)
3566 return;
3567
3568 /*
3569 * Calculate the group which has the least non-idle load.
3570 * This is the group from where we need to pick up the load
3571 * for saving power
3572 */
3573 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3574 (sgs->sum_nr_running == sds->min_nr_running &&
3575 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3576 sds->group_min = group;
3577 sds->min_nr_running = sgs->sum_nr_running;
3578 sds->min_load_per_task = sgs->sum_weighted_load /
3579 sgs->sum_nr_running;
3580 }
3581
3582 /*
3583 * Calculate the group which is almost near its
3584 * capacity but still has some space to pick up some load
3585 * from other group and save more power
3586 */
3587 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3588 return;
3589
3590 if (sgs->sum_nr_running > sds->leader_nr_running ||
3591 (sgs->sum_nr_running == sds->leader_nr_running &&
3592 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3593 sds->group_leader = group;
3594 sds->leader_nr_running = sgs->sum_nr_running;
3595 }
3596}
3597
3598/**
3599 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3600 * @sds: Variable containing the statistics of the sched_domain
3601 * under consideration.
3602 * @this_cpu: Cpu at which we're currently performing load-balancing.
3603 * @imbalance: Variable to store the imbalance.
3604 *
3605 * Description:
3606 * Check if we have potential to perform some power-savings balance.
3607 * If yes, set the busiest group to be the least loaded group in the
3608 * sched_domain, so that it's CPUs can be put to idle.
3609 *
3610 * Returns 1 if there is potential to perform power-savings balance.
3611 * Else returns 0.
3612 */
3613static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3614 int this_cpu, unsigned long *imbalance)
3615{
3616 if (!sds->power_savings_balance)
3617 return 0;
3618
3619 if (sds->this != sds->group_leader ||
3620 sds->group_leader == sds->group_min)
3621 return 0;
3622
3623 *imbalance = sds->min_load_per_task;
3624 sds->busiest = sds->group_min;
3625
3626 return 1;
3627
3628}
3629#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3630static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3631 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3632{
3633 return;
3634}
3635
3636static inline void update_sd_power_savings_stats(struct sched_group *group,
3637 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3638{
3639 return;
3640}
3641
3642static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3643 int this_cpu, unsigned long *imbalance)
3644{
3645 return 0;
3646}
3647#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3648
3649
3650unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3651{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003652 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003653}
3654
3655unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3656{
3657 return default_scale_freq_power(sd, cpu);
3658}
3659
3660unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3661{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003662 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003663 unsigned long smt_gain = sd->smt_gain;
3664
3665 smt_gain /= weight;
3666
3667 return smt_gain;
3668}
3669
3670unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3671{
3672 return default_scale_smt_power(sd, cpu);
3673}
3674
3675unsigned long scale_rt_power(int cpu)
3676{
3677 struct rq *rq = cpu_rq(cpu);
3678 u64 total, available;
3679
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003680 total = sched_avg_period() + (rq->clock - rq->age_stamp);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003681
3682 if (unlikely(total < rq->rt_avg)) {
3683 /* Ensures that power won't end up being negative */
3684 available = 0;
3685 } else {
3686 available = total - rq->rt_avg;
3687 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003688
Nikhil Rao1399fa72011-05-18 10:09:39 -07003689 if (unlikely((s64)total < SCHED_POWER_SCALE))
3690 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003691
Nikhil Rao1399fa72011-05-18 10:09:39 -07003692 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003693
3694 return div_u64(available, total);
3695}
3696
3697static void update_cpu_power(struct sched_domain *sd, int cpu)
3698{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003699 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003700 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003701 struct sched_group *sdg = sd->groups;
3702
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003703 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3704 if (sched_feat(ARCH_POWER))
3705 power *= arch_scale_smt_power(sd, cpu);
3706 else
3707 power *= default_scale_smt_power(sd, cpu);
3708
Nikhil Rao1399fa72011-05-18 10:09:39 -07003709 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003710 }
3711
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003712 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003713
3714 if (sched_feat(ARCH_POWER))
3715 power *= arch_scale_freq_power(sd, cpu);
3716 else
3717 power *= default_scale_freq_power(sd, cpu);
3718
Nikhil Rao1399fa72011-05-18 10:09:39 -07003719 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003720
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003721 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003722 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003723
3724 if (!power)
3725 power = 1;
3726
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003727 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003728 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003729}
3730
Peter Zijlstra029632f2011-10-25 10:00:11 +02003731void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003732{
3733 struct sched_domain *child = sd->child;
3734 struct sched_group *group, *sdg = sd->groups;
3735 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003736 unsigned long interval;
3737
3738 interval = msecs_to_jiffies(sd->balance_interval);
3739 interval = clamp(interval, 1UL, max_load_balance_interval);
3740 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003741
3742 if (!child) {
3743 update_cpu_power(sd, cpu);
3744 return;
3745 }
3746
3747 power = 0;
3748
3749 group = child->groups;
3750 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003751 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003752 group = group->next;
3753 } while (group != child->groups);
3754
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003755 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003756}
3757
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003758/*
3759 * Try and fix up capacity for tiny siblings, this is needed when
3760 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3761 * which on its own isn't powerful enough.
3762 *
3763 * See update_sd_pick_busiest() and check_asym_packing().
3764 */
3765static inline int
3766fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3767{
3768 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003769 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003770 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003771 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003772 return 0;
3773
3774 /*
3775 * If ~90% of the cpu_power is still there, we're good.
3776 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003777 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003778 return 1;
3779
3780 return 0;
3781}
3782
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003783/**
3784 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3785 * @sd: The sched_domain whose statistics are to be updated.
3786 * @group: sched_group whose statistics are to be updated.
3787 * @this_cpu: Cpu for which load balance is currently performed.
3788 * @idle: Idle status of this_cpu
3789 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003790 * @local_group: Does group contain this_cpu.
3791 * @cpus: Set of cpus considered for load balancing.
3792 * @balance: Should we balance.
3793 * @sgs: variable to hold the statistics for this group.
3794 */
3795static inline void update_sg_lb_stats(struct sched_domain *sd,
3796 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003797 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003798 int local_group, const struct cpumask *cpus,
3799 int *balance, struct sg_lb_stats *sgs)
3800{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003801 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003802 int i;
3803 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003804 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003805
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003806 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003807 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003808
3809 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003810 max_cpu_load = 0;
3811 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003812 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003813
3814 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3815 struct rq *rq = cpu_rq(i);
3816
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003817 /* Bias balancing toward cpus of our domain */
3818 if (local_group) {
3819 if (idle_cpu(i) && !first_idle_cpu) {
3820 first_idle_cpu = 1;
3821 balance_cpu = i;
3822 }
3823
3824 load = target_load(i, load_idx);
3825 } else {
3826 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003827 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003828 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003829 max_nr_running = rq->nr_running;
3830 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003831 if (min_cpu_load > load)
3832 min_cpu_load = load;
3833 }
3834
3835 sgs->group_load += load;
3836 sgs->sum_nr_running += rq->nr_running;
3837 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003838 if (idle_cpu(i))
3839 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003840 }
3841
3842 /*
3843 * First idle cpu or the first cpu(busiest) in this sched group
3844 * is eligible for doing load balancing at this and above
3845 * domains. In the newly idle case, we will allow all the cpu's
3846 * to do the newly idle load balance.
3847 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003848 if (local_group) {
3849 if (idle != CPU_NEWLY_IDLE) {
3850 if (balance_cpu != this_cpu) {
3851 *balance = 0;
3852 return;
3853 }
3854 update_group_power(sd, this_cpu);
3855 } else if (time_after_eq(jiffies, group->sgp->next_update))
3856 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003857 }
3858
3859 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003860 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003861
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003862 /*
3863 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003864 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003865 *
3866 * APZ: with cgroup the avg task weight can vary wildly and
3867 * might not be a suitable number - should we keep a
3868 * normalized nr_running number somewhere that negates
3869 * the hierarchy?
3870 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003871 if (sgs->sum_nr_running)
3872 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003873
Peter Zijlstra866ab432011-02-21 18:56:47 +01003874 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003875 sgs->group_imb = 1;
3876
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003877 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003878 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003879 if (!sgs->group_capacity)
3880 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003881 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003882
3883 if (sgs->group_capacity > sgs->sum_nr_running)
3884 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003885}
3886
3887/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003888 * update_sd_pick_busiest - return 1 on busiest group
3889 * @sd: sched_domain whose statistics are to be checked
3890 * @sds: sched_domain statistics
3891 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003892 * @sgs: sched_group statistics
3893 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10003894 *
3895 * Determine if @sg is a busier group than the previously selected
3896 * busiest group.
3897 */
3898static bool update_sd_pick_busiest(struct sched_domain *sd,
3899 struct sd_lb_stats *sds,
3900 struct sched_group *sg,
3901 struct sg_lb_stats *sgs,
3902 int this_cpu)
3903{
3904 if (sgs->avg_load <= sds->max_load)
3905 return false;
3906
3907 if (sgs->sum_nr_running > sgs->group_capacity)
3908 return true;
3909
3910 if (sgs->group_imb)
3911 return true;
3912
3913 /*
3914 * ASYM_PACKING needs to move all the work to the lowest
3915 * numbered CPUs in the group, therefore mark all groups
3916 * higher than ourself as busy.
3917 */
3918 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3919 this_cpu < group_first_cpu(sg)) {
3920 if (!sds->busiest)
3921 return true;
3922
3923 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3924 return true;
3925 }
3926
3927 return false;
3928}
3929
3930/**
Hui Kang461819a2011-10-11 23:00:59 -04003931 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003932 * @sd: sched_domain whose statistics are to be updated.
3933 * @this_cpu: Cpu for which load balance is currently performed.
3934 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003935 * @cpus: Set of cpus considered for load balancing.
3936 * @balance: Should we balance.
3937 * @sds: variable to hold the statistics for this sched_domain.
3938 */
3939static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003940 enum cpu_idle_type idle, const struct cpumask *cpus,
3941 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003942{
3943 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003944 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003945 struct sg_lb_stats sgs;
3946 int load_idx, prefer_sibling = 0;
3947
3948 if (child && child->flags & SD_PREFER_SIBLING)
3949 prefer_sibling = 1;
3950
3951 init_sd_power_savings_stats(sd, sds, idle);
3952 load_idx = get_sd_load_idx(sd, idle);
3953
3954 do {
3955 int local_group;
3956
Michael Neuling532cb4c2010-06-08 14:57:02 +10003957 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003958 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003959 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003960 local_group, cpus, balance, &sgs);
3961
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003962 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003963 return;
3964
3965 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003966 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003967
3968 /*
3969 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10003970 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07003971 * and move all the excess tasks away. We lower the capacity
3972 * of a group only if the local group has the capacity to fit
3973 * these excess tasks, i.e. nr_running < group_capacity. The
3974 * extra check prevents the case where you always pull from the
3975 * heaviest group when it is already under-utilized (possible
3976 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003977 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07003978 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003979 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3980
3981 if (local_group) {
3982 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003983 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003984 sds->this_nr_running = sgs.sum_nr_running;
3985 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003986 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003987 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003988 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003989 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003990 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003991 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003992 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003993 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003994 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003995 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003996 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003997 sds->group_imb = sgs.group_imb;
3998 }
3999
Michael Neuling532cb4c2010-06-08 14:57:02 +10004000 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
4001 sg = sg->next;
4002 } while (sg != sd->groups);
4003}
4004
Michael Neuling532cb4c2010-06-08 14:57:02 +10004005/**
4006 * check_asym_packing - Check to see if the group is packed into the
4007 * sched doman.
4008 *
4009 * This is primarily intended to used at the sibling level. Some
4010 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4011 * case of POWER7, it can move to lower SMT modes only when higher
4012 * threads are idle. When in lower SMT modes, the threads will
4013 * perform better since they share less core resources. Hence when we
4014 * have idle threads, we want them to be the higher ones.
4015 *
4016 * This packing function is run on idle threads. It checks to see if
4017 * the busiest CPU in this domain (core in the P7 case) has a higher
4018 * CPU number than the packing function is being run on. Here we are
4019 * assuming lower CPU number will be equivalent to lower a SMT thread
4020 * number.
4021 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004022 * Returns 1 when packing is required and a task should be moved to
4023 * this CPU. The amount of the imbalance is returned in *imbalance.
4024 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10004025 * @sd: The sched_domain whose packing is to be checked.
4026 * @sds: Statistics of the sched_domain which is to be packed
4027 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4028 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004029 */
4030static int check_asym_packing(struct sched_domain *sd,
4031 struct sd_lb_stats *sds,
4032 int this_cpu, unsigned long *imbalance)
4033{
4034 int busiest_cpu;
4035
4036 if (!(sd->flags & SD_ASYM_PACKING))
4037 return 0;
4038
4039 if (!sds->busiest)
4040 return 0;
4041
4042 busiest_cpu = group_first_cpu(sds->busiest);
4043 if (this_cpu > busiest_cpu)
4044 return 0;
4045
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004046 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004047 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004048 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004049}
4050
4051/**
4052 * fix_small_imbalance - Calculate the minor imbalance that exists
4053 * amongst the groups of a sched_domain, during
4054 * load balancing.
4055 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4056 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4057 * @imbalance: Variable to store the imbalance.
4058 */
4059static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4060 int this_cpu, unsigned long *imbalance)
4061{
4062 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4063 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004064 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004065
4066 if (sds->this_nr_running) {
4067 sds->this_load_per_task /= sds->this_nr_running;
4068 if (sds->busiest_load_per_task >
4069 sds->this_load_per_task)
4070 imbn = 1;
4071 } else
4072 sds->this_load_per_task =
4073 cpu_avg_load_per_task(this_cpu);
4074
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004075 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004076 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004077 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004078
4079 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4080 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004081 *imbalance = sds->busiest_load_per_task;
4082 return;
4083 }
4084
4085 /*
4086 * OK, we don't have enough imbalance to justify moving tasks,
4087 * however we may be able to increase total CPU power used by
4088 * moving them.
4089 */
4090
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004091 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004092 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004093 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004095 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004096
4097 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004098 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004099 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004100 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004101 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004102 min(sds->busiest_load_per_task, sds->max_load - tmp);
4103
4104 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004105 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004106 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004107 tmp = (sds->max_load * sds->busiest->sgp->power) /
4108 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004109 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004110 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004111 sds->this->sgp->power;
4112 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004113 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004114 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004115
4116 /* Move if we gain throughput */
4117 if (pwr_move > pwr_now)
4118 *imbalance = sds->busiest_load_per_task;
4119}
4120
4121/**
4122 * calculate_imbalance - Calculate the amount of imbalance present within the
4123 * groups of a given sched_domain during load balance.
4124 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4125 * @this_cpu: Cpu for which currently load balance is being performed.
4126 * @imbalance: The variable to store the imbalance.
4127 */
4128static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4129 unsigned long *imbalance)
4130{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004131 unsigned long max_pull, load_above_capacity = ~0UL;
4132
4133 sds->busiest_load_per_task /= sds->busiest_nr_running;
4134 if (sds->group_imb) {
4135 sds->busiest_load_per_task =
4136 min(sds->busiest_load_per_task, sds->avg_load);
4137 }
4138
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004139 /*
4140 * In the presence of smp nice balancing, certain scenarios can have
4141 * max load less than avg load(as we skip the groups at or below
4142 * its cpu_power, while calculating max_load..)
4143 */
4144 if (sds->max_load < sds->avg_load) {
4145 *imbalance = 0;
4146 return fix_small_imbalance(sds, this_cpu, imbalance);
4147 }
4148
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004149 if (!sds->group_imb) {
4150 /*
4151 * Don't want to pull so many tasks that a group would go idle.
4152 */
4153 load_above_capacity = (sds->busiest_nr_running -
4154 sds->busiest_group_capacity);
4155
Nikhil Rao1399fa72011-05-18 10:09:39 -07004156 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004157
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004158 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004159 }
4160
4161 /*
4162 * We're trying to get all the cpus to the average_load, so we don't
4163 * want to push ourselves above the average load, nor do we wish to
4164 * reduce the max loaded cpu below the average load. At the same time,
4165 * we also don't want to reduce the group load below the group capacity
4166 * (so that we can implement power-savings policies etc). Thus we look
4167 * for the minimum possible imbalance.
4168 * Be careful of negative numbers as they'll appear as very large values
4169 * with unsigned longs.
4170 */
4171 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004172
4173 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004174 *imbalance = min(max_pull * sds->busiest->sgp->power,
4175 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004176 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004177
4178 /*
4179 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004180 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004181 * a think about bumping its value to force at least one task to be
4182 * moved
4183 */
4184 if (*imbalance < sds->busiest_load_per_task)
4185 return fix_small_imbalance(sds, this_cpu, imbalance);
4186
4187}
Nikhil Raofab47622010-10-15 13:12:29 -07004188
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004189/******* find_busiest_group() helpers end here *********************/
4190
4191/**
4192 * find_busiest_group - Returns the busiest group within the sched_domain
4193 * if there is an imbalance. If there isn't an imbalance, and
4194 * the user has opted for power-savings, it returns a group whose
4195 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4196 * such a group exists.
4197 *
4198 * Also calculates the amount of weighted load which should be moved
4199 * to restore balance.
4200 *
4201 * @sd: The sched_domain whose busiest group is to be returned.
4202 * @this_cpu: The cpu for which load balancing is currently being performed.
4203 * @imbalance: Variable which stores amount of weighted load which should
4204 * be moved to restore balance/put a group to idle.
4205 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004206 * @cpus: The set of CPUs under consideration for load-balancing.
4207 * @balance: Pointer to a variable indicating if this_cpu
4208 * is the appropriate cpu to perform load balancing at this_level.
4209 *
4210 * Returns: - the busiest group if imbalance exists.
4211 * - If no imbalance and user has opted for power-savings balance,
4212 * return the least loaded group whose CPUs can be
4213 * put to idle by rebalancing its tasks onto our group.
4214 */
4215static struct sched_group *
4216find_busiest_group(struct sched_domain *sd, int this_cpu,
4217 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004218 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004219{
4220 struct sd_lb_stats sds;
4221
4222 memset(&sds, 0, sizeof(sds));
4223
4224 /*
4225 * Compute the various statistics relavent for load balancing at
4226 * this level.
4227 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004228 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004229
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004230 /*
4231 * this_cpu is not the appropriate cpu to perform load balancing at
4232 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004233 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004234 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004235 goto ret;
4236
Michael Neuling532cb4c2010-06-08 14:57:02 +10004237 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
4238 check_asym_packing(sd, &sds, this_cpu, imbalance))
4239 return sds.busiest;
4240
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004241 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004242 if (!sds.busiest || sds.busiest_nr_running == 0)
4243 goto out_balanced;
4244
Nikhil Rao1399fa72011-05-18 10:09:39 -07004245 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004246
Peter Zijlstra866ab432011-02-21 18:56:47 +01004247 /*
4248 * If the busiest group is imbalanced the below checks don't
4249 * work because they assumes all things are equal, which typically
4250 * isn't true due to cpus_allowed constraints and the like.
4251 */
4252 if (sds.group_imb)
4253 goto force_balance;
4254
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004255 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07004256 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4257 !sds.busiest_has_capacity)
4258 goto force_balance;
4259
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004260 /*
4261 * If the local group is more busy than the selected busiest group
4262 * don't try and pull any tasks.
4263 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004264 if (sds.this_load >= sds.max_load)
4265 goto out_balanced;
4266
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004267 /*
4268 * Don't pull any tasks if this group is already above the domain
4269 * average load.
4270 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004271 if (sds.this_load >= sds.avg_load)
4272 goto out_balanced;
4273
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004274 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004275 /*
4276 * This cpu is idle. If the busiest group load doesn't
4277 * have more tasks than the number of available cpu's and
4278 * there is no imbalance between this and busiest group
4279 * wrt to idle cpu's, it is balanced.
4280 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004281 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004282 sds.busiest_nr_running <= sds.busiest_group_weight)
4283 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004284 } else {
4285 /*
4286 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4287 * imbalance_pct to be conservative.
4288 */
4289 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4290 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004291 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004292
Nikhil Raofab47622010-10-15 13:12:29 -07004293force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004294 /* Looks like there is an imbalance. Compute it */
4295 calculate_imbalance(&sds, this_cpu, imbalance);
4296 return sds.busiest;
4297
4298out_balanced:
4299 /*
4300 * There is no obvious imbalance. But check if we can do some balancing
4301 * to save power.
4302 */
4303 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4304 return sds.busiest;
4305ret:
4306 *imbalance = 0;
4307 return NULL;
4308}
4309
4310/*
4311 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4312 */
4313static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004314find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4315 enum cpu_idle_type idle, unsigned long imbalance,
4316 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004317{
4318 struct rq *busiest = NULL, *rq;
4319 unsigned long max_load = 0;
4320 int i;
4321
4322 for_each_cpu(i, sched_group_cpus(group)) {
4323 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004324 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4325 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004326 unsigned long wl;
4327
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004328 if (!capacity)
4329 capacity = fix_small_capacity(sd, group);
4330
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004331 if (!cpumask_test_cpu(i, cpus))
4332 continue;
4333
4334 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004335 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004336
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004337 /*
4338 * When comparing with imbalance, use weighted_cpuload()
4339 * which is not scaled with the cpu power.
4340 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004341 if (capacity && rq->nr_running == 1 && wl > imbalance)
4342 continue;
4343
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004344 /*
4345 * For the load comparisons with the other cpu's, consider
4346 * the weighted_cpuload() scaled with the cpu power, so that
4347 * the load can be moved away from the cpu that is potentially
4348 * running at a lower capacity.
4349 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004350 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004351
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004352 if (wl > max_load) {
4353 max_load = wl;
4354 busiest = rq;
4355 }
4356 }
4357
4358 return busiest;
4359}
4360
4361/*
4362 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4363 * so long as it is large enough.
4364 */
4365#define MAX_PINNED_INTERVAL 512
4366
4367/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004368DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004369
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004370static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004371 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004372{
4373 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004374
4375 /*
4376 * ASYM_PACKING needs to force migrate tasks from busy but
4377 * higher numbered CPUs in order to pack all tasks in the
4378 * lowest numbered CPUs.
4379 */
4380 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
4381 return 1;
4382
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004383 /*
4384 * The only task running in a non-idle cpu can be moved to this
4385 * cpu in an attempt to completely freeup the other CPU
4386 * package.
4387 *
4388 * The package power saving logic comes from
4389 * find_busiest_group(). If there are no imbalance, then
4390 * f_b_g() will return NULL. However when sched_mc={1,2} then
4391 * f_b_g() will select a group from which a running task may be
4392 * pulled to this cpu in order to make the other package idle.
4393 * If there is no opportunity to make a package idle and if
4394 * there are no imbalance, then f_b_g() will return NULL and no
4395 * action will be taken in load_balance_newidle().
4396 *
4397 * Under normal task pull operation due to imbalance, there
4398 * will be more than one task in the source run queue and
4399 * move_tasks() will succeed. ld_moved will be true and this
4400 * active balance code will not be triggered.
4401 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004402 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4403 return 0;
4404 }
4405
4406 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4407}
4408
Tejun Heo969c7922010-05-06 18:49:21 +02004409static int active_load_balance_cpu_stop(void *data);
4410
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004411/*
4412 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4413 * tasks if there is an imbalance.
4414 */
4415static int load_balance(int this_cpu, struct rq *this_rq,
4416 struct sched_domain *sd, enum cpu_idle_type idle,
4417 int *balance)
4418{
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004419 int ld_moved, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004420 struct sched_group *group;
4421 unsigned long imbalance;
4422 struct rq *busiest;
4423 unsigned long flags;
4424 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4425
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004426 struct lb_env env = {
4427 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004428 .dst_cpu = this_cpu,
4429 .dst_rq = this_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004430 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004431 .loop_break = sched_nr_migrate_break,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004432 };
4433
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004434 cpumask_copy(cpus, cpu_active_mask);
4435
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004436 schedstat_inc(sd, lb_count[idle]);
4437
4438redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004439 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004440 cpus, balance);
4441
4442 if (*balance == 0)
4443 goto out_balanced;
4444
4445 if (!group) {
4446 schedstat_inc(sd, lb_nobusyg[idle]);
4447 goto out_balanced;
4448 }
4449
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004450 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004451 if (!busiest) {
4452 schedstat_inc(sd, lb_nobusyq[idle]);
4453 goto out_balanced;
4454 }
4455
4456 BUG_ON(busiest == this_rq);
4457
4458 schedstat_add(sd, lb_imbalance[idle], imbalance);
4459
4460 ld_moved = 0;
4461 if (busiest->nr_running > 1) {
4462 /*
4463 * Attempt to move tasks. If find_busiest_group has found
4464 * an imbalance but busiest->nr_running <= 1, the group is
4465 * still unbalanced. ld_moved simply stays zero, so it is
4466 * correctly treated as an imbalance.
4467 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004468 env.flags |= LBF_ALL_PINNED;
Peter Zijlstraeb953082012-04-17 13:38:40 +02004469 env.load_move = imbalance;
4470 env.src_cpu = busiest->cpu;
4471 env.src_rq = busiest;
4472 env.loop_max = min_t(unsigned long, sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004473
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004474more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004475 local_irq_save(flags);
4476 double_rq_lock(this_rq, busiest);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004477 if (!env.loop)
4478 update_h_load(env.src_cpu);
4479 ld_moved += move_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004480 double_rq_unlock(this_rq, busiest);
4481 local_irq_restore(flags);
4482
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004483 if (env.flags & LBF_NEED_BREAK) {
4484 env.flags &= ~LBF_NEED_BREAK;
4485 goto more_balance;
4486 }
4487
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004488 /*
4489 * some other cpu did the load balance for us.
4490 */
4491 if (ld_moved && this_cpu != smp_processor_id())
4492 resched_cpu(this_cpu);
4493
4494 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004495 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004496 cpumask_clear_cpu(cpu_of(busiest), cpus);
4497 if (!cpumask_empty(cpus))
4498 goto redo;
4499 goto out_balanced;
4500 }
4501 }
4502
4503 if (!ld_moved) {
4504 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004505 /*
4506 * Increment the failure counter only on periodic balance.
4507 * We do not want newidle balance, which can be very
4508 * frequent, pollute the failure counter causing
4509 * excessive cache_hot migrations and active balances.
4510 */
4511 if (idle != CPU_NEWLY_IDLE)
4512 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004513
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004514 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004515 raw_spin_lock_irqsave(&busiest->lock, flags);
4516
Tejun Heo969c7922010-05-06 18:49:21 +02004517 /* don't kick the active_load_balance_cpu_stop,
4518 * if the curr task on busiest cpu can't be
4519 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004520 */
4521 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004522 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004523 raw_spin_unlock_irqrestore(&busiest->lock,
4524 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004525 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004526 goto out_one_pinned;
4527 }
4528
Tejun Heo969c7922010-05-06 18:49:21 +02004529 /*
4530 * ->active_balance synchronizes accesses to
4531 * ->active_balance_work. Once set, it's cleared
4532 * only after active load balance is finished.
4533 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004534 if (!busiest->active_balance) {
4535 busiest->active_balance = 1;
4536 busiest->push_cpu = this_cpu;
4537 active_balance = 1;
4538 }
4539 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004540
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004541 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02004542 stop_one_cpu_nowait(cpu_of(busiest),
4543 active_load_balance_cpu_stop, busiest,
4544 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004545
4546 /*
4547 * We've kicked active balancing, reset the failure
4548 * counter.
4549 */
4550 sd->nr_balance_failed = sd->cache_nice_tries+1;
4551 }
Steve Muckle8f77c282013-03-11 16:33:42 -07004552 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004553 sd->nr_balance_failed = 0;
Steve Muckle8f77c282013-03-11 16:33:42 -07004554 if (per_cpu(dbs_boost_needed, this_cpu)) {
4555 per_cpu(dbs_boost_needed, this_cpu) = false;
4556 atomic_notifier_call_chain(&migration_notifier_head,
4557 this_cpu,
4558 (void *)cpu_of(busiest));
4559 }
4560 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004561 if (likely(!active_balance)) {
4562 /* We were unbalanced, so reset the balancing interval */
4563 sd->balance_interval = sd->min_interval;
4564 } else {
4565 /*
4566 * If we've begun active balancing, start to back off. This
4567 * case may not be covered by the all_pinned logic if there
4568 * is only 1 task on the busy runqueue (because we don't call
4569 * move_tasks).
4570 */
4571 if (sd->balance_interval < sd->max_interval)
4572 sd->balance_interval *= 2;
4573 }
4574
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004575 goto out;
4576
4577out_balanced:
4578 schedstat_inc(sd, lb_balanced[idle]);
4579
4580 sd->nr_balance_failed = 0;
4581
4582out_one_pinned:
4583 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004584 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004585 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004586 (sd->balance_interval < sd->max_interval))
4587 sd->balance_interval *= 2;
4588
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004589 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004590out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004591 return ld_moved;
4592}
4593
4594/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004595 * idle_balance is called by schedule() if this_cpu is about to become
4596 * idle. Attempts to pull tasks from other CPUs.
4597 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004598void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004599{
4600 struct sched_domain *sd;
4601 int pulled_task = 0;
4602 unsigned long next_balance = jiffies + HZ;
4603
4604 this_rq->idle_stamp = this_rq->clock;
4605
4606 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4607 return;
4608
Peter Zijlstraf492e122009-12-23 15:29:42 +01004609 /*
4610 * Drop the rq->lock, but keep IRQ/preempt disabled.
4611 */
4612 raw_spin_unlock(&this_rq->lock);
4613
Paul Turnerc66eaf62010-11-15 15:47:07 -08004614 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004615 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004616 for_each_domain(this_cpu, sd) {
4617 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004618 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004619
4620 if (!(sd->flags & SD_LOAD_BALANCE))
4621 continue;
4622
Peter Zijlstraf492e122009-12-23 15:29:42 +01004623 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004624 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004625 pulled_task = load_balance(this_cpu, this_rq,
4626 sd, CPU_NEWLY_IDLE, &balance);
4627 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004628
4629 interval = msecs_to_jiffies(sd->balance_interval);
4630 if (time_after(next_balance, sd->last_balance + interval))
4631 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004632 if (pulled_task) {
4633 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004634 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004635 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004636 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004637 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004638
4639 raw_spin_lock(&this_rq->lock);
4640
Srivatsa Vaddagirice1a0412013-03-07 12:14:53 -08004641 if (!pulled_task || time_after(jiffies, this_rq->next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004642 /*
4643 * We are going idle. next_balance may be set based on
4644 * a busy processor. So reset next_balance.
4645 */
4646 this_rq->next_balance = next_balance;
4647 }
4648}
4649
4650/*
Tejun Heo969c7922010-05-06 18:49:21 +02004651 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4652 * running tasks off the busiest CPU onto idle CPUs. It requires at
4653 * least 1 task to be running on each physical CPU where possible, and
4654 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004655 */
Tejun Heo969c7922010-05-06 18:49:21 +02004656static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004657{
Tejun Heo969c7922010-05-06 18:49:21 +02004658 struct rq *busiest_rq = data;
4659 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004660 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004661 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004662 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004663
4664 raw_spin_lock_irq(&busiest_rq->lock);
4665
4666 /* make sure the requested cpu hasn't gone down in the meantime */
4667 if (unlikely(busiest_cpu != smp_processor_id() ||
4668 !busiest_rq->active_balance))
4669 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004670
4671 /* Is there any task to move? */
4672 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004673 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004674
4675 /*
4676 * This condition is "impossible", if it occurs
4677 * we need to fix it. Originally reported by
4678 * Bjorn Helgaas on a 128-cpu setup.
4679 */
4680 BUG_ON(busiest_rq == target_rq);
4681
4682 /* move a task from busiest_rq to target_rq */
4683 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004684
4685 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004686 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004687 for_each_domain(target_cpu, sd) {
4688 if ((sd->flags & SD_LOAD_BALANCE) &&
4689 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4690 break;
4691 }
4692
4693 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004694 struct lb_env env = {
4695 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004696 .dst_cpu = target_cpu,
4697 .dst_rq = target_rq,
4698 .src_cpu = busiest_rq->cpu,
4699 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004700 .idle = CPU_IDLE,
4701 };
4702
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004703 schedstat_inc(sd, alb_count);
4704
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004705 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004706 schedstat_inc(sd, alb_pushed);
4707 else
4708 schedstat_inc(sd, alb_failed);
4709 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004710 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004711 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004712out_unlock:
4713 busiest_rq->active_balance = 0;
4714 raw_spin_unlock_irq(&busiest_rq->lock);
Steve Muckle8f77c282013-03-11 16:33:42 -07004715 if (per_cpu(dbs_boost_needed, target_cpu)) {
4716 per_cpu(dbs_boost_needed, target_cpu) = false;
4717 atomic_notifier_call_chain(&migration_notifier_head,
4718 target_cpu,
4719 (void *)cpu_of(busiest_rq));
4720 }
Tejun Heo969c7922010-05-06 18:49:21 +02004721 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004722}
4723
4724#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004725/*
4726 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004727 * - When one of the busy CPUs notice that there may be an idle rebalancing
4728 * needed, they will kick the idle load balancer, which then does idle
4729 * load balancing for all the idle CPUs.
4730 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004731static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004732 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004733 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004734 unsigned long next_balance; /* in jiffy units */
4735} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004736
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004737#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4738/**
4739 * lowest_flag_domain - Return lowest sched_domain containing flag.
4740 * @cpu: The cpu whose lowest level of sched domain is to
4741 * be returned.
4742 * @flag: The flag to check for the lowest sched_domain
4743 * for the given cpu.
4744 *
4745 * Returns the lowest sched_domain of a cpu which contains the given flag.
4746 */
4747static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4748{
4749 struct sched_domain *sd;
4750
4751 for_each_domain(cpu, sd)
Hillf Danton08354712011-06-16 21:55:19 -04004752 if (sd->flags & flag)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004753 break;
4754
4755 return sd;
4756}
4757
4758/**
4759 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4760 * @cpu: The cpu whose domains we're iterating over.
4761 * @sd: variable holding the value of the power_savings_sd
4762 * for cpu.
4763 * @flag: The flag to filter the sched_domains to be iterated.
4764 *
4765 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4766 * set, starting from the lowest sched_domain to the highest.
4767 */
4768#define for_each_flag_domain(cpu, sd, flag) \
4769 for (sd = lowest_flag_domain(cpu, flag); \
4770 (sd && (sd->flags & flag)); sd = sd->parent)
4771
4772/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004773 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4774 * @cpu: The cpu which is nominating a new idle_load_balancer.
4775 *
4776 * Returns: Returns the id of the idle load balancer if it exists,
4777 * Else, returns >= nr_cpu_ids.
4778 *
4779 * This algorithm picks the idle load balancer such that it belongs to a
4780 * semi-idle powersavings sched_domain. The idea is to try and avoid
4781 * completely idle packages/cores just for the purpose of idle load balancing
4782 * when there are other idle cpu's which are better suited for that job.
4783 */
4784static int find_new_ilb(int cpu)
4785{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004786 int ilb = cpumask_first(nohz.idle_cpus_mask);
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004787 struct sched_group *ilbg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004788 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004789
4790 /*
4791 * Have idle load balancer selection from semi-idle packages only
4792 * when power-aware load balancing is enabled
4793 */
4794 if (!(sched_smt_power_savings || sched_mc_power_savings))
4795 goto out_done;
4796
4797 /*
4798 * Optimize for the case when we have no idle CPUs or only one
4799 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4800 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004801 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004802 goto out_done;
4803
Peter Zijlstradce840a2011-04-07 14:09:50 +02004804 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004805 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004806 ilbg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004807
4808 do {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004809 if (ilbg->group_weight !=
4810 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4811 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4812 sched_group_cpus(ilbg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02004813 goto unlock;
4814 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004815
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004816 ilbg = ilbg->next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004817
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004818 } while (ilbg != sd->groups);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004819 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004820unlock:
4821 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004822
4823out_done:
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004824 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4825 return ilb;
4826
4827 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004828}
4829#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4830static inline int find_new_ilb(int call_cpu)
4831{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004832 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004833}
4834#endif
4835
4836/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004837 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4838 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4839 * CPU (if there is one).
4840 */
4841static void nohz_balancer_kick(int cpu)
4842{
4843 int ilb_cpu;
4844
4845 nohz.next_balance++;
4846
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004847 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004848
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004849 if (ilb_cpu >= nr_cpu_ids)
4850 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004851
Suresh Siddhacd490c52011-12-06 11:26:34 -08004852 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004853 return;
4854 /*
4855 * Use smp_send_reschedule() instead of resched_cpu().
4856 * This way we generate a sched IPI on the target cpu which
4857 * is idle. And the softirq performing nohz idle load balance
4858 * will be run before returning from the IPI.
4859 */
4860 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004861 return;
4862}
4863
Suresh Siddha71325962012-01-19 18:28:57 -08004864static inline void clear_nohz_tick_stopped(int cpu)
4865{
4866 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4867 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4868 atomic_dec(&nohz.nr_cpus);
4869 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4870 }
4871}
4872
Suresh Siddha69e1e812011-12-01 17:07:33 -08004873static inline void set_cpu_sd_state_busy(void)
4874{
4875 struct sched_domain *sd;
4876 int cpu = smp_processor_id();
4877
4878 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4879 return;
4880 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4881
4882 rcu_read_lock();
4883 for_each_domain(cpu, sd)
4884 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4885 rcu_read_unlock();
4886}
4887
4888void set_cpu_sd_state_idle(void)
4889{
4890 struct sched_domain *sd;
4891 int cpu = smp_processor_id();
4892
4893 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4894 return;
4895 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4896
4897 rcu_read_lock();
4898 for_each_domain(cpu, sd)
4899 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4900 rcu_read_unlock();
4901}
4902
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004903/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004904 * This routine will record that this cpu is going idle with tick stopped.
4905 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004906 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004907void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004908{
4909 int cpu = smp_processor_id();
4910
Suresh Siddha71325962012-01-19 18:28:57 -08004911 /*
4912 * If this cpu is going down, then nothing needs to be done.
4913 */
4914 if (!cpu_active(cpu))
4915 return;
4916
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004917 if (stop_tick) {
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004918 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004919 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004920
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004921 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004922 atomic_inc(&nohz.nr_cpus);
Suresh Siddha1c792db2011-12-01 17:07:32 -08004923 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004924 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004925 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004926}
Suresh Siddha71325962012-01-19 18:28:57 -08004927
4928static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4929 unsigned long action, void *hcpu)
4930{
4931 switch (action & ~CPU_TASKS_FROZEN) {
4932 case CPU_DYING:
4933 clear_nohz_tick_stopped(smp_processor_id());
4934 return NOTIFY_OK;
4935 default:
4936 return NOTIFY_DONE;
4937 }
4938}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004939#endif
4940
4941static DEFINE_SPINLOCK(balancing);
4942
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004943/*
4944 * Scale the max load_balance interval with the number of CPUs in the system.
4945 * This trades load-balance latency on larger machines for less cross talk.
4946 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004947void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004948{
4949 max_load_balance_interval = HZ*num_online_cpus()/10;
4950}
4951
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004952/*
4953 * It checks each scheduling domain to see if it is due to be balanced,
4954 * and initiates a balancing operation if so.
4955 *
4956 * Balancing parameters are set up in arch_init_sched_domains.
4957 */
4958static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4959{
4960 int balance = 1;
4961 struct rq *rq = cpu_rq(cpu);
4962 unsigned long interval;
4963 struct sched_domain *sd;
4964 /* Earliest time when we have to do rebalance again */
4965 unsigned long next_balance = jiffies + 60*HZ;
4966 int update_next_balance = 0;
4967 int need_serialize;
4968
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004969 update_shares(cpu);
4970
Peter Zijlstradce840a2011-04-07 14:09:50 +02004971 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004972 for_each_domain(cpu, sd) {
4973 if (!(sd->flags & SD_LOAD_BALANCE))
4974 continue;
4975
4976 interval = sd->balance_interval;
4977 if (idle != CPU_IDLE)
4978 interval *= sd->busy_factor;
4979
4980 /* scale ms to jiffies */
4981 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004982 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004983
4984 need_serialize = sd->flags & SD_SERIALIZE;
4985
4986 if (need_serialize) {
4987 if (!spin_trylock(&balancing))
4988 goto out;
4989 }
4990
4991 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4992 if (load_balance(cpu, rq, sd, idle, &balance)) {
4993 /*
4994 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004995 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004996 */
4997 idle = CPU_NOT_IDLE;
4998 }
4999 sd->last_balance = jiffies;
5000 }
5001 if (need_serialize)
5002 spin_unlock(&balancing);
5003out:
5004 if (time_after(next_balance, sd->last_balance + interval)) {
5005 next_balance = sd->last_balance + interval;
5006 update_next_balance = 1;
5007 }
5008
5009 /*
5010 * Stop the load balance at this level. There is another
5011 * CPU in our sched group which is doing load balancing more
5012 * actively.
5013 */
5014 if (!balance)
5015 break;
5016 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005017 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005018
5019 /*
5020 * next_balance will be updated only when there is a need.
5021 * When the cpu is attached to null domain for ex, it will not be
5022 * updated.
5023 */
5024 if (likely(update_next_balance))
5025 rq->next_balance = next_balance;
5026}
5027
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005028#ifdef CONFIG_NO_HZ
5029/*
5030 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5031 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5032 */
5033static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5034{
5035 struct rq *this_rq = cpu_rq(this_cpu);
5036 struct rq *rq;
5037 int balance_cpu;
5038
Suresh Siddha1c792db2011-12-01 17:07:32 -08005039 if (idle != CPU_IDLE ||
5040 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5041 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005042
5043 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005044 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005045 continue;
5046
5047 /*
5048 * If this cpu gets work to do, stop the load balancing
5049 * work being done for other cpus. Next load
5050 * balancing owner will pick it up.
5051 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005052 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005053 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005054
5055 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02005056 update_rq_clock(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005057 update_cpu_load(this_rq);
5058 raw_spin_unlock_irq(&this_rq->lock);
5059
5060 rebalance_domains(balance_cpu, CPU_IDLE);
5061
5062 rq = cpu_rq(balance_cpu);
5063 if (time_after(this_rq->next_balance, rq->next_balance))
5064 this_rq->next_balance = rq->next_balance;
5065 }
5066 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005067end:
5068 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005069}
5070
5071/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005072 * Current heuristic for kicking the idle load balancer in the presence
5073 * of an idle cpu is the system.
5074 * - This rq has more than one task.
5075 * - At any scheduler domain level, this cpu's scheduler group has multiple
5076 * busy cpu's exceeding the group's power.
5077 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5078 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005079 */
5080static inline int nohz_kick_needed(struct rq *rq, int cpu)
5081{
5082 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005083 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084
Suresh Siddha1c792db2011-12-01 17:07:32 -08005085 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005086 return 0;
5087
Suresh Siddha1c792db2011-12-01 17:07:32 -08005088 /*
5089 * We may be recently in ticked or tickless idle mode. At the first
5090 * busy tick after returning from idle, we will update the busy stats.
5091 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005092 set_cpu_sd_state_busy();
Suresh Siddha71325962012-01-19 18:28:57 -08005093 clear_nohz_tick_stopped(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005094
5095 /*
5096 * None are in tickless mode and hence no need for NOHZ idle load
5097 * balancing.
5098 */
5099 if (likely(!atomic_read(&nohz.nr_cpus)))
5100 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005101
5102 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005103 return 0;
5104
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005105 if (rq->nr_running >= 2)
5106 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005107
Peter Zijlstra067491b2011-12-07 14:32:08 +01005108 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005109 for_each_domain(cpu, sd) {
5110 struct sched_group *sg = sd->groups;
5111 struct sched_group_power *sgp = sg->sgp;
5112 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005113
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005114 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005115 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005116
5117 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5118 && (cpumask_first_and(nohz.idle_cpus_mask,
5119 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005120 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005121
5122 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5123 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005124 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005125 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005126 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005127
5128need_kick_unlock:
5129 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005130need_kick:
5131 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005132}
5133#else
5134static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5135#endif
5136
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005137/*
5138 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005139 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005140 */
5141static void run_rebalance_domains(struct softirq_action *h)
5142{
5143 int this_cpu = smp_processor_id();
5144 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005145 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005146 CPU_IDLE : CPU_NOT_IDLE;
5147
5148 rebalance_domains(this_cpu, idle);
5149
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005150 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005151 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005152 * balancing on behalf of the other idle cpus whose ticks are
5153 * stopped.
5154 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005155 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005156}
5157
5158static inline int on_null_domain(int cpu)
5159{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005160 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005161}
5162
5163/*
5164 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005165 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005166void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005167{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005168 /* Don't need to rebalance while attached to NULL domain */
5169 if (time_after_eq(jiffies, rq->next_balance) &&
5170 likely(!on_null_domain(cpu)))
5171 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005172#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005173 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005174 nohz_balancer_kick(cpu);
5175#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005176}
5177
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005178static void rq_online_fair(struct rq *rq)
5179{
5180 update_sysctl();
5181}
5182
5183static void rq_offline_fair(struct rq *rq)
5184{
5185 update_sysctl();
5186}
5187
Dhaval Giani55e12e52008-06-24 23:39:43 +05305188#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005189
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005190/*
5191 * scheduler tick hitting a task of our scheduling class:
5192 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005193static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005194{
5195 struct cfs_rq *cfs_rq;
5196 struct sched_entity *se = &curr->se;
5197
5198 for_each_sched_entity(se) {
5199 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005200 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005201 }
5202}
5203
5204/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005205 * called on fork with the child task as argument from the parent's context
5206 * - child not yet on the tasklist
5207 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005208 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005209static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005210{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005211 struct cfs_rq *cfs_rq;
5212 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005213 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005214 struct rq *rq = this_rq();
5215 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005216
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005217 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005218
Peter Zijlstra861d0342010-08-19 13:31:43 +02005219 update_rq_clock(rq);
5220
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005221 cfs_rq = task_cfs_rq(current);
5222 curr = cfs_rq->curr;
5223
Daisuke Nishimura628753322013-09-10 18:16:36 +09005224 /*
5225 * Not only the cpu but also the task_group of the parent might have
5226 * been changed after parent->se.parent,cfs_rq were copied to
5227 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
5228 * of child point to valid ones.
5229 */
5230 rcu_read_lock();
5231 __set_task_cpu(p, this_cpu);
5232 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005233
Ting Yang7109c442007-08-28 12:53:24 +02005234 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005235
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005236 if (curr)
5237 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005238 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005239
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005240 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005241 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005242 * Upon rescheduling, sched_class::put_prev_task() will place
5243 * 'current' within the tree based on its new key value.
5244 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005245 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305246 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005247 }
5248
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005249 se->vruntime -= cfs_rq->min_vruntime;
5250
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005251 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005252}
5253
Steven Rostedtcb469842008-01-25 21:08:22 +01005254/*
5255 * Priority of the task has changed. Check to see if we preempt
5256 * the current task.
5257 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005258static void
5259prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005260{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005261 if (!p->se.on_rq)
5262 return;
5263
Steven Rostedtcb469842008-01-25 21:08:22 +01005264 /*
5265 * Reschedule if we are currently running on this runqueue and
5266 * our priority decreased, or if we are not currently running on
5267 * this runqueue and our priority is higher than the current's
5268 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005269 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005270 if (p->prio > oldprio)
5271 resched_task(rq->curr);
5272 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005273 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005274}
5275
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005276static void switched_from_fair(struct rq *rq, struct task_struct *p)
5277{
5278 struct sched_entity *se = &p->se;
5279 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5280
5281 /*
5282 * Ensure the task's vruntime is normalized, so that when its
5283 * switched back to the fair class the enqueue_entity(.flags=0) will
5284 * do the right thing.
5285 *
5286 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5287 * have normalized the vruntime, if it was !on_rq, then only when
5288 * the task is sleeping will it still have non-normalized vruntime.
5289 */
5290 if (!se->on_rq && p->state != TASK_RUNNING) {
5291 /*
5292 * Fix up our vruntime so that the current sleep doesn't
5293 * cause 'unlimited' sleep bonus.
5294 */
5295 place_entity(cfs_rq, se, 0);
5296 se->vruntime -= cfs_rq->min_vruntime;
5297 }
5298}
5299
Steven Rostedtcb469842008-01-25 21:08:22 +01005300/*
5301 * We switched to the sched_fair class.
5302 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005303static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005304{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005305 if (!p->se.on_rq)
5306 return;
5307
Steven Rostedtcb469842008-01-25 21:08:22 +01005308 /*
5309 * We were most likely switched from sched_rt, so
5310 * kick off the schedule if running, otherwise just see
5311 * if we can still preempt the current task.
5312 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005313 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005314 resched_task(rq->curr);
5315 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005316 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005317}
5318
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005319/* Account for a task changing its policy or group.
5320 *
5321 * This routine is mostly called to set cfs_rq->curr field when a task
5322 * migrates between groups/classes.
5323 */
5324static void set_curr_task_fair(struct rq *rq)
5325{
5326 struct sched_entity *se = &rq->curr->se;
5327
Paul Turnerec12cb72011-07-21 09:43:30 -07005328 for_each_sched_entity(se) {
5329 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5330
5331 set_next_entity(cfs_rq, se);
5332 /* ensure bandwidth has been allocated on our new cfs_rq */
5333 account_cfs_rq_runtime(cfs_rq, 0);
5334 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005335}
5336
Peter Zijlstra029632f2011-10-25 10:00:11 +02005337void init_cfs_rq(struct cfs_rq *cfs_rq)
5338{
5339 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005340 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5341#ifndef CONFIG_64BIT
5342 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5343#endif
5344}
5345
Peter Zijlstra810b3812008-02-29 15:21:01 -05005346#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005347static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005348{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005349 /*
5350 * If the task was not on the rq at the time of this cgroup movement
5351 * it must have been asleep, sleeping tasks keep their ->vruntime
5352 * absolute on their old rq until wakeup (needed for the fair sleeper
5353 * bonus in place_entity()).
5354 *
5355 * If it was on the rq, we've just 'preempted' it, which does convert
5356 * ->vruntime to a relative base.
5357 *
5358 * Make sure both cases convert their relative position when migrating
5359 * to another cgroup's rq. This does somewhat interfere with the
5360 * fair sleeper stuff for the first placement, but who cares.
5361 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005362 /*
5363 * When !on_rq, vruntime of the task has usually NOT been normalized.
5364 * But there are some cases where it has already been normalized:
5365 *
5366 * - Moving a forked child which is waiting for being woken up by
5367 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005368 * - Moving a task which has been woken up by try_to_wake_up() and
5369 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005370 *
5371 * To prevent boost or penalty in the new cfs_rq caused by delta
5372 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5373 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005374 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005375 on_rq = 1;
5376
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005377 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005378 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5379 set_task_rq(p, task_cpu(p));
5380 if (!on_rq)
5381 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005382}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005383
5384void free_fair_sched_group(struct task_group *tg)
5385{
5386 int i;
5387
5388 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5389
5390 for_each_possible_cpu(i) {
5391 if (tg->cfs_rq)
5392 kfree(tg->cfs_rq[i]);
5393 if (tg->se)
5394 kfree(tg->se[i]);
5395 }
5396
5397 kfree(tg->cfs_rq);
5398 kfree(tg->se);
5399}
5400
5401int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5402{
5403 struct cfs_rq *cfs_rq;
5404 struct sched_entity *se;
5405 int i;
5406
5407 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5408 if (!tg->cfs_rq)
5409 goto err;
5410 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5411 if (!tg->se)
5412 goto err;
5413
5414 tg->shares = NICE_0_LOAD;
5415
5416 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5417
5418 for_each_possible_cpu(i) {
5419 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5420 GFP_KERNEL, cpu_to_node(i));
5421 if (!cfs_rq)
5422 goto err;
5423
5424 se = kzalloc_node(sizeof(struct sched_entity),
5425 GFP_KERNEL, cpu_to_node(i));
5426 if (!se)
5427 goto err_free_rq;
5428
5429 init_cfs_rq(cfs_rq);
5430 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5431 }
5432
5433 return 1;
5434
5435err_free_rq:
5436 kfree(cfs_rq);
5437err:
5438 return 0;
5439}
5440
5441void unregister_fair_sched_group(struct task_group *tg, int cpu)
5442{
5443 struct rq *rq = cpu_rq(cpu);
5444 unsigned long flags;
5445
5446 /*
5447 * Only empty task groups can be destroyed; so we can speculatively
5448 * check on_list without danger of it being re-added.
5449 */
5450 if (!tg->cfs_rq[cpu]->on_list)
5451 return;
5452
5453 raw_spin_lock_irqsave(&rq->lock, flags);
5454 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5455 raw_spin_unlock_irqrestore(&rq->lock, flags);
5456}
5457
5458void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5459 struct sched_entity *se, int cpu,
5460 struct sched_entity *parent)
5461{
5462 struct rq *rq = cpu_rq(cpu);
5463
5464 cfs_rq->tg = tg;
5465 cfs_rq->rq = rq;
5466#ifdef CONFIG_SMP
5467 /* allow initial update_cfs_load() to truncate */
5468 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005469#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005470 init_cfs_rq_runtime(cfs_rq);
5471
5472 tg->cfs_rq[cpu] = cfs_rq;
5473 tg->se[cpu] = se;
5474
5475 /* se could be NULL for root_task_group */
5476 if (!se)
5477 return;
5478
5479 if (!parent)
5480 se->cfs_rq = &rq->cfs;
5481 else
5482 se->cfs_rq = parent->my_q;
5483
5484 se->my_q = cfs_rq;
5485 update_load_set(&se->load, 0);
5486 se->parent = parent;
5487}
5488
5489static DEFINE_MUTEX(shares_mutex);
5490
5491int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5492{
5493 int i;
5494 unsigned long flags;
5495
5496 /*
5497 * We can't change the weight of the root cgroup.
5498 */
5499 if (!tg->se[0])
5500 return -EINVAL;
5501
5502 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5503
5504 mutex_lock(&shares_mutex);
5505 if (tg->shares == shares)
5506 goto done;
5507
5508 tg->shares = shares;
5509 for_each_possible_cpu(i) {
5510 struct rq *rq = cpu_rq(i);
5511 struct sched_entity *se;
5512
5513 se = tg->se[i];
5514 /* Propagate contribution to hierarchy */
5515 raw_spin_lock_irqsave(&rq->lock, flags);
5516 for_each_sched_entity(se)
5517 update_cfs_shares(group_cfs_rq(se));
5518 raw_spin_unlock_irqrestore(&rq->lock, flags);
5519 }
5520
5521done:
5522 mutex_unlock(&shares_mutex);
5523 return 0;
5524}
5525#else /* CONFIG_FAIR_GROUP_SCHED */
5526
5527void free_fair_sched_group(struct task_group *tg) { }
5528
5529int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5530{
5531 return 1;
5532}
5533
5534void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5535
5536#endif /* CONFIG_FAIR_GROUP_SCHED */
5537
Peter Zijlstra810b3812008-02-29 15:21:01 -05005538
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005539static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005540{
5541 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005542 unsigned int rr_interval = 0;
5543
5544 /*
5545 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5546 * idle runqueue:
5547 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005548 if (rq->cfs.load.weight)
Zhu Yanhai1836cd12013-01-08 12:56:52 +08005549 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005550
5551 return rr_interval;
5552}
5553
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005554/*
5555 * All the scheduling class methods:
5556 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005557const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005558 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005559 .enqueue_task = enqueue_task_fair,
5560 .dequeue_task = dequeue_task_fair,
5561 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005562 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005563
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005564 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005565
5566 .pick_next_task = pick_next_task_fair,
5567 .put_prev_task = put_prev_task_fair,
5568
Peter Williams681f3e62007-10-24 18:23:51 +02005569#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005570 .select_task_rq = select_task_rq_fair,
5571
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005572 .rq_online = rq_online_fair,
5573 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005574
5575 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005576#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005577
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005578 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005579 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005580 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005581
5582 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005583 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005584 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005585
Peter Williams0d721ce2009-09-21 01:31:53 +00005586 .get_rr_interval = get_rr_interval_fair,
5587
Peter Zijlstra810b3812008-02-29 15:21:01 -05005588#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005589 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005590#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005591};
5592
5593#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005594void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005595{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005596 struct cfs_rq *cfs_rq;
5597
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005598 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005599 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005600 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005601 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005602}
5603#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005604
5605__init void init_sched_fair_class(void)
5606{
5607#ifdef CONFIG_SMP
5608 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5609
5610#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005611 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005612 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005613 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005614#endif
5615#endif /* SMP */
5616
5617}