blob: 873c9f5c5796faef1ed6577f18e44a96c7f715d6 [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 Zijlstra6d0f0ebd2007-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 Turnerf1b17282012-10-04 13:18:31 +02001225static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
1226
Paul Turner9d85f212012-10-04 13:18:29 +02001227/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001228static inline void update_entity_load_avg(struct sched_entity *se,
1229 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001230{
Paul Turner2dac7542012-10-04 13:18:30 +02001231 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1232 long contrib_delta;
Paul Turnerf1b17282012-10-04 13:18:31 +02001233 u64 now;
Paul Turner2dac7542012-10-04 13:18:30 +02001234
Paul Turnerf1b17282012-10-04 13:18:31 +02001235 /*
1236 * For a group entity we need to use their owned cfs_rq_clock_task() in
1237 * case they are the parent of a throttled hierarchy.
1238 */
1239 if (entity_is_task(se))
1240 now = cfs_rq_clock_task(cfs_rq);
1241 else
1242 now = cfs_rq_clock_task(group_cfs_rq(se));
1243
1244 if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
Paul Turner2dac7542012-10-04 13:18:30 +02001245 return;
1246
1247 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001248
1249 if (!update_cfs_rq)
1250 return;
1251
Paul Turner2dac7542012-10-04 13:18:30 +02001252 if (se->on_rq)
1253 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001254 else
1255 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1256}
1257
1258/*
1259 * Decay the load contributed by all blocked children and account this so that
1260 * their contribution may appropriately discounted when they wake up.
1261 */
Paul Turneraff3e492012-10-04 13:18:30 +02001262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001263{
Paul Turnerf1b17282012-10-04 13:18:31 +02001264 u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
Paul Turner9ee474f2012-10-04 13:18:30 +02001265 u64 decays;
1266
1267 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001268 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001269 return;
1270
Paul Turneraff3e492012-10-04 13:18:30 +02001271 if (atomic64_read(&cfs_rq->removed_load)) {
1272 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1273 subtract_blocked_load_contrib(cfs_rq, removed_load);
1274 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001275
Paul Turneraff3e492012-10-04 13:18:30 +02001276 if (decays) {
1277 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1278 decays);
1279 atomic64_add(decays, &cfs_rq->decay_counter);
1280 cfs_rq->last_decay = now;
1281 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001282
1283 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02001284}
Ben Segall18bf2802012-10-04 12:51:20 +02001285
1286static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1287{
1288 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
Paul Turnerbb17f652012-10-04 13:18:31 +02001289 __update_tg_runnable_avg(&rq->avg, &rq->cfs);
Ben Segall18bf2802012-10-04 12:51:20 +02001290}
Paul Turner2dac7542012-10-04 13:18:30 +02001291
1292/* Add the load generated by se into cfs_rq's child load-average */
1293static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001294 struct sched_entity *se,
1295 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001296{
Paul Turneraff3e492012-10-04 13:18:30 +02001297 /*
1298 * We track migrations using entity decay_count <= 0, on a wake-up
1299 * migration we use a negative decay count to track the remote decays
1300 * accumulated while sleeping.
1301 */
1302 if (unlikely(se->avg.decay_count <= 0)) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001303 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
Paul Turneraff3e492012-10-04 13:18:30 +02001304 if (se->avg.decay_count) {
1305 /*
1306 * In a wake-up migration we have to approximate the
1307 * time sleeping. This is because we can't synchronize
1308 * clock_task between the two cpus, and it is not
1309 * guaranteed to be read-safe. Instead, we can
1310 * approximate this using our carried decays, which are
1311 * explicitly atomically readable.
1312 */
1313 se->avg.last_runnable_update -= (-se->avg.decay_count)
1314 << 20;
1315 update_entity_load_avg(se, 0);
1316 /* Indicate that we're now synchronized and on-rq */
1317 se->avg.decay_count = 0;
1318 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001319 wakeup = 0;
1320 } else {
1321 __synchronize_entity_decay(se);
1322 }
1323
Paul Turneraff3e492012-10-04 13:18:30 +02001324 /* migrated tasks did not contribute to our blocked load */
1325 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001326 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02001327 update_entity_load_avg(se, 0);
1328 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001329
Paul Turner2dac7542012-10-04 13:18:30 +02001330 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02001331 /* we force update consideration on load-balancer moves */
1332 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02001333}
1334
Paul Turner9ee474f2012-10-04 13:18:30 +02001335/*
1336 * Remove se's load from this cfs_rq child load-average, if the entity is
1337 * transitioning to a blocked state we track its projected decay using
1338 * blocked_load_avg.
1339 */
Paul Turner2dac7542012-10-04 13:18:30 +02001340static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001341 struct sched_entity *se,
1342 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001343{
Paul Turner9ee474f2012-10-04 13:18:30 +02001344 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001345 /* we force update consideration on load-balancer moves */
1346 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02001347
Paul Turner2dac7542012-10-04 13:18:30 +02001348 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001349 if (sleep) {
1350 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1351 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1352 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001353}
Paul Turner9d85f212012-10-04 13:18:29 +02001354#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001355static inline void update_entity_load_avg(struct sched_entity *se,
1356 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001357static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001358static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001359 struct sched_entity *se,
1360 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001361static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001362 struct sched_entity *se,
1363 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02001364static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
1365 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001366#endif
1367
Ingo Molnar2396af62007-08-09 11:16:48 +02001368static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001369{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001370#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001371 struct task_struct *tsk = NULL;
1372
1373 if (entity_is_task(se))
1374 tsk = task_of(se);
1375
Lucas De Marchi41acab82010-03-10 23:37:45 -03001376 if (se->statistics.sleep_start) {
1377 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001378
1379 if ((s64)delta < 0)
1380 delta = 0;
1381
Lucas De Marchi41acab82010-03-10 23:37:45 -03001382 if (unlikely(delta > se->statistics.sleep_max))
1383 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001384
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001385 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001386 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001387
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001388 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001389 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001390 trace_sched_stat_sleep(tsk, delta);
1391 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001392 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001393 if (se->statistics.block_start) {
1394 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001395
1396 if ((s64)delta < 0)
1397 delta = 0;
1398
Lucas De Marchi41acab82010-03-10 23:37:45 -03001399 if (unlikely(delta > se->statistics.block_max))
1400 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001401
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001402 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001403 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001404
Peter Zijlstrae4143142009-07-23 20:13:26 +02001405 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001406 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001407 se->statistics.iowait_sum += delta;
1408 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001409 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001410 }
1411
Andrew Vaginb781a602011-11-28 12:03:35 +03001412 trace_sched_stat_blocked(tsk, delta);
1413
Peter Zijlstrae4143142009-07-23 20:13:26 +02001414 /*
1415 * Blocking time is in units of nanosecs, so shift by
1416 * 20 to get a milliseconds-range estimation of the
1417 * amount of time that the task spent sleeping:
1418 */
1419 if (unlikely(prof_on == SLEEP_PROFILING)) {
1420 profile_hits(SLEEP_PROFILING,
1421 (void *)get_wchan(tsk),
1422 delta >> 20);
1423 }
1424 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001425 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001426 }
1427#endif
1428}
1429
Peter Zijlstraddc97292007-10-15 17:00:10 +02001430static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1431{
1432#ifdef CONFIG_SCHED_DEBUG
1433 s64 d = se->vruntime - cfs_rq->min_vruntime;
1434
1435 if (d < 0)
1436 d = -d;
1437
1438 if (d > 3*sysctl_sched_latency)
1439 schedstat_inc(cfs_rq, nr_spread_over);
1440#endif
1441}
1442
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001443static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001444place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1445{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001446 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001447
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001448 /*
1449 * The 'current' period is already promised to the current tasks,
1450 * however the extra weight of the new task will slow them down a
1451 * little, place the new task so that it fits in the slot that
1452 * stays open at the end.
1453 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001454 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001455 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001456
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001457 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001458 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001459 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001460
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001461 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001462 * Halve their sleep time's effect, to allow
1463 * for a gentler effect of sleepers:
1464 */
1465 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1466 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001467
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001468 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001469 }
1470
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001471 /* ensure we never gain time by being placed backwards. */
1472 vruntime = max_vruntime(se->vruntime, vruntime);
1473
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001474 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001475}
1476
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001477static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1478
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001479static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001480enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001481{
1482 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001483 * Update the normalized vruntime before updating min_vruntime
1484 * through callig update_curr().
1485 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001486 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001487 se->vruntime += cfs_rq->min_vruntime;
1488
1489 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001490 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001491 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001492 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001493 update_cfs_load(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02001494 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001495 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001496 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001497
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001498 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001499 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001500 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001501 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001502
Ingo Molnard2417e52007-08-09 11:16:47 +02001503 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001504 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001505 if (se != cfs_rq->curr)
1506 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001507 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001508
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001509 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001510 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001511 check_enqueue_throttle(cfs_rq);
1512 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001513}
1514
Rik van Riel2c13c9192011-02-01 09:48:37 -05001515static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001516{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001517 for_each_sched_entity(se) {
1518 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1519 if (cfs_rq->last == se)
1520 cfs_rq->last = NULL;
1521 else
1522 break;
1523 }
1524}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001525
Rik van Riel2c13c9192011-02-01 09:48:37 -05001526static void __clear_buddies_next(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->next == se)
1531 cfs_rq->next = NULL;
1532 else
1533 break;
1534 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001535}
1536
Rik van Rielac53db52011-02-01 09:51:03 -05001537static void __clear_buddies_skip(struct sched_entity *se)
1538{
1539 for_each_sched_entity(se) {
1540 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1541 if (cfs_rq->skip == se)
1542 cfs_rq->skip = NULL;
1543 else
1544 break;
1545 }
1546}
1547
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001548static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1549{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001550 if (cfs_rq->last == se)
1551 __clear_buddies_last(se);
1552
1553 if (cfs_rq->next == se)
1554 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001555
1556 if (cfs_rq->skip == se)
1557 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001558}
1559
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001560static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001562static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001563dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001564{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001565 /*
1566 * Update run-time statistics of the 'current'.
1567 */
1568 update_curr(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02001569 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001570
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001571 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001572 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001573#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001574 if (entity_is_task(se)) {
1575 struct task_struct *tsk = task_of(se);
1576
1577 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001578 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001579 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001580 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001581 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001582#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001583 }
1584
Peter Zijlstra2002c692008-11-11 11:52:33 +01001585 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001586
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001587 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001588 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001589 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001590 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001591 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001592
1593 /*
1594 * Normalize the entity after updating the min_vruntime because the
1595 * update can refer to the ->curr item and we need to reflect this
1596 * movement in our normalized position.
1597 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001598 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001599 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001600
Paul Turnerd8b49862011-07-21 09:43:41 -07001601 /* return excess runtime on last dequeue */
1602 return_cfs_rq_runtime(cfs_rq);
1603
Peter Zijlstra1e876232011-05-17 16:21:10 -07001604 update_min_vruntime(cfs_rq);
1605 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001606}
1607
1608/*
1609 * Preempt the current task with a newly woken task if needed:
1610 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001611static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001612check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001613{
Peter Zijlstra11697832007-09-05 14:32:49 +02001614 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001615 struct sched_entity *se;
1616 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001617
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001618 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001619 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001620 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001621 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001622 /*
1623 * The current task ran long enough, ensure it doesn't get
1624 * re-elected due to buddy favours.
1625 */
1626 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001627 return;
1628 }
1629
1630 /*
1631 * Ensure that a task that missed wakeup preemption by a
1632 * narrow margin doesn't have to wait for a full slice.
1633 * This also mitigates buddy induced latencies under load.
1634 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001635 if (delta_exec < sysctl_sched_min_granularity)
1636 return;
1637
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001638 se = __pick_first_entity(cfs_rq);
1639 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001640
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001641 if (delta < 0)
1642 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001643
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001644 if (delta > ideal_runtime)
1645 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001646}
1647
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001648static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001649set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001650{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001651 /* 'current' is not kept within the tree. */
1652 if (se->on_rq) {
1653 /*
1654 * Any task has to be enqueued before it get to execute on
1655 * a CPU. So account for the time it spent waiting on the
1656 * runqueue.
1657 */
1658 update_stats_wait_end(cfs_rq, se);
1659 __dequeue_entity(cfs_rq, se);
1660 }
1661
Ingo Molnar79303e92007-08-09 11:16:47 +02001662 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001663 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001664#ifdef CONFIG_SCHEDSTATS
1665 /*
1666 * Track our maximum slice length, if the CPU's load is at
1667 * least twice that of our own weight (i.e. dont track it
1668 * when there are only lesser-weight tasks around):
1669 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001670 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001671 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001672 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1673 }
1674#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001675 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001676}
1677
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001678static int
1679wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1680
Rik van Rielac53db52011-02-01 09:51:03 -05001681/*
1682 * Pick the next process, keeping these things in mind, in this order:
1683 * 1) keep things fair between processes/task groups
1684 * 2) pick the "next" process, since someone really wants that to run
1685 * 3) pick the "last" process, for cache locality
1686 * 4) do not run the "skip" process, if something else is available
1687 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001688static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001689{
Rik van Rielac53db52011-02-01 09:51:03 -05001690 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001691 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001692
Rik van Rielac53db52011-02-01 09:51:03 -05001693 /*
1694 * Avoid running the skip buddy, if running something else can
1695 * be done without getting too unfair.
1696 */
1697 if (cfs_rq->skip == se) {
1698 struct sched_entity *second = __pick_next_entity(se);
1699 if (second && wakeup_preempt_entity(second, left) < 1)
1700 se = second;
1701 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001702
Mike Galbraithf685cea2009-10-23 23:09:22 +02001703 /*
1704 * Prefer last buddy, try to return the CPU to a preempted task.
1705 */
1706 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1707 se = cfs_rq->last;
1708
Rik van Rielac53db52011-02-01 09:51:03 -05001709 /*
1710 * Someone really wants this to run. If it's not unfair, run it.
1711 */
1712 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1713 se = cfs_rq->next;
1714
Mike Galbraithf685cea2009-10-23 23:09:22 +02001715 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001716
1717 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001718}
1719
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001720static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1721
Ingo Molnarab6cde22007-08-09 11:16:48 +02001722static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001723{
1724 /*
1725 * If still on the runqueue then deactivate_task()
1726 * was not called and update_curr() has to be done:
1727 */
1728 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001729 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001730
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001731 /* throttle cfs_rqs exceeding runtime */
1732 check_cfs_rq_runtime(cfs_rq);
1733
Peter Zijlstraddc97292007-10-15 17:00:10 +02001734 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001735 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001736 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001737 /* Put 'current' back into the tree. */
1738 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001739 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001740 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001741 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001742 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001743}
1744
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001745static void
1746entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001747{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001748 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001749 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001750 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001751 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001752
Paul Turner43365bd2010-12-15 19:10:17 -08001753 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001754 * Ensure that runnable average is periodically updated.
1755 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001756 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001757 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001758
1759 /*
Paul Turner43365bd2010-12-15 19:10:17 -08001760 * Update share accounting for long-running entities.
1761 */
1762 update_entity_shares_tick(cfs_rq);
1763
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001764#ifdef CONFIG_SCHED_HRTICK
1765 /*
1766 * queued ticks are scheduled to match the slice, so don't bother
1767 * validating it and just reschedule.
1768 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001769 if (queued) {
1770 resched_task(rq_of(cfs_rq)->curr);
1771 return;
1772 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001773 /*
1774 * don't let the period tick interfere with the hrtick preemption
1775 */
1776 if (!sched_feat(DOUBLE_TICK) &&
1777 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1778 return;
1779#endif
1780
Yong Zhang2c2efae2011-07-29 16:20:33 +08001781 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001782 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001783}
1784
Paul Turnerab84d312011-07-21 09:43:28 -07001785
1786/**************************************************
1787 * CFS bandwidth control machinery
1788 */
1789
1790#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001791
1792#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001793static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001794
1795static inline bool cfs_bandwidth_used(void)
1796{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001797 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001798}
1799
1800void account_cfs_bandwidth_used(int enabled, int was_enabled)
1801{
1802 /* only need to count groups transitioning between enabled/!enabled */
1803 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001804 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001805 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001806 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001807}
1808#else /* HAVE_JUMP_LABEL */
1809static bool cfs_bandwidth_used(void)
1810{
1811 return true;
1812}
1813
1814void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1815#endif /* HAVE_JUMP_LABEL */
1816
Paul Turnerab84d312011-07-21 09:43:28 -07001817/*
1818 * default period for cfs group bandwidth.
1819 * default: 0.1s, units: nanoseconds
1820 */
1821static inline u64 default_cfs_period(void)
1822{
1823 return 100000000ULL;
1824}
Paul Turnerec12cb72011-07-21 09:43:30 -07001825
1826static inline u64 sched_cfs_bandwidth_slice(void)
1827{
1828 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1829}
1830
Paul Turnera9cf55b2011-07-21 09:43:32 -07001831/*
1832 * Replenish runtime according to assigned quota and update expiration time.
1833 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1834 * additional synchronization around rq->lock.
1835 *
1836 * requires cfs_b->lock
1837 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001838void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001839{
1840 u64 now;
1841
1842 if (cfs_b->quota == RUNTIME_INF)
1843 return;
1844
1845 now = sched_clock_cpu(smp_processor_id());
1846 cfs_b->runtime = cfs_b->quota;
1847 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1848}
1849
Peter Zijlstra029632f2011-10-25 10:00:11 +02001850static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1851{
1852 return &tg->cfs_bandwidth;
1853}
1854
Paul Turnerf1b17282012-10-04 13:18:31 +02001855/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
1856static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
1857{
1858 if (unlikely(cfs_rq->throttle_count))
1859 return cfs_rq->throttled_clock_task;
1860
1861 return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time;
1862}
1863
Paul Turner85dac902011-07-21 09:43:33 -07001864/* returns 0 on failure to allocate runtime */
1865static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001866{
1867 struct task_group *tg = cfs_rq->tg;
1868 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001869 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001870
1871 /* note: this is a positive sum as runtime_remaining <= 0 */
1872 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1873
1874 raw_spin_lock(&cfs_b->lock);
1875 if (cfs_b->quota == RUNTIME_INF)
1876 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001877 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001878 /*
1879 * If the bandwidth pool has become inactive, then at least one
1880 * period must have elapsed since the last consumption.
1881 * Refresh the global state and ensure bandwidth timer becomes
1882 * active.
1883 */
1884 if (!cfs_b->timer_active) {
1885 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001886 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001887 }
Paul Turner58088ad2011-07-21 09:43:31 -07001888
1889 if (cfs_b->runtime > 0) {
1890 amount = min(cfs_b->runtime, min_amount);
1891 cfs_b->runtime -= amount;
1892 cfs_b->idle = 0;
1893 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001894 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001895 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001896 raw_spin_unlock(&cfs_b->lock);
1897
1898 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001899 /*
1900 * we may have advanced our local expiration to account for allowed
1901 * spread between our sched_clock and the one on which runtime was
1902 * issued.
1903 */
1904 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1905 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001906
1907 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001908}
1909
1910/*
1911 * Note: This depends on the synchronization provided by sched_clock and the
1912 * fact that rq->clock snapshots this value.
1913 */
1914static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1915{
1916 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1917 struct rq *rq = rq_of(cfs_rq);
1918
1919 /* if the deadline is ahead of our clock, nothing to do */
1920 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1921 return;
1922
1923 if (cfs_rq->runtime_remaining < 0)
1924 return;
1925
1926 /*
1927 * If the local deadline has passed we have to consider the
1928 * possibility that our sched_clock is 'fast' and the global deadline
1929 * has not truly expired.
1930 *
1931 * Fortunately we can check determine whether this the case by checking
1932 * whether the global deadline has advanced.
1933 */
1934
1935 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1936 /* extend local deadline, drift is bounded above by 2 ticks */
1937 cfs_rq->runtime_expires += TICK_NSEC;
1938 } else {
1939 /* global deadline is ahead, expiration has passed */
1940 cfs_rq->runtime_remaining = 0;
1941 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001942}
1943
1944static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1945 unsigned long delta_exec)
1946{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001947 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001948 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001949 expire_cfs_rq_runtime(cfs_rq);
1950
1951 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001952 return;
1953
Paul Turner85dac902011-07-21 09:43:33 -07001954 /*
1955 * if we're unable to extend our runtime we resched so that the active
1956 * hierarchy can be throttled
1957 */
1958 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1959 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001960}
1961
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001962static __always_inline
1963void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001964{
Paul Turner56f570e2011-11-07 20:26:33 -08001965 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001966 return;
1967
1968 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1969}
1970
Paul Turner85dac902011-07-21 09:43:33 -07001971static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1972{
Paul Turner56f570e2011-11-07 20:26:33 -08001973 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001974}
1975
Paul Turner64660c82011-07-21 09:43:36 -07001976/* check whether cfs_rq, or any parent, is throttled */
1977static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1978{
Paul Turner56f570e2011-11-07 20:26:33 -08001979 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001980}
1981
1982/*
1983 * Ensure that neither of the group entities corresponding to src_cpu or
1984 * dest_cpu are members of a throttled hierarchy when performing group
1985 * load-balance operations.
1986 */
1987static inline int throttled_lb_pair(struct task_group *tg,
1988 int src_cpu, int dest_cpu)
1989{
1990 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1991
1992 src_cfs_rq = tg->cfs_rq[src_cpu];
1993 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1994
1995 return throttled_hierarchy(src_cfs_rq) ||
1996 throttled_hierarchy(dest_cfs_rq);
1997}
1998
1999/* updated child weight may affect parent so we have to do this bottom up */
2000static int tg_unthrottle_up(struct task_group *tg, void *data)
2001{
2002 struct rq *rq = data;
2003 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
2004
2005 cfs_rq->throttle_count--;
2006#ifdef CONFIG_SMP
2007 if (!cfs_rq->throttle_count) {
2008 u64 delta = rq->clock_task - cfs_rq->load_stamp;
2009
2010 /* leaving throttled state, advance shares averaging windows */
2011 cfs_rq->load_stamp += delta;
2012 cfs_rq->load_last += delta;
2013
Paul Turnerf1b17282012-10-04 13:18:31 +02002014 /* adjust cfs_rq_clock_task() */
2015 cfs_rq->throttled_clock_task_time += rq->clock_task -
2016 cfs_rq->throttled_clock_task;
2017
Paul Turner64660c82011-07-21 09:43:36 -07002018 /* update entity weight now that we are on_rq again */
2019 update_cfs_shares(cfs_rq);
2020 }
2021#endif
2022
2023 return 0;
2024}
2025
2026static int tg_throttle_down(struct task_group *tg, void *data)
2027{
2028 struct rq *rq = data;
2029 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
2030
2031 /* group is entering throttled state, record last load */
Paul Turnerf1b17282012-10-04 13:18:31 +02002032 if (!cfs_rq->throttle_count) {
Paul Turner64660c82011-07-21 09:43:36 -07002033 update_cfs_load(cfs_rq, 0);
Paul Turnerf1b17282012-10-04 13:18:31 +02002034 cfs_rq->throttled_clock_task = rq->clock_task;
2035 }
Paul Turner64660c82011-07-21 09:43:36 -07002036 cfs_rq->throttle_count++;
2037
2038 return 0;
2039}
2040
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002041static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07002042{
2043 struct rq *rq = rq_of(cfs_rq);
2044 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2045 struct sched_entity *se;
2046 long task_delta, dequeue = 1;
2047
2048 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2049
Paul Turnerf1b17282012-10-04 13:18:31 +02002050 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07002051 rcu_read_lock();
2052 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
2053 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07002054
2055 task_delta = cfs_rq->h_nr_running;
2056 for_each_sched_entity(se) {
2057 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
2058 /* throttled entity or throttle-on-deactivate */
2059 if (!se->on_rq)
2060 break;
2061
2062 if (dequeue)
2063 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
2064 qcfs_rq->h_nr_running -= task_delta;
2065
2066 if (qcfs_rq->load.weight)
2067 dequeue = 0;
2068 }
2069
2070 if (!se)
2071 rq->nr_running -= task_delta;
2072
2073 cfs_rq->throttled = 1;
Paul Turnerf1b17282012-10-04 13:18:31 +02002074 cfs_rq->throttled_clock = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07002075 raw_spin_lock(&cfs_b->lock);
2076 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
2077 raw_spin_unlock(&cfs_b->lock);
2078}
2079
Peter Zijlstra029632f2011-10-25 10:00:11 +02002080void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07002081{
2082 struct rq *rq = rq_of(cfs_rq);
2083 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2084 struct sched_entity *se;
2085 int enqueue = 1;
2086 long task_delta;
2087
2088 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2089
2090 cfs_rq->throttled = 0;
2091 raw_spin_lock(&cfs_b->lock);
Paul Turnerf1b17282012-10-04 13:18:31 +02002092 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07002093 list_del_rcu(&cfs_rq->throttled_list);
2094 raw_spin_unlock(&cfs_b->lock);
2095
Paul Turner64660c82011-07-21 09:43:36 -07002096 update_rq_clock(rq);
2097 /* update hierarchical throttle state */
2098 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
2099
Paul Turner671fd9d2011-07-21 09:43:34 -07002100 if (!cfs_rq->load.weight)
2101 return;
2102
2103 task_delta = cfs_rq->h_nr_running;
2104 for_each_sched_entity(se) {
2105 if (se->on_rq)
2106 enqueue = 0;
2107
2108 cfs_rq = cfs_rq_of(se);
2109 if (enqueue)
2110 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2111 cfs_rq->h_nr_running += task_delta;
2112
2113 if (cfs_rq_throttled(cfs_rq))
2114 break;
2115 }
2116
2117 if (!se)
2118 rq->nr_running += task_delta;
2119
2120 /* determine whether we need to wake up potentially idle cpu */
2121 if (rq->curr == rq->idle && rq->cfs.nr_running)
2122 resched_task(rq->curr);
2123}
2124
2125static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2126 u64 remaining, u64 expires)
2127{
2128 struct cfs_rq *cfs_rq;
2129 u64 runtime = remaining;
2130
2131 rcu_read_lock();
2132 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2133 throttled_list) {
2134 struct rq *rq = rq_of(cfs_rq);
2135
2136 raw_spin_lock(&rq->lock);
2137 if (!cfs_rq_throttled(cfs_rq))
2138 goto next;
2139
2140 runtime = -cfs_rq->runtime_remaining + 1;
2141 if (runtime > remaining)
2142 runtime = remaining;
2143 remaining -= runtime;
2144
2145 cfs_rq->runtime_remaining += runtime;
2146 cfs_rq->runtime_expires = expires;
2147
2148 /* we check whether we're throttled above */
2149 if (cfs_rq->runtime_remaining > 0)
2150 unthrottle_cfs_rq(cfs_rq);
2151
2152next:
2153 raw_spin_unlock(&rq->lock);
2154
2155 if (!remaining)
2156 break;
2157 }
2158 rcu_read_unlock();
2159
2160 return remaining;
2161}
2162
Paul Turner58088ad2011-07-21 09:43:31 -07002163/*
2164 * Responsible for refilling a task_group's bandwidth and unthrottling its
2165 * cfs_rqs as appropriate. If there has been no activity within the last
2166 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2167 * used to track this state.
2168 */
2169static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2170{
Paul Turner671fd9d2011-07-21 09:43:34 -07002171 u64 runtime, runtime_expires;
2172 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002173
2174 raw_spin_lock(&cfs_b->lock);
2175 /* no need to continue the timer with no bandwidth constraint */
2176 if (cfs_b->quota == RUNTIME_INF)
2177 goto out_unlock;
2178
Paul Turner671fd9d2011-07-21 09:43:34 -07002179 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2180 /* idle depends on !throttled (for the case of a large deficit) */
2181 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002182 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002183
Paul Turnera9cf55b2011-07-21 09:43:32 -07002184 /* if we're going inactive then everything else can be deferred */
2185 if (idle)
2186 goto out_unlock;
2187
2188 __refill_cfs_bandwidth_runtime(cfs_b);
2189
Paul Turner671fd9d2011-07-21 09:43:34 -07002190 if (!throttled) {
2191 /* mark as potentially idle for the upcoming period */
2192 cfs_b->idle = 1;
2193 goto out_unlock;
2194 }
Paul Turner58088ad2011-07-21 09:43:31 -07002195
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002196 /* account preceding periods in which throttling occurred */
2197 cfs_b->nr_throttled += overrun;
2198
Paul Turner671fd9d2011-07-21 09:43:34 -07002199 /*
2200 * There are throttled entities so we must first use the new bandwidth
2201 * to unthrottle them before making it generally available. This
2202 * ensures that all existing debts will be paid before a new cfs_rq is
2203 * allowed to run.
2204 */
2205 runtime = cfs_b->runtime;
2206 runtime_expires = cfs_b->runtime_expires;
2207 cfs_b->runtime = 0;
2208
2209 /*
2210 * This check is repeated as we are holding onto the new bandwidth
2211 * while we unthrottle. This can potentially race with an unthrottled
2212 * group trying to acquire new bandwidth from the global pool.
2213 */
2214 while (throttled && runtime > 0) {
2215 raw_spin_unlock(&cfs_b->lock);
2216 /* we can't nest cfs_b->lock while distributing bandwidth */
2217 runtime = distribute_cfs_runtime(cfs_b, runtime,
2218 runtime_expires);
2219 raw_spin_lock(&cfs_b->lock);
2220
2221 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2222 }
2223
2224 /* return (any) remaining runtime */
2225 cfs_b->runtime = runtime;
2226 /*
2227 * While we are ensured activity in the period following an
2228 * unthrottle, this also covers the case in which the new bandwidth is
2229 * insufficient to cover the existing bandwidth deficit. (Forcing the
2230 * timer to remain active while there are any throttled entities.)
2231 */
2232 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002233out_unlock:
2234 if (idle)
2235 cfs_b->timer_active = 0;
2236 raw_spin_unlock(&cfs_b->lock);
2237
2238 return idle;
2239}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002240
Paul Turnerd8b49862011-07-21 09:43:41 -07002241/* a cfs_rq won't donate quota below this amount */
2242static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2243/* minimum remaining period time to redistribute slack quota */
2244static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2245/* how long we wait to gather additional slack before distributing */
2246static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2247
2248/* are we near the end of the current quota period? */
2249static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2250{
2251 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2252 u64 remaining;
2253
2254 /* if the call-back is running a quota refresh is already occurring */
2255 if (hrtimer_callback_running(refresh_timer))
2256 return 1;
2257
2258 /* is a quota refresh about to occur? */
2259 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2260 if (remaining < min_expire)
2261 return 1;
2262
2263 return 0;
2264}
2265
2266static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2267{
2268 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2269
2270 /* if there's a quota refresh soon don't bother with slack */
2271 if (runtime_refresh_within(cfs_b, min_left))
2272 return;
2273
2274 start_bandwidth_timer(&cfs_b->slack_timer,
2275 ns_to_ktime(cfs_bandwidth_slack_period));
2276}
2277
2278/* we know any runtime found here is valid as update_curr() precedes return */
2279static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2280{
2281 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2282 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2283
2284 if (slack_runtime <= 0)
2285 return;
2286
2287 raw_spin_lock(&cfs_b->lock);
2288 if (cfs_b->quota != RUNTIME_INF &&
2289 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2290 cfs_b->runtime += slack_runtime;
2291
2292 /* we are under rq->lock, defer unthrottling using a timer */
2293 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2294 !list_empty(&cfs_b->throttled_cfs_rq))
2295 start_cfs_slack_bandwidth(cfs_b);
2296 }
2297 raw_spin_unlock(&cfs_b->lock);
2298
2299 /* even if it's not valid for return we don't want to try again */
2300 cfs_rq->runtime_remaining -= slack_runtime;
2301}
2302
2303static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2304{
Paul Turner56f570e2011-11-07 20:26:33 -08002305 if (!cfs_bandwidth_used())
2306 return;
2307
Paul Turnerfccfdc62011-11-07 20:26:34 -08002308 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002309 return;
2310
2311 __return_cfs_rq_runtime(cfs_rq);
2312}
2313
2314/*
2315 * This is done with a timer (instead of inline with bandwidth return) since
2316 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2317 */
2318static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2319{
2320 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2321 u64 expires;
2322
2323 /* confirm we're still not at a refresh boundary */
2324 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2325 return;
2326
2327 raw_spin_lock(&cfs_b->lock);
2328 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2329 runtime = cfs_b->runtime;
2330 cfs_b->runtime = 0;
2331 }
2332 expires = cfs_b->runtime_expires;
2333 raw_spin_unlock(&cfs_b->lock);
2334
2335 if (!runtime)
2336 return;
2337
2338 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2339
2340 raw_spin_lock(&cfs_b->lock);
2341 if (expires == cfs_b->runtime_expires)
2342 cfs_b->runtime = runtime;
2343 raw_spin_unlock(&cfs_b->lock);
2344}
2345
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002346/*
2347 * When a group wakes up we want to make sure that its quota is not already
2348 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2349 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2350 */
2351static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2352{
Paul Turner56f570e2011-11-07 20:26:33 -08002353 if (!cfs_bandwidth_used())
2354 return;
2355
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002356 /* an active group must be handled by the update_curr()->put() path */
2357 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2358 return;
2359
2360 /* ensure the group is not already throttled */
2361 if (cfs_rq_throttled(cfs_rq))
2362 return;
2363
2364 /* update runtime allocation */
2365 account_cfs_rq_runtime(cfs_rq, 0);
2366 if (cfs_rq->runtime_remaining <= 0)
2367 throttle_cfs_rq(cfs_rq);
2368}
2369
2370/* conditionally throttle active cfs_rq's from put_prev_entity() */
2371static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2372{
Paul Turner56f570e2011-11-07 20:26:33 -08002373 if (!cfs_bandwidth_used())
2374 return;
2375
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002376 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2377 return;
2378
2379 /*
2380 * it's possible for a throttled entity to be forced into a running
2381 * state (e.g. set_curr_task), in this case we're finished.
2382 */
2383 if (cfs_rq_throttled(cfs_rq))
2384 return;
2385
2386 throttle_cfs_rq(cfs_rq);
2387}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002388
2389static inline u64 default_cfs_period(void);
2390static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2391static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2392
2393static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2394{
2395 struct cfs_bandwidth *cfs_b =
2396 container_of(timer, struct cfs_bandwidth, slack_timer);
2397 do_sched_cfs_slack_timer(cfs_b);
2398
2399 return HRTIMER_NORESTART;
2400}
2401
2402static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2403{
2404 struct cfs_bandwidth *cfs_b =
2405 container_of(timer, struct cfs_bandwidth, period_timer);
2406 ktime_t now;
2407 int overrun;
2408 int idle = 0;
2409
2410 for (;;) {
2411 now = hrtimer_cb_get_time(timer);
2412 overrun = hrtimer_forward(timer, now, cfs_b->period);
2413
2414 if (!overrun)
2415 break;
2416
2417 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2418 }
2419
2420 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2421}
2422
2423void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2424{
2425 raw_spin_lock_init(&cfs_b->lock);
2426 cfs_b->runtime = 0;
2427 cfs_b->quota = RUNTIME_INF;
2428 cfs_b->period = ns_to_ktime(default_cfs_period());
2429
2430 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2431 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2432 cfs_b->period_timer.function = sched_cfs_period_timer;
2433 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2434 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2435}
2436
2437static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2438{
2439 cfs_rq->runtime_enabled = 0;
2440 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2441}
2442
2443/* requires cfs_b->lock, may release to reprogram timer */
2444void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2445{
2446 /*
2447 * The timer may be active because we're trying to set a new bandwidth
2448 * period or because we're racing with the tear-down path
2449 * (timer_active==0 becomes visible before the hrtimer call-back
2450 * terminates). In either case we ensure that it's re-programmed
2451 */
2452 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2453 raw_spin_unlock(&cfs_b->lock);
2454 /* ensure cfs_b->lock is available while we wait */
2455 hrtimer_cancel(&cfs_b->period_timer);
2456
2457 raw_spin_lock(&cfs_b->lock);
2458 /* if someone else restarted the timer then we're done */
2459 if (cfs_b->timer_active)
2460 return;
2461 }
2462
2463 cfs_b->timer_active = 1;
2464 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2465}
2466
2467static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2468{
2469 hrtimer_cancel(&cfs_b->period_timer);
2470 hrtimer_cancel(&cfs_b->slack_timer);
2471}
2472
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002473static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002474{
2475 struct cfs_rq *cfs_rq;
2476
2477 for_each_leaf_cfs_rq(rq, cfs_rq) {
2478 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2479
2480 if (!cfs_rq->runtime_enabled)
2481 continue;
2482
2483 /*
2484 * clock_task is not advancing so we just need to make sure
2485 * there's some valid quota amount
2486 */
2487 cfs_rq->runtime_remaining = cfs_b->quota;
2488 if (cfs_rq_throttled(cfs_rq))
2489 unthrottle_cfs_rq(cfs_rq);
2490 }
2491}
2492
2493#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02002494static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
2495{
2496 return rq_of(cfs_rq)->clock_task;
2497}
2498
2499static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
2500 unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002501static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2502static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002503static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002504
2505static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2506{
2507 return 0;
2508}
Paul Turner64660c82011-07-21 09:43:36 -07002509
2510static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2511{
2512 return 0;
2513}
2514
2515static inline int throttled_lb_pair(struct task_group *tg,
2516 int src_cpu, int dest_cpu)
2517{
2518 return 0;
2519}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002520
2521void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2522
2523#ifdef CONFIG_FAIR_GROUP_SCHED
2524static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002525#endif
2526
Peter Zijlstra029632f2011-10-25 10:00:11 +02002527static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2528{
2529 return NULL;
2530}
2531static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002532static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002533
2534#endif /* CONFIG_CFS_BANDWIDTH */
2535
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002536/**************************************************
2537 * CFS operations on tasks:
2538 */
2539
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002540#ifdef CONFIG_SCHED_HRTICK
2541static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2542{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002543 struct sched_entity *se = &p->se;
2544 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2545
2546 WARN_ON(task_rq(p) != rq);
2547
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002548 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002549 u64 slice = sched_slice(cfs_rq, se);
2550 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2551 s64 delta = slice - ran;
2552
2553 if (delta < 0) {
2554 if (rq->curr == p)
2555 resched_task(p);
2556 return;
2557 }
2558
2559 /*
2560 * Don't schedule slices shorter than 10000ns, that just
2561 * doesn't make sense. Rely on vruntime for fairness.
2562 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002563 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002564 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002565
Peter Zijlstra31656512008-07-18 18:01:23 +02002566 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002567 }
2568}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002569
2570/*
2571 * called from enqueue/dequeue and updates the hrtick when the
2572 * current task is from our class and nr_running is low enough
2573 * to matter.
2574 */
2575static void hrtick_update(struct rq *rq)
2576{
2577 struct task_struct *curr = rq->curr;
2578
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002579 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002580 return;
2581
2582 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2583 hrtick_start_fair(rq, curr);
2584}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302585#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002586static inline void
2587hrtick_start_fair(struct rq *rq, struct task_struct *p)
2588{
2589}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002590
2591static inline void hrtick_update(struct rq *rq)
2592{
2593}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002594#endif
2595
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002596/*
2597 * The enqueue_task method is called before nr_running is
2598 * increased. Here we update the fair scheduling stats and
2599 * then put the task into the rbtree:
2600 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002601static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002602enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002603{
2604 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002605 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002606
2607 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002608 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002609 break;
2610 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002611 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002612
2613 /*
2614 * end evaluation on encountering a throttled cfs_rq
2615 *
2616 * note: in the case of encountering a throttled cfs_rq we will
2617 * post the final h_nr_running increment below.
2618 */
2619 if (cfs_rq_throttled(cfs_rq))
2620 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002621 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002622
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002623 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002624 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002625
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002626 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002627 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002628 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002629
Paul Turner85dac902011-07-21 09:43:33 -07002630 if (cfs_rq_throttled(cfs_rq))
2631 break;
2632
Paul Turnerd6b55912010-11-15 15:47:09 -08002633 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002634 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002635 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002636 }
2637
Ben Segall18bf2802012-10-04 12:51:20 +02002638 if (!se) {
2639 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002640 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002641 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002642 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002643}
2644
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002645static void set_next_buddy(struct sched_entity *se);
2646
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002647/*
2648 * The dequeue_task method is called before nr_running is
2649 * decreased. We remove the task from the rbtree and
2650 * update the fair scheduling stats:
2651 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002652static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002653{
2654 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002655 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002656 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002657
2658 for_each_sched_entity(se) {
2659 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002660 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002661
2662 /*
2663 * end evaluation on encountering a throttled cfs_rq
2664 *
2665 * note: in the case of encountering a throttled cfs_rq we will
2666 * post the final h_nr_running decrement below.
2667 */
2668 if (cfs_rq_throttled(cfs_rq))
2669 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002670 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002671
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002672 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002673 if (cfs_rq->load.weight) {
2674 /*
2675 * Bias pick_next to pick a task from this cfs_rq, as
2676 * p is sleeping when it is within its sched_slice.
2677 */
2678 if (task_sleep && parent_entity(se))
2679 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002680
2681 /* avoid re-evaluating load for this entity */
2682 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002683 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002684 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002685 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002686 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002687
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002688 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002689 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002690 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002691
Paul Turner85dac902011-07-21 09:43:33 -07002692 if (cfs_rq_throttled(cfs_rq))
2693 break;
2694
Paul Turnerd6b55912010-11-15 15:47:09 -08002695 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002696 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002697 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002698 }
2699
Ben Segall18bf2802012-10-04 12:51:20 +02002700 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002701 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002702 update_rq_runnable_avg(rq, 1);
2703 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002704 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002705}
2706
Gregory Haskinse7693a32008-01-25 21:08:09 +01002707#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002708/* Used instead of source_load when we know the type == 0 */
2709static unsigned long weighted_cpuload(const int cpu)
2710{
2711 return cpu_rq(cpu)->load.weight;
2712}
2713
2714/*
2715 * Return a low guess at the load of a migration-source cpu weighted
2716 * according to the scheduling class and "nice" value.
2717 *
2718 * We want to under-estimate the load of migration sources, to
2719 * balance conservatively.
2720 */
2721static unsigned long source_load(int cpu, int type)
2722{
2723 struct rq *rq = cpu_rq(cpu);
2724 unsigned long total = weighted_cpuload(cpu);
2725
2726 if (type == 0 || !sched_feat(LB_BIAS))
2727 return total;
2728
2729 return min(rq->cpu_load[type-1], total);
2730}
2731
2732/*
2733 * Return a high guess at the load of a migration-target cpu weighted
2734 * according to the scheduling class and "nice" value.
2735 */
2736static unsigned long target_load(int cpu, int type)
2737{
2738 struct rq *rq = cpu_rq(cpu);
2739 unsigned long total = weighted_cpuload(cpu);
2740
2741 if (type == 0 || !sched_feat(LB_BIAS))
2742 return total;
2743
2744 return max(rq->cpu_load[type-1], total);
2745}
2746
2747static unsigned long power_of(int cpu)
2748{
2749 return cpu_rq(cpu)->cpu_power;
2750}
2751
2752static unsigned long cpu_avg_load_per_task(int cpu)
2753{
2754 struct rq *rq = cpu_rq(cpu);
2755 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2756
2757 if (nr_running)
2758 return rq->load.weight / nr_running;
2759
2760 return 0;
2761}
2762
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002763
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002764static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002765{
2766 struct sched_entity *se = &p->se;
2767 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002768 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002769
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002770#ifndef CONFIG_64BIT
2771 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002772
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002773 do {
2774 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2775 smp_rmb();
2776 min_vruntime = cfs_rq->min_vruntime;
2777 } while (min_vruntime != min_vruntime_copy);
2778#else
2779 min_vruntime = cfs_rq->min_vruntime;
2780#endif
2781
2782 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002783}
2784
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002785#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002786/*
2787 * effective_load() calculates the load change as seen from the root_task_group
2788 *
2789 * Adding load to a group doesn't make a group heavier, but can cause movement
2790 * of group shares between cpus. Assuming the shares were perfectly aligned one
2791 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002792 *
2793 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2794 * on this @cpu and results in a total addition (subtraction) of @wg to the
2795 * total group weight.
2796 *
2797 * Given a runqueue weight distribution (rw_i) we can compute a shares
2798 * distribution (s_i) using:
2799 *
2800 * s_i = rw_i / \Sum rw_j (1)
2801 *
2802 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2803 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2804 * shares distribution (s_i):
2805 *
2806 * rw_i = { 2, 4, 1, 0 }
2807 * s_i = { 2/7, 4/7, 1/7, 0 }
2808 *
2809 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2810 * task used to run on and the CPU the waker is running on), we need to
2811 * compute the effect of waking a task on either CPU and, in case of a sync
2812 * wakeup, compute the effect of the current task going to sleep.
2813 *
2814 * So for a change of @wl to the local @cpu with an overall group weight change
2815 * of @wl we can compute the new shares distribution (s'_i) using:
2816 *
2817 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2818 *
2819 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2820 * differences in waking a task to CPU 0. The additional task changes the
2821 * weight and shares distributions like:
2822 *
2823 * rw'_i = { 3, 4, 1, 0 }
2824 * s'_i = { 3/8, 4/8, 1/8, 0 }
2825 *
2826 * We can then compute the difference in effective weight by using:
2827 *
2828 * dw_i = S * (s'_i - s_i) (3)
2829 *
2830 * Where 'S' is the group weight as seen by its parent.
2831 *
2832 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2833 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2834 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002835 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002836static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002837{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002838 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002839
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002840 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002841 return wl;
2842
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002843 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002844 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002845
Paul Turner977dda72011-01-14 17:57:50 -08002846 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002847
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002848 /*
2849 * W = @wg + \Sum rw_j
2850 */
2851 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002852
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002853 /*
2854 * w = rw_i + @wl
2855 */
2856 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002857
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002858 /*
2859 * wl = S * s'_i; see (2)
2860 */
2861 if (W > 0 && w < W)
2862 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002863 else
2864 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002865
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002866 /*
2867 * Per the above, wl is the new se->load.weight value; since
2868 * those are clipped to [MIN_SHARES, ...) do so now. See
2869 * calc_cfs_shares().
2870 */
Paul Turner977dda72011-01-14 17:57:50 -08002871 if (wl < MIN_SHARES)
2872 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002873
2874 /*
2875 * wl = dw_i = S * (s'_i - s_i); see (3)
2876 */
Paul Turner977dda72011-01-14 17:57:50 -08002877 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002878
2879 /*
2880 * Recursively apply this logic to all parent groups to compute
2881 * the final effective load change on the root group. Since
2882 * only the @tg group gets extra weight, all parent groups can
2883 * only redistribute existing shares. @wl is the shift in shares
2884 * resulting from this level per the above.
2885 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002886 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002887 }
2888
2889 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002890}
2891#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002892
Peter Zijlstra83378262008-06-27 13:41:37 +02002893static inline unsigned long effective_load(struct task_group *tg, int cpu,
2894 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002895{
Peter Zijlstra83378262008-06-27 13:41:37 +02002896 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002897}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002898
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002899#endif
2900
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002901static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002902{
Paul Turnere37b6a72011-01-21 20:44:59 -08002903 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002904 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002905 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002906 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002907 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002908 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002909
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002910 idx = sd->wake_idx;
2911 this_cpu = smp_processor_id();
2912 prev_cpu = task_cpu(p);
2913 load = source_load(prev_cpu, idx);
2914 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002915
2916 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002917 * If sync wakeup then subtract the (maximum possible)
2918 * effect of the currently running task from the load
2919 * of the current CPU:
2920 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002921 if (sync) {
2922 tg = task_group(current);
2923 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002924
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002925 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002926 load += effective_load(tg, prev_cpu, 0, -weight);
2927 }
2928
2929 tg = task_group(p);
2930 weight = p->se.load.weight;
2931
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002932 /*
2933 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002934 * due to the sync cause above having dropped this_load to 0, we'll
2935 * always have an imbalance, but there's really nothing you can do
2936 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002937 *
2938 * Otherwise check if either cpus are near enough in load to allow this
2939 * task to be woken on this_cpu.
2940 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002941 if (this_load > 0) {
2942 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002943
2944 this_eff_load = 100;
2945 this_eff_load *= power_of(prev_cpu);
2946 this_eff_load *= this_load +
2947 effective_load(tg, this_cpu, weight, weight);
2948
2949 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2950 prev_eff_load *= power_of(this_cpu);
2951 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2952
2953 balanced = this_eff_load <= prev_eff_load;
2954 } else
2955 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002956
2957 /*
2958 * If the currently running task will sleep within
2959 * a reasonable amount of time then attract this newly
2960 * woken task:
2961 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002962 if (sync && balanced)
2963 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002964
Lucas De Marchi41acab82010-03-10 23:37:45 -03002965 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002966 tl_per_task = cpu_avg_load_per_task(this_cpu);
2967
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002968 if (balanced ||
2969 (this_load <= load &&
2970 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002971 /*
2972 * This domain has SD_WAKE_AFFINE and
2973 * p is cache cold in this domain, and
2974 * there is no bad imbalance.
2975 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002976 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002977 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002978
2979 return 1;
2980 }
2981 return 0;
2982}
2983
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002984/*
2985 * find_idlest_group finds and returns the least busy CPU group within the
2986 * domain.
2987 */
2988static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002989find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002990 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002991{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002992 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002993 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002994 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002995
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002996 do {
2997 unsigned long load, avg_load;
2998 int local_group;
2999 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003000
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003001 /* Skip over this group if it has no CPUs allowed */
3002 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003003 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003004 continue;
3005
3006 local_group = cpumask_test_cpu(this_cpu,
3007 sched_group_cpus(group));
3008
3009 /* Tally up the load of all CPUs in the group */
3010 avg_load = 0;
3011
3012 for_each_cpu(i, sched_group_cpus(group)) {
3013 /* Bias balancing toward cpus of our domain */
3014 if (local_group)
3015 load = source_load(i, load_idx);
3016 else
3017 load = target_load(i, load_idx);
3018
3019 avg_load += load;
3020 }
3021
3022 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003023 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003024
3025 if (local_group) {
3026 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003027 } else if (avg_load < min_load) {
3028 min_load = avg_load;
3029 idlest = group;
3030 }
3031 } while (group = group->next, group != sd->groups);
3032
3033 if (!idlest || 100*this_load < imbalance*min_load)
3034 return NULL;
3035 return idlest;
3036}
3037
3038/*
3039 * find_idlest_cpu - find the idlest cpu among the cpus in group.
3040 */
3041static int
3042find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
3043{
3044 unsigned long load, min_load = ULONG_MAX;
3045 int idlest = -1;
3046 int i;
3047
3048 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003049 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003050 load = weighted_cpuload(i);
3051
3052 if (load < min_load || (load == min_load && i == this_cpu)) {
3053 min_load = load;
3054 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003055 }
3056 }
3057
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003058 return idlest;
3059}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003060
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003061/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003062 * Try and locate an idle CPU in the sched_domain.
3063 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003064static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003065{
3066 int cpu = smp_processor_id();
3067 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003068 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07003069 struct sched_group *sg;
3070 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003071
3072 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003073 * If the task is going to be woken-up on this cpu and if it is
3074 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003075 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003076 if (target == cpu && idle_cpu(cpu))
3077 return cpu;
3078
3079 /*
3080 * If the task is going to be woken-up on the cpu where it previously
3081 * ran and if it is currently idle, then it the right target.
3082 */
3083 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003084 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003085
3086 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07003087 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003088 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01003089 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08003090 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07003091 sg = sd->groups;
3092 do {
3093 if (!cpumask_intersects(sched_group_cpus(sg),
3094 tsk_cpus_allowed(p)))
3095 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02003096
Linus Torvalds37407ea2012-09-16 12:29:43 -07003097 for_each_cpu(i, sched_group_cpus(sg)) {
3098 if (!idle_cpu(i))
3099 goto next;
3100 }
3101
3102 target = cpumask_first_and(sched_group_cpus(sg),
3103 tsk_cpus_allowed(p));
3104 goto done;
3105next:
3106 sg = sg->next;
3107 } while (sg != sd->groups);
3108 }
3109done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003110 return target;
3111}
3112
3113/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003114 * sched_balance_self: balance the current task (running on cpu) in domains
3115 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3116 * SD_BALANCE_EXEC.
3117 *
3118 * Balance, ie. select the least loaded group.
3119 *
3120 * Returns the target CPU number, or the same CPU if no balancing is needed.
3121 *
3122 * preempt must be disabled.
3123 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003124static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003125select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003126{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003127 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003128 int cpu = smp_processor_id();
3129 int prev_cpu = task_cpu(p);
3130 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003131 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003132 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003133
Peter Zijlstra29baa742012-04-23 12:11:21 +02003134 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003135 return prev_cpu;
3136
Peter Zijlstra0763a662009-09-14 19:37:39 +02003137 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003138 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003139 want_affine = 1;
3140 new_cpu = prev_cpu;
3141 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003142
Peter Zijlstradce840a2011-04-07 14:09:50 +02003143 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003144 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01003145 if (!(tmp->flags & SD_LOAD_BALANCE))
3146 continue;
3147
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003148 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003149 * If both cpu and prev_cpu are part of this domain,
3150 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003151 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003152 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3153 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3154 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003155 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003156 }
3157
Alex Shif03542a2012-07-26 08:55:34 +08003158 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003159 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003160 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003161
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003162 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003163 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003164 prev_cpu = cpu;
3165
3166 new_cpu = select_idle_sibling(p, prev_cpu);
3167 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003168 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003169
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003170 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003171 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003172 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003173 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003174
Peter Zijlstra0763a662009-09-14 19:37:39 +02003175 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003176 sd = sd->child;
3177 continue;
3178 }
3179
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003180 if (sd_flag & SD_BALANCE_WAKE)
3181 load_idx = sd->wake_idx;
3182
3183 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003184 if (!group) {
3185 sd = sd->child;
3186 continue;
3187 }
3188
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003189 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003190 if (new_cpu == -1 || new_cpu == cpu) {
3191 /* Now try balancing at a lower domain level of cpu */
3192 sd = sd->child;
3193 continue;
3194 }
3195
3196 /* Now try balancing at a lower domain level of new_cpu */
3197 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003198 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003199 sd = NULL;
3200 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003201 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003202 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003203 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003204 sd = tmp;
3205 }
3206 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003207 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003208unlock:
3209 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003210
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003211 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003212}
Paul Turner0a74bef2012-10-04 13:18:30 +02003213
3214/*
3215 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3216 * cfs_rq_of(p) references at time of call are still valid and identify the
3217 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3218 * other assumptions, including the state of rq->lock, should be made.
3219 */
3220static void
3221migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3222{
Paul Turneraff3e492012-10-04 13:18:30 +02003223 struct sched_entity *se = &p->se;
3224 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3225
3226 /*
3227 * Load tracking: accumulate removed load so that it can be processed
3228 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3229 * to blocked load iff they have a positive decay-count. It can never
3230 * be negative here since on-rq tasks have decay-count == 0.
3231 */
3232 if (se->avg.decay_count) {
3233 se->avg.decay_count = -__synchronize_entity_decay(se);
3234 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3235 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003236}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003237#endif /* CONFIG_SMP */
3238
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003239static unsigned long
3240wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003241{
3242 unsigned long gran = sysctl_sched_wakeup_granularity;
3243
3244 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003245 * Since its curr running now, convert the gran from real-time
3246 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003247 *
3248 * By using 'se' instead of 'curr' we penalize light tasks, so
3249 * they get preempted easier. That is, if 'se' < 'curr' then
3250 * the resulting gran will be larger, therefore penalizing the
3251 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3252 * be smaller, again penalizing the lighter task.
3253 *
3254 * This is especially important for buddies when the leftmost
3255 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003256 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003257 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003258}
3259
3260/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003261 * Should 'se' preempt 'curr'.
3262 *
3263 * |s1
3264 * |s2
3265 * |s3
3266 * g
3267 * |<--->|c
3268 *
3269 * w(c, s1) = -1
3270 * w(c, s2) = 0
3271 * w(c, s3) = 1
3272 *
3273 */
3274static int
3275wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3276{
3277 s64 gran, vdiff = curr->vruntime - se->vruntime;
3278
3279 if (vdiff <= 0)
3280 return -1;
3281
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003282 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003283 if (vdiff > gran)
3284 return 1;
3285
3286 return 0;
3287}
3288
Peter Zijlstra02479092008-11-04 21:25:10 +01003289static void set_last_buddy(struct sched_entity *se)
3290{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003291 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3292 return;
3293
3294 for_each_sched_entity(se)
3295 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003296}
3297
3298static void set_next_buddy(struct sched_entity *se)
3299{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003300 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3301 return;
3302
3303 for_each_sched_entity(se)
3304 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003305}
3306
Rik van Rielac53db52011-02-01 09:51:03 -05003307static void set_skip_buddy(struct sched_entity *se)
3308{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003309 for_each_sched_entity(se)
3310 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003311}
3312
Peter Zijlstra464b7522008-10-24 11:06:15 +02003313/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003314 * Preempt the current task with a newly woken task if needed:
3315 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003316static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003317{
3318 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003319 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003320 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003321 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003322 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003323
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003324 if (unlikely(se == pse))
3325 return;
3326
Paul Turner5238cdd2011-07-21 09:43:37 -07003327 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003328 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003329 * unconditionally check_prempt_curr() after an enqueue (which may have
3330 * lead to a throttle). This both saves work and prevents false
3331 * next-buddy nomination below.
3332 */
3333 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3334 return;
3335
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003336 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003337 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003338 next_buddy_marked = 1;
3339 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003340
Bharata B Raoaec0a512008-08-28 14:42:49 +05303341 /*
3342 * We can come here with TIF_NEED_RESCHED already set from new task
3343 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003344 *
3345 * Note: this also catches the edge-case of curr being in a throttled
3346 * group (e.g. via set_curr_task), since update_curr() (in the
3347 * enqueue of curr) will have resulted in resched being set. This
3348 * prevents us from potentially nominating it as a false LAST_BUDDY
3349 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303350 */
3351 if (test_tsk_need_resched(curr))
3352 return;
3353
Darren Harta2f5c9a2011-02-22 13:04:33 -08003354 /* Idle tasks are by definition preempted by non-idle tasks. */
3355 if (unlikely(curr->policy == SCHED_IDLE) &&
3356 likely(p->policy != SCHED_IDLE))
3357 goto preempt;
3358
Ingo Molnar91c234b2007-10-15 17:00:18 +02003359 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003360 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3361 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003362 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003363 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003364 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003365
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003366 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003367 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003368 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003369 if (wakeup_preempt_entity(se, pse) == 1) {
3370 /*
3371 * Bias pick_next to pick the sched entity that is
3372 * triggering this preemption.
3373 */
3374 if (!next_buddy_marked)
3375 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003376 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003377 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003378
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003379 return;
3380
3381preempt:
3382 resched_task(curr);
3383 /*
3384 * Only set the backward buddy when the current task is still
3385 * on the rq. This can happen when a wakeup gets interleaved
3386 * with schedule on the ->pre_schedule() or idle_balance()
3387 * point, either of which can * drop the rq lock.
3388 *
3389 * Also, during early boot the idle thread is in the fair class,
3390 * for obvious reasons its a bad idea to schedule back to it.
3391 */
3392 if (unlikely(!se->on_rq || curr == rq->idle))
3393 return;
3394
3395 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3396 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003397}
3398
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003399static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003400{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003401 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003402 struct cfs_rq *cfs_rq = &rq->cfs;
3403 struct sched_entity *se;
3404
Tim Blechmann36ace272009-11-24 11:55:45 +01003405 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003406 return NULL;
3407
3408 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003409 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003410 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003411 cfs_rq = group_cfs_rq(se);
3412 } while (cfs_rq);
3413
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003414 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003415 if (hrtick_enabled(rq))
3416 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003417
3418 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003419}
3420
3421/*
3422 * Account for a descheduled task:
3423 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003424static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003425{
3426 struct sched_entity *se = &prev->se;
3427 struct cfs_rq *cfs_rq;
3428
3429 for_each_sched_entity(se) {
3430 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003431 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003432 }
3433}
3434
Rik van Rielac53db52011-02-01 09:51:03 -05003435/*
3436 * sched_yield() is very simple
3437 *
3438 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3439 */
3440static void yield_task_fair(struct rq *rq)
3441{
3442 struct task_struct *curr = rq->curr;
3443 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3444 struct sched_entity *se = &curr->se;
3445
3446 /*
3447 * Are we the only task in the tree?
3448 */
3449 if (unlikely(rq->nr_running == 1))
3450 return;
3451
3452 clear_buddies(cfs_rq, se);
3453
3454 if (curr->policy != SCHED_BATCH) {
3455 update_rq_clock(rq);
3456 /*
3457 * Update run-time statistics of the 'current'.
3458 */
3459 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003460 /*
3461 * Tell update_rq_clock() that we've just updated,
3462 * so we don't do microscopic update in schedule()
3463 * and double the fastpath cost.
3464 */
3465 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003466 }
3467
3468 set_skip_buddy(se);
3469}
3470
Mike Galbraithd95f4122011-02-01 09:50:51 -05003471static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3472{
3473 struct sched_entity *se = &p->se;
3474
Paul Turner5238cdd2011-07-21 09:43:37 -07003475 /* throttled hierarchies are not runnable */
3476 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003477 return false;
3478
3479 /* Tell the scheduler that we'd really like pse to run next. */
3480 set_next_buddy(se);
3481
Mike Galbraithd95f4122011-02-01 09:50:51 -05003482 yield_task_fair(rq);
3483
3484 return true;
3485}
3486
Peter Williams681f3e62007-10-24 18:23:51 +02003487#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003488/**************************************************
3489 * Fair scheduling class load-balancing methods:
3490 */
3491
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003492static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3493
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003494#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003495#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303496#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003497
3498struct lb_env {
3499 struct sched_domain *sd;
3500
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003501 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303502 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003503
3504 int dst_cpu;
3505 struct rq *dst_rq;
3506
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303507 struct cpumask *dst_grpmask;
3508 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003509 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003510 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08003511 /* The set of CPUs under consideration for load-balancing */
3512 struct cpumask *cpus;
3513
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003514 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003515
3516 unsigned int loop;
3517 unsigned int loop_break;
3518 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003519};
3520
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003521/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003522 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003523 * Both runqueues must be locked.
3524 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003525static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003526{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003527 deactivate_task(env->src_rq, p, 0);
3528 set_task_cpu(p, env->dst_cpu);
3529 activate_task(env->dst_rq, p, 0);
3530 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003531}
3532
3533/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003534 * Is this task likely cache-hot:
3535 */
3536static int
3537task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3538{
3539 s64 delta;
3540
3541 if (p->sched_class != &fair_sched_class)
3542 return 0;
3543
3544 if (unlikely(p->policy == SCHED_IDLE))
3545 return 0;
3546
3547 /*
3548 * Buddy candidates are cache hot:
3549 */
3550 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3551 (&p->se == cfs_rq_of(&p->se)->next ||
3552 &p->se == cfs_rq_of(&p->se)->last))
3553 return 1;
3554
3555 if (sysctl_sched_migration_cost == -1)
3556 return 1;
3557 if (sysctl_sched_migration_cost == 0)
3558 return 0;
3559
3560 delta = now - p->se.exec_start;
3561
3562 return delta < (s64)sysctl_sched_migration_cost;
3563}
3564
3565/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003566 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3567 */
3568static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003569int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003570{
3571 int tsk_cache_hot = 0;
3572 /*
3573 * We do not migrate tasks that are:
3574 * 1) running (obviously), or
3575 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3576 * 3) are cache-hot on their current CPU.
3577 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003578 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303579 int new_dst_cpu;
3580
Lucas De Marchi41acab82010-03-10 23:37:45 -03003581 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303582
3583 /*
3584 * Remember if this task can be migrated to any other cpu in
3585 * our sched_group. We may want to revisit it if we couldn't
3586 * meet load balance goals by pulling other tasks on src_cpu.
3587 *
3588 * Also avoid computing new_dst_cpu if we have already computed
3589 * one in current iteration.
3590 */
3591 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3592 return 0;
3593
3594 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3595 tsk_cpus_allowed(p));
3596 if (new_dst_cpu < nr_cpu_ids) {
3597 env->flags |= LBF_SOME_PINNED;
3598 env->new_dst_cpu = new_dst_cpu;
3599 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003600 return 0;
3601 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303602
3603 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003604 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003605
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003606 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003607 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003608 return 0;
3609 }
3610
3611 /*
3612 * Aggressive migration if:
3613 * 1) task is cache cold, or
3614 * 2) too many balance attempts have failed.
3615 */
3616
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003617 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003618 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003619 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003620#ifdef CONFIG_SCHEDSTATS
3621 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003622 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003623 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003624 }
3625#endif
3626 return 1;
3627 }
3628
3629 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003630 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003631 return 0;
3632 }
3633 return 1;
3634}
3635
Peter Zijlstra897c3952009-12-17 17:45:42 +01003636/*
3637 * move_one_task tries to move exactly one task from busiest to this_rq, as
3638 * part of active balancing operations within "domain".
3639 * Returns 1 if successful and 0 otherwise.
3640 *
3641 * Called with both runqueues locked.
3642 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003643static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003644{
3645 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003646
Peter Zijlstra367456c2012-02-20 21:49:09 +01003647 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3648 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3649 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003650
Peter Zijlstra367456c2012-02-20 21:49:09 +01003651 if (!can_migrate_task(p, env))
3652 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003653
Peter Zijlstra367456c2012-02-20 21:49:09 +01003654 move_task(p, env);
3655 /*
3656 * Right now, this is only the second place move_task()
3657 * is called, so we can safely collect move_task()
3658 * stats here rather than inside move_task().
3659 */
3660 schedstat_inc(env->sd, lb_gained[env->idle]);
3661 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003662 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003663 return 0;
3664}
3665
Peter Zijlstra367456c2012-02-20 21:49:09 +01003666static unsigned long task_h_load(struct task_struct *p);
3667
Peter Zijlstraeb953082012-04-17 13:38:40 +02003668static const unsigned int sched_nr_migrate_break = 32;
3669
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003670/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003671 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003672 * this_rq, as part of a balancing operation within domain "sd".
3673 * Returns 1 if successful and 0 otherwise.
3674 *
3675 * Called with both runqueues locked.
3676 */
3677static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003678{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003679 struct list_head *tasks = &env->src_rq->cfs_tasks;
3680 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003681 unsigned long load;
3682 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003683
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003684 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003685 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003686
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003687 while (!list_empty(tasks)) {
3688 p = list_first_entry(tasks, struct task_struct, se.group_node);
3689
Peter Zijlstra367456c2012-02-20 21:49:09 +01003690 env->loop++;
3691 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003692 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003693 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003694
3695 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003696 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003697 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003698 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003699 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003700 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003701
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003702 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003703 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003704
Peter Zijlstra367456c2012-02-20 21:49:09 +01003705 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003706
Peter Zijlstraeb953082012-04-17 13:38:40 +02003707 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003708 goto next;
3709
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003710 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003711 goto next;
3712
3713 if (!can_migrate_task(p, env))
3714 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003715
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003716 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003717 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003718 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003719
3720#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003721 /*
3722 * NEWIDLE balancing is a source of latency, so preemptible
3723 * kernels will stop after the first task is pulled to minimize
3724 * the critical section.
3725 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003726 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003727 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003728#endif
3729
Peter Zijlstraee00e662009-12-17 17:25:20 +01003730 /*
3731 * We only want to steal up to the prescribed amount of
3732 * weighted load.
3733 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003734 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003735 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003736
Peter Zijlstra367456c2012-02-20 21:49:09 +01003737 continue;
3738next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003739 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003740 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003741
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003742 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003743 * Right now, this is one of only two places move_task() is called,
3744 * so we can safely collect move_task() stats here rather than
3745 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003746 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003747 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003748
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003749 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003750}
3751
Peter Zijlstra230059de2009-12-17 17:47:12 +01003752#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003753/*
3754 * update tg->load_weight by folding this cpu's load_avg
3755 */
Paul Turner67e86252010-11-15 15:47:05 -08003756static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003757{
3758 struct cfs_rq *cfs_rq;
3759 unsigned long flags;
3760 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003761
3762 if (!tg->se[cpu])
3763 return 0;
3764
3765 rq = cpu_rq(cpu);
3766 cfs_rq = tg->cfs_rq[cpu];
3767
3768 raw_spin_lock_irqsave(&rq->lock, flags);
3769
3770 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003771 update_cfs_load(cfs_rq, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02003772 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003773
3774 /*
3775 * We need to update shares after updating tg->load_weight in
3776 * order to adjust the weight of groups with long running tasks.
3777 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003778 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003779
3780 raw_spin_unlock_irqrestore(&rq->lock, flags);
3781
3782 return 0;
3783}
3784
3785static void update_shares(int cpu)
3786{
3787 struct cfs_rq *cfs_rq;
3788 struct rq *rq = cpu_rq(cpu);
3789
3790 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003791 /*
3792 * Iterates the task_group tree in a bottom up fashion, see
3793 * list_add_leaf_cfs_rq() for details.
3794 */
Paul Turner64660c82011-07-21 09:43:36 -07003795 for_each_leaf_cfs_rq(rq, cfs_rq) {
3796 /* throttled entities do not contribute to load */
3797 if (throttled_hierarchy(cfs_rq))
3798 continue;
3799
Paul Turner67e86252010-11-15 15:47:05 -08003800 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003801 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003802 rcu_read_unlock();
3803}
3804
Peter Zijlstra9763b672011-07-13 13:09:25 +02003805/*
3806 * Compute the cpu's hierarchical load factor for each task group.
3807 * This needs to be done in a top-down fashion because the load of a child
3808 * group is a fraction of its parents load.
3809 */
3810static int tg_load_down(struct task_group *tg, void *data)
3811{
3812 unsigned long load;
3813 long cpu = (long)data;
3814
3815 if (!tg->parent) {
3816 load = cpu_rq(cpu)->load.weight;
3817 } else {
3818 load = tg->parent->cfs_rq[cpu]->h_load;
3819 load *= tg->se[cpu]->load.weight;
3820 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3821 }
3822
3823 tg->cfs_rq[cpu]->h_load = load;
3824
3825 return 0;
3826}
3827
3828static void update_h_load(long cpu)
3829{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003830 struct rq *rq = cpu_rq(cpu);
3831 unsigned long now = jiffies;
3832
3833 if (rq->h_load_throttle == now)
3834 return;
3835
3836 rq->h_load_throttle = now;
3837
Peter Zijlstra367456c2012-02-20 21:49:09 +01003838 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003839 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003840 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003841}
3842
Peter Zijlstra367456c2012-02-20 21:49:09 +01003843static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003844{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003845 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3846 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003847
Peter Zijlstra367456c2012-02-20 21:49:09 +01003848 load = p->se.load.weight;
3849 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003850
Peter Zijlstra367456c2012-02-20 21:49:09 +01003851 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003852}
3853#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003854static inline void update_shares(int cpu)
3855{
3856}
3857
Peter Zijlstra367456c2012-02-20 21:49:09 +01003858static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003859{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003860}
3861
3862static unsigned long task_h_load(struct task_struct *p)
3863{
3864 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003865}
3866#endif
3867
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003868/********** Helpers for find_busiest_group ************************/
3869/*
3870 * sd_lb_stats - Structure to store the statistics of a sched_domain
3871 * during load balancing.
3872 */
3873struct sd_lb_stats {
3874 struct sched_group *busiest; /* Busiest group in this sd */
3875 struct sched_group *this; /* Local group in this sd */
3876 unsigned long total_load; /* Total load of all groups in sd */
3877 unsigned long total_pwr; /* Total power of all groups in sd */
3878 unsigned long avg_load; /* Average load across all groups in sd */
3879
3880 /** Statistics of this group */
3881 unsigned long this_load;
3882 unsigned long this_load_per_task;
3883 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003884 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003885 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003886
3887 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003888 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003889 unsigned long max_load;
3890 unsigned long busiest_load_per_task;
3891 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003892 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003893 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003894 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003895
3896 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003897};
3898
3899/*
3900 * sg_lb_stats - stats of a sched_group required for load_balancing
3901 */
3902struct sg_lb_stats {
3903 unsigned long avg_load; /*Avg load across the CPUs of the group */
3904 unsigned long group_load; /* Total load over the CPUs of the group */
3905 unsigned long sum_nr_running; /* Nr tasks running in the group */
3906 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3907 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003908 unsigned long idle_cpus;
3909 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003910 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003911 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003912};
3913
3914/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003915 * get_sd_load_idx - Obtain the load index for a given sched domain.
3916 * @sd: The sched_domain whose load_idx is to be obtained.
3917 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3918 */
3919static inline int get_sd_load_idx(struct sched_domain *sd,
3920 enum cpu_idle_type idle)
3921{
3922 int load_idx;
3923
3924 switch (idle) {
3925 case CPU_NOT_IDLE:
3926 load_idx = sd->busy_idx;
3927 break;
3928
3929 case CPU_NEWLY_IDLE:
3930 load_idx = sd->newidle_idx;
3931 break;
3932 default:
3933 load_idx = sd->idle_idx;
3934 break;
3935 }
3936
3937 return load_idx;
3938}
3939
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3941{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003942 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943}
3944
3945unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3946{
3947 return default_scale_freq_power(sd, cpu);
3948}
3949
3950unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3951{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003952 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003953 unsigned long smt_gain = sd->smt_gain;
3954
3955 smt_gain /= weight;
3956
3957 return smt_gain;
3958}
3959
3960unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3961{
3962 return default_scale_smt_power(sd, cpu);
3963}
3964
3965unsigned long scale_rt_power(int cpu)
3966{
3967 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003968 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003969
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003970 /*
3971 * Since we're reading these variables without serialization make sure
3972 * we read them once before doing sanity checks on them.
3973 */
3974 age_stamp = ACCESS_ONCE(rq->age_stamp);
3975 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003976
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003977 total = sched_avg_period() + (rq->clock - age_stamp);
3978
3979 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003980 /* Ensures that power won't end up being negative */
3981 available = 0;
3982 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003983 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003984 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003985
Nikhil Rao1399fa72011-05-18 10:09:39 -07003986 if (unlikely((s64)total < SCHED_POWER_SCALE))
3987 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003988
Nikhil Rao1399fa72011-05-18 10:09:39 -07003989 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003990
3991 return div_u64(available, total);
3992}
3993
3994static void update_cpu_power(struct sched_domain *sd, int cpu)
3995{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003996 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003997 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003998 struct sched_group *sdg = sd->groups;
3999
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004000 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
4001 if (sched_feat(ARCH_POWER))
4002 power *= arch_scale_smt_power(sd, cpu);
4003 else
4004 power *= default_scale_smt_power(sd, cpu);
4005
Nikhil Rao1399fa72011-05-18 10:09:39 -07004006 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004007 }
4008
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004009 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004010
4011 if (sched_feat(ARCH_POWER))
4012 power *= arch_scale_freq_power(sd, cpu);
4013 else
4014 power *= default_scale_freq_power(sd, cpu);
4015
Nikhil Rao1399fa72011-05-18 10:09:39 -07004016 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004017
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004018 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004019 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004020
4021 if (!power)
4022 power = 1;
4023
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004024 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004025 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004026}
4027
Peter Zijlstra029632f2011-10-25 10:00:11 +02004028void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004029{
4030 struct sched_domain *child = sd->child;
4031 struct sched_group *group, *sdg = sd->groups;
4032 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01004033 unsigned long interval;
4034
4035 interval = msecs_to_jiffies(sd->balance_interval);
4036 interval = clamp(interval, 1UL, max_load_balance_interval);
4037 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004038
4039 if (!child) {
4040 update_cpu_power(sd, cpu);
4041 return;
4042 }
4043
4044 power = 0;
4045
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02004046 if (child->flags & SD_OVERLAP) {
4047 /*
4048 * SD_OVERLAP domains cannot assume that child groups
4049 * span the current group.
4050 */
4051
4052 for_each_cpu(cpu, sched_group_cpus(sdg))
4053 power += power_of(cpu);
4054 } else {
4055 /*
4056 * !SD_OVERLAP domains can assume that child groups
4057 * span the current group.
4058 */
4059
4060 group = child->groups;
4061 do {
4062 power += group->sgp->power;
4063 group = group->next;
4064 } while (group != child->groups);
4065 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004066
Peter Zijlstrac3decf02012-05-31 12:05:32 +02004067 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004068}
4069
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004070/*
4071 * Try and fix up capacity for tiny siblings, this is needed when
4072 * things like SD_ASYM_PACKING need f_b_g to select another sibling
4073 * which on its own isn't powerful enough.
4074 *
4075 * See update_sd_pick_busiest() and check_asym_packing().
4076 */
4077static inline int
4078fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
4079{
4080 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07004081 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004082 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02004083 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004084 return 0;
4085
4086 /*
4087 * If ~90% of the cpu_power is still there, we're good.
4088 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004089 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004090 return 1;
4091
4092 return 0;
4093}
4094
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004095/**
4096 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004097 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004099 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004100 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004101 * @balance: Should we balance.
4102 * @sgs: variable to hold the statistics for this group.
4103 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004104static inline void update_sg_lb_stats(struct lb_env *env,
4105 struct sched_group *group, int load_idx,
Michael Wangb94031302012-07-12 16:10:13 +08004106 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004108 unsigned long nr_running, max_nr_running, min_nr_running;
4109 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004110 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004111 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004112 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004113
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004114 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004115 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004116
4117 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004118 max_cpu_load = 0;
4119 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004120 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004121 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004122
Michael Wangb94031302012-07-12 16:10:13 +08004123 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004124 struct rq *rq = cpu_rq(i);
4125
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004126 nr_running = rq->nr_running;
4127
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004128 /* Bias balancing toward cpus of our domain */
4129 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004130 if (idle_cpu(i) && !first_idle_cpu &&
4131 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004132 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004133 balance_cpu = i;
4134 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004135
4136 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004137 } else {
4138 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004139 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004140 max_cpu_load = load;
4141 if (min_cpu_load > load)
4142 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004143
4144 if (nr_running > max_nr_running)
4145 max_nr_running = nr_running;
4146 if (min_nr_running > nr_running)
4147 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004148 }
4149
4150 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004151 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004152 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004153 if (idle_cpu(i))
4154 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004155 }
4156
4157 /*
4158 * First idle cpu or the first cpu(busiest) in this sched group
4159 * is eligible for doing load balancing at this and above
4160 * domains. In the newly idle case, we will allow all the cpu's
4161 * to do the newly idle load balance.
4162 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004163 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004164 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004165 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004166 *balance = 0;
4167 return;
4168 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004169 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004170 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004171 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004172 }
4173
4174 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004175 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004176
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004177 /*
4178 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004179 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004180 *
4181 * APZ: with cgroup the avg task weight can vary wildly and
4182 * might not be a suitable number - should we keep a
4183 * normalized nr_running number somewhere that negates
4184 * the hierarchy?
4185 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004186 if (sgs->sum_nr_running)
4187 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004188
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004189 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4190 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004191 sgs->group_imb = 1;
4192
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004193 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004194 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004195 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004196 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004197 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004198
4199 if (sgs->group_capacity > sgs->sum_nr_running)
4200 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004201}
4202
4203/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004204 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004205 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004206 * @sds: sched_domain statistics
4207 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004208 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004209 *
4210 * Determine if @sg is a busier group than the previously selected
4211 * busiest group.
4212 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004213static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004214 struct sd_lb_stats *sds,
4215 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004216 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004217{
4218 if (sgs->avg_load <= sds->max_load)
4219 return false;
4220
4221 if (sgs->sum_nr_running > sgs->group_capacity)
4222 return true;
4223
4224 if (sgs->group_imb)
4225 return true;
4226
4227 /*
4228 * ASYM_PACKING needs to move all the work to the lowest
4229 * numbered CPUs in the group, therefore mark all groups
4230 * higher than ourself as busy.
4231 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004232 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4233 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004234 if (!sds->busiest)
4235 return true;
4236
4237 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4238 return true;
4239 }
4240
4241 return false;
4242}
4243
4244/**
Hui Kang461819a2011-10-11 23:00:59 -04004245 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004246 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004247 * @balance: Should we balance.
4248 * @sds: variable to hold the statistics for this sched_domain.
4249 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004250static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004251 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004252{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004253 struct sched_domain *child = env->sd->child;
4254 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004255 struct sg_lb_stats sgs;
4256 int load_idx, prefer_sibling = 0;
4257
4258 if (child && child->flags & SD_PREFER_SIBLING)
4259 prefer_sibling = 1;
4260
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004261 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004262
4263 do {
4264 int local_group;
4265
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004266 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004267 memset(&sgs, 0, sizeof(sgs));
Michael Wangb94031302012-07-12 16:10:13 +08004268 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004269
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004270 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004271 return;
4272
4273 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004274 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004275
4276 /*
4277 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004278 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004279 * and move all the excess tasks away. We lower the capacity
4280 * of a group only if the local group has the capacity to fit
4281 * these excess tasks, i.e. nr_running < group_capacity. The
4282 * extra check prevents the case where you always pull from the
4283 * heaviest group when it is already under-utilized (possible
4284 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004285 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004286 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004287 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4288
4289 if (local_group) {
4290 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004291 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004292 sds->this_nr_running = sgs.sum_nr_running;
4293 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004294 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004295 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004296 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004297 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004298 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004299 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004300 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004301 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004302 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004303 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004304 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004305 sds->group_imb = sgs.group_imb;
4306 }
4307
Michael Neuling532cb4c2010-06-08 14:57:02 +10004308 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004309 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004310}
4311
Michael Neuling532cb4c2010-06-08 14:57:02 +10004312/**
4313 * check_asym_packing - Check to see if the group is packed into the
4314 * sched doman.
4315 *
4316 * This is primarily intended to used at the sibling level. Some
4317 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4318 * case of POWER7, it can move to lower SMT modes only when higher
4319 * threads are idle. When in lower SMT modes, the threads will
4320 * perform better since they share less core resources. Hence when we
4321 * have idle threads, we want them to be the higher ones.
4322 *
4323 * This packing function is run on idle threads. It checks to see if
4324 * the busiest CPU in this domain (core in the P7 case) has a higher
4325 * CPU number than the packing function is being run on. Here we are
4326 * assuming lower CPU number will be equivalent to lower a SMT thread
4327 * number.
4328 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004329 * Returns 1 when packing is required and a task should be moved to
4330 * this CPU. The amount of the imbalance is returned in *imbalance.
4331 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004332 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004333 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004334 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004335static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004336{
4337 int busiest_cpu;
4338
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004339 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004340 return 0;
4341
4342 if (!sds->busiest)
4343 return 0;
4344
4345 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004346 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004347 return 0;
4348
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004349 env->imbalance = DIV_ROUND_CLOSEST(
4350 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4351
Michael Neuling532cb4c2010-06-08 14:57:02 +10004352 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004353}
4354
4355/**
4356 * fix_small_imbalance - Calculate the minor imbalance that exists
4357 * amongst the groups of a sched_domain, during
4358 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004359 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004360 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004361 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004362static inline
4363void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004364{
4365 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4366 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004367 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004368
4369 if (sds->this_nr_running) {
4370 sds->this_load_per_task /= sds->this_nr_running;
4371 if (sds->busiest_load_per_task >
4372 sds->this_load_per_task)
4373 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004374 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004375 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004376 cpu_avg_load_per_task(env->dst_cpu);
4377 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004378
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004379 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004380 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004381 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004382
4383 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4384 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004385 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004386 return;
4387 }
4388
4389 /*
4390 * OK, we don't have enough imbalance to justify moving tasks,
4391 * however we may be able to increase total CPU power used by
4392 * moving them.
4393 */
4394
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004395 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004396 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004397 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004398 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004399 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004400
4401 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004402 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004403 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004404 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004405 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004406 min(sds->busiest_load_per_task, sds->max_load - tmp);
4407
4408 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004409 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004410 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004411 tmp = (sds->max_load * sds->busiest->sgp->power) /
4412 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004413 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004414 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004415 sds->this->sgp->power;
4416 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004417 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004418 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004419
4420 /* Move if we gain throughput */
4421 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004422 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004423}
4424
4425/**
4426 * calculate_imbalance - Calculate the amount of imbalance present within the
4427 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004428 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004429 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004430 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004431static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004432{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004433 unsigned long max_pull, load_above_capacity = ~0UL;
4434
4435 sds->busiest_load_per_task /= sds->busiest_nr_running;
4436 if (sds->group_imb) {
4437 sds->busiest_load_per_task =
4438 min(sds->busiest_load_per_task, sds->avg_load);
4439 }
4440
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004441 /*
4442 * In the presence of smp nice balancing, certain scenarios can have
4443 * max load less than avg load(as we skip the groups at or below
4444 * its cpu_power, while calculating max_load..)
4445 */
4446 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004447 env->imbalance = 0;
4448 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004449 }
4450
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004451 if (!sds->group_imb) {
4452 /*
4453 * Don't want to pull so many tasks that a group would go idle.
4454 */
4455 load_above_capacity = (sds->busiest_nr_running -
4456 sds->busiest_group_capacity);
4457
Nikhil Rao1399fa72011-05-18 10:09:39 -07004458 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004459
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004460 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004461 }
4462
4463 /*
4464 * We're trying to get all the cpus to the average_load, so we don't
4465 * want to push ourselves above the average load, nor do we wish to
4466 * reduce the max loaded cpu below the average load. At the same time,
4467 * we also don't want to reduce the group load below the group capacity
4468 * (so that we can implement power-savings policies etc). Thus we look
4469 * for the minimum possible imbalance.
4470 * Be careful of negative numbers as they'll appear as very large values
4471 * with unsigned longs.
4472 */
4473 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004474
4475 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004476 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004477 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004478 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004479
4480 /*
4481 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004482 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004483 * a think about bumping its value to force at least one task to be
4484 * moved
4485 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004486 if (env->imbalance < sds->busiest_load_per_task)
4487 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004488
4489}
Nikhil Raofab47622010-10-15 13:12:29 -07004490
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004491/******* find_busiest_group() helpers end here *********************/
4492
4493/**
4494 * find_busiest_group - Returns the busiest group within the sched_domain
4495 * if there is an imbalance. If there isn't an imbalance, and
4496 * the user has opted for power-savings, it returns a group whose
4497 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4498 * such a group exists.
4499 *
4500 * Also calculates the amount of weighted load which should be moved
4501 * to restore balance.
4502 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004503 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004504 * @balance: Pointer to a variable indicating if this_cpu
4505 * is the appropriate cpu to perform load balancing at this_level.
4506 *
4507 * Returns: - the busiest group if imbalance exists.
4508 * - If no imbalance and user has opted for power-savings balance,
4509 * return the least loaded group whose CPUs can be
4510 * put to idle by rebalancing its tasks onto our group.
4511 */
4512static struct sched_group *
Michael Wangb94031302012-07-12 16:10:13 +08004513find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004514{
4515 struct sd_lb_stats sds;
4516
4517 memset(&sds, 0, sizeof(sds));
4518
4519 /*
4520 * Compute the various statistics relavent for load balancing at
4521 * this level.
4522 */
Michael Wangb94031302012-07-12 16:10:13 +08004523 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004524
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004525 /*
4526 * this_cpu is not the appropriate cpu to perform load balancing at
4527 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004528 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004529 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004530 goto ret;
4531
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004532 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4533 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004534 return sds.busiest;
4535
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004536 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004537 if (!sds.busiest || sds.busiest_nr_running == 0)
4538 goto out_balanced;
4539
Nikhil Rao1399fa72011-05-18 10:09:39 -07004540 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004541
Peter Zijlstra866ab432011-02-21 18:56:47 +01004542 /*
4543 * If the busiest group is imbalanced the below checks don't
4544 * work because they assumes all things are equal, which typically
4545 * isn't true due to cpus_allowed constraints and the like.
4546 */
4547 if (sds.group_imb)
4548 goto force_balance;
4549
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004550 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004551 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004552 !sds.busiest_has_capacity)
4553 goto force_balance;
4554
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004555 /*
4556 * If the local group is more busy than the selected busiest group
4557 * don't try and pull any tasks.
4558 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004559 if (sds.this_load >= sds.max_load)
4560 goto out_balanced;
4561
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004562 /*
4563 * Don't pull any tasks if this group is already above the domain
4564 * average load.
4565 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004566 if (sds.this_load >= sds.avg_load)
4567 goto out_balanced;
4568
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004569 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004570 /*
4571 * This cpu is idle. If the busiest group load doesn't
4572 * have more tasks than the number of available cpu's and
4573 * there is no imbalance between this and busiest group
4574 * wrt to idle cpu's, it is balanced.
4575 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004576 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004577 sds.busiest_nr_running <= sds.busiest_group_weight)
4578 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004579 } else {
4580 /*
4581 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4582 * imbalance_pct to be conservative.
4583 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004584 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004585 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004586 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004587
Nikhil Raofab47622010-10-15 13:12:29 -07004588force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004589 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004590 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004591 return sds.busiest;
4592
4593out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004594ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004595 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004596 return NULL;
4597}
4598
4599/*
4600 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4601 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004602static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004603 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004604{
4605 struct rq *busiest = NULL, *rq;
4606 unsigned long max_load = 0;
4607 int i;
4608
4609 for_each_cpu(i, sched_group_cpus(group)) {
4610 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004611 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4612 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004613 unsigned long wl;
4614
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004615 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004616 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004617
Michael Wangb94031302012-07-12 16:10:13 +08004618 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004619 continue;
4620
4621 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004622 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004623
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004624 /*
4625 * When comparing with imbalance, use weighted_cpuload()
4626 * which is not scaled with the cpu power.
4627 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004628 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004629 continue;
4630
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004631 /*
4632 * For the load comparisons with the other cpu's, consider
4633 * the weighted_cpuload() scaled with the cpu power, so that
4634 * the load can be moved away from the cpu that is potentially
4635 * running at a lower capacity.
4636 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004637 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004638
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004639 if (wl > max_load) {
4640 max_load = wl;
4641 busiest = rq;
4642 }
4643 }
4644
4645 return busiest;
4646}
4647
4648/*
4649 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4650 * so long as it is large enough.
4651 */
4652#define MAX_PINNED_INTERVAL 512
4653
4654/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004655DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004656
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004657static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004658{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004659 struct sched_domain *sd = env->sd;
4660
4661 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004662
4663 /*
4664 * ASYM_PACKING needs to force migrate tasks from busy but
4665 * higher numbered CPUs in order to pack all tasks in the
4666 * lowest numbered CPUs.
4667 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004668 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004669 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004670 }
4671
4672 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4673}
4674
Tejun Heo969c7922010-05-06 18:49:21 +02004675static int active_load_balance_cpu_stop(void *data);
4676
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004677/*
4678 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4679 * tasks if there is an imbalance.
4680 */
4681static int load_balance(int this_cpu, struct rq *this_rq,
4682 struct sched_domain *sd, enum cpu_idle_type idle,
4683 int *balance)
4684{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304685 int ld_moved, cur_ld_moved, active_balance = 0;
4686 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004687 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004688 struct rq *busiest;
4689 unsigned long flags;
4690 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4691
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004692 struct lb_env env = {
4693 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004694 .dst_cpu = this_cpu,
4695 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304696 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004697 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004698 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08004699 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004700 };
4701
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004702 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304703 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004704
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004705 schedstat_inc(sd, lb_count[idle]);
4706
4707redo:
Michael Wangb94031302012-07-12 16:10:13 +08004708 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004709
4710 if (*balance == 0)
4711 goto out_balanced;
4712
4713 if (!group) {
4714 schedstat_inc(sd, lb_nobusyg[idle]);
4715 goto out_balanced;
4716 }
4717
Michael Wangb94031302012-07-12 16:10:13 +08004718 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004719 if (!busiest) {
4720 schedstat_inc(sd, lb_nobusyq[idle]);
4721 goto out_balanced;
4722 }
4723
Michael Wang78feefc2012-08-06 16:41:59 +08004724 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004725
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004726 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004727
4728 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304729 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004730 if (busiest->nr_running > 1) {
4731 /*
4732 * Attempt to move tasks. If find_busiest_group has found
4733 * an imbalance but busiest->nr_running <= 1, the group is
4734 * still unbalanced. ld_moved simply stays zero, so it is
4735 * correctly treated as an imbalance.
4736 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004737 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004738 env.src_cpu = busiest->cpu;
4739 env.src_rq = busiest;
4740 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004741
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004742 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004743more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004744 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004745 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304746
4747 /*
4748 * cur_ld_moved - load moved in current iteration
4749 * ld_moved - cumulative load moved across iterations
4750 */
4751 cur_ld_moved = move_tasks(&env);
4752 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004753 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004754 local_irq_restore(flags);
4755
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004756 if (env.flags & LBF_NEED_BREAK) {
4757 env.flags &= ~LBF_NEED_BREAK;
4758 goto more_balance;
4759 }
4760
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004761 /*
4762 * some other cpu did the load balance for us.
4763 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304764 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4765 resched_cpu(env.dst_cpu);
4766
4767 /*
4768 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4769 * us and move them to an alternate dst_cpu in our sched_group
4770 * where they can run. The upper limit on how many times we
4771 * iterate on same src_cpu is dependent on number of cpus in our
4772 * sched_group.
4773 *
4774 * This changes load balance semantics a bit on who can move
4775 * load to a given_cpu. In addition to the given_cpu itself
4776 * (or a ilb_cpu acting on its behalf where given_cpu is
4777 * nohz-idle), we now have balance_cpu in a position to move
4778 * load to given_cpu. In rare situations, this may cause
4779 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4780 * _independently_ and at _same_ time to move some load to
4781 * given_cpu) causing exceess load to be moved to given_cpu.
4782 * This however should not happen so much in practice and
4783 * moreover subsequent load balance cycles should correct the
4784 * excess load moved.
4785 */
4786 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4787 lb_iterations++ < max_lb_iterations) {
4788
Michael Wang78feefc2012-08-06 16:41:59 +08004789 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304790 env.dst_cpu = env.new_dst_cpu;
4791 env.flags &= ~LBF_SOME_PINNED;
4792 env.loop = 0;
4793 env.loop_break = sched_nr_migrate_break;
4794 /*
4795 * Go back to "more_balance" rather than "redo" since we
4796 * need to continue with same src_cpu.
4797 */
4798 goto more_balance;
4799 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004800
4801 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004802 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004803 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304804 if (!cpumask_empty(cpus)) {
4805 env.loop = 0;
4806 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004807 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304808 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004809 goto out_balanced;
4810 }
4811 }
4812
4813 if (!ld_moved) {
4814 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004815 /*
4816 * Increment the failure counter only on periodic balance.
4817 * We do not want newidle balance, which can be very
4818 * frequent, pollute the failure counter causing
4819 * excessive cache_hot migrations and active balances.
4820 */
4821 if (idle != CPU_NEWLY_IDLE)
4822 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004823
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004824 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004825 raw_spin_lock_irqsave(&busiest->lock, flags);
4826
Tejun Heo969c7922010-05-06 18:49:21 +02004827 /* don't kick the active_load_balance_cpu_stop,
4828 * if the curr task on busiest cpu can't be
4829 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004830 */
4831 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004832 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004833 raw_spin_unlock_irqrestore(&busiest->lock,
4834 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004835 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004836 goto out_one_pinned;
4837 }
4838
Tejun Heo969c7922010-05-06 18:49:21 +02004839 /*
4840 * ->active_balance synchronizes accesses to
4841 * ->active_balance_work. Once set, it's cleared
4842 * only after active load balance is finished.
4843 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004844 if (!busiest->active_balance) {
4845 busiest->active_balance = 1;
4846 busiest->push_cpu = this_cpu;
4847 active_balance = 1;
4848 }
4849 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004850
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004851 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004852 stop_one_cpu_nowait(cpu_of(busiest),
4853 active_load_balance_cpu_stop, busiest,
4854 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004855 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004856
4857 /*
4858 * We've kicked active balancing, reset the failure
4859 * counter.
4860 */
4861 sd->nr_balance_failed = sd->cache_nice_tries+1;
4862 }
4863 } else
4864 sd->nr_balance_failed = 0;
4865
4866 if (likely(!active_balance)) {
4867 /* We were unbalanced, so reset the balancing interval */
4868 sd->balance_interval = sd->min_interval;
4869 } else {
4870 /*
4871 * If we've begun active balancing, start to back off. This
4872 * case may not be covered by the all_pinned logic if there
4873 * is only 1 task on the busy runqueue (because we don't call
4874 * move_tasks).
4875 */
4876 if (sd->balance_interval < sd->max_interval)
4877 sd->balance_interval *= 2;
4878 }
4879
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004880 goto out;
4881
4882out_balanced:
4883 schedstat_inc(sd, lb_balanced[idle]);
4884
4885 sd->nr_balance_failed = 0;
4886
4887out_one_pinned:
4888 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004889 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004890 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004891 (sd->balance_interval < sd->max_interval))
4892 sd->balance_interval *= 2;
4893
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004894 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004895out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004896 return ld_moved;
4897}
4898
4899/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004900 * idle_balance is called by schedule() if this_cpu is about to become
4901 * idle. Attempts to pull tasks from other CPUs.
4902 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004903void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004904{
4905 struct sched_domain *sd;
4906 int pulled_task = 0;
4907 unsigned long next_balance = jiffies + HZ;
4908
4909 this_rq->idle_stamp = this_rq->clock;
4910
4911 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4912 return;
4913
Ben Segall18bf2802012-10-04 12:51:20 +02004914 update_rq_runnable_avg(this_rq, 1);
4915
Peter Zijlstraf492e122009-12-23 15:29:42 +01004916 /*
4917 * Drop the rq->lock, but keep IRQ/preempt disabled.
4918 */
4919 raw_spin_unlock(&this_rq->lock);
4920
Paul Turnerc66eaf62010-11-15 15:47:07 -08004921 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004922 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004923 for_each_domain(this_cpu, sd) {
4924 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004925 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004926
4927 if (!(sd->flags & SD_LOAD_BALANCE))
4928 continue;
4929
Peter Zijlstraf492e122009-12-23 15:29:42 +01004930 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004931 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004932 pulled_task = load_balance(this_cpu, this_rq,
4933 sd, CPU_NEWLY_IDLE, &balance);
4934 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004935
4936 interval = msecs_to_jiffies(sd->balance_interval);
4937 if (time_after(next_balance, sd->last_balance + interval))
4938 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004939 if (pulled_task) {
4940 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004941 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004942 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004943 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004944 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004945
4946 raw_spin_lock(&this_rq->lock);
4947
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004948 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4949 /*
4950 * We are going idle. next_balance may be set based on
4951 * a busy processor. So reset next_balance.
4952 */
4953 this_rq->next_balance = next_balance;
4954 }
4955}
4956
4957/*
Tejun Heo969c7922010-05-06 18:49:21 +02004958 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4959 * running tasks off the busiest CPU onto idle CPUs. It requires at
4960 * least 1 task to be running on each physical CPU where possible, and
4961 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004962 */
Tejun Heo969c7922010-05-06 18:49:21 +02004963static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004964{
Tejun Heo969c7922010-05-06 18:49:21 +02004965 struct rq *busiest_rq = data;
4966 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004967 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004968 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004969 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004970
4971 raw_spin_lock_irq(&busiest_rq->lock);
4972
4973 /* make sure the requested cpu hasn't gone down in the meantime */
4974 if (unlikely(busiest_cpu != smp_processor_id() ||
4975 !busiest_rq->active_balance))
4976 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004977
4978 /* Is there any task to move? */
4979 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004980 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004981
4982 /*
4983 * This condition is "impossible", if it occurs
4984 * we need to fix it. Originally reported by
4985 * Bjorn Helgaas on a 128-cpu setup.
4986 */
4987 BUG_ON(busiest_rq == target_rq);
4988
4989 /* move a task from busiest_rq to target_rq */
4990 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004991
4992 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004993 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004994 for_each_domain(target_cpu, sd) {
4995 if ((sd->flags & SD_LOAD_BALANCE) &&
4996 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4997 break;
4998 }
4999
5000 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005001 struct lb_env env = {
5002 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005003 .dst_cpu = target_cpu,
5004 .dst_rq = target_rq,
5005 .src_cpu = busiest_rq->cpu,
5006 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005007 .idle = CPU_IDLE,
5008 };
5009
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005010 schedstat_inc(sd, alb_count);
5011
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005012 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005013 schedstat_inc(sd, alb_pushed);
5014 else
5015 schedstat_inc(sd, alb_failed);
5016 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005017 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005018 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02005019out_unlock:
5020 busiest_rq->active_balance = 0;
5021 raw_spin_unlock_irq(&busiest_rq->lock);
5022 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005023}
5024
5025#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005026/*
5027 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005028 * - When one of the busy CPUs notice that there may be an idle rebalancing
5029 * needed, they will kick the idle load balancer, which then does idle
5030 * load balancing for all the idle CPUs.
5031 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005032static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005033 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005034 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005035 unsigned long next_balance; /* in jiffy units */
5036} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005037
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01005038static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005039{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005040 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005041
Suresh Siddha786d6dc72011-12-01 17:07:35 -08005042 if (ilb < nr_cpu_ids && idle_cpu(ilb))
5043 return ilb;
5044
5045 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005046}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005047
5048/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005049 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
5050 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
5051 * CPU (if there is one).
5052 */
5053static void nohz_balancer_kick(int cpu)
5054{
5055 int ilb_cpu;
5056
5057 nohz.next_balance++;
5058
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005059 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005060
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005061 if (ilb_cpu >= nr_cpu_ids)
5062 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005063
Suresh Siddhacd490c52011-12-06 11:26:34 -08005064 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08005065 return;
5066 /*
5067 * Use smp_send_reschedule() instead of resched_cpu().
5068 * This way we generate a sched IPI on the target cpu which
5069 * is idle. And the softirq performing nohz idle load balance
5070 * will be run before returning from the IPI.
5071 */
5072 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005073 return;
5074}
5075
Alex Shic1cc0172012-09-10 15:10:58 +08005076static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08005077{
5078 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
5079 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
5080 atomic_dec(&nohz.nr_cpus);
5081 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
5082 }
5083}
5084
Suresh Siddha69e1e812011-12-01 17:07:33 -08005085static inline void set_cpu_sd_state_busy(void)
5086{
5087 struct sched_domain *sd;
5088 int cpu = smp_processor_id();
5089
5090 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5091 return;
5092 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
5093
5094 rcu_read_lock();
5095 for_each_domain(cpu, sd)
5096 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
5097 rcu_read_unlock();
5098}
5099
5100void set_cpu_sd_state_idle(void)
5101{
5102 struct sched_domain *sd;
5103 int cpu = smp_processor_id();
5104
5105 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5106 return;
5107 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5108
5109 rcu_read_lock();
5110 for_each_domain(cpu, sd)
5111 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5112 rcu_read_unlock();
5113}
5114
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005115/*
Alex Shic1cc0172012-09-10 15:10:58 +08005116 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005117 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005118 */
Alex Shic1cc0172012-09-10 15:10:58 +08005119void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005120{
Suresh Siddha71325962012-01-19 18:28:57 -08005121 /*
5122 * If this cpu is going down, then nothing needs to be done.
5123 */
5124 if (!cpu_active(cpu))
5125 return;
5126
Alex Shic1cc0172012-09-10 15:10:58 +08005127 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5128 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005129
Alex Shic1cc0172012-09-10 15:10:58 +08005130 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5131 atomic_inc(&nohz.nr_cpus);
5132 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005133}
Suresh Siddha71325962012-01-19 18:28:57 -08005134
5135static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5136 unsigned long action, void *hcpu)
5137{
5138 switch (action & ~CPU_TASKS_FROZEN) {
5139 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005140 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005141 return NOTIFY_OK;
5142 default:
5143 return NOTIFY_DONE;
5144 }
5145}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005146#endif
5147
5148static DEFINE_SPINLOCK(balancing);
5149
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005150/*
5151 * Scale the max load_balance interval with the number of CPUs in the system.
5152 * This trades load-balance latency on larger machines for less cross talk.
5153 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005154void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005155{
5156 max_load_balance_interval = HZ*num_online_cpus()/10;
5157}
5158
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005159/*
5160 * It checks each scheduling domain to see if it is due to be balanced,
5161 * and initiates a balancing operation if so.
5162 *
5163 * Balancing parameters are set up in arch_init_sched_domains.
5164 */
5165static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5166{
5167 int balance = 1;
5168 struct rq *rq = cpu_rq(cpu);
5169 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005170 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005171 /* Earliest time when we have to do rebalance again */
5172 unsigned long next_balance = jiffies + 60*HZ;
5173 int update_next_balance = 0;
5174 int need_serialize;
5175
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005176 update_shares(cpu);
5177
Peter Zijlstradce840a2011-04-07 14:09:50 +02005178 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005179 for_each_domain(cpu, sd) {
5180 if (!(sd->flags & SD_LOAD_BALANCE))
5181 continue;
5182
5183 interval = sd->balance_interval;
5184 if (idle != CPU_IDLE)
5185 interval *= sd->busy_factor;
5186
5187 /* scale ms to jiffies */
5188 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005189 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005190
5191 need_serialize = sd->flags & SD_SERIALIZE;
5192
5193 if (need_serialize) {
5194 if (!spin_trylock(&balancing))
5195 goto out;
5196 }
5197
5198 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5199 if (load_balance(cpu, rq, sd, idle, &balance)) {
5200 /*
5201 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005202 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005203 */
5204 idle = CPU_NOT_IDLE;
5205 }
5206 sd->last_balance = jiffies;
5207 }
5208 if (need_serialize)
5209 spin_unlock(&balancing);
5210out:
5211 if (time_after(next_balance, sd->last_balance + interval)) {
5212 next_balance = sd->last_balance + interval;
5213 update_next_balance = 1;
5214 }
5215
5216 /*
5217 * Stop the load balance at this level. There is another
5218 * CPU in our sched group which is doing load balancing more
5219 * actively.
5220 */
5221 if (!balance)
5222 break;
5223 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005224 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005225
5226 /*
5227 * next_balance will be updated only when there is a need.
5228 * When the cpu is attached to null domain for ex, it will not be
5229 * updated.
5230 */
5231 if (likely(update_next_balance))
5232 rq->next_balance = next_balance;
5233}
5234
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005235#ifdef CONFIG_NO_HZ
5236/*
5237 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5238 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5239 */
5240static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5241{
5242 struct rq *this_rq = cpu_rq(this_cpu);
5243 struct rq *rq;
5244 int balance_cpu;
5245
Suresh Siddha1c792db2011-12-01 17:07:32 -08005246 if (idle != CPU_IDLE ||
5247 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5248 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005249
5250 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005251 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005252 continue;
5253
5254 /*
5255 * If this cpu gets work to do, stop the load balancing
5256 * work being done for other cpus. Next load
5257 * balancing owner will pick it up.
5258 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005259 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005260 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005261
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005262 rq = cpu_rq(balance_cpu);
5263
5264 raw_spin_lock_irq(&rq->lock);
5265 update_rq_clock(rq);
5266 update_idle_cpu_load(rq);
5267 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005268
5269 rebalance_domains(balance_cpu, CPU_IDLE);
5270
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005271 if (time_after(this_rq->next_balance, rq->next_balance))
5272 this_rq->next_balance = rq->next_balance;
5273 }
5274 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005275end:
5276 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005277}
5278
5279/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005280 * Current heuristic for kicking the idle load balancer in the presence
5281 * of an idle cpu is the system.
5282 * - This rq has more than one task.
5283 * - At any scheduler domain level, this cpu's scheduler group has multiple
5284 * busy cpu's exceeding the group's power.
5285 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5286 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005287 */
5288static inline int nohz_kick_needed(struct rq *rq, int cpu)
5289{
5290 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005291 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005292
Suresh Siddha1c792db2011-12-01 17:07:32 -08005293 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005294 return 0;
5295
Suresh Siddha1c792db2011-12-01 17:07:32 -08005296 /*
5297 * We may be recently in ticked or tickless idle mode. At the first
5298 * busy tick after returning from idle, we will update the busy stats.
5299 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005300 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005301 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005302
5303 /*
5304 * None are in tickless mode and hence no need for NOHZ idle load
5305 * balancing.
5306 */
5307 if (likely(!atomic_read(&nohz.nr_cpus)))
5308 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005309
5310 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005311 return 0;
5312
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005313 if (rq->nr_running >= 2)
5314 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005315
Peter Zijlstra067491b2011-12-07 14:32:08 +01005316 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005317 for_each_domain(cpu, sd) {
5318 struct sched_group *sg = sd->groups;
5319 struct sched_group_power *sgp = sg->sgp;
5320 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005321
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005322 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005323 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005324
5325 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5326 && (cpumask_first_and(nohz.idle_cpus_mask,
5327 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005328 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005329
5330 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5331 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005332 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005333 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005334 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005335
5336need_kick_unlock:
5337 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005338need_kick:
5339 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005340}
5341#else
5342static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5343#endif
5344
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005345/*
5346 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005347 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005348 */
5349static void run_rebalance_domains(struct softirq_action *h)
5350{
5351 int this_cpu = smp_processor_id();
5352 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005353 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005354 CPU_IDLE : CPU_NOT_IDLE;
5355
5356 rebalance_domains(this_cpu, idle);
5357
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005358 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005359 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005360 * balancing on behalf of the other idle cpus whose ticks are
5361 * stopped.
5362 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005363 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005364}
5365
5366static inline int on_null_domain(int cpu)
5367{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005368 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005369}
5370
5371/*
5372 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005373 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005374void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005375{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005376 /* Don't need to rebalance while attached to NULL domain */
5377 if (time_after_eq(jiffies, rq->next_balance) &&
5378 likely(!on_null_domain(cpu)))
5379 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005380#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005381 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005382 nohz_balancer_kick(cpu);
5383#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005384}
5385
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005386static void rq_online_fair(struct rq *rq)
5387{
5388 update_sysctl();
5389}
5390
5391static void rq_offline_fair(struct rq *rq)
5392{
5393 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005394
5395 /* Ensure any throttled groups are reachable by pick_next_task */
5396 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005397}
5398
Dhaval Giani55e12e52008-06-24 23:39:43 +05305399#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005400
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005401/*
5402 * scheduler tick hitting a task of our scheduling class:
5403 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005404static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005405{
5406 struct cfs_rq *cfs_rq;
5407 struct sched_entity *se = &curr->se;
5408
5409 for_each_sched_entity(se) {
5410 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005411 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005412 }
Ben Segall18bf2802012-10-04 12:51:20 +02005413
5414 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005415}
5416
5417/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005418 * called on fork with the child task as argument from the parent's context
5419 * - child not yet on the tasklist
5420 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005421 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005422static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005423{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005424 struct cfs_rq *cfs_rq;
5425 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005426 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005427 struct rq *rq = this_rq();
5428 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005429
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005430 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005431
Peter Zijlstra861d0342010-08-19 13:31:43 +02005432 update_rq_clock(rq);
5433
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005434 cfs_rq = task_cfs_rq(current);
5435 curr = cfs_rq->curr;
5436
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005437 if (unlikely(task_cpu(p) != this_cpu)) {
5438 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005439 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005440 rcu_read_unlock();
5441 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005442
Ting Yang7109c442007-08-28 12:53:24 +02005443 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005444
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005445 if (curr)
5446 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005447 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005448
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005449 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005450 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005451 * Upon rescheduling, sched_class::put_prev_task() will place
5452 * 'current' within the tree based on its new key value.
5453 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005454 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305455 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005456 }
5457
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005458 se->vruntime -= cfs_rq->min_vruntime;
5459
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005460 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005461}
5462
Steven Rostedtcb469842008-01-25 21:08:22 +01005463/*
5464 * Priority of the task has changed. Check to see if we preempt
5465 * the current task.
5466 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005467static void
5468prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005469{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005470 if (!p->se.on_rq)
5471 return;
5472
Steven Rostedtcb469842008-01-25 21:08:22 +01005473 /*
5474 * Reschedule if we are currently running on this runqueue and
5475 * our priority decreased, or if we are not currently running on
5476 * this runqueue and our priority is higher than the current's
5477 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005478 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005479 if (p->prio > oldprio)
5480 resched_task(rq->curr);
5481 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005482 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005483}
5484
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005485static void switched_from_fair(struct rq *rq, struct task_struct *p)
5486{
5487 struct sched_entity *se = &p->se;
5488 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5489
5490 /*
5491 * Ensure the task's vruntime is normalized, so that when its
5492 * switched back to the fair class the enqueue_entity(.flags=0) will
5493 * do the right thing.
5494 *
5495 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5496 * have normalized the vruntime, if it was !on_rq, then only when
5497 * the task is sleeping will it still have non-normalized vruntime.
5498 */
5499 if (!se->on_rq && p->state != TASK_RUNNING) {
5500 /*
5501 * Fix up our vruntime so that the current sleep doesn't
5502 * cause 'unlimited' sleep bonus.
5503 */
5504 place_entity(cfs_rq, se, 0);
5505 se->vruntime -= cfs_rq->min_vruntime;
5506 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005507
5508#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5509 /*
5510 * Remove our load from contribution when we leave sched_fair
5511 * and ensure we don't carry in an old decay_count if we
5512 * switch back.
5513 */
5514 if (p->se.avg.decay_count) {
5515 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5516 __synchronize_entity_decay(&p->se);
5517 subtract_blocked_load_contrib(cfs_rq,
5518 p->se.avg.load_avg_contrib);
5519 }
5520#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005521}
5522
Steven Rostedtcb469842008-01-25 21:08:22 +01005523/*
5524 * We switched to the sched_fair class.
5525 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005526static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005527{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005528 if (!p->se.on_rq)
5529 return;
5530
Steven Rostedtcb469842008-01-25 21:08:22 +01005531 /*
5532 * We were most likely switched from sched_rt, so
5533 * kick off the schedule if running, otherwise just see
5534 * if we can still preempt the current task.
5535 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005536 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005537 resched_task(rq->curr);
5538 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005539 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005540}
5541
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005542/* Account for a task changing its policy or group.
5543 *
5544 * This routine is mostly called to set cfs_rq->curr field when a task
5545 * migrates between groups/classes.
5546 */
5547static void set_curr_task_fair(struct rq *rq)
5548{
5549 struct sched_entity *se = &rq->curr->se;
5550
Paul Turnerec12cb72011-07-21 09:43:30 -07005551 for_each_sched_entity(se) {
5552 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5553
5554 set_next_entity(cfs_rq, se);
5555 /* ensure bandwidth has been allocated on our new cfs_rq */
5556 account_cfs_rq_runtime(cfs_rq, 0);
5557 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005558}
5559
Peter Zijlstra029632f2011-10-25 10:00:11 +02005560void init_cfs_rq(struct cfs_rq *cfs_rq)
5561{
5562 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005563 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5564#ifndef CONFIG_64BIT
5565 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5566#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005567#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5568 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005569 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005570#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005571}
5572
Peter Zijlstra810b3812008-02-29 15:21:01 -05005573#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005574static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005575{
Paul Turneraff3e492012-10-04 13:18:30 +02005576 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005577 /*
5578 * If the task was not on the rq at the time of this cgroup movement
5579 * it must have been asleep, sleeping tasks keep their ->vruntime
5580 * absolute on their old rq until wakeup (needed for the fair sleeper
5581 * bonus in place_entity()).
5582 *
5583 * If it was on the rq, we've just 'preempted' it, which does convert
5584 * ->vruntime to a relative base.
5585 *
5586 * Make sure both cases convert their relative position when migrating
5587 * to another cgroup's rq. This does somewhat interfere with the
5588 * fair sleeper stuff for the first placement, but who cares.
5589 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005590 /*
5591 * When !on_rq, vruntime of the task has usually NOT been normalized.
5592 * But there are some cases where it has already been normalized:
5593 *
5594 * - Moving a forked child which is waiting for being woken up by
5595 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005596 * - Moving a task which has been woken up by try_to_wake_up() and
5597 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005598 *
5599 * To prevent boost or penalty in the new cfs_rq caused by delta
5600 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5601 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005602 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005603 on_rq = 1;
5604
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005605 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005606 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5607 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005608 if (!on_rq) {
5609 cfs_rq = cfs_rq_of(&p->se);
5610 p->se.vruntime += cfs_rq->min_vruntime;
5611#ifdef CONFIG_SMP
5612 /*
5613 * migrate_task_rq_fair() will have removed our previous
5614 * contribution, but we must synchronize for ongoing future
5615 * decay.
5616 */
5617 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5618 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5619#endif
5620 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005621}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005622
5623void free_fair_sched_group(struct task_group *tg)
5624{
5625 int i;
5626
5627 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5628
5629 for_each_possible_cpu(i) {
5630 if (tg->cfs_rq)
5631 kfree(tg->cfs_rq[i]);
5632 if (tg->se)
5633 kfree(tg->se[i]);
5634 }
5635
5636 kfree(tg->cfs_rq);
5637 kfree(tg->se);
5638}
5639
5640int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5641{
5642 struct cfs_rq *cfs_rq;
5643 struct sched_entity *se;
5644 int i;
5645
5646 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5647 if (!tg->cfs_rq)
5648 goto err;
5649 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5650 if (!tg->se)
5651 goto err;
5652
5653 tg->shares = NICE_0_LOAD;
5654
5655 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5656
5657 for_each_possible_cpu(i) {
5658 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5659 GFP_KERNEL, cpu_to_node(i));
5660 if (!cfs_rq)
5661 goto err;
5662
5663 se = kzalloc_node(sizeof(struct sched_entity),
5664 GFP_KERNEL, cpu_to_node(i));
5665 if (!se)
5666 goto err_free_rq;
5667
5668 init_cfs_rq(cfs_rq);
5669 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5670 }
5671
5672 return 1;
5673
5674err_free_rq:
5675 kfree(cfs_rq);
5676err:
5677 return 0;
5678}
5679
5680void unregister_fair_sched_group(struct task_group *tg, int cpu)
5681{
5682 struct rq *rq = cpu_rq(cpu);
5683 unsigned long flags;
5684
5685 /*
5686 * Only empty task groups can be destroyed; so we can speculatively
5687 * check on_list without danger of it being re-added.
5688 */
5689 if (!tg->cfs_rq[cpu]->on_list)
5690 return;
5691
5692 raw_spin_lock_irqsave(&rq->lock, flags);
5693 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5694 raw_spin_unlock_irqrestore(&rq->lock, flags);
5695}
5696
5697void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5698 struct sched_entity *se, int cpu,
5699 struct sched_entity *parent)
5700{
5701 struct rq *rq = cpu_rq(cpu);
5702
5703 cfs_rq->tg = tg;
5704 cfs_rq->rq = rq;
5705#ifdef CONFIG_SMP
5706 /* allow initial update_cfs_load() to truncate */
5707 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005708#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005709 init_cfs_rq_runtime(cfs_rq);
5710
5711 tg->cfs_rq[cpu] = cfs_rq;
5712 tg->se[cpu] = se;
5713
5714 /* se could be NULL for root_task_group */
5715 if (!se)
5716 return;
5717
5718 if (!parent)
5719 se->cfs_rq = &rq->cfs;
5720 else
5721 se->cfs_rq = parent->my_q;
5722
5723 se->my_q = cfs_rq;
5724 update_load_set(&se->load, 0);
5725 se->parent = parent;
5726}
5727
5728static DEFINE_MUTEX(shares_mutex);
5729
5730int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5731{
5732 int i;
5733 unsigned long flags;
5734
5735 /*
5736 * We can't change the weight of the root cgroup.
5737 */
5738 if (!tg->se[0])
5739 return -EINVAL;
5740
5741 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5742
5743 mutex_lock(&shares_mutex);
5744 if (tg->shares == shares)
5745 goto done;
5746
5747 tg->shares = shares;
5748 for_each_possible_cpu(i) {
5749 struct rq *rq = cpu_rq(i);
5750 struct sched_entity *se;
5751
5752 se = tg->se[i];
5753 /* Propagate contribution to hierarchy */
5754 raw_spin_lock_irqsave(&rq->lock, flags);
5755 for_each_sched_entity(se)
5756 update_cfs_shares(group_cfs_rq(se));
5757 raw_spin_unlock_irqrestore(&rq->lock, flags);
5758 }
5759
5760done:
5761 mutex_unlock(&shares_mutex);
5762 return 0;
5763}
5764#else /* CONFIG_FAIR_GROUP_SCHED */
5765
5766void free_fair_sched_group(struct task_group *tg) { }
5767
5768int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5769{
5770 return 1;
5771}
5772
5773void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5774
5775#endif /* CONFIG_FAIR_GROUP_SCHED */
5776
Peter Zijlstra810b3812008-02-29 15:21:01 -05005777
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005778static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005779{
5780 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005781 unsigned int rr_interval = 0;
5782
5783 /*
5784 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5785 * idle runqueue:
5786 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005787 if (rq->cfs.load.weight)
5788 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005789
5790 return rr_interval;
5791}
5792
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005793/*
5794 * All the scheduling class methods:
5795 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005796const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005797 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005798 .enqueue_task = enqueue_task_fair,
5799 .dequeue_task = dequeue_task_fair,
5800 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005801 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005802
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005803 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005804
5805 .pick_next_task = pick_next_task_fair,
5806 .put_prev_task = put_prev_task_fair,
5807
Peter Williams681f3e62007-10-24 18:23:51 +02005808#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005809 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005810 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005811
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005812 .rq_online = rq_online_fair,
5813 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005814
5815 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005816#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005817
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005818 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005819 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005820 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005821
5822 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005823 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005824 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005825
Peter Williams0d721ce2009-09-21 01:31:53 +00005826 .get_rr_interval = get_rr_interval_fair,
5827
Peter Zijlstra810b3812008-02-29 15:21:01 -05005828#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005829 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005830#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005831};
5832
5833#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005834void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005835{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005836 struct cfs_rq *cfs_rq;
5837
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005838 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005839 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005840 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005841 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005842}
5843#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005844
5845__init void init_sched_fair_class(void)
5846{
5847#ifdef CONFIG_SMP
5848 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5849
5850#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005851 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005852 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005853 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005854#endif
5855#endif /* SMP */
5856
5857}