blob: 9e49722da03263e54e23053340c848d1d01d6c1b [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Paul Turneraff3e492012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
263 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200264
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800265static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
266{
267 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800268 /*
269 * Ensure we either appear before our parent (if already
270 * enqueued) or force our parent to appear after us when it is
271 * enqueued. The fact that we always enqueue bottom-up
272 * reduces this to two cases.
273 */
274 if (cfs_rq->tg->parent &&
275 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
276 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800278 } else {
279 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
280 &rq_of(cfs_rq)->leaf_cfs_rq_list);
281 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282
283 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200284 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200285 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800286 }
287}
288
289static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (cfs_rq->on_list) {
292 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
293 cfs_rq->on_list = 0;
294 }
295}
296
Peter Zijlstrab7581492008-04-19 19:45:00 +0200297/* Iterate thr' all leaf cfs_rq's on a runqueue */
298#define for_each_leaf_cfs_rq(rq, cfs_rq) \
299 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
300
301/* Do the two (enqueued) entities belong to the same group ? */
302static inline int
303is_same_group(struct sched_entity *se, struct sched_entity *pse)
304{
305 if (se->cfs_rq == pse->cfs_rq)
306 return 1;
307
308 return 0;
309}
310
311static inline struct sched_entity *parent_entity(struct sched_entity *se)
312{
313 return se->parent;
314}
315
Peter Zijlstra464b7522008-10-24 11:06:15 +0200316/* return depth at which a sched entity is present in the hierarchy */
317static inline int depth_se(struct sched_entity *se)
318{
319 int depth = 0;
320
321 for_each_sched_entity(se)
322 depth++;
323
324 return depth;
325}
326
327static void
328find_matching_se(struct sched_entity **se, struct sched_entity **pse)
329{
330 int se_depth, pse_depth;
331
332 /*
333 * preemption test can be made between sibling entities who are in the
334 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
335 * both tasks until we find their ancestors who are siblings of common
336 * parent.
337 */
338
339 /* First walk up until both entities are at same depth */
340 se_depth = depth_se(*se);
341 pse_depth = depth_se(*pse);
342
343 while (se_depth > pse_depth) {
344 se_depth--;
345 *se = parent_entity(*se);
346 }
347
348 while (pse_depth > se_depth) {
349 pse_depth--;
350 *pse = parent_entity(*pse);
351 }
352
353 while (!is_same_group(*se, *pse)) {
354 *se = parent_entity(*se);
355 *pse = parent_entity(*pse);
356 }
357}
358
Peter Zijlstra8f488942009-07-24 12:25:30 +0200359#else /* !CONFIG_FAIR_GROUP_SCHED */
360
361static inline struct task_struct *task_of(struct sched_entity *se)
362{
363 return container_of(se, struct task_struct, se);
364}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365
366static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
367{
368 return container_of(cfs_rq, struct rq, cfs);
369}
370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200371#define entity_is_task(se) 1
372
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373#define for_each_sched_entity(se) \
374 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200379}
380
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
382{
383 struct task_struct *p = task_of(se);
384 struct rq *rq = task_rq(p);
385
386 return &rq->cfs;
387}
388
389/* runqueue "owned" by this group */
390static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
391{
392 return NULL;
393}
394
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800395static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
396{
397}
398
399static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
400{
401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403#define for_each_leaf_cfs_rq(rq, cfs_rq) \
404 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
405
406static inline int
407is_same_group(struct sched_entity *se, struct sched_entity *pse)
408{
409 return 1;
410}
411
412static inline struct sched_entity *parent_entity(struct sched_entity *se)
413{
414 return NULL;
415}
416
Peter Zijlstra464b7522008-10-24 11:06:15 +0200417static inline void
418find_matching_se(struct sched_entity **se, struct sched_entity **pse)
419{
420}
421
Peter Zijlstrab7581492008-04-19 19:45:00 +0200422#endif /* CONFIG_FAIR_GROUP_SCHED */
423
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700424static __always_inline
425void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427/**************************************************************
428 * Scheduling class tree data structure manipulation methods:
429 */
430
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200431static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200432{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200433 s64 delta = (s64)(vruntime - min_vruntime);
434 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435 min_vruntime = vruntime;
436
437 return min_vruntime;
438}
439
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200440static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200441{
442 s64 delta = (s64)(vruntime - min_vruntime);
443 if (delta < 0)
444 min_vruntime = vruntime;
445
446 return min_vruntime;
447}
448
Fabio Checconi54fdc582009-07-16 12:32:27 +0200449static inline int entity_before(struct sched_entity *a,
450 struct sched_entity *b)
451{
452 return (s64)(a->vruntime - b->vruntime) < 0;
453}
454
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200455static void update_min_vruntime(struct cfs_rq *cfs_rq)
456{
457 u64 vruntime = cfs_rq->min_vruntime;
458
459 if (cfs_rq->curr)
460 vruntime = cfs_rq->curr->vruntime;
461
462 if (cfs_rq->rb_leftmost) {
463 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
464 struct sched_entity,
465 run_node);
466
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100467 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200468 vruntime = se->vruntime;
469 else
470 vruntime = min_vruntime(vruntime, se->vruntime);
471 }
472
473 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200474#ifndef CONFIG_64BIT
475 smp_wmb();
476 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
477#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478}
479
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200480/*
481 * Enqueue an entity into the rb-tree:
482 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200483static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484{
485 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
486 struct rb_node *parent = NULL;
487 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488 int leftmost = 1;
489
490 /*
491 * Find the right place in the rbtree:
492 */
493 while (*link) {
494 parent = *link;
495 entry = rb_entry(parent, struct sched_entity, run_node);
496 /*
497 * We dont care about collisions. Nodes with
498 * the same key stay together.
499 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200500 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200501 link = &parent->rb_left;
502 } else {
503 link = &parent->rb_right;
504 leftmost = 0;
505 }
506 }
507
508 /*
509 * Maintain a cache of leftmost tree entries (it is frequently
510 * used):
511 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200512 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200513 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200514
515 rb_link_node(&se->run_node, parent, link);
516 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517}
518
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200519static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 if (cfs_rq->rb_leftmost == &se->run_node) {
522 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100523
524 next_node = rb_next(&se->run_node);
525 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200527
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200529}
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100533 struct rb_node *left = cfs_rq->rb_leftmost;
534
535 if (!left)
536 return NULL;
537
538 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200539}
540
Rik van Rielac53db52011-02-01 09:51:03 -0500541static struct sched_entity *__pick_next_entity(struct sched_entity *se)
542{
543 struct rb_node *next = rb_next(&se->run_node);
544
545 if (!next)
546 return NULL;
547
548 return rb_entry(next, struct sched_entity, run_node);
549}
550
551#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100554 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555
Balbir Singh70eee742008-02-22 13:25:53 +0530556 if (!last)
557 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100558
559 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200560}
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562/**************************************************************
563 * Scheduling class statistics methods:
564 */
565
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700567 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100568 loff_t *ppos)
569{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100571 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100572
573 if (ret || !write)
574 return ret;
575
576 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
577 sysctl_sched_min_granularity);
578
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#define WRT_SYSCTL(name) \
580 (normalized_sysctl_##name = sysctl_##name / (factor))
581 WRT_SYSCTL(sched_min_granularity);
582 WRT_SYSCTL(sched_latency);
583 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100584#undef WRT_SYSCTL
585
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586 return 0;
587}
588#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200589
590/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200592 */
593static inline unsigned long
594calc_delta_fair(unsigned long delta, struct sched_entity *se)
595{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200596 if (unlikely(se->load.weight != NICE_0_LOAD))
597 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200598
599 return delta;
600}
601
602/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603 * The idea is to set a period in which each task runs once.
604 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200605 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * this period because otherwise the slices get too small.
607 *
608 * p = (nr <= nl) ? l : l*nr/nl
609 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200610static u64 __sched_period(unsigned long nr_running)
611{
612 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100613 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200614
615 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100616 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200618 }
619
620 return period;
621}
622
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623/*
624 * We calculate the wall-time slice from the period by taking a part
625 * proportional to the weight.
626 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200629static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200630{
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200632
Mike Galbraith0a582442009-01-02 12:16:42 +0100633 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100634 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200635 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100636
637 cfs_rq = cfs_rq_of(se);
638 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200641 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100642
643 update_load_add(&lw, se->load.weight);
644 load = &lw;
645 }
646 slice = calc_delta_mine(slice, se->load.weight, load);
647 }
648 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200649}
650
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200652 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200658 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659}
660
Paul Turnerd6b55912010-11-15 15:47:09 -0800661static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800662static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800663
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200664/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200665 * Update the current task's runtime statistics. Skip current tasks that
666 * are not in our scheduling class.
667 */
668static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200669__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
670 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200672 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200673
Lucas De Marchi41acab82010-03-10 23:37:45 -0300674 schedstat_set(curr->statistics.exec_max,
675 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200676
677 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200678 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200679 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100680
Ingo Molnare9acbff2007-10-15 17:00:04 +0200681 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200682 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800683
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100684#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800685 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800686#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200687}
688
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200689static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200691 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700692 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693 unsigned long delta_exec;
694
695 if (unlikely(!curr))
696 return;
697
698 /*
699 * Get the amount of time the current task was running
700 * since the last time we changed load (this cannot
701 * overflow on 32 bits):
702 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200703 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100704 if (!delta_exec)
705 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200706
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200707 __update_curr(cfs_rq, curr, delta_exec);
708 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100709
710 if (entity_is_task(curr)) {
711 struct task_struct *curtask = task_of(curr);
712
Ingo Molnarf977bb42009-09-13 18:15:54 +0200713 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100714 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700715 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100716 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700717
718 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200719}
720
721static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200722update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200723{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300724 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200725}
726
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200727/*
728 * Task is being enqueued - update stats:
729 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200730static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200732 /*
733 * Are we enqueueing a waiting task? (for current tasks
734 * a dequeue/enqueue event is a NOP)
735 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200736 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200737 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200738}
739
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200741update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200742{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300743 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
744 rq_of(cfs_rq)->clock - se->statistics.wait_start));
745 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
746 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
747 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200748#ifdef CONFIG_SCHEDSTATS
749 if (entity_is_task(se)) {
750 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300751 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200752 }
753#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300754 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200755}
756
757static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200758update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200759{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200760 /*
761 * Mark the end of the wait period if dequeueing a
762 * waiting task:
763 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200764 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200765 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200766}
767
768/*
769 * We are picking a new current task - update its stats:
770 */
771static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200772update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200773{
774 /*
775 * We are starting a new run period:
776 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700777 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200778}
779
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200780/**************************************************
781 * Scheduling class queueing methods:
782 */
783
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200784static void
785account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
786{
787 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200788 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200789 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100790#ifdef CONFIG_SMP
791 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200792 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100793#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200794 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200795}
796
797static void
798account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
799{
800 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200801 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200802 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100803 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530804 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200805 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200806}
807
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800808#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700809/* we need this in update_cfs_load and load-balance functions below */
810static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800811# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800812static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
813 int global_update)
814{
815 struct task_group *tg = cfs_rq->tg;
816 long load_avg;
817
818 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
819 load_avg -= cfs_rq->load_contribution;
820
821 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
822 atomic_add(load_avg, &tg->load_weight);
823 cfs_rq->load_contribution += load_avg;
824 }
825}
826
827static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800828{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800829 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800830 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800831 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800832
Paul Turner64660c82011-07-21 09:43:36 -0700833 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800834 return;
835
Paul Turner05ca62c2011-01-21 20:45:02 -0800836 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800837 delta = now - cfs_rq->load_stamp;
838
Paul Turnere33078b2010-11-15 15:47:04 -0800839 /* truncate load history at 4 idle periods */
840 if (cfs_rq->load_stamp > cfs_rq->load_last &&
841 now - cfs_rq->load_last > 4 * period) {
842 cfs_rq->load_period = 0;
843 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800844 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800845 }
846
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800847 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800848 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800849 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800850 if (load) {
851 cfs_rq->load_last = now;
852 cfs_rq->load_avg += delta * load;
853 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800854
Paul Turnerd6b55912010-11-15 15:47:09 -0800855 /* consider updating load contribution on each fold or truncate */
856 if (global_update || cfs_rq->load_period > period
857 || !cfs_rq->load_period)
858 update_cfs_rq_load_contribution(cfs_rq, global_update);
859
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800860 while (cfs_rq->load_period > period) {
861 /*
862 * Inline assembly required to prevent the compiler
863 * optimising this loop into a divmod call.
864 * See __iter_div_u64_rem() for another example of this.
865 */
866 asm("" : "+rm" (cfs_rq->load_period));
867 cfs_rq->load_period /= 2;
868 cfs_rq->load_avg /= 2;
869 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800870
Paul Turnere33078b2010-11-15 15:47:04 -0800871 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
872 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800873}
874
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200875static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
876{
877 long tg_weight;
878
879 /*
880 * Use this CPU's actual weight instead of the last load_contribution
881 * to gain a more accurate current total weight. See
882 * update_cfs_rq_load_contribution().
883 */
884 tg_weight = atomic_read(&tg->load_weight);
885 tg_weight -= cfs_rq->load_contribution;
886 tg_weight += cfs_rq->load.weight;
887
888 return tg_weight;
889}
890
Paul Turner6d5ab292011-01-21 20:45:01 -0800891static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800892{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200893 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800894
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200895 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800896 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800898 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200899 if (tg_weight)
900 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800901
902 if (shares < MIN_SHARES)
903 shares = MIN_SHARES;
904 if (shares > tg->shares)
905 shares = tg->shares;
906
907 return shares;
908}
909
910static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
911{
912 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
913 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800914 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800915 }
916}
917# else /* CONFIG_SMP */
918static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
919{
920}
921
Paul Turner6d5ab292011-01-21 20:45:01 -0800922static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800923{
924 return tg->shares;
925}
926
927static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
928{
929}
930# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800931static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
932 unsigned long weight)
933{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800934 if (se->on_rq) {
935 /* commit outstanding execution time */
936 if (cfs_rq->curr == se)
937 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800938 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800939 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800940
941 update_load_set(&se->load, weight);
942
943 if (se->on_rq)
944 account_entity_enqueue(cfs_rq, se);
945}
946
Paul Turner6d5ab292011-01-21 20:45:01 -0800947static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800948{
949 struct task_group *tg;
950 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800951 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800952
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800953 tg = cfs_rq->tg;
954 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700955 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800956 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800957#ifndef CONFIG_SMP
958 if (likely(se->load.weight == tg->shares))
959 return;
960#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800961 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800962
963 reweight_entity(cfs_rq_of(se), se, shares);
964}
965#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800966static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800967{
968}
969
Paul Turner6d5ab292011-01-21 20:45:01 -0800970static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800971{
972}
Paul Turner43365bd2010-12-15 19:10:17 -0800973
974static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
975{
976}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800977#endif /* CONFIG_FAIR_GROUP_SCHED */
978
Paul Turner9d85f212012-10-04 13:18:29 +0200979#ifdef CONFIG_SMP
980/*
981 * Approximate:
982 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
983 */
984static __always_inline u64 decay_load(u64 val, u64 n)
985{
986 for (; n && val; n--) {
987 val *= 4008;
988 val >>= 12;
989 }
990
991 return val;
992}
993
994/*
995 * We can represent the historical contribution to runnable average as the
996 * coefficients of a geometric series. To do this we sub-divide our runnable
997 * history into segments of approximately 1ms (1024us); label the segment that
998 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
999 *
1000 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
1001 * p0 p1 p2
1002 * (now) (~1ms ago) (~2ms ago)
1003 *
1004 * Let u_i denote the fraction of p_i that the entity was runnable.
1005 *
1006 * We then designate the fractions u_i as our co-efficients, yielding the
1007 * following representation of historical load:
1008 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
1009 *
1010 * We choose y based on the with of a reasonably scheduling period, fixing:
1011 * y^32 = 0.5
1012 *
1013 * This means that the contribution to load ~32ms ago (u_32) will be weighted
1014 * approximately half as much as the contribution to load within the last ms
1015 * (u_0).
1016 *
1017 * When a period "rolls over" and we have new u_0`, multiplying the previous
1018 * sum again by y is sufficient to update:
1019 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1020 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1021 */
1022static __always_inline int __update_entity_runnable_avg(u64 now,
1023 struct sched_avg *sa,
1024 int runnable)
1025{
1026 u64 delta;
1027 int delta_w, decayed = 0;
1028
1029 delta = now - sa->last_runnable_update;
1030 /*
1031 * This should only happen when time goes backwards, which it
1032 * unfortunately does during sched clock init when we swap over to TSC.
1033 */
1034 if ((s64)delta < 0) {
1035 sa->last_runnable_update = now;
1036 return 0;
1037 }
1038
1039 /*
1040 * Use 1024ns as the unit of measurement since it's a reasonable
1041 * approximation of 1us and fast to compute.
1042 */
1043 delta >>= 10;
1044 if (!delta)
1045 return 0;
1046 sa->last_runnable_update = now;
1047
1048 /* delta_w is the amount already accumulated against our next period */
1049 delta_w = sa->runnable_avg_period % 1024;
1050 if (delta + delta_w >= 1024) {
1051 /* period roll-over */
1052 decayed = 1;
1053
1054 /*
1055 * Now that we know we're crossing a period boundary, figure
1056 * out how much from delta we need to complete the current
1057 * period and accrue it.
1058 */
1059 delta_w = 1024 - delta_w;
1060 BUG_ON(delta_w > delta);
1061 do {
1062 if (runnable)
1063 sa->runnable_avg_sum += delta_w;
1064 sa->runnable_avg_period += delta_w;
1065
1066 /*
1067 * Remainder of delta initiates a new period, roll over
1068 * the previous.
1069 */
1070 sa->runnable_avg_sum =
1071 decay_load(sa->runnable_avg_sum, 1);
1072 sa->runnable_avg_period =
1073 decay_load(sa->runnable_avg_period, 1);
1074
1075 delta -= delta_w;
1076 /* New period is empty */
1077 delta_w = 1024;
1078 } while (delta >= 1024);
1079 }
1080
1081 /* Remainder of delta accrued against u_0` */
1082 if (runnable)
1083 sa->runnable_avg_sum += delta;
1084 sa->runnable_avg_period += delta;
1085
1086 return decayed;
1087}
1088
Paul Turner9ee474f2012-10-04 13:18:30 +02001089/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02001090static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02001091{
1092 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1093 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1094
1095 decays -= se->avg.decay_count;
1096 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02001097 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02001098
1099 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1100 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02001101
1102 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02001103}
1104
Paul Turnerc566e8e2012-10-04 13:18:30 +02001105#ifdef CONFIG_FAIR_GROUP_SCHED
1106static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1107 int force_update)
1108{
1109 struct task_group *tg = cfs_rq->tg;
1110 s64 tg_contrib;
1111
1112 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
1113 tg_contrib -= cfs_rq->tg_load_contrib;
1114
1115 if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
1116 atomic64_add(tg_contrib, &tg->load_avg);
1117 cfs_rq->tg_load_contrib += tg_contrib;
1118 }
1119}
Paul Turner8165e142012-10-04 13:18:31 +02001120
Paul Turnerbb17f652012-10-04 13:18:31 +02001121/*
1122 * Aggregate cfs_rq runnable averages into an equivalent task_group
1123 * representation for computing load contributions.
1124 */
1125static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1126 struct cfs_rq *cfs_rq)
1127{
1128 struct task_group *tg = cfs_rq->tg;
1129 long contrib;
1130
1131 /* The fraction of a cpu used by this cfs_rq */
1132 contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
1133 sa->runnable_avg_period + 1);
1134 contrib -= cfs_rq->tg_runnable_contrib;
1135
1136 if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
1137 atomic_add(contrib, &tg->runnable_avg);
1138 cfs_rq->tg_runnable_contrib += contrib;
1139 }
1140}
1141
Paul Turner8165e142012-10-04 13:18:31 +02001142static inline void __update_group_entity_contrib(struct sched_entity *se)
1143{
1144 struct cfs_rq *cfs_rq = group_cfs_rq(se);
1145 struct task_group *tg = cfs_rq->tg;
Paul Turnerbb17f652012-10-04 13:18:31 +02001146 int runnable_avg;
1147
Paul Turner8165e142012-10-04 13:18:31 +02001148 u64 contrib;
1149
1150 contrib = cfs_rq->tg_load_contrib * tg->shares;
1151 se->avg.load_avg_contrib = div64_u64(contrib,
1152 atomic64_read(&tg->load_avg) + 1);
Paul Turnerbb17f652012-10-04 13:18:31 +02001153
1154 /*
1155 * For group entities we need to compute a correction term in the case
1156 * that they are consuming <1 cpu so that we would contribute the same
1157 * load as a task of equal weight.
1158 *
1159 * Explicitly co-ordinating this measurement would be expensive, but
1160 * fortunately the sum of each cpus contribution forms a usable
1161 * lower-bound on the true value.
1162 *
1163 * Consider the aggregate of 2 contributions. Either they are disjoint
1164 * (and the sum represents true value) or they are disjoint and we are
1165 * understating by the aggregate of their overlap.
1166 *
1167 * Extending this to N cpus, for a given overlap, the maximum amount we
1168 * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
1169 * cpus that overlap for this interval and w_i is the interval width.
1170 *
1171 * On a small machine; the first term is well-bounded which bounds the
1172 * total error since w_i is a subset of the period. Whereas on a
1173 * larger machine, while this first term can be larger, if w_i is the
1174 * of consequential size guaranteed to see n_i*w_i quickly converge to
1175 * our upper bound of 1-cpu.
1176 */
1177 runnable_avg = atomic_read(&tg->runnable_avg);
1178 if (runnable_avg < NICE_0_LOAD) {
1179 se->avg.load_avg_contrib *= runnable_avg;
1180 se->avg.load_avg_contrib >>= NICE_0_SHIFT;
1181 }
Paul Turner8165e142012-10-04 13:18:31 +02001182}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001183#else
1184static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1185 int force_update) {}
Paul Turnerbb17f652012-10-04 13:18:31 +02001186static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1187 struct cfs_rq *cfs_rq) {}
Paul Turner8165e142012-10-04 13:18:31 +02001188static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001189#endif
1190
Paul Turner8165e142012-10-04 13:18:31 +02001191static inline void __update_task_entity_contrib(struct sched_entity *se)
1192{
1193 u32 contrib;
1194
1195 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
1196 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
1197 contrib /= (se->avg.runnable_avg_period + 1);
1198 se->avg.load_avg_contrib = scale_load(contrib);
1199}
1200
Paul Turner2dac7542012-10-04 13:18:30 +02001201/* Compute the current contribution to load_avg by se, return any delta */
1202static long __update_entity_load_avg_contrib(struct sched_entity *se)
1203{
1204 long old_contrib = se->avg.load_avg_contrib;
1205
Paul Turner8165e142012-10-04 13:18:31 +02001206 if (entity_is_task(se)) {
1207 __update_task_entity_contrib(se);
1208 } else {
Paul Turnerbb17f652012-10-04 13:18:31 +02001209 __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
Paul Turner8165e142012-10-04 13:18:31 +02001210 __update_group_entity_contrib(se);
1211 }
Paul Turner2dac7542012-10-04 13:18:30 +02001212
1213 return se->avg.load_avg_contrib - old_contrib;
1214}
1215
Paul Turner9ee474f2012-10-04 13:18:30 +02001216static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1217 long load_contrib)
1218{
1219 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1220 cfs_rq->blocked_load_avg -= load_contrib;
1221 else
1222 cfs_rq->blocked_load_avg = 0;
1223}
1224
Paul Turner9d85f212012-10-04 13:18:29 +02001225/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001226static inline void update_entity_load_avg(struct sched_entity *se,
1227 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001228{
Paul Turner2dac7542012-10-04 13:18:30 +02001229 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1230 long contrib_delta;
1231
1232 if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
1233 se->on_rq))
1234 return;
1235
1236 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001237
1238 if (!update_cfs_rq)
1239 return;
1240
Paul Turner2dac7542012-10-04 13:18:30 +02001241 if (se->on_rq)
1242 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001243 else
1244 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1245}
1246
1247/*
1248 * Decay the load contributed by all blocked children and account this so that
1249 * their contribution may appropriately discounted when they wake up.
1250 */
Paul Turneraff3e492012-10-04 13:18:30 +02001251static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001252{
1253 u64 now = rq_of(cfs_rq)->clock_task >> 20;
1254 u64 decays;
1255
1256 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001257 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001258 return;
1259
Paul Turneraff3e492012-10-04 13:18:30 +02001260 if (atomic64_read(&cfs_rq->removed_load)) {
1261 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1262 subtract_blocked_load_contrib(cfs_rq, removed_load);
1263 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001264
Paul Turneraff3e492012-10-04 13:18:30 +02001265 if (decays) {
1266 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1267 decays);
1268 atomic64_add(decays, &cfs_rq->decay_counter);
1269 cfs_rq->last_decay = now;
1270 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001271
1272 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02001273}
Ben Segall18bf2802012-10-04 12:51:20 +02001274
1275static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1276{
1277 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
Paul Turnerbb17f652012-10-04 13:18:31 +02001278 __update_tg_runnable_avg(&rq->avg, &rq->cfs);
Ben Segall18bf2802012-10-04 12:51:20 +02001279}
Paul Turner2dac7542012-10-04 13:18:30 +02001280
1281/* Add the load generated by se into cfs_rq's child load-average */
1282static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001283 struct sched_entity *se,
1284 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001285{
Paul Turneraff3e492012-10-04 13:18:30 +02001286 /*
1287 * We track migrations using entity decay_count <= 0, on a wake-up
1288 * migration we use a negative decay count to track the remote decays
1289 * accumulated while sleeping.
1290 */
1291 if (unlikely(se->avg.decay_count <= 0)) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001292 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
Paul Turneraff3e492012-10-04 13:18:30 +02001293 if (se->avg.decay_count) {
1294 /*
1295 * In a wake-up migration we have to approximate the
1296 * time sleeping. This is because we can't synchronize
1297 * clock_task between the two cpus, and it is not
1298 * guaranteed to be read-safe. Instead, we can
1299 * approximate this using our carried decays, which are
1300 * explicitly atomically readable.
1301 */
1302 se->avg.last_runnable_update -= (-se->avg.decay_count)
1303 << 20;
1304 update_entity_load_avg(se, 0);
1305 /* Indicate that we're now synchronized and on-rq */
1306 se->avg.decay_count = 0;
1307 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001308 wakeup = 0;
1309 } else {
1310 __synchronize_entity_decay(se);
1311 }
1312
Paul Turneraff3e492012-10-04 13:18:30 +02001313 /* migrated tasks did not contribute to our blocked load */
1314 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001315 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02001316 update_entity_load_avg(se, 0);
1317 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001318
Paul Turner2dac7542012-10-04 13:18:30 +02001319 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02001320 /* we force update consideration on load-balancer moves */
1321 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02001322}
1323
Paul Turner9ee474f2012-10-04 13:18:30 +02001324/*
1325 * Remove se's load from this cfs_rq child load-average, if the entity is
1326 * transitioning to a blocked state we track its projected decay using
1327 * blocked_load_avg.
1328 */
Paul Turner2dac7542012-10-04 13:18:30 +02001329static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001330 struct sched_entity *se,
1331 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001332{
Paul Turner9ee474f2012-10-04 13:18:30 +02001333 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001334 /* we force update consideration on load-balancer moves */
1335 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02001336
Paul Turner2dac7542012-10-04 13:18:30 +02001337 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001338 if (sleep) {
1339 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1340 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1341 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001342}
Paul Turner9d85f212012-10-04 13:18:29 +02001343#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001344static inline void update_entity_load_avg(struct sched_entity *se,
1345 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001346static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001347static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001348 struct sched_entity *se,
1349 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001350static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001351 struct sched_entity *se,
1352 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02001353static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
1354 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001355#endif
1356
Ingo Molnar2396af62007-08-09 11:16:48 +02001357static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001358{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001359#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001360 struct task_struct *tsk = NULL;
1361
1362 if (entity_is_task(se))
1363 tsk = task_of(se);
1364
Lucas De Marchi41acab82010-03-10 23:37:45 -03001365 if (se->statistics.sleep_start) {
1366 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001367
1368 if ((s64)delta < 0)
1369 delta = 0;
1370
Lucas De Marchi41acab82010-03-10 23:37:45 -03001371 if (unlikely(delta > se->statistics.sleep_max))
1372 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001373
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001374 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001375 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001376
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001377 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001378 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001379 trace_sched_stat_sleep(tsk, delta);
1380 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001381 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001382 if (se->statistics.block_start) {
1383 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001384
1385 if ((s64)delta < 0)
1386 delta = 0;
1387
Lucas De Marchi41acab82010-03-10 23:37:45 -03001388 if (unlikely(delta > se->statistics.block_max))
1389 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001390
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001391 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001392 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001393
Peter Zijlstrae4143142009-07-23 20:13:26 +02001394 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001395 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001396 se->statistics.iowait_sum += delta;
1397 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001398 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001399 }
1400
Andrew Vaginb781a602011-11-28 12:03:35 +03001401 trace_sched_stat_blocked(tsk, delta);
1402
Peter Zijlstrae4143142009-07-23 20:13:26 +02001403 /*
1404 * Blocking time is in units of nanosecs, so shift by
1405 * 20 to get a milliseconds-range estimation of the
1406 * amount of time that the task spent sleeping:
1407 */
1408 if (unlikely(prof_on == SLEEP_PROFILING)) {
1409 profile_hits(SLEEP_PROFILING,
1410 (void *)get_wchan(tsk),
1411 delta >> 20);
1412 }
1413 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001414 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001415 }
1416#endif
1417}
1418
Peter Zijlstraddc97292007-10-15 17:00:10 +02001419static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1420{
1421#ifdef CONFIG_SCHED_DEBUG
1422 s64 d = se->vruntime - cfs_rq->min_vruntime;
1423
1424 if (d < 0)
1425 d = -d;
1426
1427 if (d > 3*sysctl_sched_latency)
1428 schedstat_inc(cfs_rq, nr_spread_over);
1429#endif
1430}
1431
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001432static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001433place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1434{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001435 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001436
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001437 /*
1438 * The 'current' period is already promised to the current tasks,
1439 * however the extra weight of the new task will slow them down a
1440 * little, place the new task so that it fits in the slot that
1441 * stays open at the end.
1442 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001443 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001444 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001445
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001446 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001447 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001448 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001449
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001450 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001451 * Halve their sleep time's effect, to allow
1452 * for a gentler effect of sleepers:
1453 */
1454 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1455 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001456
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001457 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001458 }
1459
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001460 /* ensure we never gain time by being placed backwards. */
1461 vruntime = max_vruntime(se->vruntime, vruntime);
1462
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001463 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001464}
1465
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001466static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1467
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001468static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001469enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001470{
1471 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001472 * Update the normalized vruntime before updating min_vruntime
1473 * through callig update_curr().
1474 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001475 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001476 se->vruntime += cfs_rq->min_vruntime;
1477
1478 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001479 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001480 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001481 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001482 update_cfs_load(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02001483 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001484 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001485 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001486
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001487 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001488 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001489 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001490 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001491
Ingo Molnard2417e52007-08-09 11:16:47 +02001492 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001493 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001494 if (se != cfs_rq->curr)
1495 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001496 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001497
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001498 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001499 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001500 check_enqueue_throttle(cfs_rq);
1501 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001502}
1503
Rik van Riel2c13c9192011-02-01 09:48:37 -05001504static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001505{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001506 for_each_sched_entity(se) {
1507 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1508 if (cfs_rq->last == se)
1509 cfs_rq->last = NULL;
1510 else
1511 break;
1512 }
1513}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001514
Rik van Riel2c13c9192011-02-01 09:48:37 -05001515static void __clear_buddies_next(struct sched_entity *se)
1516{
1517 for_each_sched_entity(se) {
1518 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1519 if (cfs_rq->next == se)
1520 cfs_rq->next = NULL;
1521 else
1522 break;
1523 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001524}
1525
Rik van Rielac53db52011-02-01 09:51:03 -05001526static void __clear_buddies_skip(struct sched_entity *se)
1527{
1528 for_each_sched_entity(se) {
1529 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1530 if (cfs_rq->skip == se)
1531 cfs_rq->skip = NULL;
1532 else
1533 break;
1534 }
1535}
1536
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001537static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1538{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001539 if (cfs_rq->last == se)
1540 __clear_buddies_last(se);
1541
1542 if (cfs_rq->next == se)
1543 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001544
1545 if (cfs_rq->skip == se)
1546 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001547}
1548
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001549static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001550
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001551static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001552dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001553{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001554 /*
1555 * Update run-time statistics of the 'current'.
1556 */
1557 update_curr(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02001558 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001559
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001560 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001561 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001562#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001563 if (entity_is_task(se)) {
1564 struct task_struct *tsk = task_of(se);
1565
1566 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001567 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001568 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001569 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001570 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001571#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001572 }
1573
Peter Zijlstra2002c692008-11-11 11:52:33 +01001574 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001575
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001576 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001577 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001578 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001579 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001580 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001581
1582 /*
1583 * Normalize the entity after updating the min_vruntime because the
1584 * update can refer to the ->curr item and we need to reflect this
1585 * movement in our normalized position.
1586 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001587 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001588 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001589
Paul Turnerd8b49862011-07-21 09:43:41 -07001590 /* return excess runtime on last dequeue */
1591 return_cfs_rq_runtime(cfs_rq);
1592
Peter Zijlstra1e876232011-05-17 16:21:10 -07001593 update_min_vruntime(cfs_rq);
1594 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001595}
1596
1597/*
1598 * Preempt the current task with a newly woken task if needed:
1599 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001600static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001601check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001602{
Peter Zijlstra11697832007-09-05 14:32:49 +02001603 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001604 struct sched_entity *se;
1605 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001606
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001607 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001608 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001609 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001610 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001611 /*
1612 * The current task ran long enough, ensure it doesn't get
1613 * re-elected due to buddy favours.
1614 */
1615 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001616 return;
1617 }
1618
1619 /*
1620 * Ensure that a task that missed wakeup preemption by a
1621 * narrow margin doesn't have to wait for a full slice.
1622 * This also mitigates buddy induced latencies under load.
1623 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001624 if (delta_exec < sysctl_sched_min_granularity)
1625 return;
1626
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001627 se = __pick_first_entity(cfs_rq);
1628 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001629
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001630 if (delta < 0)
1631 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001632
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001633 if (delta > ideal_runtime)
1634 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001635}
1636
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001637static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001638set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001639{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001640 /* 'current' is not kept within the tree. */
1641 if (se->on_rq) {
1642 /*
1643 * Any task has to be enqueued before it get to execute on
1644 * a CPU. So account for the time it spent waiting on the
1645 * runqueue.
1646 */
1647 update_stats_wait_end(cfs_rq, se);
1648 __dequeue_entity(cfs_rq, se);
1649 }
1650
Ingo Molnar79303e92007-08-09 11:16:47 +02001651 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001652 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001653#ifdef CONFIG_SCHEDSTATS
1654 /*
1655 * Track our maximum slice length, if the CPU's load is at
1656 * least twice that of our own weight (i.e. dont track it
1657 * when there are only lesser-weight tasks around):
1658 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001659 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001660 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001661 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1662 }
1663#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001664 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001665}
1666
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001667static int
1668wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1669
Rik van Rielac53db52011-02-01 09:51:03 -05001670/*
1671 * Pick the next process, keeping these things in mind, in this order:
1672 * 1) keep things fair between processes/task groups
1673 * 2) pick the "next" process, since someone really wants that to run
1674 * 3) pick the "last" process, for cache locality
1675 * 4) do not run the "skip" process, if something else is available
1676 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001677static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001678{
Rik van Rielac53db52011-02-01 09:51:03 -05001679 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001680 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001681
Rik van Rielac53db52011-02-01 09:51:03 -05001682 /*
1683 * Avoid running the skip buddy, if running something else can
1684 * be done without getting too unfair.
1685 */
1686 if (cfs_rq->skip == se) {
1687 struct sched_entity *second = __pick_next_entity(se);
1688 if (second && wakeup_preempt_entity(second, left) < 1)
1689 se = second;
1690 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001691
Mike Galbraithf685cea2009-10-23 23:09:22 +02001692 /*
1693 * Prefer last buddy, try to return the CPU to a preempted task.
1694 */
1695 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1696 se = cfs_rq->last;
1697
Rik van Rielac53db52011-02-01 09:51:03 -05001698 /*
1699 * Someone really wants this to run. If it's not unfair, run it.
1700 */
1701 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1702 se = cfs_rq->next;
1703
Mike Galbraithf685cea2009-10-23 23:09:22 +02001704 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001705
1706 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001707}
1708
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001709static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1710
Ingo Molnarab6cde22007-08-09 11:16:48 +02001711static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001712{
1713 /*
1714 * If still on the runqueue then deactivate_task()
1715 * was not called and update_curr() has to be done:
1716 */
1717 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001718 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001719
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001720 /* throttle cfs_rqs exceeding runtime */
1721 check_cfs_rq_runtime(cfs_rq);
1722
Peter Zijlstraddc97292007-10-15 17:00:10 +02001723 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001724 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001725 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001726 /* Put 'current' back into the tree. */
1727 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001728 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001729 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001730 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001731 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001732}
1733
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001734static void
1735entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001736{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001737 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001738 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001739 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001740 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001741
Paul Turner43365bd2010-12-15 19:10:17 -08001742 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001743 * Ensure that runnable average is periodically updated.
1744 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001745 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001746 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001747
1748 /*
Paul Turner43365bd2010-12-15 19:10:17 -08001749 * Update share accounting for long-running entities.
1750 */
1751 update_entity_shares_tick(cfs_rq);
1752
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001753#ifdef CONFIG_SCHED_HRTICK
1754 /*
1755 * queued ticks are scheduled to match the slice, so don't bother
1756 * validating it and just reschedule.
1757 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001758 if (queued) {
1759 resched_task(rq_of(cfs_rq)->curr);
1760 return;
1761 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001762 /*
1763 * don't let the period tick interfere with the hrtick preemption
1764 */
1765 if (!sched_feat(DOUBLE_TICK) &&
1766 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1767 return;
1768#endif
1769
Yong Zhang2c2efae2011-07-29 16:20:33 +08001770 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001771 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001772}
1773
Paul Turnerab84d312011-07-21 09:43:28 -07001774
1775/**************************************************
1776 * CFS bandwidth control machinery
1777 */
1778
1779#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001780
1781#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001782static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001783
1784static inline bool cfs_bandwidth_used(void)
1785{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001786 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001787}
1788
1789void account_cfs_bandwidth_used(int enabled, int was_enabled)
1790{
1791 /* only need to count groups transitioning between enabled/!enabled */
1792 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001793 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001794 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001795 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001796}
1797#else /* HAVE_JUMP_LABEL */
1798static bool cfs_bandwidth_used(void)
1799{
1800 return true;
1801}
1802
1803void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1804#endif /* HAVE_JUMP_LABEL */
1805
Paul Turnerab84d312011-07-21 09:43:28 -07001806/*
1807 * default period for cfs group bandwidth.
1808 * default: 0.1s, units: nanoseconds
1809 */
1810static inline u64 default_cfs_period(void)
1811{
1812 return 100000000ULL;
1813}
Paul Turnerec12cb72011-07-21 09:43:30 -07001814
1815static inline u64 sched_cfs_bandwidth_slice(void)
1816{
1817 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1818}
1819
Paul Turnera9cf55b2011-07-21 09:43:32 -07001820/*
1821 * Replenish runtime according to assigned quota and update expiration time.
1822 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1823 * additional synchronization around rq->lock.
1824 *
1825 * requires cfs_b->lock
1826 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001827void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001828{
1829 u64 now;
1830
1831 if (cfs_b->quota == RUNTIME_INF)
1832 return;
1833
1834 now = sched_clock_cpu(smp_processor_id());
1835 cfs_b->runtime = cfs_b->quota;
1836 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1837}
1838
Peter Zijlstra029632f2011-10-25 10:00:11 +02001839static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1840{
1841 return &tg->cfs_bandwidth;
1842}
1843
Paul Turner85dac902011-07-21 09:43:33 -07001844/* returns 0 on failure to allocate runtime */
1845static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001846{
1847 struct task_group *tg = cfs_rq->tg;
1848 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001849 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001850
1851 /* note: this is a positive sum as runtime_remaining <= 0 */
1852 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1853
1854 raw_spin_lock(&cfs_b->lock);
1855 if (cfs_b->quota == RUNTIME_INF)
1856 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001857 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001858 /*
1859 * If the bandwidth pool has become inactive, then at least one
1860 * period must have elapsed since the last consumption.
1861 * Refresh the global state and ensure bandwidth timer becomes
1862 * active.
1863 */
1864 if (!cfs_b->timer_active) {
1865 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001866 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001867 }
Paul Turner58088ad2011-07-21 09:43:31 -07001868
1869 if (cfs_b->runtime > 0) {
1870 amount = min(cfs_b->runtime, min_amount);
1871 cfs_b->runtime -= amount;
1872 cfs_b->idle = 0;
1873 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001874 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001875 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001876 raw_spin_unlock(&cfs_b->lock);
1877
1878 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001879 /*
1880 * we may have advanced our local expiration to account for allowed
1881 * spread between our sched_clock and the one on which runtime was
1882 * issued.
1883 */
1884 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1885 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001886
1887 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001888}
1889
1890/*
1891 * Note: This depends on the synchronization provided by sched_clock and the
1892 * fact that rq->clock snapshots this value.
1893 */
1894static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1895{
1896 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1897 struct rq *rq = rq_of(cfs_rq);
1898
1899 /* if the deadline is ahead of our clock, nothing to do */
1900 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1901 return;
1902
1903 if (cfs_rq->runtime_remaining < 0)
1904 return;
1905
1906 /*
1907 * If the local deadline has passed we have to consider the
1908 * possibility that our sched_clock is 'fast' and the global deadline
1909 * has not truly expired.
1910 *
1911 * Fortunately we can check determine whether this the case by checking
1912 * whether the global deadline has advanced.
1913 */
1914
1915 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1916 /* extend local deadline, drift is bounded above by 2 ticks */
1917 cfs_rq->runtime_expires += TICK_NSEC;
1918 } else {
1919 /* global deadline is ahead, expiration has passed */
1920 cfs_rq->runtime_remaining = 0;
1921 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001922}
1923
1924static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1925 unsigned long delta_exec)
1926{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001927 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001928 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001929 expire_cfs_rq_runtime(cfs_rq);
1930
1931 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001932 return;
1933
Paul Turner85dac902011-07-21 09:43:33 -07001934 /*
1935 * if we're unable to extend our runtime we resched so that the active
1936 * hierarchy can be throttled
1937 */
1938 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1939 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001940}
1941
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001942static __always_inline
1943void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001944{
Paul Turner56f570e2011-11-07 20:26:33 -08001945 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001946 return;
1947
1948 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1949}
1950
Paul Turner85dac902011-07-21 09:43:33 -07001951static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1952{
Paul Turner56f570e2011-11-07 20:26:33 -08001953 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001954}
1955
Paul Turner64660c82011-07-21 09:43:36 -07001956/* check whether cfs_rq, or any parent, is throttled */
1957static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1958{
Paul Turner56f570e2011-11-07 20:26:33 -08001959 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001960}
1961
1962/*
1963 * Ensure that neither of the group entities corresponding to src_cpu or
1964 * dest_cpu are members of a throttled hierarchy when performing group
1965 * load-balance operations.
1966 */
1967static inline int throttled_lb_pair(struct task_group *tg,
1968 int src_cpu, int dest_cpu)
1969{
1970 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1971
1972 src_cfs_rq = tg->cfs_rq[src_cpu];
1973 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1974
1975 return throttled_hierarchy(src_cfs_rq) ||
1976 throttled_hierarchy(dest_cfs_rq);
1977}
1978
1979/* updated child weight may affect parent so we have to do this bottom up */
1980static int tg_unthrottle_up(struct task_group *tg, void *data)
1981{
1982 struct rq *rq = data;
1983 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1984
1985 cfs_rq->throttle_count--;
1986#ifdef CONFIG_SMP
1987 if (!cfs_rq->throttle_count) {
1988 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1989
1990 /* leaving throttled state, advance shares averaging windows */
1991 cfs_rq->load_stamp += delta;
1992 cfs_rq->load_last += delta;
1993
1994 /* update entity weight now that we are on_rq again */
1995 update_cfs_shares(cfs_rq);
1996 }
1997#endif
1998
1999 return 0;
2000}
2001
2002static int tg_throttle_down(struct task_group *tg, void *data)
2003{
2004 struct rq *rq = data;
2005 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
2006
2007 /* group is entering throttled state, record last load */
2008 if (!cfs_rq->throttle_count)
2009 update_cfs_load(cfs_rq, 0);
2010 cfs_rq->throttle_count++;
2011
2012 return 0;
2013}
2014
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002015static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07002016{
2017 struct rq *rq = rq_of(cfs_rq);
2018 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2019 struct sched_entity *se;
2020 long task_delta, dequeue = 1;
2021
2022 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2023
2024 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07002025 rcu_read_lock();
2026 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
2027 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07002028
2029 task_delta = cfs_rq->h_nr_running;
2030 for_each_sched_entity(se) {
2031 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
2032 /* throttled entity or throttle-on-deactivate */
2033 if (!se->on_rq)
2034 break;
2035
2036 if (dequeue)
2037 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
2038 qcfs_rq->h_nr_running -= task_delta;
2039
2040 if (qcfs_rq->load.weight)
2041 dequeue = 0;
2042 }
2043
2044 if (!se)
2045 rq->nr_running -= task_delta;
2046
2047 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002048 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07002049 raw_spin_lock(&cfs_b->lock);
2050 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
2051 raw_spin_unlock(&cfs_b->lock);
2052}
2053
Peter Zijlstra029632f2011-10-25 10:00:11 +02002054void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07002055{
2056 struct rq *rq = rq_of(cfs_rq);
2057 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2058 struct sched_entity *se;
2059 int enqueue = 1;
2060 long task_delta;
2061
2062 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2063
2064 cfs_rq->throttled = 0;
2065 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002066 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07002067 list_del_rcu(&cfs_rq->throttled_list);
2068 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002069 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07002070
Paul Turner64660c82011-07-21 09:43:36 -07002071 update_rq_clock(rq);
2072 /* update hierarchical throttle state */
2073 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
2074
Paul Turner671fd9d2011-07-21 09:43:34 -07002075 if (!cfs_rq->load.weight)
2076 return;
2077
2078 task_delta = cfs_rq->h_nr_running;
2079 for_each_sched_entity(se) {
2080 if (se->on_rq)
2081 enqueue = 0;
2082
2083 cfs_rq = cfs_rq_of(se);
2084 if (enqueue)
2085 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2086 cfs_rq->h_nr_running += task_delta;
2087
2088 if (cfs_rq_throttled(cfs_rq))
2089 break;
2090 }
2091
2092 if (!se)
2093 rq->nr_running += task_delta;
2094
2095 /* determine whether we need to wake up potentially idle cpu */
2096 if (rq->curr == rq->idle && rq->cfs.nr_running)
2097 resched_task(rq->curr);
2098}
2099
2100static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2101 u64 remaining, u64 expires)
2102{
2103 struct cfs_rq *cfs_rq;
2104 u64 runtime = remaining;
2105
2106 rcu_read_lock();
2107 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2108 throttled_list) {
2109 struct rq *rq = rq_of(cfs_rq);
2110
2111 raw_spin_lock(&rq->lock);
2112 if (!cfs_rq_throttled(cfs_rq))
2113 goto next;
2114
2115 runtime = -cfs_rq->runtime_remaining + 1;
2116 if (runtime > remaining)
2117 runtime = remaining;
2118 remaining -= runtime;
2119
2120 cfs_rq->runtime_remaining += runtime;
2121 cfs_rq->runtime_expires = expires;
2122
2123 /* we check whether we're throttled above */
2124 if (cfs_rq->runtime_remaining > 0)
2125 unthrottle_cfs_rq(cfs_rq);
2126
2127next:
2128 raw_spin_unlock(&rq->lock);
2129
2130 if (!remaining)
2131 break;
2132 }
2133 rcu_read_unlock();
2134
2135 return remaining;
2136}
2137
Paul Turner58088ad2011-07-21 09:43:31 -07002138/*
2139 * Responsible for refilling a task_group's bandwidth and unthrottling its
2140 * cfs_rqs as appropriate. If there has been no activity within the last
2141 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2142 * used to track this state.
2143 */
2144static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2145{
Paul Turner671fd9d2011-07-21 09:43:34 -07002146 u64 runtime, runtime_expires;
2147 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002148
2149 raw_spin_lock(&cfs_b->lock);
2150 /* no need to continue the timer with no bandwidth constraint */
2151 if (cfs_b->quota == RUNTIME_INF)
2152 goto out_unlock;
2153
Paul Turner671fd9d2011-07-21 09:43:34 -07002154 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2155 /* idle depends on !throttled (for the case of a large deficit) */
2156 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002157 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002158
Paul Turnera9cf55b2011-07-21 09:43:32 -07002159 /* if we're going inactive then everything else can be deferred */
2160 if (idle)
2161 goto out_unlock;
2162
2163 __refill_cfs_bandwidth_runtime(cfs_b);
2164
Paul Turner671fd9d2011-07-21 09:43:34 -07002165 if (!throttled) {
2166 /* mark as potentially idle for the upcoming period */
2167 cfs_b->idle = 1;
2168 goto out_unlock;
2169 }
Paul Turner58088ad2011-07-21 09:43:31 -07002170
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002171 /* account preceding periods in which throttling occurred */
2172 cfs_b->nr_throttled += overrun;
2173
Paul Turner671fd9d2011-07-21 09:43:34 -07002174 /*
2175 * There are throttled entities so we must first use the new bandwidth
2176 * to unthrottle them before making it generally available. This
2177 * ensures that all existing debts will be paid before a new cfs_rq is
2178 * allowed to run.
2179 */
2180 runtime = cfs_b->runtime;
2181 runtime_expires = cfs_b->runtime_expires;
2182 cfs_b->runtime = 0;
2183
2184 /*
2185 * This check is repeated as we are holding onto the new bandwidth
2186 * while we unthrottle. This can potentially race with an unthrottled
2187 * group trying to acquire new bandwidth from the global pool.
2188 */
2189 while (throttled && runtime > 0) {
2190 raw_spin_unlock(&cfs_b->lock);
2191 /* we can't nest cfs_b->lock while distributing bandwidth */
2192 runtime = distribute_cfs_runtime(cfs_b, runtime,
2193 runtime_expires);
2194 raw_spin_lock(&cfs_b->lock);
2195
2196 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2197 }
2198
2199 /* return (any) remaining runtime */
2200 cfs_b->runtime = runtime;
2201 /*
2202 * While we are ensured activity in the period following an
2203 * unthrottle, this also covers the case in which the new bandwidth is
2204 * insufficient to cover the existing bandwidth deficit. (Forcing the
2205 * timer to remain active while there are any throttled entities.)
2206 */
2207 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002208out_unlock:
2209 if (idle)
2210 cfs_b->timer_active = 0;
2211 raw_spin_unlock(&cfs_b->lock);
2212
2213 return idle;
2214}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002215
Paul Turnerd8b49862011-07-21 09:43:41 -07002216/* a cfs_rq won't donate quota below this amount */
2217static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2218/* minimum remaining period time to redistribute slack quota */
2219static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2220/* how long we wait to gather additional slack before distributing */
2221static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2222
2223/* are we near the end of the current quota period? */
2224static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2225{
2226 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2227 u64 remaining;
2228
2229 /* if the call-back is running a quota refresh is already occurring */
2230 if (hrtimer_callback_running(refresh_timer))
2231 return 1;
2232
2233 /* is a quota refresh about to occur? */
2234 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2235 if (remaining < min_expire)
2236 return 1;
2237
2238 return 0;
2239}
2240
2241static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2242{
2243 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2244
2245 /* if there's a quota refresh soon don't bother with slack */
2246 if (runtime_refresh_within(cfs_b, min_left))
2247 return;
2248
2249 start_bandwidth_timer(&cfs_b->slack_timer,
2250 ns_to_ktime(cfs_bandwidth_slack_period));
2251}
2252
2253/* we know any runtime found here is valid as update_curr() precedes return */
2254static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2255{
2256 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2257 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2258
2259 if (slack_runtime <= 0)
2260 return;
2261
2262 raw_spin_lock(&cfs_b->lock);
2263 if (cfs_b->quota != RUNTIME_INF &&
2264 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2265 cfs_b->runtime += slack_runtime;
2266
2267 /* we are under rq->lock, defer unthrottling using a timer */
2268 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2269 !list_empty(&cfs_b->throttled_cfs_rq))
2270 start_cfs_slack_bandwidth(cfs_b);
2271 }
2272 raw_spin_unlock(&cfs_b->lock);
2273
2274 /* even if it's not valid for return we don't want to try again */
2275 cfs_rq->runtime_remaining -= slack_runtime;
2276}
2277
2278static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2279{
Paul Turner56f570e2011-11-07 20:26:33 -08002280 if (!cfs_bandwidth_used())
2281 return;
2282
Paul Turnerfccfdc62011-11-07 20:26:34 -08002283 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002284 return;
2285
2286 __return_cfs_rq_runtime(cfs_rq);
2287}
2288
2289/*
2290 * This is done with a timer (instead of inline with bandwidth return) since
2291 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2292 */
2293static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2294{
2295 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2296 u64 expires;
2297
2298 /* confirm we're still not at a refresh boundary */
2299 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2300 return;
2301
2302 raw_spin_lock(&cfs_b->lock);
2303 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2304 runtime = cfs_b->runtime;
2305 cfs_b->runtime = 0;
2306 }
2307 expires = cfs_b->runtime_expires;
2308 raw_spin_unlock(&cfs_b->lock);
2309
2310 if (!runtime)
2311 return;
2312
2313 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2314
2315 raw_spin_lock(&cfs_b->lock);
2316 if (expires == cfs_b->runtime_expires)
2317 cfs_b->runtime = runtime;
2318 raw_spin_unlock(&cfs_b->lock);
2319}
2320
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002321/*
2322 * When a group wakes up we want to make sure that its quota is not already
2323 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2324 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2325 */
2326static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2327{
Paul Turner56f570e2011-11-07 20:26:33 -08002328 if (!cfs_bandwidth_used())
2329 return;
2330
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002331 /* an active group must be handled by the update_curr()->put() path */
2332 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2333 return;
2334
2335 /* ensure the group is not already throttled */
2336 if (cfs_rq_throttled(cfs_rq))
2337 return;
2338
2339 /* update runtime allocation */
2340 account_cfs_rq_runtime(cfs_rq, 0);
2341 if (cfs_rq->runtime_remaining <= 0)
2342 throttle_cfs_rq(cfs_rq);
2343}
2344
2345/* conditionally throttle active cfs_rq's from put_prev_entity() */
2346static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2347{
Paul Turner56f570e2011-11-07 20:26:33 -08002348 if (!cfs_bandwidth_used())
2349 return;
2350
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002351 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2352 return;
2353
2354 /*
2355 * it's possible for a throttled entity to be forced into a running
2356 * state (e.g. set_curr_task), in this case we're finished.
2357 */
2358 if (cfs_rq_throttled(cfs_rq))
2359 return;
2360
2361 throttle_cfs_rq(cfs_rq);
2362}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002363
2364static inline u64 default_cfs_period(void);
2365static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2366static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2367
2368static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2369{
2370 struct cfs_bandwidth *cfs_b =
2371 container_of(timer, struct cfs_bandwidth, slack_timer);
2372 do_sched_cfs_slack_timer(cfs_b);
2373
2374 return HRTIMER_NORESTART;
2375}
2376
2377static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2378{
2379 struct cfs_bandwidth *cfs_b =
2380 container_of(timer, struct cfs_bandwidth, period_timer);
2381 ktime_t now;
2382 int overrun;
2383 int idle = 0;
2384
2385 for (;;) {
2386 now = hrtimer_cb_get_time(timer);
2387 overrun = hrtimer_forward(timer, now, cfs_b->period);
2388
2389 if (!overrun)
2390 break;
2391
2392 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2393 }
2394
2395 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2396}
2397
2398void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2399{
2400 raw_spin_lock_init(&cfs_b->lock);
2401 cfs_b->runtime = 0;
2402 cfs_b->quota = RUNTIME_INF;
2403 cfs_b->period = ns_to_ktime(default_cfs_period());
2404
2405 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2406 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2407 cfs_b->period_timer.function = sched_cfs_period_timer;
2408 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2409 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2410}
2411
2412static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2413{
2414 cfs_rq->runtime_enabled = 0;
2415 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2416}
2417
2418/* requires cfs_b->lock, may release to reprogram timer */
2419void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2420{
2421 /*
2422 * The timer may be active because we're trying to set a new bandwidth
2423 * period or because we're racing with the tear-down path
2424 * (timer_active==0 becomes visible before the hrtimer call-back
2425 * terminates). In either case we ensure that it's re-programmed
2426 */
2427 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2428 raw_spin_unlock(&cfs_b->lock);
2429 /* ensure cfs_b->lock is available while we wait */
2430 hrtimer_cancel(&cfs_b->period_timer);
2431
2432 raw_spin_lock(&cfs_b->lock);
2433 /* if someone else restarted the timer then we're done */
2434 if (cfs_b->timer_active)
2435 return;
2436 }
2437
2438 cfs_b->timer_active = 1;
2439 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2440}
2441
2442static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2443{
2444 hrtimer_cancel(&cfs_b->period_timer);
2445 hrtimer_cancel(&cfs_b->slack_timer);
2446}
2447
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002448static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002449{
2450 struct cfs_rq *cfs_rq;
2451
2452 for_each_leaf_cfs_rq(rq, cfs_rq) {
2453 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2454
2455 if (!cfs_rq->runtime_enabled)
2456 continue;
2457
2458 /*
2459 * clock_task is not advancing so we just need to make sure
2460 * there's some valid quota amount
2461 */
2462 cfs_rq->runtime_remaining = cfs_b->quota;
2463 if (cfs_rq_throttled(cfs_rq))
2464 unthrottle_cfs_rq(cfs_rq);
2465 }
2466}
2467
2468#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002469static __always_inline
2470void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002471static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2472static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002473static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002474
2475static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2476{
2477 return 0;
2478}
Paul Turner64660c82011-07-21 09:43:36 -07002479
2480static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2481{
2482 return 0;
2483}
2484
2485static inline int throttled_lb_pair(struct task_group *tg,
2486 int src_cpu, int dest_cpu)
2487{
2488 return 0;
2489}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002490
2491void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2492
2493#ifdef CONFIG_FAIR_GROUP_SCHED
2494static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002495#endif
2496
Peter Zijlstra029632f2011-10-25 10:00:11 +02002497static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2498{
2499 return NULL;
2500}
2501static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002502static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002503
2504#endif /* CONFIG_CFS_BANDWIDTH */
2505
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002506/**************************************************
2507 * CFS operations on tasks:
2508 */
2509
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002510#ifdef CONFIG_SCHED_HRTICK
2511static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2512{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002513 struct sched_entity *se = &p->se;
2514 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2515
2516 WARN_ON(task_rq(p) != rq);
2517
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002518 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002519 u64 slice = sched_slice(cfs_rq, se);
2520 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2521 s64 delta = slice - ran;
2522
2523 if (delta < 0) {
2524 if (rq->curr == p)
2525 resched_task(p);
2526 return;
2527 }
2528
2529 /*
2530 * Don't schedule slices shorter than 10000ns, that just
2531 * doesn't make sense. Rely on vruntime for fairness.
2532 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002533 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002534 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002535
Peter Zijlstra31656512008-07-18 18:01:23 +02002536 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002537 }
2538}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002539
2540/*
2541 * called from enqueue/dequeue and updates the hrtick when the
2542 * current task is from our class and nr_running is low enough
2543 * to matter.
2544 */
2545static void hrtick_update(struct rq *rq)
2546{
2547 struct task_struct *curr = rq->curr;
2548
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002549 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002550 return;
2551
2552 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2553 hrtick_start_fair(rq, curr);
2554}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302555#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002556static inline void
2557hrtick_start_fair(struct rq *rq, struct task_struct *p)
2558{
2559}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002560
2561static inline void hrtick_update(struct rq *rq)
2562{
2563}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002564#endif
2565
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002566/*
2567 * The enqueue_task method is called before nr_running is
2568 * increased. Here we update the fair scheduling stats and
2569 * then put the task into the rbtree:
2570 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002571static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002572enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002573{
2574 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002575 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002576
2577 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002578 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002579 break;
2580 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002581 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002582
2583 /*
2584 * end evaluation on encountering a throttled cfs_rq
2585 *
2586 * note: in the case of encountering a throttled cfs_rq we will
2587 * post the final h_nr_running increment below.
2588 */
2589 if (cfs_rq_throttled(cfs_rq))
2590 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002591 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002592
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002593 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002594 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002595
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002596 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002597 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002598 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002599
Paul Turner85dac902011-07-21 09:43:33 -07002600 if (cfs_rq_throttled(cfs_rq))
2601 break;
2602
Paul Turnerd6b55912010-11-15 15:47:09 -08002603 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002604 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002605 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002606 }
2607
Ben Segall18bf2802012-10-04 12:51:20 +02002608 if (!se) {
2609 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002610 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002611 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002612 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002613}
2614
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002615static void set_next_buddy(struct sched_entity *se);
2616
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002617/*
2618 * The dequeue_task method is called before nr_running is
2619 * decreased. We remove the task from the rbtree and
2620 * update the fair scheduling stats:
2621 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002622static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002623{
2624 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002625 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002626 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002627
2628 for_each_sched_entity(se) {
2629 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002630 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002631
2632 /*
2633 * end evaluation on encountering a throttled cfs_rq
2634 *
2635 * note: in the case of encountering a throttled cfs_rq we will
2636 * post the final h_nr_running decrement below.
2637 */
2638 if (cfs_rq_throttled(cfs_rq))
2639 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002640 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002641
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002642 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002643 if (cfs_rq->load.weight) {
2644 /*
2645 * Bias pick_next to pick a task from this cfs_rq, as
2646 * p is sleeping when it is within its sched_slice.
2647 */
2648 if (task_sleep && parent_entity(se))
2649 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002650
2651 /* avoid re-evaluating load for this entity */
2652 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002653 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002654 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002655 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002656 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002657
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002658 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002659 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002660 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002661
Paul Turner85dac902011-07-21 09:43:33 -07002662 if (cfs_rq_throttled(cfs_rq))
2663 break;
2664
Paul Turnerd6b55912010-11-15 15:47:09 -08002665 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002666 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002667 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002668 }
2669
Ben Segall18bf2802012-10-04 12:51:20 +02002670 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002671 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002672 update_rq_runnable_avg(rq, 1);
2673 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002674 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002675}
2676
Gregory Haskinse7693a32008-01-25 21:08:09 +01002677#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002678/* Used instead of source_load when we know the type == 0 */
2679static unsigned long weighted_cpuload(const int cpu)
2680{
2681 return cpu_rq(cpu)->load.weight;
2682}
2683
2684/*
2685 * Return a low guess at the load of a migration-source cpu weighted
2686 * according to the scheduling class and "nice" value.
2687 *
2688 * We want to under-estimate the load of migration sources, to
2689 * balance conservatively.
2690 */
2691static unsigned long source_load(int cpu, int type)
2692{
2693 struct rq *rq = cpu_rq(cpu);
2694 unsigned long total = weighted_cpuload(cpu);
2695
2696 if (type == 0 || !sched_feat(LB_BIAS))
2697 return total;
2698
2699 return min(rq->cpu_load[type-1], total);
2700}
2701
2702/*
2703 * Return a high guess at the load of a migration-target cpu weighted
2704 * according to the scheduling class and "nice" value.
2705 */
2706static unsigned long target_load(int cpu, int type)
2707{
2708 struct rq *rq = cpu_rq(cpu);
2709 unsigned long total = weighted_cpuload(cpu);
2710
2711 if (type == 0 || !sched_feat(LB_BIAS))
2712 return total;
2713
2714 return max(rq->cpu_load[type-1], total);
2715}
2716
2717static unsigned long power_of(int cpu)
2718{
2719 return cpu_rq(cpu)->cpu_power;
2720}
2721
2722static unsigned long cpu_avg_load_per_task(int cpu)
2723{
2724 struct rq *rq = cpu_rq(cpu);
2725 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2726
2727 if (nr_running)
2728 return rq->load.weight / nr_running;
2729
2730 return 0;
2731}
2732
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002733
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002734static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002735{
2736 struct sched_entity *se = &p->se;
2737 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002738 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002739
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002740#ifndef CONFIG_64BIT
2741 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002742
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002743 do {
2744 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2745 smp_rmb();
2746 min_vruntime = cfs_rq->min_vruntime;
2747 } while (min_vruntime != min_vruntime_copy);
2748#else
2749 min_vruntime = cfs_rq->min_vruntime;
2750#endif
2751
2752 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002753}
2754
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002755#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002756/*
2757 * effective_load() calculates the load change as seen from the root_task_group
2758 *
2759 * Adding load to a group doesn't make a group heavier, but can cause movement
2760 * of group shares between cpus. Assuming the shares were perfectly aligned one
2761 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002762 *
2763 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2764 * on this @cpu and results in a total addition (subtraction) of @wg to the
2765 * total group weight.
2766 *
2767 * Given a runqueue weight distribution (rw_i) we can compute a shares
2768 * distribution (s_i) using:
2769 *
2770 * s_i = rw_i / \Sum rw_j (1)
2771 *
2772 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2773 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2774 * shares distribution (s_i):
2775 *
2776 * rw_i = { 2, 4, 1, 0 }
2777 * s_i = { 2/7, 4/7, 1/7, 0 }
2778 *
2779 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2780 * task used to run on and the CPU the waker is running on), we need to
2781 * compute the effect of waking a task on either CPU and, in case of a sync
2782 * wakeup, compute the effect of the current task going to sleep.
2783 *
2784 * So for a change of @wl to the local @cpu with an overall group weight change
2785 * of @wl we can compute the new shares distribution (s'_i) using:
2786 *
2787 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2788 *
2789 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2790 * differences in waking a task to CPU 0. The additional task changes the
2791 * weight and shares distributions like:
2792 *
2793 * rw'_i = { 3, 4, 1, 0 }
2794 * s'_i = { 3/8, 4/8, 1/8, 0 }
2795 *
2796 * We can then compute the difference in effective weight by using:
2797 *
2798 * dw_i = S * (s'_i - s_i) (3)
2799 *
2800 * Where 'S' is the group weight as seen by its parent.
2801 *
2802 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2803 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2804 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002805 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002806static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002807{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002808 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002809
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002810 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002811 return wl;
2812
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002813 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002814 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002815
Paul Turner977dda72011-01-14 17:57:50 -08002816 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002817
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002818 /*
2819 * W = @wg + \Sum rw_j
2820 */
2821 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002822
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002823 /*
2824 * w = rw_i + @wl
2825 */
2826 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002827
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002828 /*
2829 * wl = S * s'_i; see (2)
2830 */
2831 if (W > 0 && w < W)
2832 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002833 else
2834 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002835
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002836 /*
2837 * Per the above, wl is the new se->load.weight value; since
2838 * those are clipped to [MIN_SHARES, ...) do so now. See
2839 * calc_cfs_shares().
2840 */
Paul Turner977dda72011-01-14 17:57:50 -08002841 if (wl < MIN_SHARES)
2842 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002843
2844 /*
2845 * wl = dw_i = S * (s'_i - s_i); see (3)
2846 */
Paul Turner977dda72011-01-14 17:57:50 -08002847 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002848
2849 /*
2850 * Recursively apply this logic to all parent groups to compute
2851 * the final effective load change on the root group. Since
2852 * only the @tg group gets extra weight, all parent groups can
2853 * only redistribute existing shares. @wl is the shift in shares
2854 * resulting from this level per the above.
2855 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002856 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002857 }
2858
2859 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002860}
2861#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002862
Peter Zijlstra83378262008-06-27 13:41:37 +02002863static inline unsigned long effective_load(struct task_group *tg, int cpu,
2864 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002865{
Peter Zijlstra83378262008-06-27 13:41:37 +02002866 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002867}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002868
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002869#endif
2870
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002871static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002872{
Paul Turnere37b6a72011-01-21 20:44:59 -08002873 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002874 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002875 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002876 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002877 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002878 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002879
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002880 idx = sd->wake_idx;
2881 this_cpu = smp_processor_id();
2882 prev_cpu = task_cpu(p);
2883 load = source_load(prev_cpu, idx);
2884 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002885
2886 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002887 * If sync wakeup then subtract the (maximum possible)
2888 * effect of the currently running task from the load
2889 * of the current CPU:
2890 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002891 if (sync) {
2892 tg = task_group(current);
2893 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002894
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002895 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002896 load += effective_load(tg, prev_cpu, 0, -weight);
2897 }
2898
2899 tg = task_group(p);
2900 weight = p->se.load.weight;
2901
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002902 /*
2903 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002904 * due to the sync cause above having dropped this_load to 0, we'll
2905 * always have an imbalance, but there's really nothing you can do
2906 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002907 *
2908 * Otherwise check if either cpus are near enough in load to allow this
2909 * task to be woken on this_cpu.
2910 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002911 if (this_load > 0) {
2912 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002913
2914 this_eff_load = 100;
2915 this_eff_load *= power_of(prev_cpu);
2916 this_eff_load *= this_load +
2917 effective_load(tg, this_cpu, weight, weight);
2918
2919 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2920 prev_eff_load *= power_of(this_cpu);
2921 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2922
2923 balanced = this_eff_load <= prev_eff_load;
2924 } else
2925 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002926
2927 /*
2928 * If the currently running task will sleep within
2929 * a reasonable amount of time then attract this newly
2930 * woken task:
2931 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002932 if (sync && balanced)
2933 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002934
Lucas De Marchi41acab82010-03-10 23:37:45 -03002935 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002936 tl_per_task = cpu_avg_load_per_task(this_cpu);
2937
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002938 if (balanced ||
2939 (this_load <= load &&
2940 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002941 /*
2942 * This domain has SD_WAKE_AFFINE and
2943 * p is cache cold in this domain, and
2944 * there is no bad imbalance.
2945 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002946 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002947 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002948
2949 return 1;
2950 }
2951 return 0;
2952}
2953
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002954/*
2955 * find_idlest_group finds and returns the least busy CPU group within the
2956 * domain.
2957 */
2958static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002959find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002960 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002961{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002962 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002963 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002964 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002965
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002966 do {
2967 unsigned long load, avg_load;
2968 int local_group;
2969 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002970
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002971 /* Skip over this group if it has no CPUs allowed */
2972 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002973 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002974 continue;
2975
2976 local_group = cpumask_test_cpu(this_cpu,
2977 sched_group_cpus(group));
2978
2979 /* Tally up the load of all CPUs in the group */
2980 avg_load = 0;
2981
2982 for_each_cpu(i, sched_group_cpus(group)) {
2983 /* Bias balancing toward cpus of our domain */
2984 if (local_group)
2985 load = source_load(i, load_idx);
2986 else
2987 load = target_load(i, load_idx);
2988
2989 avg_load += load;
2990 }
2991
2992 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002993 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002994
2995 if (local_group) {
2996 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002997 } else if (avg_load < min_load) {
2998 min_load = avg_load;
2999 idlest = group;
3000 }
3001 } while (group = group->next, group != sd->groups);
3002
3003 if (!idlest || 100*this_load < imbalance*min_load)
3004 return NULL;
3005 return idlest;
3006}
3007
3008/*
3009 * find_idlest_cpu - find the idlest cpu among the cpus in group.
3010 */
3011static int
3012find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
3013{
3014 unsigned long load, min_load = ULONG_MAX;
3015 int idlest = -1;
3016 int i;
3017
3018 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003019 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003020 load = weighted_cpuload(i);
3021
3022 if (load < min_load || (load == min_load && i == this_cpu)) {
3023 min_load = load;
3024 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003025 }
3026 }
3027
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003028 return idlest;
3029}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003030
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003031/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003032 * Try and locate an idle CPU in the sched_domain.
3033 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003034static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003035{
3036 int cpu = smp_processor_id();
3037 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003038 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07003039 struct sched_group *sg;
3040 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003041
3042 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003043 * If the task is going to be woken-up on this cpu and if it is
3044 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003045 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003046 if (target == cpu && idle_cpu(cpu))
3047 return cpu;
3048
3049 /*
3050 * If the task is going to be woken-up on the cpu where it previously
3051 * ran and if it is currently idle, then it the right target.
3052 */
3053 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003054 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003055
3056 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07003057 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003058 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01003059 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08003060 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07003061 sg = sd->groups;
3062 do {
3063 if (!cpumask_intersects(sched_group_cpus(sg),
3064 tsk_cpus_allowed(p)))
3065 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02003066
Linus Torvalds37407ea2012-09-16 12:29:43 -07003067 for_each_cpu(i, sched_group_cpus(sg)) {
3068 if (!idle_cpu(i))
3069 goto next;
3070 }
3071
3072 target = cpumask_first_and(sched_group_cpus(sg),
3073 tsk_cpus_allowed(p));
3074 goto done;
3075next:
3076 sg = sg->next;
3077 } while (sg != sd->groups);
3078 }
3079done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003080 return target;
3081}
3082
3083/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003084 * sched_balance_self: balance the current task (running on cpu) in domains
3085 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3086 * SD_BALANCE_EXEC.
3087 *
3088 * Balance, ie. select the least loaded group.
3089 *
3090 * Returns the target CPU number, or the same CPU if no balancing is needed.
3091 *
3092 * preempt must be disabled.
3093 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003094static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003095select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003096{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003097 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003098 int cpu = smp_processor_id();
3099 int prev_cpu = task_cpu(p);
3100 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003101 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003102 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003103
Peter Zijlstra29baa742012-04-23 12:11:21 +02003104 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003105 return prev_cpu;
3106
Peter Zijlstra0763a662009-09-14 19:37:39 +02003107 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003108 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003109 want_affine = 1;
3110 new_cpu = prev_cpu;
3111 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003112
Peter Zijlstradce840a2011-04-07 14:09:50 +02003113 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003114 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01003115 if (!(tmp->flags & SD_LOAD_BALANCE))
3116 continue;
3117
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003118 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003119 * If both cpu and prev_cpu are part of this domain,
3120 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003121 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003122 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3123 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3124 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003125 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003126 }
3127
Alex Shif03542a2012-07-26 08:55:34 +08003128 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003129 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003130 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003131
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003132 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003133 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003134 prev_cpu = cpu;
3135
3136 new_cpu = select_idle_sibling(p, prev_cpu);
3137 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003138 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003139
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003140 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003141 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003142 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003143 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003144
Peter Zijlstra0763a662009-09-14 19:37:39 +02003145 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003146 sd = sd->child;
3147 continue;
3148 }
3149
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003150 if (sd_flag & SD_BALANCE_WAKE)
3151 load_idx = sd->wake_idx;
3152
3153 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003154 if (!group) {
3155 sd = sd->child;
3156 continue;
3157 }
3158
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003159 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003160 if (new_cpu == -1 || new_cpu == cpu) {
3161 /* Now try balancing at a lower domain level of cpu */
3162 sd = sd->child;
3163 continue;
3164 }
3165
3166 /* Now try balancing at a lower domain level of new_cpu */
3167 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003168 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003169 sd = NULL;
3170 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003171 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003172 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003173 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003174 sd = tmp;
3175 }
3176 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003177 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003178unlock:
3179 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003180
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003181 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003182}
Paul Turner0a74bef2012-10-04 13:18:30 +02003183
3184/*
3185 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3186 * cfs_rq_of(p) references at time of call are still valid and identify the
3187 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3188 * other assumptions, including the state of rq->lock, should be made.
3189 */
3190static void
3191migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3192{
Paul Turneraff3e492012-10-04 13:18:30 +02003193 struct sched_entity *se = &p->se;
3194 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3195
3196 /*
3197 * Load tracking: accumulate removed load so that it can be processed
3198 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3199 * to blocked load iff they have a positive decay-count. It can never
3200 * be negative here since on-rq tasks have decay-count == 0.
3201 */
3202 if (se->avg.decay_count) {
3203 se->avg.decay_count = -__synchronize_entity_decay(se);
3204 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3205 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003206}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003207#endif /* CONFIG_SMP */
3208
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003209static unsigned long
3210wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003211{
3212 unsigned long gran = sysctl_sched_wakeup_granularity;
3213
3214 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003215 * Since its curr running now, convert the gran from real-time
3216 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003217 *
3218 * By using 'se' instead of 'curr' we penalize light tasks, so
3219 * they get preempted easier. That is, if 'se' < 'curr' then
3220 * the resulting gran will be larger, therefore penalizing the
3221 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3222 * be smaller, again penalizing the lighter task.
3223 *
3224 * This is especially important for buddies when the leftmost
3225 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003226 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003227 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003228}
3229
3230/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003231 * Should 'se' preempt 'curr'.
3232 *
3233 * |s1
3234 * |s2
3235 * |s3
3236 * g
3237 * |<--->|c
3238 *
3239 * w(c, s1) = -1
3240 * w(c, s2) = 0
3241 * w(c, s3) = 1
3242 *
3243 */
3244static int
3245wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3246{
3247 s64 gran, vdiff = curr->vruntime - se->vruntime;
3248
3249 if (vdiff <= 0)
3250 return -1;
3251
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003252 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003253 if (vdiff > gran)
3254 return 1;
3255
3256 return 0;
3257}
3258
Peter Zijlstra02479092008-11-04 21:25:10 +01003259static void set_last_buddy(struct sched_entity *se)
3260{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003261 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3262 return;
3263
3264 for_each_sched_entity(se)
3265 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003266}
3267
3268static void set_next_buddy(struct sched_entity *se)
3269{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003270 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3271 return;
3272
3273 for_each_sched_entity(se)
3274 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003275}
3276
Rik van Rielac53db52011-02-01 09:51:03 -05003277static void set_skip_buddy(struct sched_entity *se)
3278{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003279 for_each_sched_entity(se)
3280 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003281}
3282
Peter Zijlstra464b7522008-10-24 11:06:15 +02003283/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003284 * Preempt the current task with a newly woken task if needed:
3285 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003286static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003287{
3288 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003289 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003290 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003291 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003292 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003293
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003294 if (unlikely(se == pse))
3295 return;
3296
Paul Turner5238cdd2011-07-21 09:43:37 -07003297 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003298 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003299 * unconditionally check_prempt_curr() after an enqueue (which may have
3300 * lead to a throttle). This both saves work and prevents false
3301 * next-buddy nomination below.
3302 */
3303 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3304 return;
3305
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003306 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003307 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003308 next_buddy_marked = 1;
3309 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003310
Bharata B Raoaec0a512008-08-28 14:42:49 +05303311 /*
3312 * We can come here with TIF_NEED_RESCHED already set from new task
3313 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003314 *
3315 * Note: this also catches the edge-case of curr being in a throttled
3316 * group (e.g. via set_curr_task), since update_curr() (in the
3317 * enqueue of curr) will have resulted in resched being set. This
3318 * prevents us from potentially nominating it as a false LAST_BUDDY
3319 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303320 */
3321 if (test_tsk_need_resched(curr))
3322 return;
3323
Darren Harta2f5c9a2011-02-22 13:04:33 -08003324 /* Idle tasks are by definition preempted by non-idle tasks. */
3325 if (unlikely(curr->policy == SCHED_IDLE) &&
3326 likely(p->policy != SCHED_IDLE))
3327 goto preempt;
3328
Ingo Molnar91c234b2007-10-15 17:00:18 +02003329 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003330 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3331 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003332 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003333 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003334 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003335
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003336 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003337 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003338 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003339 if (wakeup_preempt_entity(se, pse) == 1) {
3340 /*
3341 * Bias pick_next to pick the sched entity that is
3342 * triggering this preemption.
3343 */
3344 if (!next_buddy_marked)
3345 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003346 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003347 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003348
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003349 return;
3350
3351preempt:
3352 resched_task(curr);
3353 /*
3354 * Only set the backward buddy when the current task is still
3355 * on the rq. This can happen when a wakeup gets interleaved
3356 * with schedule on the ->pre_schedule() or idle_balance()
3357 * point, either of which can * drop the rq lock.
3358 *
3359 * Also, during early boot the idle thread is in the fair class,
3360 * for obvious reasons its a bad idea to schedule back to it.
3361 */
3362 if (unlikely(!se->on_rq || curr == rq->idle))
3363 return;
3364
3365 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3366 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003367}
3368
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003369static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003370{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003371 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003372 struct cfs_rq *cfs_rq = &rq->cfs;
3373 struct sched_entity *se;
3374
Tim Blechmann36ace272009-11-24 11:55:45 +01003375 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003376 return NULL;
3377
3378 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003379 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003380 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003381 cfs_rq = group_cfs_rq(se);
3382 } while (cfs_rq);
3383
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003384 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003385 if (hrtick_enabled(rq))
3386 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003387
3388 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003389}
3390
3391/*
3392 * Account for a descheduled task:
3393 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003394static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003395{
3396 struct sched_entity *se = &prev->se;
3397 struct cfs_rq *cfs_rq;
3398
3399 for_each_sched_entity(se) {
3400 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003401 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003402 }
3403}
3404
Rik van Rielac53db52011-02-01 09:51:03 -05003405/*
3406 * sched_yield() is very simple
3407 *
3408 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3409 */
3410static void yield_task_fair(struct rq *rq)
3411{
3412 struct task_struct *curr = rq->curr;
3413 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3414 struct sched_entity *se = &curr->se;
3415
3416 /*
3417 * Are we the only task in the tree?
3418 */
3419 if (unlikely(rq->nr_running == 1))
3420 return;
3421
3422 clear_buddies(cfs_rq, se);
3423
3424 if (curr->policy != SCHED_BATCH) {
3425 update_rq_clock(rq);
3426 /*
3427 * Update run-time statistics of the 'current'.
3428 */
3429 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003430 /*
3431 * Tell update_rq_clock() that we've just updated,
3432 * so we don't do microscopic update in schedule()
3433 * and double the fastpath cost.
3434 */
3435 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003436 }
3437
3438 set_skip_buddy(se);
3439}
3440
Mike Galbraithd95f4122011-02-01 09:50:51 -05003441static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3442{
3443 struct sched_entity *se = &p->se;
3444
Paul Turner5238cdd2011-07-21 09:43:37 -07003445 /* throttled hierarchies are not runnable */
3446 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003447 return false;
3448
3449 /* Tell the scheduler that we'd really like pse to run next. */
3450 set_next_buddy(se);
3451
Mike Galbraithd95f4122011-02-01 09:50:51 -05003452 yield_task_fair(rq);
3453
3454 return true;
3455}
3456
Peter Williams681f3e62007-10-24 18:23:51 +02003457#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003458/**************************************************
3459 * Fair scheduling class load-balancing methods:
3460 */
3461
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003462static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3463
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003464#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003465#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303466#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003467
3468struct lb_env {
3469 struct sched_domain *sd;
3470
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003471 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303472 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003473
3474 int dst_cpu;
3475 struct rq *dst_rq;
3476
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303477 struct cpumask *dst_grpmask;
3478 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003479 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003480 long imbalance;
Michael Wangb9403132012-07-12 16:10:13 +08003481 /* The set of CPUs under consideration for load-balancing */
3482 struct cpumask *cpus;
3483
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003484 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003485
3486 unsigned int loop;
3487 unsigned int loop_break;
3488 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003489};
3490
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003491/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003492 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003493 * Both runqueues must be locked.
3494 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003495static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003496{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003497 deactivate_task(env->src_rq, p, 0);
3498 set_task_cpu(p, env->dst_cpu);
3499 activate_task(env->dst_rq, p, 0);
3500 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003501}
3502
3503/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003504 * Is this task likely cache-hot:
3505 */
3506static int
3507task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3508{
3509 s64 delta;
3510
3511 if (p->sched_class != &fair_sched_class)
3512 return 0;
3513
3514 if (unlikely(p->policy == SCHED_IDLE))
3515 return 0;
3516
3517 /*
3518 * Buddy candidates are cache hot:
3519 */
3520 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3521 (&p->se == cfs_rq_of(&p->se)->next ||
3522 &p->se == cfs_rq_of(&p->se)->last))
3523 return 1;
3524
3525 if (sysctl_sched_migration_cost == -1)
3526 return 1;
3527 if (sysctl_sched_migration_cost == 0)
3528 return 0;
3529
3530 delta = now - p->se.exec_start;
3531
3532 return delta < (s64)sysctl_sched_migration_cost;
3533}
3534
3535/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003536 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3537 */
3538static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003539int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003540{
3541 int tsk_cache_hot = 0;
3542 /*
3543 * We do not migrate tasks that are:
3544 * 1) running (obviously), or
3545 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3546 * 3) are cache-hot on their current CPU.
3547 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003548 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303549 int new_dst_cpu;
3550
Lucas De Marchi41acab82010-03-10 23:37:45 -03003551 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303552
3553 /*
3554 * Remember if this task can be migrated to any other cpu in
3555 * our sched_group. We may want to revisit it if we couldn't
3556 * meet load balance goals by pulling other tasks on src_cpu.
3557 *
3558 * Also avoid computing new_dst_cpu if we have already computed
3559 * one in current iteration.
3560 */
3561 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3562 return 0;
3563
3564 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3565 tsk_cpus_allowed(p));
3566 if (new_dst_cpu < nr_cpu_ids) {
3567 env->flags |= LBF_SOME_PINNED;
3568 env->new_dst_cpu = new_dst_cpu;
3569 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003570 return 0;
3571 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303572
3573 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003574 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003575
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003576 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003577 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003578 return 0;
3579 }
3580
3581 /*
3582 * Aggressive migration if:
3583 * 1) task is cache cold, or
3584 * 2) too many balance attempts have failed.
3585 */
3586
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003587 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003588 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003589 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003590#ifdef CONFIG_SCHEDSTATS
3591 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003592 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003593 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003594 }
3595#endif
3596 return 1;
3597 }
3598
3599 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003600 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003601 return 0;
3602 }
3603 return 1;
3604}
3605
Peter Zijlstra897c3952009-12-17 17:45:42 +01003606/*
3607 * move_one_task tries to move exactly one task from busiest to this_rq, as
3608 * part of active balancing operations within "domain".
3609 * Returns 1 if successful and 0 otherwise.
3610 *
3611 * Called with both runqueues locked.
3612 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003613static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003614{
3615 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003616
Peter Zijlstra367456c2012-02-20 21:49:09 +01003617 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3618 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3619 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003620
Peter Zijlstra367456c2012-02-20 21:49:09 +01003621 if (!can_migrate_task(p, env))
3622 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003623
Peter Zijlstra367456c2012-02-20 21:49:09 +01003624 move_task(p, env);
3625 /*
3626 * Right now, this is only the second place move_task()
3627 * is called, so we can safely collect move_task()
3628 * stats here rather than inside move_task().
3629 */
3630 schedstat_inc(env->sd, lb_gained[env->idle]);
3631 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003632 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003633 return 0;
3634}
3635
Peter Zijlstra367456c2012-02-20 21:49:09 +01003636static unsigned long task_h_load(struct task_struct *p);
3637
Peter Zijlstraeb953082012-04-17 13:38:40 +02003638static const unsigned int sched_nr_migrate_break = 32;
3639
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003640/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003641 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003642 * this_rq, as part of a balancing operation within domain "sd".
3643 * Returns 1 if successful and 0 otherwise.
3644 *
3645 * Called with both runqueues locked.
3646 */
3647static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003648{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003649 struct list_head *tasks = &env->src_rq->cfs_tasks;
3650 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003651 unsigned long load;
3652 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003653
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003654 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003655 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003656
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003657 while (!list_empty(tasks)) {
3658 p = list_first_entry(tasks, struct task_struct, se.group_node);
3659
Peter Zijlstra367456c2012-02-20 21:49:09 +01003660 env->loop++;
3661 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003662 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003663 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003664
3665 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003666 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003667 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003668 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003669 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003670 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003671
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003672 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003673 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003674
Peter Zijlstra367456c2012-02-20 21:49:09 +01003675 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003676
Peter Zijlstraeb953082012-04-17 13:38:40 +02003677 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003678 goto next;
3679
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003680 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003681 goto next;
3682
3683 if (!can_migrate_task(p, env))
3684 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003685
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003686 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003687 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003688 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003689
3690#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003691 /*
3692 * NEWIDLE balancing is a source of latency, so preemptible
3693 * kernels will stop after the first task is pulled to minimize
3694 * the critical section.
3695 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003696 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003697 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003698#endif
3699
Peter Zijlstraee00e662009-12-17 17:25:20 +01003700 /*
3701 * We only want to steal up to the prescribed amount of
3702 * weighted load.
3703 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003704 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003705 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003706
Peter Zijlstra367456c2012-02-20 21:49:09 +01003707 continue;
3708next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003709 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003710 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003711
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003712 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003713 * Right now, this is one of only two places move_task() is called,
3714 * so we can safely collect move_task() stats here rather than
3715 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003716 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003717 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003718
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003719 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003720}
3721
Peter Zijlstra230059de2009-12-17 17:47:12 +01003722#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003723/*
3724 * update tg->load_weight by folding this cpu's load_avg
3725 */
Paul Turner67e86252010-11-15 15:47:05 -08003726static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003727{
3728 struct cfs_rq *cfs_rq;
3729 unsigned long flags;
3730 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003731
3732 if (!tg->se[cpu])
3733 return 0;
3734
3735 rq = cpu_rq(cpu);
3736 cfs_rq = tg->cfs_rq[cpu];
3737
3738 raw_spin_lock_irqsave(&rq->lock, flags);
3739
3740 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003741 update_cfs_load(cfs_rq, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02003742 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003743
3744 /*
3745 * We need to update shares after updating tg->load_weight in
3746 * order to adjust the weight of groups with long running tasks.
3747 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003748 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003749
3750 raw_spin_unlock_irqrestore(&rq->lock, flags);
3751
3752 return 0;
3753}
3754
3755static void update_shares(int cpu)
3756{
3757 struct cfs_rq *cfs_rq;
3758 struct rq *rq = cpu_rq(cpu);
3759
3760 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003761 /*
3762 * Iterates the task_group tree in a bottom up fashion, see
3763 * list_add_leaf_cfs_rq() for details.
3764 */
Paul Turner64660c82011-07-21 09:43:36 -07003765 for_each_leaf_cfs_rq(rq, cfs_rq) {
3766 /* throttled entities do not contribute to load */
3767 if (throttled_hierarchy(cfs_rq))
3768 continue;
3769
Paul Turner67e86252010-11-15 15:47:05 -08003770 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003771 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003772 rcu_read_unlock();
3773}
3774
Peter Zijlstra9763b672011-07-13 13:09:25 +02003775/*
3776 * Compute the cpu's hierarchical load factor for each task group.
3777 * This needs to be done in a top-down fashion because the load of a child
3778 * group is a fraction of its parents load.
3779 */
3780static int tg_load_down(struct task_group *tg, void *data)
3781{
3782 unsigned long load;
3783 long cpu = (long)data;
3784
3785 if (!tg->parent) {
3786 load = cpu_rq(cpu)->load.weight;
3787 } else {
3788 load = tg->parent->cfs_rq[cpu]->h_load;
3789 load *= tg->se[cpu]->load.weight;
3790 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3791 }
3792
3793 tg->cfs_rq[cpu]->h_load = load;
3794
3795 return 0;
3796}
3797
3798static void update_h_load(long cpu)
3799{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003800 struct rq *rq = cpu_rq(cpu);
3801 unsigned long now = jiffies;
3802
3803 if (rq->h_load_throttle == now)
3804 return;
3805
3806 rq->h_load_throttle = now;
3807
Peter Zijlstra367456c2012-02-20 21:49:09 +01003808 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003809 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003810 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003811}
3812
Peter Zijlstra367456c2012-02-20 21:49:09 +01003813static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003814{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003815 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3816 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003817
Peter Zijlstra367456c2012-02-20 21:49:09 +01003818 load = p->se.load.weight;
3819 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003820
Peter Zijlstra367456c2012-02-20 21:49:09 +01003821 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003822}
3823#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003824static inline void update_shares(int cpu)
3825{
3826}
3827
Peter Zijlstra367456c2012-02-20 21:49:09 +01003828static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003829{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003830}
3831
3832static unsigned long task_h_load(struct task_struct *p)
3833{
3834 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003835}
3836#endif
3837
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003838/********** Helpers for find_busiest_group ************************/
3839/*
3840 * sd_lb_stats - Structure to store the statistics of a sched_domain
3841 * during load balancing.
3842 */
3843struct sd_lb_stats {
3844 struct sched_group *busiest; /* Busiest group in this sd */
3845 struct sched_group *this; /* Local group in this sd */
3846 unsigned long total_load; /* Total load of all groups in sd */
3847 unsigned long total_pwr; /* Total power of all groups in sd */
3848 unsigned long avg_load; /* Average load across all groups in sd */
3849
3850 /** Statistics of this group */
3851 unsigned long this_load;
3852 unsigned long this_load_per_task;
3853 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003854 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003855 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003856
3857 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003858 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003859 unsigned long max_load;
3860 unsigned long busiest_load_per_task;
3861 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003862 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003863 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003864 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003865
3866 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003867};
3868
3869/*
3870 * sg_lb_stats - stats of a sched_group required for load_balancing
3871 */
3872struct sg_lb_stats {
3873 unsigned long avg_load; /*Avg load across the CPUs of the group */
3874 unsigned long group_load; /* Total load over the CPUs of the group */
3875 unsigned long sum_nr_running; /* Nr tasks running in the group */
3876 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3877 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003878 unsigned long idle_cpus;
3879 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003880 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003881 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003882};
3883
3884/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003885 * get_sd_load_idx - Obtain the load index for a given sched domain.
3886 * @sd: The sched_domain whose load_idx is to be obtained.
3887 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3888 */
3889static inline int get_sd_load_idx(struct sched_domain *sd,
3890 enum cpu_idle_type idle)
3891{
3892 int load_idx;
3893
3894 switch (idle) {
3895 case CPU_NOT_IDLE:
3896 load_idx = sd->busy_idx;
3897 break;
3898
3899 case CPU_NEWLY_IDLE:
3900 load_idx = sd->newidle_idx;
3901 break;
3902 default:
3903 load_idx = sd->idle_idx;
3904 break;
3905 }
3906
3907 return load_idx;
3908}
3909
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003910unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3911{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003912 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003913}
3914
3915unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3916{
3917 return default_scale_freq_power(sd, cpu);
3918}
3919
3920unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3921{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003922 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003923 unsigned long smt_gain = sd->smt_gain;
3924
3925 smt_gain /= weight;
3926
3927 return smt_gain;
3928}
3929
3930unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3931{
3932 return default_scale_smt_power(sd, cpu);
3933}
3934
3935unsigned long scale_rt_power(int cpu)
3936{
3937 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003938 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003939
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003940 /*
3941 * Since we're reading these variables without serialization make sure
3942 * we read them once before doing sanity checks on them.
3943 */
3944 age_stamp = ACCESS_ONCE(rq->age_stamp);
3945 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003946
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003947 total = sched_avg_period() + (rq->clock - age_stamp);
3948
3949 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003950 /* Ensures that power won't end up being negative */
3951 available = 0;
3952 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003953 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003954 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003955
Nikhil Rao1399fa72011-05-18 10:09:39 -07003956 if (unlikely((s64)total < SCHED_POWER_SCALE))
3957 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003958
Nikhil Rao1399fa72011-05-18 10:09:39 -07003959 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003960
3961 return div_u64(available, total);
3962}
3963
3964static void update_cpu_power(struct sched_domain *sd, int cpu)
3965{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003966 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003967 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003968 struct sched_group *sdg = sd->groups;
3969
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003970 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3971 if (sched_feat(ARCH_POWER))
3972 power *= arch_scale_smt_power(sd, cpu);
3973 else
3974 power *= default_scale_smt_power(sd, cpu);
3975
Nikhil Rao1399fa72011-05-18 10:09:39 -07003976 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003977 }
3978
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003979 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003980
3981 if (sched_feat(ARCH_POWER))
3982 power *= arch_scale_freq_power(sd, cpu);
3983 else
3984 power *= default_scale_freq_power(sd, cpu);
3985
Nikhil Rao1399fa72011-05-18 10:09:39 -07003986 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003987
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003988 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003989 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003990
3991 if (!power)
3992 power = 1;
3993
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003994 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003995 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003996}
3997
Peter Zijlstra029632f2011-10-25 10:00:11 +02003998void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003999{
4000 struct sched_domain *child = sd->child;
4001 struct sched_group *group, *sdg = sd->groups;
4002 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01004003 unsigned long interval;
4004
4005 interval = msecs_to_jiffies(sd->balance_interval);
4006 interval = clamp(interval, 1UL, max_load_balance_interval);
4007 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004008
4009 if (!child) {
4010 update_cpu_power(sd, cpu);
4011 return;
4012 }
4013
4014 power = 0;
4015
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02004016 if (child->flags & SD_OVERLAP) {
4017 /*
4018 * SD_OVERLAP domains cannot assume that child groups
4019 * span the current group.
4020 */
4021
4022 for_each_cpu(cpu, sched_group_cpus(sdg))
4023 power += power_of(cpu);
4024 } else {
4025 /*
4026 * !SD_OVERLAP domains can assume that child groups
4027 * span the current group.
4028 */
4029
4030 group = child->groups;
4031 do {
4032 power += group->sgp->power;
4033 group = group->next;
4034 } while (group != child->groups);
4035 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004036
Peter Zijlstrac3decf02012-05-31 12:05:32 +02004037 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004038}
4039
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004040/*
4041 * Try and fix up capacity for tiny siblings, this is needed when
4042 * things like SD_ASYM_PACKING need f_b_g to select another sibling
4043 * which on its own isn't powerful enough.
4044 *
4045 * See update_sd_pick_busiest() and check_asym_packing().
4046 */
4047static inline int
4048fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
4049{
4050 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07004051 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004052 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02004053 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004054 return 0;
4055
4056 /*
4057 * If ~90% of the cpu_power is still there, we're good.
4058 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004059 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004060 return 1;
4061
4062 return 0;
4063}
4064
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004065/**
4066 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004067 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004068 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004069 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004070 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004071 * @balance: Should we balance.
4072 * @sgs: variable to hold the statistics for this group.
4073 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004074static inline void update_sg_lb_stats(struct lb_env *env,
4075 struct sched_group *group, int load_idx,
Michael Wangb9403132012-07-12 16:10:13 +08004076 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004077{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004078 unsigned long nr_running, max_nr_running, min_nr_running;
4079 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004080 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004081 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004082 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004083
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004084 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004085 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004086
4087 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004088 max_cpu_load = 0;
4089 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004090 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004091 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004092
Michael Wangb9403132012-07-12 16:10:13 +08004093 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094 struct rq *rq = cpu_rq(i);
4095
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004096 nr_running = rq->nr_running;
4097
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098 /* Bias balancing toward cpus of our domain */
4099 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004100 if (idle_cpu(i) && !first_idle_cpu &&
4101 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004102 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004103 balance_cpu = i;
4104 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004105
4106 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107 } else {
4108 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004109 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004110 max_cpu_load = load;
4111 if (min_cpu_load > load)
4112 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004113
4114 if (nr_running > max_nr_running)
4115 max_nr_running = nr_running;
4116 if (min_nr_running > nr_running)
4117 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004118 }
4119
4120 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004121 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004122 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004123 if (idle_cpu(i))
4124 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004125 }
4126
4127 /*
4128 * First idle cpu or the first cpu(busiest) in this sched group
4129 * is eligible for doing load balancing at this and above
4130 * domains. In the newly idle case, we will allow all the cpu's
4131 * to do the newly idle load balance.
4132 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004133 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004134 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004135 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004136 *balance = 0;
4137 return;
4138 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004139 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004140 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004141 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004142 }
4143
4144 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004145 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004146
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004147 /*
4148 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004149 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004150 *
4151 * APZ: with cgroup the avg task weight can vary wildly and
4152 * might not be a suitable number - should we keep a
4153 * normalized nr_running number somewhere that negates
4154 * the hierarchy?
4155 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004156 if (sgs->sum_nr_running)
4157 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004158
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004159 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4160 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004161 sgs->group_imb = 1;
4162
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004163 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004164 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004165 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004166 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004167 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004168
4169 if (sgs->group_capacity > sgs->sum_nr_running)
4170 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004171}
4172
4173/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004174 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004175 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004176 * @sds: sched_domain statistics
4177 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004178 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004179 *
4180 * Determine if @sg is a busier group than the previously selected
4181 * busiest group.
4182 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004183static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004184 struct sd_lb_stats *sds,
4185 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004186 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004187{
4188 if (sgs->avg_load <= sds->max_load)
4189 return false;
4190
4191 if (sgs->sum_nr_running > sgs->group_capacity)
4192 return true;
4193
4194 if (sgs->group_imb)
4195 return true;
4196
4197 /*
4198 * ASYM_PACKING needs to move all the work to the lowest
4199 * numbered CPUs in the group, therefore mark all groups
4200 * higher than ourself as busy.
4201 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004202 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4203 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004204 if (!sds->busiest)
4205 return true;
4206
4207 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4208 return true;
4209 }
4210
4211 return false;
4212}
4213
4214/**
Hui Kang461819a2011-10-11 23:00:59 -04004215 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004216 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004217 * @balance: Should we balance.
4218 * @sds: variable to hold the statistics for this sched_domain.
4219 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004220static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004221 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004222{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004223 struct sched_domain *child = env->sd->child;
4224 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004225 struct sg_lb_stats sgs;
4226 int load_idx, prefer_sibling = 0;
4227
4228 if (child && child->flags & SD_PREFER_SIBLING)
4229 prefer_sibling = 1;
4230
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004231 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004232
4233 do {
4234 int local_group;
4235
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004236 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004237 memset(&sgs, 0, sizeof(sgs));
Michael Wangb9403132012-07-12 16:10:13 +08004238 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004239
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004240 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004241 return;
4242
4243 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004244 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004245
4246 /*
4247 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004248 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004249 * and move all the excess tasks away. We lower the capacity
4250 * of a group only if the local group has the capacity to fit
4251 * these excess tasks, i.e. nr_running < group_capacity. The
4252 * extra check prevents the case where you always pull from the
4253 * heaviest group when it is already under-utilized (possible
4254 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004255 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004256 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004257 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4258
4259 if (local_group) {
4260 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004261 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004262 sds->this_nr_running = sgs.sum_nr_running;
4263 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004264 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004265 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004266 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004267 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004268 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004269 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004270 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004271 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004272 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004273 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004274 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004275 sds->group_imb = sgs.group_imb;
4276 }
4277
Michael Neuling532cb4c2010-06-08 14:57:02 +10004278 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004279 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004280}
4281
Michael Neuling532cb4c2010-06-08 14:57:02 +10004282/**
4283 * check_asym_packing - Check to see if the group is packed into the
4284 * sched doman.
4285 *
4286 * This is primarily intended to used at the sibling level. Some
4287 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4288 * case of POWER7, it can move to lower SMT modes only when higher
4289 * threads are idle. When in lower SMT modes, the threads will
4290 * perform better since they share less core resources. Hence when we
4291 * have idle threads, we want them to be the higher ones.
4292 *
4293 * This packing function is run on idle threads. It checks to see if
4294 * the busiest CPU in this domain (core in the P7 case) has a higher
4295 * CPU number than the packing function is being run on. Here we are
4296 * assuming lower CPU number will be equivalent to lower a SMT thread
4297 * number.
4298 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004299 * Returns 1 when packing is required and a task should be moved to
4300 * this CPU. The amount of the imbalance is returned in *imbalance.
4301 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004302 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004303 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004304 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004305static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004306{
4307 int busiest_cpu;
4308
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004309 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004310 return 0;
4311
4312 if (!sds->busiest)
4313 return 0;
4314
4315 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004316 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004317 return 0;
4318
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004319 env->imbalance = DIV_ROUND_CLOSEST(
4320 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4321
Michael Neuling532cb4c2010-06-08 14:57:02 +10004322 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004323}
4324
4325/**
4326 * fix_small_imbalance - Calculate the minor imbalance that exists
4327 * amongst the groups of a sched_domain, during
4328 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004329 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004330 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004331 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004332static inline
4333void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004334{
4335 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4336 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004337 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004338
4339 if (sds->this_nr_running) {
4340 sds->this_load_per_task /= sds->this_nr_running;
4341 if (sds->busiest_load_per_task >
4342 sds->this_load_per_task)
4343 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004344 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004345 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004346 cpu_avg_load_per_task(env->dst_cpu);
4347 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004348
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004349 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004350 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004351 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004352
4353 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4354 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004355 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004356 return;
4357 }
4358
4359 /*
4360 * OK, we don't have enough imbalance to justify moving tasks,
4361 * however we may be able to increase total CPU power used by
4362 * moving them.
4363 */
4364
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004365 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004366 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004367 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004368 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004369 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004370
4371 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004372 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004373 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004374 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004375 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004376 min(sds->busiest_load_per_task, sds->max_load - tmp);
4377
4378 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004379 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004380 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004381 tmp = (sds->max_load * sds->busiest->sgp->power) /
4382 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004383 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004384 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004385 sds->this->sgp->power;
4386 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004387 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004388 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004389
4390 /* Move if we gain throughput */
4391 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004392 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004393}
4394
4395/**
4396 * calculate_imbalance - Calculate the amount of imbalance present within the
4397 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004398 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004399 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004400 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004401static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004402{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004403 unsigned long max_pull, load_above_capacity = ~0UL;
4404
4405 sds->busiest_load_per_task /= sds->busiest_nr_running;
4406 if (sds->group_imb) {
4407 sds->busiest_load_per_task =
4408 min(sds->busiest_load_per_task, sds->avg_load);
4409 }
4410
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004411 /*
4412 * In the presence of smp nice balancing, certain scenarios can have
4413 * max load less than avg load(as we skip the groups at or below
4414 * its cpu_power, while calculating max_load..)
4415 */
4416 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004417 env->imbalance = 0;
4418 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004419 }
4420
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004421 if (!sds->group_imb) {
4422 /*
4423 * Don't want to pull so many tasks that a group would go idle.
4424 */
4425 load_above_capacity = (sds->busiest_nr_running -
4426 sds->busiest_group_capacity);
4427
Nikhil Rao1399fa72011-05-18 10:09:39 -07004428 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004429
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004430 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004431 }
4432
4433 /*
4434 * We're trying to get all the cpus to the average_load, so we don't
4435 * want to push ourselves above the average load, nor do we wish to
4436 * reduce the max loaded cpu below the average load. At the same time,
4437 * we also don't want to reduce the group load below the group capacity
4438 * (so that we can implement power-savings policies etc). Thus we look
4439 * for the minimum possible imbalance.
4440 * Be careful of negative numbers as they'll appear as very large values
4441 * with unsigned longs.
4442 */
4443 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004444
4445 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004446 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004447 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004448 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004449
4450 /*
4451 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004452 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004453 * a think about bumping its value to force at least one task to be
4454 * moved
4455 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004456 if (env->imbalance < sds->busiest_load_per_task)
4457 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004458
4459}
Nikhil Raofab47622010-10-15 13:12:29 -07004460
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004461/******* find_busiest_group() helpers end here *********************/
4462
4463/**
4464 * find_busiest_group - Returns the busiest group within the sched_domain
4465 * if there is an imbalance. If there isn't an imbalance, and
4466 * the user has opted for power-savings, it returns a group whose
4467 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4468 * such a group exists.
4469 *
4470 * Also calculates the amount of weighted load which should be moved
4471 * to restore balance.
4472 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004473 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004474 * @balance: Pointer to a variable indicating if this_cpu
4475 * is the appropriate cpu to perform load balancing at this_level.
4476 *
4477 * Returns: - the busiest group if imbalance exists.
4478 * - If no imbalance and user has opted for power-savings balance,
4479 * return the least loaded group whose CPUs can be
4480 * put to idle by rebalancing its tasks onto our group.
4481 */
4482static struct sched_group *
Michael Wangb9403132012-07-12 16:10:13 +08004483find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004484{
4485 struct sd_lb_stats sds;
4486
4487 memset(&sds, 0, sizeof(sds));
4488
4489 /*
4490 * Compute the various statistics relavent for load balancing at
4491 * this level.
4492 */
Michael Wangb9403132012-07-12 16:10:13 +08004493 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004494
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004495 /*
4496 * this_cpu is not the appropriate cpu to perform load balancing at
4497 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004498 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004499 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004500 goto ret;
4501
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004502 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4503 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004504 return sds.busiest;
4505
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004506 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004507 if (!sds.busiest || sds.busiest_nr_running == 0)
4508 goto out_balanced;
4509
Nikhil Rao1399fa72011-05-18 10:09:39 -07004510 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004511
Peter Zijlstra866ab432011-02-21 18:56:47 +01004512 /*
4513 * If the busiest group is imbalanced the below checks don't
4514 * work because they assumes all things are equal, which typically
4515 * isn't true due to cpus_allowed constraints and the like.
4516 */
4517 if (sds.group_imb)
4518 goto force_balance;
4519
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004520 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004521 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004522 !sds.busiest_has_capacity)
4523 goto force_balance;
4524
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004525 /*
4526 * If the local group is more busy than the selected busiest group
4527 * don't try and pull any tasks.
4528 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004529 if (sds.this_load >= sds.max_load)
4530 goto out_balanced;
4531
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004532 /*
4533 * Don't pull any tasks if this group is already above the domain
4534 * average load.
4535 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004536 if (sds.this_load >= sds.avg_load)
4537 goto out_balanced;
4538
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004539 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004540 /*
4541 * This cpu is idle. If the busiest group load doesn't
4542 * have more tasks than the number of available cpu's and
4543 * there is no imbalance between this and busiest group
4544 * wrt to idle cpu's, it is balanced.
4545 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004546 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004547 sds.busiest_nr_running <= sds.busiest_group_weight)
4548 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004549 } else {
4550 /*
4551 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4552 * imbalance_pct to be conservative.
4553 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004554 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004555 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004556 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004557
Nikhil Raofab47622010-10-15 13:12:29 -07004558force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004559 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004560 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004561 return sds.busiest;
4562
4563out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004564ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004565 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004566 return NULL;
4567}
4568
4569/*
4570 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4571 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004572static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004573 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004574{
4575 struct rq *busiest = NULL, *rq;
4576 unsigned long max_load = 0;
4577 int i;
4578
4579 for_each_cpu(i, sched_group_cpus(group)) {
4580 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004581 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4582 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004583 unsigned long wl;
4584
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004585 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004586 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004587
Michael Wangb9403132012-07-12 16:10:13 +08004588 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004589 continue;
4590
4591 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004592 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004593
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004594 /*
4595 * When comparing with imbalance, use weighted_cpuload()
4596 * which is not scaled with the cpu power.
4597 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004598 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004599 continue;
4600
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004601 /*
4602 * For the load comparisons with the other cpu's, consider
4603 * the weighted_cpuload() scaled with the cpu power, so that
4604 * the load can be moved away from the cpu that is potentially
4605 * running at a lower capacity.
4606 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004607 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004608
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004609 if (wl > max_load) {
4610 max_load = wl;
4611 busiest = rq;
4612 }
4613 }
4614
4615 return busiest;
4616}
4617
4618/*
4619 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4620 * so long as it is large enough.
4621 */
4622#define MAX_PINNED_INTERVAL 512
4623
4624/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004625DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004626
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004627static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004628{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004629 struct sched_domain *sd = env->sd;
4630
4631 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004632
4633 /*
4634 * ASYM_PACKING needs to force migrate tasks from busy but
4635 * higher numbered CPUs in order to pack all tasks in the
4636 * lowest numbered CPUs.
4637 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004638 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004639 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004640 }
4641
4642 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4643}
4644
Tejun Heo969c7922010-05-06 18:49:21 +02004645static int active_load_balance_cpu_stop(void *data);
4646
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647/*
4648 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4649 * tasks if there is an imbalance.
4650 */
4651static int load_balance(int this_cpu, struct rq *this_rq,
4652 struct sched_domain *sd, enum cpu_idle_type idle,
4653 int *balance)
4654{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304655 int ld_moved, cur_ld_moved, active_balance = 0;
4656 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004657 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004658 struct rq *busiest;
4659 unsigned long flags;
4660 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4661
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004662 struct lb_env env = {
4663 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004664 .dst_cpu = this_cpu,
4665 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304666 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004667 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004668 .loop_break = sched_nr_migrate_break,
Michael Wangb9403132012-07-12 16:10:13 +08004669 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004670 };
4671
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004672 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304673 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004674
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004675 schedstat_inc(sd, lb_count[idle]);
4676
4677redo:
Michael Wangb9403132012-07-12 16:10:13 +08004678 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004679
4680 if (*balance == 0)
4681 goto out_balanced;
4682
4683 if (!group) {
4684 schedstat_inc(sd, lb_nobusyg[idle]);
4685 goto out_balanced;
4686 }
4687
Michael Wangb9403132012-07-12 16:10:13 +08004688 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004689 if (!busiest) {
4690 schedstat_inc(sd, lb_nobusyq[idle]);
4691 goto out_balanced;
4692 }
4693
Michael Wang78feefc2012-08-06 16:41:59 +08004694 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004695
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004696 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004697
4698 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304699 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004700 if (busiest->nr_running > 1) {
4701 /*
4702 * Attempt to move tasks. If find_busiest_group has found
4703 * an imbalance but busiest->nr_running <= 1, the group is
4704 * still unbalanced. ld_moved simply stays zero, so it is
4705 * correctly treated as an imbalance.
4706 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004707 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004708 env.src_cpu = busiest->cpu;
4709 env.src_rq = busiest;
4710 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004711
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004712 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004713more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004714 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004715 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304716
4717 /*
4718 * cur_ld_moved - load moved in current iteration
4719 * ld_moved - cumulative load moved across iterations
4720 */
4721 cur_ld_moved = move_tasks(&env);
4722 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004723 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004724 local_irq_restore(flags);
4725
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004726 if (env.flags & LBF_NEED_BREAK) {
4727 env.flags &= ~LBF_NEED_BREAK;
4728 goto more_balance;
4729 }
4730
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004731 /*
4732 * some other cpu did the load balance for us.
4733 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304734 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4735 resched_cpu(env.dst_cpu);
4736
4737 /*
4738 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4739 * us and move them to an alternate dst_cpu in our sched_group
4740 * where they can run. The upper limit on how many times we
4741 * iterate on same src_cpu is dependent on number of cpus in our
4742 * sched_group.
4743 *
4744 * This changes load balance semantics a bit on who can move
4745 * load to a given_cpu. In addition to the given_cpu itself
4746 * (or a ilb_cpu acting on its behalf where given_cpu is
4747 * nohz-idle), we now have balance_cpu in a position to move
4748 * load to given_cpu. In rare situations, this may cause
4749 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4750 * _independently_ and at _same_ time to move some load to
4751 * given_cpu) causing exceess load to be moved to given_cpu.
4752 * This however should not happen so much in practice and
4753 * moreover subsequent load balance cycles should correct the
4754 * excess load moved.
4755 */
4756 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4757 lb_iterations++ < max_lb_iterations) {
4758
Michael Wang78feefc2012-08-06 16:41:59 +08004759 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304760 env.dst_cpu = env.new_dst_cpu;
4761 env.flags &= ~LBF_SOME_PINNED;
4762 env.loop = 0;
4763 env.loop_break = sched_nr_migrate_break;
4764 /*
4765 * Go back to "more_balance" rather than "redo" since we
4766 * need to continue with same src_cpu.
4767 */
4768 goto more_balance;
4769 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004770
4771 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004772 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004773 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304774 if (!cpumask_empty(cpus)) {
4775 env.loop = 0;
4776 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004777 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304778 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004779 goto out_balanced;
4780 }
4781 }
4782
4783 if (!ld_moved) {
4784 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004785 /*
4786 * Increment the failure counter only on periodic balance.
4787 * We do not want newidle balance, which can be very
4788 * frequent, pollute the failure counter causing
4789 * excessive cache_hot migrations and active balances.
4790 */
4791 if (idle != CPU_NEWLY_IDLE)
4792 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004793
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004794 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004795 raw_spin_lock_irqsave(&busiest->lock, flags);
4796
Tejun Heo969c7922010-05-06 18:49:21 +02004797 /* don't kick the active_load_balance_cpu_stop,
4798 * if the curr task on busiest cpu can't be
4799 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004800 */
4801 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004802 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004803 raw_spin_unlock_irqrestore(&busiest->lock,
4804 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004805 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004806 goto out_one_pinned;
4807 }
4808
Tejun Heo969c7922010-05-06 18:49:21 +02004809 /*
4810 * ->active_balance synchronizes accesses to
4811 * ->active_balance_work. Once set, it's cleared
4812 * only after active load balance is finished.
4813 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004814 if (!busiest->active_balance) {
4815 busiest->active_balance = 1;
4816 busiest->push_cpu = this_cpu;
4817 active_balance = 1;
4818 }
4819 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004820
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004821 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004822 stop_one_cpu_nowait(cpu_of(busiest),
4823 active_load_balance_cpu_stop, busiest,
4824 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004825 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004826
4827 /*
4828 * We've kicked active balancing, reset the failure
4829 * counter.
4830 */
4831 sd->nr_balance_failed = sd->cache_nice_tries+1;
4832 }
4833 } else
4834 sd->nr_balance_failed = 0;
4835
4836 if (likely(!active_balance)) {
4837 /* We were unbalanced, so reset the balancing interval */
4838 sd->balance_interval = sd->min_interval;
4839 } else {
4840 /*
4841 * If we've begun active balancing, start to back off. This
4842 * case may not be covered by the all_pinned logic if there
4843 * is only 1 task on the busy runqueue (because we don't call
4844 * move_tasks).
4845 */
4846 if (sd->balance_interval < sd->max_interval)
4847 sd->balance_interval *= 2;
4848 }
4849
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004850 goto out;
4851
4852out_balanced:
4853 schedstat_inc(sd, lb_balanced[idle]);
4854
4855 sd->nr_balance_failed = 0;
4856
4857out_one_pinned:
4858 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004859 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004860 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004861 (sd->balance_interval < sd->max_interval))
4862 sd->balance_interval *= 2;
4863
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004864 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004865out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004866 return ld_moved;
4867}
4868
4869/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004870 * idle_balance is called by schedule() if this_cpu is about to become
4871 * idle. Attempts to pull tasks from other CPUs.
4872 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004873void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004874{
4875 struct sched_domain *sd;
4876 int pulled_task = 0;
4877 unsigned long next_balance = jiffies + HZ;
4878
4879 this_rq->idle_stamp = this_rq->clock;
4880
4881 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4882 return;
4883
Ben Segall18bf2802012-10-04 12:51:20 +02004884 update_rq_runnable_avg(this_rq, 1);
4885
Peter Zijlstraf492e122009-12-23 15:29:42 +01004886 /*
4887 * Drop the rq->lock, but keep IRQ/preempt disabled.
4888 */
4889 raw_spin_unlock(&this_rq->lock);
4890
Paul Turnerc66eaf62010-11-15 15:47:07 -08004891 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004892 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004893 for_each_domain(this_cpu, sd) {
4894 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004895 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004896
4897 if (!(sd->flags & SD_LOAD_BALANCE))
4898 continue;
4899
Peter Zijlstraf492e122009-12-23 15:29:42 +01004900 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004901 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004902 pulled_task = load_balance(this_cpu, this_rq,
4903 sd, CPU_NEWLY_IDLE, &balance);
4904 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004905
4906 interval = msecs_to_jiffies(sd->balance_interval);
4907 if (time_after(next_balance, sd->last_balance + interval))
4908 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004909 if (pulled_task) {
4910 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004911 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004912 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004913 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004914 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004915
4916 raw_spin_lock(&this_rq->lock);
4917
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004918 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4919 /*
4920 * We are going idle. next_balance may be set based on
4921 * a busy processor. So reset next_balance.
4922 */
4923 this_rq->next_balance = next_balance;
4924 }
4925}
4926
4927/*
Tejun Heo969c7922010-05-06 18:49:21 +02004928 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4929 * running tasks off the busiest CPU onto idle CPUs. It requires at
4930 * least 1 task to be running on each physical CPU where possible, and
4931 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004932 */
Tejun Heo969c7922010-05-06 18:49:21 +02004933static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004934{
Tejun Heo969c7922010-05-06 18:49:21 +02004935 struct rq *busiest_rq = data;
4936 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004937 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004938 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004939 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004940
4941 raw_spin_lock_irq(&busiest_rq->lock);
4942
4943 /* make sure the requested cpu hasn't gone down in the meantime */
4944 if (unlikely(busiest_cpu != smp_processor_id() ||
4945 !busiest_rq->active_balance))
4946 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004947
4948 /* Is there any task to move? */
4949 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004950 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004951
4952 /*
4953 * This condition is "impossible", if it occurs
4954 * we need to fix it. Originally reported by
4955 * Bjorn Helgaas on a 128-cpu setup.
4956 */
4957 BUG_ON(busiest_rq == target_rq);
4958
4959 /* move a task from busiest_rq to target_rq */
4960 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004961
4962 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004963 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004964 for_each_domain(target_cpu, sd) {
4965 if ((sd->flags & SD_LOAD_BALANCE) &&
4966 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4967 break;
4968 }
4969
4970 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004971 struct lb_env env = {
4972 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004973 .dst_cpu = target_cpu,
4974 .dst_rq = target_rq,
4975 .src_cpu = busiest_rq->cpu,
4976 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004977 .idle = CPU_IDLE,
4978 };
4979
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004980 schedstat_inc(sd, alb_count);
4981
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004982 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004983 schedstat_inc(sd, alb_pushed);
4984 else
4985 schedstat_inc(sd, alb_failed);
4986 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004987 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004988 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004989out_unlock:
4990 busiest_rq->active_balance = 0;
4991 raw_spin_unlock_irq(&busiest_rq->lock);
4992 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004993}
4994
4995#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004996/*
4997 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004998 * - When one of the busy CPUs notice that there may be an idle rebalancing
4999 * needed, they will kick the idle load balancer, which then does idle
5000 * load balancing for all the idle CPUs.
5001 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005002static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005003 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005004 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005005 unsigned long next_balance; /* in jiffy units */
5006} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005007
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01005008static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005009{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005010 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005011
Suresh Siddha786d6dc2011-12-01 17:07:35 -08005012 if (ilb < nr_cpu_ids && idle_cpu(ilb))
5013 return ilb;
5014
5015 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005016}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005017
5018/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005019 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
5020 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
5021 * CPU (if there is one).
5022 */
5023static void nohz_balancer_kick(int cpu)
5024{
5025 int ilb_cpu;
5026
5027 nohz.next_balance++;
5028
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005029 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005030
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005031 if (ilb_cpu >= nr_cpu_ids)
5032 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005033
Suresh Siddhacd490c52011-12-06 11:26:34 -08005034 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08005035 return;
5036 /*
5037 * Use smp_send_reschedule() instead of resched_cpu().
5038 * This way we generate a sched IPI on the target cpu which
5039 * is idle. And the softirq performing nohz idle load balance
5040 * will be run before returning from the IPI.
5041 */
5042 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005043 return;
5044}
5045
Alex Shic1cc0172012-09-10 15:10:58 +08005046static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08005047{
5048 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
5049 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
5050 atomic_dec(&nohz.nr_cpus);
5051 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
5052 }
5053}
5054
Suresh Siddha69e1e812011-12-01 17:07:33 -08005055static inline void set_cpu_sd_state_busy(void)
5056{
5057 struct sched_domain *sd;
5058 int cpu = smp_processor_id();
5059
5060 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5061 return;
5062 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
5063
5064 rcu_read_lock();
5065 for_each_domain(cpu, sd)
5066 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
5067 rcu_read_unlock();
5068}
5069
5070void set_cpu_sd_state_idle(void)
5071{
5072 struct sched_domain *sd;
5073 int cpu = smp_processor_id();
5074
5075 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5076 return;
5077 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5078
5079 rcu_read_lock();
5080 for_each_domain(cpu, sd)
5081 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5082 rcu_read_unlock();
5083}
5084
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005085/*
Alex Shic1cc0172012-09-10 15:10:58 +08005086 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005087 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005088 */
Alex Shic1cc0172012-09-10 15:10:58 +08005089void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005090{
Suresh Siddha71325962012-01-19 18:28:57 -08005091 /*
5092 * If this cpu is going down, then nothing needs to be done.
5093 */
5094 if (!cpu_active(cpu))
5095 return;
5096
Alex Shic1cc0172012-09-10 15:10:58 +08005097 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5098 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005099
Alex Shic1cc0172012-09-10 15:10:58 +08005100 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5101 atomic_inc(&nohz.nr_cpus);
5102 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005103}
Suresh Siddha71325962012-01-19 18:28:57 -08005104
5105static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5106 unsigned long action, void *hcpu)
5107{
5108 switch (action & ~CPU_TASKS_FROZEN) {
5109 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005110 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005111 return NOTIFY_OK;
5112 default:
5113 return NOTIFY_DONE;
5114 }
5115}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005116#endif
5117
5118static DEFINE_SPINLOCK(balancing);
5119
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005120/*
5121 * Scale the max load_balance interval with the number of CPUs in the system.
5122 * This trades load-balance latency on larger machines for less cross talk.
5123 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005124void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005125{
5126 max_load_balance_interval = HZ*num_online_cpus()/10;
5127}
5128
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005129/*
5130 * It checks each scheduling domain to see if it is due to be balanced,
5131 * and initiates a balancing operation if so.
5132 *
5133 * Balancing parameters are set up in arch_init_sched_domains.
5134 */
5135static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5136{
5137 int balance = 1;
5138 struct rq *rq = cpu_rq(cpu);
5139 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005140 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005141 /* Earliest time when we have to do rebalance again */
5142 unsigned long next_balance = jiffies + 60*HZ;
5143 int update_next_balance = 0;
5144 int need_serialize;
5145
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005146 update_shares(cpu);
5147
Peter Zijlstradce840a2011-04-07 14:09:50 +02005148 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005149 for_each_domain(cpu, sd) {
5150 if (!(sd->flags & SD_LOAD_BALANCE))
5151 continue;
5152
5153 interval = sd->balance_interval;
5154 if (idle != CPU_IDLE)
5155 interval *= sd->busy_factor;
5156
5157 /* scale ms to jiffies */
5158 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005159 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005160
5161 need_serialize = sd->flags & SD_SERIALIZE;
5162
5163 if (need_serialize) {
5164 if (!spin_trylock(&balancing))
5165 goto out;
5166 }
5167
5168 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5169 if (load_balance(cpu, rq, sd, idle, &balance)) {
5170 /*
5171 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005172 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005173 */
5174 idle = CPU_NOT_IDLE;
5175 }
5176 sd->last_balance = jiffies;
5177 }
5178 if (need_serialize)
5179 spin_unlock(&balancing);
5180out:
5181 if (time_after(next_balance, sd->last_balance + interval)) {
5182 next_balance = sd->last_balance + interval;
5183 update_next_balance = 1;
5184 }
5185
5186 /*
5187 * Stop the load balance at this level. There is another
5188 * CPU in our sched group which is doing load balancing more
5189 * actively.
5190 */
5191 if (!balance)
5192 break;
5193 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005194 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005195
5196 /*
5197 * next_balance will be updated only when there is a need.
5198 * When the cpu is attached to null domain for ex, it will not be
5199 * updated.
5200 */
5201 if (likely(update_next_balance))
5202 rq->next_balance = next_balance;
5203}
5204
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005205#ifdef CONFIG_NO_HZ
5206/*
5207 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5208 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5209 */
5210static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5211{
5212 struct rq *this_rq = cpu_rq(this_cpu);
5213 struct rq *rq;
5214 int balance_cpu;
5215
Suresh Siddha1c792db2011-12-01 17:07:32 -08005216 if (idle != CPU_IDLE ||
5217 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5218 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005219
5220 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005221 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005222 continue;
5223
5224 /*
5225 * If this cpu gets work to do, stop the load balancing
5226 * work being done for other cpus. Next load
5227 * balancing owner will pick it up.
5228 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005229 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005230 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005231
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005232 rq = cpu_rq(balance_cpu);
5233
5234 raw_spin_lock_irq(&rq->lock);
5235 update_rq_clock(rq);
5236 update_idle_cpu_load(rq);
5237 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005238
5239 rebalance_domains(balance_cpu, CPU_IDLE);
5240
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005241 if (time_after(this_rq->next_balance, rq->next_balance))
5242 this_rq->next_balance = rq->next_balance;
5243 }
5244 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005245end:
5246 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005247}
5248
5249/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005250 * Current heuristic for kicking the idle load balancer in the presence
5251 * of an idle cpu is the system.
5252 * - This rq has more than one task.
5253 * - At any scheduler domain level, this cpu's scheduler group has multiple
5254 * busy cpu's exceeding the group's power.
5255 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5256 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005257 */
5258static inline int nohz_kick_needed(struct rq *rq, int cpu)
5259{
5260 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005261 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005262
Suresh Siddha1c792db2011-12-01 17:07:32 -08005263 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005264 return 0;
5265
Suresh Siddha1c792db2011-12-01 17:07:32 -08005266 /*
5267 * We may be recently in ticked or tickless idle mode. At the first
5268 * busy tick after returning from idle, we will update the busy stats.
5269 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005270 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005271 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005272
5273 /*
5274 * None are in tickless mode and hence no need for NOHZ idle load
5275 * balancing.
5276 */
5277 if (likely(!atomic_read(&nohz.nr_cpus)))
5278 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005279
5280 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005281 return 0;
5282
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005283 if (rq->nr_running >= 2)
5284 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005285
Peter Zijlstra067491b2011-12-07 14:32:08 +01005286 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005287 for_each_domain(cpu, sd) {
5288 struct sched_group *sg = sd->groups;
5289 struct sched_group_power *sgp = sg->sgp;
5290 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005291
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005292 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005293 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005294
5295 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5296 && (cpumask_first_and(nohz.idle_cpus_mask,
5297 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005298 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005299
5300 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5301 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005302 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005303 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005304 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005305
5306need_kick_unlock:
5307 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005308need_kick:
5309 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005310}
5311#else
5312static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5313#endif
5314
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005315/*
5316 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005317 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005318 */
5319static void run_rebalance_domains(struct softirq_action *h)
5320{
5321 int this_cpu = smp_processor_id();
5322 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005323 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005324 CPU_IDLE : CPU_NOT_IDLE;
5325
5326 rebalance_domains(this_cpu, idle);
5327
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005328 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005329 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005330 * balancing on behalf of the other idle cpus whose ticks are
5331 * stopped.
5332 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005333 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005334}
5335
5336static inline int on_null_domain(int cpu)
5337{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005338 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005339}
5340
5341/*
5342 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005343 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005344void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005345{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005346 /* Don't need to rebalance while attached to NULL domain */
5347 if (time_after_eq(jiffies, rq->next_balance) &&
5348 likely(!on_null_domain(cpu)))
5349 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005350#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005351 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005352 nohz_balancer_kick(cpu);
5353#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005354}
5355
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005356static void rq_online_fair(struct rq *rq)
5357{
5358 update_sysctl();
5359}
5360
5361static void rq_offline_fair(struct rq *rq)
5362{
5363 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005364
5365 /* Ensure any throttled groups are reachable by pick_next_task */
5366 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005367}
5368
Dhaval Giani55e12e52008-06-24 23:39:43 +05305369#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005371/*
5372 * scheduler tick hitting a task of our scheduling class:
5373 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005374static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005375{
5376 struct cfs_rq *cfs_rq;
5377 struct sched_entity *se = &curr->se;
5378
5379 for_each_sched_entity(se) {
5380 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005381 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005382 }
Ben Segall18bf2802012-10-04 12:51:20 +02005383
5384 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005385}
5386
5387/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005388 * called on fork with the child task as argument from the parent's context
5389 * - child not yet on the tasklist
5390 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005391 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005392static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005393{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005394 struct cfs_rq *cfs_rq;
5395 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005396 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005397 struct rq *rq = this_rq();
5398 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005399
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005400 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005401
Peter Zijlstra861d0342010-08-19 13:31:43 +02005402 update_rq_clock(rq);
5403
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005404 cfs_rq = task_cfs_rq(current);
5405 curr = cfs_rq->curr;
5406
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005407 if (unlikely(task_cpu(p) != this_cpu)) {
5408 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005409 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005410 rcu_read_unlock();
5411 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005412
Ting Yang7109c442007-08-28 12:53:24 +02005413 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005414
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005415 if (curr)
5416 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005417 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005418
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005419 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005420 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005421 * Upon rescheduling, sched_class::put_prev_task() will place
5422 * 'current' within the tree based on its new key value.
5423 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005424 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305425 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005426 }
5427
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005428 se->vruntime -= cfs_rq->min_vruntime;
5429
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005430 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005431}
5432
Steven Rostedtcb469842008-01-25 21:08:22 +01005433/*
5434 * Priority of the task has changed. Check to see if we preempt
5435 * the current task.
5436 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005437static void
5438prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005439{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005440 if (!p->se.on_rq)
5441 return;
5442
Steven Rostedtcb469842008-01-25 21:08:22 +01005443 /*
5444 * Reschedule if we are currently running on this runqueue and
5445 * our priority decreased, or if we are not currently running on
5446 * this runqueue and our priority is higher than the current's
5447 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005448 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005449 if (p->prio > oldprio)
5450 resched_task(rq->curr);
5451 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005452 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005453}
5454
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005455static void switched_from_fair(struct rq *rq, struct task_struct *p)
5456{
5457 struct sched_entity *se = &p->se;
5458 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5459
5460 /*
5461 * Ensure the task's vruntime is normalized, so that when its
5462 * switched back to the fair class the enqueue_entity(.flags=0) will
5463 * do the right thing.
5464 *
5465 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5466 * have normalized the vruntime, if it was !on_rq, then only when
5467 * the task is sleeping will it still have non-normalized vruntime.
5468 */
5469 if (!se->on_rq && p->state != TASK_RUNNING) {
5470 /*
5471 * Fix up our vruntime so that the current sleep doesn't
5472 * cause 'unlimited' sleep bonus.
5473 */
5474 place_entity(cfs_rq, se, 0);
5475 se->vruntime -= cfs_rq->min_vruntime;
5476 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005477
5478#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5479 /*
5480 * Remove our load from contribution when we leave sched_fair
5481 * and ensure we don't carry in an old decay_count if we
5482 * switch back.
5483 */
5484 if (p->se.avg.decay_count) {
5485 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5486 __synchronize_entity_decay(&p->se);
5487 subtract_blocked_load_contrib(cfs_rq,
5488 p->se.avg.load_avg_contrib);
5489 }
5490#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005491}
5492
Steven Rostedtcb469842008-01-25 21:08:22 +01005493/*
5494 * We switched to the sched_fair class.
5495 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005496static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005497{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005498 if (!p->se.on_rq)
5499 return;
5500
Steven Rostedtcb469842008-01-25 21:08:22 +01005501 /*
5502 * We were most likely switched from sched_rt, so
5503 * kick off the schedule if running, otherwise just see
5504 * if we can still preempt the current task.
5505 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005506 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005507 resched_task(rq->curr);
5508 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005509 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005510}
5511
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005512/* Account for a task changing its policy or group.
5513 *
5514 * This routine is mostly called to set cfs_rq->curr field when a task
5515 * migrates between groups/classes.
5516 */
5517static void set_curr_task_fair(struct rq *rq)
5518{
5519 struct sched_entity *se = &rq->curr->se;
5520
Paul Turnerec12cb72011-07-21 09:43:30 -07005521 for_each_sched_entity(se) {
5522 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5523
5524 set_next_entity(cfs_rq, se);
5525 /* ensure bandwidth has been allocated on our new cfs_rq */
5526 account_cfs_rq_runtime(cfs_rq, 0);
5527 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005528}
5529
Peter Zijlstra029632f2011-10-25 10:00:11 +02005530void init_cfs_rq(struct cfs_rq *cfs_rq)
5531{
5532 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005533 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5534#ifndef CONFIG_64BIT
5535 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5536#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005537#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5538 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005539 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005540#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005541}
5542
Peter Zijlstra810b3812008-02-29 15:21:01 -05005543#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005544static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005545{
Paul Turneraff3e492012-10-04 13:18:30 +02005546 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005547 /*
5548 * If the task was not on the rq at the time of this cgroup movement
5549 * it must have been asleep, sleeping tasks keep their ->vruntime
5550 * absolute on their old rq until wakeup (needed for the fair sleeper
5551 * bonus in place_entity()).
5552 *
5553 * If it was on the rq, we've just 'preempted' it, which does convert
5554 * ->vruntime to a relative base.
5555 *
5556 * Make sure both cases convert their relative position when migrating
5557 * to another cgroup's rq. This does somewhat interfere with the
5558 * fair sleeper stuff for the first placement, but who cares.
5559 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005560 /*
5561 * When !on_rq, vruntime of the task has usually NOT been normalized.
5562 * But there are some cases where it has already been normalized:
5563 *
5564 * - Moving a forked child which is waiting for being woken up by
5565 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005566 * - Moving a task which has been woken up by try_to_wake_up() and
5567 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005568 *
5569 * To prevent boost or penalty in the new cfs_rq caused by delta
5570 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5571 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005572 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005573 on_rq = 1;
5574
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005575 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005576 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5577 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005578 if (!on_rq) {
5579 cfs_rq = cfs_rq_of(&p->se);
5580 p->se.vruntime += cfs_rq->min_vruntime;
5581#ifdef CONFIG_SMP
5582 /*
5583 * migrate_task_rq_fair() will have removed our previous
5584 * contribution, but we must synchronize for ongoing future
5585 * decay.
5586 */
5587 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5588 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5589#endif
5590 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005591}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005592
5593void free_fair_sched_group(struct task_group *tg)
5594{
5595 int i;
5596
5597 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5598
5599 for_each_possible_cpu(i) {
5600 if (tg->cfs_rq)
5601 kfree(tg->cfs_rq[i]);
5602 if (tg->se)
5603 kfree(tg->se[i]);
5604 }
5605
5606 kfree(tg->cfs_rq);
5607 kfree(tg->se);
5608}
5609
5610int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5611{
5612 struct cfs_rq *cfs_rq;
5613 struct sched_entity *se;
5614 int i;
5615
5616 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5617 if (!tg->cfs_rq)
5618 goto err;
5619 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5620 if (!tg->se)
5621 goto err;
5622
5623 tg->shares = NICE_0_LOAD;
5624
5625 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5626
5627 for_each_possible_cpu(i) {
5628 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5629 GFP_KERNEL, cpu_to_node(i));
5630 if (!cfs_rq)
5631 goto err;
5632
5633 se = kzalloc_node(sizeof(struct sched_entity),
5634 GFP_KERNEL, cpu_to_node(i));
5635 if (!se)
5636 goto err_free_rq;
5637
5638 init_cfs_rq(cfs_rq);
5639 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5640 }
5641
5642 return 1;
5643
5644err_free_rq:
5645 kfree(cfs_rq);
5646err:
5647 return 0;
5648}
5649
5650void unregister_fair_sched_group(struct task_group *tg, int cpu)
5651{
5652 struct rq *rq = cpu_rq(cpu);
5653 unsigned long flags;
5654
5655 /*
5656 * Only empty task groups can be destroyed; so we can speculatively
5657 * check on_list without danger of it being re-added.
5658 */
5659 if (!tg->cfs_rq[cpu]->on_list)
5660 return;
5661
5662 raw_spin_lock_irqsave(&rq->lock, flags);
5663 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5664 raw_spin_unlock_irqrestore(&rq->lock, flags);
5665}
5666
5667void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5668 struct sched_entity *se, int cpu,
5669 struct sched_entity *parent)
5670{
5671 struct rq *rq = cpu_rq(cpu);
5672
5673 cfs_rq->tg = tg;
5674 cfs_rq->rq = rq;
5675#ifdef CONFIG_SMP
5676 /* allow initial update_cfs_load() to truncate */
5677 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005678#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005679 init_cfs_rq_runtime(cfs_rq);
5680
5681 tg->cfs_rq[cpu] = cfs_rq;
5682 tg->se[cpu] = se;
5683
5684 /* se could be NULL for root_task_group */
5685 if (!se)
5686 return;
5687
5688 if (!parent)
5689 se->cfs_rq = &rq->cfs;
5690 else
5691 se->cfs_rq = parent->my_q;
5692
5693 se->my_q = cfs_rq;
5694 update_load_set(&se->load, 0);
5695 se->parent = parent;
5696}
5697
5698static DEFINE_MUTEX(shares_mutex);
5699
5700int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5701{
5702 int i;
5703 unsigned long flags;
5704
5705 /*
5706 * We can't change the weight of the root cgroup.
5707 */
5708 if (!tg->se[0])
5709 return -EINVAL;
5710
5711 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5712
5713 mutex_lock(&shares_mutex);
5714 if (tg->shares == shares)
5715 goto done;
5716
5717 tg->shares = shares;
5718 for_each_possible_cpu(i) {
5719 struct rq *rq = cpu_rq(i);
5720 struct sched_entity *se;
5721
5722 se = tg->se[i];
5723 /* Propagate contribution to hierarchy */
5724 raw_spin_lock_irqsave(&rq->lock, flags);
5725 for_each_sched_entity(se)
5726 update_cfs_shares(group_cfs_rq(se));
5727 raw_spin_unlock_irqrestore(&rq->lock, flags);
5728 }
5729
5730done:
5731 mutex_unlock(&shares_mutex);
5732 return 0;
5733}
5734#else /* CONFIG_FAIR_GROUP_SCHED */
5735
5736void free_fair_sched_group(struct task_group *tg) { }
5737
5738int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5739{
5740 return 1;
5741}
5742
5743void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5744
5745#endif /* CONFIG_FAIR_GROUP_SCHED */
5746
Peter Zijlstra810b3812008-02-29 15:21:01 -05005747
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005748static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005749{
5750 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005751 unsigned int rr_interval = 0;
5752
5753 /*
5754 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5755 * idle runqueue:
5756 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005757 if (rq->cfs.load.weight)
5758 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005759
5760 return rr_interval;
5761}
5762
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005763/*
5764 * All the scheduling class methods:
5765 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005766const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005767 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005768 .enqueue_task = enqueue_task_fair,
5769 .dequeue_task = dequeue_task_fair,
5770 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005771 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005772
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005773 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005774
5775 .pick_next_task = pick_next_task_fair,
5776 .put_prev_task = put_prev_task_fair,
5777
Peter Williams681f3e62007-10-24 18:23:51 +02005778#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005779 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005780 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005781
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005782 .rq_online = rq_online_fair,
5783 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005784
5785 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005786#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005787
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005788 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005789 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005790 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005791
5792 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005793 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005794 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005795
Peter Williams0d721ce2009-09-21 01:31:53 +00005796 .get_rr_interval = get_rr_interval_fair,
5797
Peter Zijlstra810b3812008-02-29 15:21:01 -05005798#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005799 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005800#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005801};
5802
5803#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005804void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005805{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005806 struct cfs_rq *cfs_rq;
5807
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005808 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005809 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005810 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005811 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005812}
5813#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005814
5815__init void init_sched_fair_class(void)
5816{
5817#ifdef CONFIG_SMP
5818 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5819
5820#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005821 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005822 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005823 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005824#endif
5825#endif /* SMP */
5826
5827}