blob: db788222f198f82fc804260e375918531954b83e [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Paul Turneraff3e492012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
263 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200264
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800265static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
266{
267 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800268 /*
269 * Ensure we either appear before our parent (if already
270 * enqueued) or force our parent to appear after us when it is
271 * enqueued. The fact that we always enqueue bottom-up
272 * reduces this to two cases.
273 */
274 if (cfs_rq->tg->parent &&
275 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
276 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800278 } else {
279 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
280 &rq_of(cfs_rq)->leaf_cfs_rq_list);
281 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282
283 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200284 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200285 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800286 }
287}
288
289static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (cfs_rq->on_list) {
292 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
293 cfs_rq->on_list = 0;
294 }
295}
296
Peter Zijlstrab7581492008-04-19 19:45:00 +0200297/* Iterate thr' all leaf cfs_rq's on a runqueue */
298#define for_each_leaf_cfs_rq(rq, cfs_rq) \
299 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
300
301/* Do the two (enqueued) entities belong to the same group ? */
302static inline int
303is_same_group(struct sched_entity *se, struct sched_entity *pse)
304{
305 if (se->cfs_rq == pse->cfs_rq)
306 return 1;
307
308 return 0;
309}
310
311static inline struct sched_entity *parent_entity(struct sched_entity *se)
312{
313 return se->parent;
314}
315
Peter Zijlstra464b7522008-10-24 11:06:15 +0200316/* return depth at which a sched entity is present in the hierarchy */
317static inline int depth_se(struct sched_entity *se)
318{
319 int depth = 0;
320
321 for_each_sched_entity(se)
322 depth++;
323
324 return depth;
325}
326
327static void
328find_matching_se(struct sched_entity **se, struct sched_entity **pse)
329{
330 int se_depth, pse_depth;
331
332 /*
333 * preemption test can be made between sibling entities who are in the
334 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
335 * both tasks until we find their ancestors who are siblings of common
336 * parent.
337 */
338
339 /* First walk up until both entities are at same depth */
340 se_depth = depth_se(*se);
341 pse_depth = depth_se(*pse);
342
343 while (se_depth > pse_depth) {
344 se_depth--;
345 *se = parent_entity(*se);
346 }
347
348 while (pse_depth > se_depth) {
349 pse_depth--;
350 *pse = parent_entity(*pse);
351 }
352
353 while (!is_same_group(*se, *pse)) {
354 *se = parent_entity(*se);
355 *pse = parent_entity(*pse);
356 }
357}
358
Peter Zijlstra8f488942009-07-24 12:25:30 +0200359#else /* !CONFIG_FAIR_GROUP_SCHED */
360
361static inline struct task_struct *task_of(struct sched_entity *se)
362{
363 return container_of(se, struct task_struct, se);
364}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365
366static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
367{
368 return container_of(cfs_rq, struct rq, cfs);
369}
370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200371#define entity_is_task(se) 1
372
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373#define for_each_sched_entity(se) \
374 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200379}
380
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
382{
383 struct task_struct *p = task_of(se);
384 struct rq *rq = task_rq(p);
385
386 return &rq->cfs;
387}
388
389/* runqueue "owned" by this group */
390static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
391{
392 return NULL;
393}
394
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800395static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
396{
397}
398
399static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
400{
401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403#define for_each_leaf_cfs_rq(rq, cfs_rq) \
404 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
405
406static inline int
407is_same_group(struct sched_entity *se, struct sched_entity *pse)
408{
409 return 1;
410}
411
412static inline struct sched_entity *parent_entity(struct sched_entity *se)
413{
414 return NULL;
415}
416
Peter Zijlstra464b7522008-10-24 11:06:15 +0200417static inline void
418find_matching_se(struct sched_entity **se, struct sched_entity **pse)
419{
420}
421
Peter Zijlstrab7581492008-04-19 19:45:00 +0200422#endif /* CONFIG_FAIR_GROUP_SCHED */
423
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700424static __always_inline
425void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427/**************************************************************
428 * Scheduling class tree data structure manipulation methods:
429 */
430
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200431static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200432{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200433 s64 delta = (s64)(vruntime - min_vruntime);
434 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435 min_vruntime = vruntime;
436
437 return min_vruntime;
438}
439
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200440static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200441{
442 s64 delta = (s64)(vruntime - min_vruntime);
443 if (delta < 0)
444 min_vruntime = vruntime;
445
446 return min_vruntime;
447}
448
Fabio Checconi54fdc582009-07-16 12:32:27 +0200449static inline int entity_before(struct sched_entity *a,
450 struct sched_entity *b)
451{
452 return (s64)(a->vruntime - b->vruntime) < 0;
453}
454
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200455static void update_min_vruntime(struct cfs_rq *cfs_rq)
456{
457 u64 vruntime = cfs_rq->min_vruntime;
458
459 if (cfs_rq->curr)
460 vruntime = cfs_rq->curr->vruntime;
461
462 if (cfs_rq->rb_leftmost) {
463 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
464 struct sched_entity,
465 run_node);
466
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100467 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200468 vruntime = se->vruntime;
469 else
470 vruntime = min_vruntime(vruntime, se->vruntime);
471 }
472
473 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200474#ifndef CONFIG_64BIT
475 smp_wmb();
476 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
477#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478}
479
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200480/*
481 * Enqueue an entity into the rb-tree:
482 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200483static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484{
485 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
486 struct rb_node *parent = NULL;
487 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488 int leftmost = 1;
489
490 /*
491 * Find the right place in the rbtree:
492 */
493 while (*link) {
494 parent = *link;
495 entry = rb_entry(parent, struct sched_entity, run_node);
496 /*
497 * We dont care about collisions. Nodes with
498 * the same key stay together.
499 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200500 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200501 link = &parent->rb_left;
502 } else {
503 link = &parent->rb_right;
504 leftmost = 0;
505 }
506 }
507
508 /*
509 * Maintain a cache of leftmost tree entries (it is frequently
510 * used):
511 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200512 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200513 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200514
515 rb_link_node(&se->run_node, parent, link);
516 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517}
518
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200519static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 if (cfs_rq->rb_leftmost == &se->run_node) {
522 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100523
524 next_node = rb_next(&se->run_node);
525 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200527
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200529}
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100533 struct rb_node *left = cfs_rq->rb_leftmost;
534
535 if (!left)
536 return NULL;
537
538 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200539}
540
Rik van Rielac53db52011-02-01 09:51:03 -0500541static struct sched_entity *__pick_next_entity(struct sched_entity *se)
542{
543 struct rb_node *next = rb_next(&se->run_node);
544
545 if (!next)
546 return NULL;
547
548 return rb_entry(next, struct sched_entity, run_node);
549}
550
551#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100554 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555
Balbir Singh70eee742008-02-22 13:25:53 +0530556 if (!last)
557 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100558
559 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200560}
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562/**************************************************************
563 * Scheduling class statistics methods:
564 */
565
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700567 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100568 loff_t *ppos)
569{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100571 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100572
573 if (ret || !write)
574 return ret;
575
576 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
577 sysctl_sched_min_granularity);
578
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#define WRT_SYSCTL(name) \
580 (normalized_sysctl_##name = sysctl_##name / (factor))
581 WRT_SYSCTL(sched_min_granularity);
582 WRT_SYSCTL(sched_latency);
583 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100584#undef WRT_SYSCTL
585
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586 return 0;
587}
588#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200589
590/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200592 */
593static inline unsigned long
594calc_delta_fair(unsigned long delta, struct sched_entity *se)
595{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200596 if (unlikely(se->load.weight != NICE_0_LOAD))
597 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200598
599 return delta;
600}
601
602/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603 * The idea is to set a period in which each task runs once.
604 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200605 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * this period because otherwise the slices get too small.
607 *
608 * p = (nr <= nl) ? l : l*nr/nl
609 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200610static u64 __sched_period(unsigned long nr_running)
611{
612 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100613 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200614
615 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100616 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200618 }
619
620 return period;
621}
622
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623/*
624 * We calculate the wall-time slice from the period by taking a part
625 * proportional to the weight.
626 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200629static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200630{
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200632
Mike Galbraith0a582442009-01-02 12:16:42 +0100633 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100634 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200635 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100636
637 cfs_rq = cfs_rq_of(se);
638 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200641 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100642
643 update_load_add(&lw, se->load.weight);
644 load = &lw;
645 }
646 slice = calc_delta_mine(slice, se->load.weight, load);
647 }
648 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200649}
650
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200652 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200658 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659}
660
Paul Turnerd6b55912010-11-15 15:47:09 -0800661static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800662static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800663
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200664/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200665 * Update the current task's runtime statistics. Skip current tasks that
666 * are not in our scheduling class.
667 */
668static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200669__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
670 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200672 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200673
Lucas De Marchi41acab82010-03-10 23:37:45 -0300674 schedstat_set(curr->statistics.exec_max,
675 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200676
677 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200678 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200679 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100680
Ingo Molnare9acbff2007-10-15 17:00:04 +0200681 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200682 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800683
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100684#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800685 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800686#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200687}
688
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200689static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200691 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700692 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693 unsigned long delta_exec;
694
695 if (unlikely(!curr))
696 return;
697
698 /*
699 * Get the amount of time the current task was running
700 * since the last time we changed load (this cannot
701 * overflow on 32 bits):
702 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200703 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100704 if (!delta_exec)
705 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200706
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200707 __update_curr(cfs_rq, curr, delta_exec);
708 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100709
710 if (entity_is_task(curr)) {
711 struct task_struct *curtask = task_of(curr);
712
Ingo Molnarf977bb42009-09-13 18:15:54 +0200713 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100714 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700715 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100716 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700717
718 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200719}
720
721static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200722update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200723{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300724 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200725}
726
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200727/*
728 * Task is being enqueued - update stats:
729 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200730static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200732 /*
733 * Are we enqueueing a waiting task? (for current tasks
734 * a dequeue/enqueue event is a NOP)
735 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200736 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200737 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200738}
739
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200741update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200742{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300743 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
744 rq_of(cfs_rq)->clock - se->statistics.wait_start));
745 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
746 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
747 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200748#ifdef CONFIG_SCHEDSTATS
749 if (entity_is_task(se)) {
750 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300751 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200752 }
753#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300754 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200755}
756
757static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200758update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200759{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200760 /*
761 * Mark the end of the wait period if dequeueing a
762 * waiting task:
763 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200764 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200765 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200766}
767
768/*
769 * We are picking a new current task - update its stats:
770 */
771static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200772update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200773{
774 /*
775 * We are starting a new run period:
776 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700777 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200778}
779
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200780/**************************************************
781 * Scheduling class queueing methods:
782 */
783
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200784static void
785account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
786{
787 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200788 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200789 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100790#ifdef CONFIG_SMP
791 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200792 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100793#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200794 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200795}
796
797static void
798account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
799{
800 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200801 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200802 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100803 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530804 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200805 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200806}
807
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800808#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700809/* we need this in update_cfs_load and load-balance functions below */
810static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800811# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800812static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
813 int global_update)
814{
815 struct task_group *tg = cfs_rq->tg;
816 long load_avg;
817
818 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
819 load_avg -= cfs_rq->load_contribution;
820
821 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
822 atomic_add(load_avg, &tg->load_weight);
823 cfs_rq->load_contribution += load_avg;
824 }
825}
826
827static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800828{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800829 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800830 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800831 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800832
Paul Turner64660c82011-07-21 09:43:36 -0700833 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800834 return;
835
Paul Turner05ca62c2011-01-21 20:45:02 -0800836 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800837 delta = now - cfs_rq->load_stamp;
838
Paul Turnere33078b2010-11-15 15:47:04 -0800839 /* truncate load history at 4 idle periods */
840 if (cfs_rq->load_stamp > cfs_rq->load_last &&
841 now - cfs_rq->load_last > 4 * period) {
842 cfs_rq->load_period = 0;
843 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800844 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800845 }
846
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800847 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800848 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800849 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800850 if (load) {
851 cfs_rq->load_last = now;
852 cfs_rq->load_avg += delta * load;
853 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800854
Paul Turnerd6b55912010-11-15 15:47:09 -0800855 /* consider updating load contribution on each fold or truncate */
856 if (global_update || cfs_rq->load_period > period
857 || !cfs_rq->load_period)
858 update_cfs_rq_load_contribution(cfs_rq, global_update);
859
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800860 while (cfs_rq->load_period > period) {
861 /*
862 * Inline assembly required to prevent the compiler
863 * optimising this loop into a divmod call.
864 * See __iter_div_u64_rem() for another example of this.
865 */
866 asm("" : "+rm" (cfs_rq->load_period));
867 cfs_rq->load_period /= 2;
868 cfs_rq->load_avg /= 2;
869 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800870
Paul Turnere33078b2010-11-15 15:47:04 -0800871 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
872 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800873}
874
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200875static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
876{
877 long tg_weight;
878
879 /*
880 * Use this CPU's actual weight instead of the last load_contribution
881 * to gain a more accurate current total weight. See
882 * update_cfs_rq_load_contribution().
883 */
884 tg_weight = atomic_read(&tg->load_weight);
885 tg_weight -= cfs_rq->load_contribution;
886 tg_weight += cfs_rq->load.weight;
887
888 return tg_weight;
889}
890
Paul Turner6d5ab292011-01-21 20:45:01 -0800891static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800892{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200893 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800894
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200895 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800896 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800898 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200899 if (tg_weight)
900 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800901
902 if (shares < MIN_SHARES)
903 shares = MIN_SHARES;
904 if (shares > tg->shares)
905 shares = tg->shares;
906
907 return shares;
908}
909
910static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
911{
912 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
913 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800914 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800915 }
916}
917# else /* CONFIG_SMP */
918static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
919{
920}
921
Paul Turner6d5ab292011-01-21 20:45:01 -0800922static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800923{
924 return tg->shares;
925}
926
927static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
928{
929}
930# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800931static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
932 unsigned long weight)
933{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800934 if (se->on_rq) {
935 /* commit outstanding execution time */
936 if (cfs_rq->curr == se)
937 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800938 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800939 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800940
941 update_load_set(&se->load, weight);
942
943 if (se->on_rq)
944 account_entity_enqueue(cfs_rq, se);
945}
946
Paul Turner6d5ab292011-01-21 20:45:01 -0800947static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800948{
949 struct task_group *tg;
950 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800951 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800952
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800953 tg = cfs_rq->tg;
954 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700955 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800956 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800957#ifndef CONFIG_SMP
958 if (likely(se->load.weight == tg->shares))
959 return;
960#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800961 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800962
963 reweight_entity(cfs_rq_of(se), se, shares);
964}
965#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800966static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800967{
968}
969
Paul Turner6d5ab292011-01-21 20:45:01 -0800970static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800971{
972}
Paul Turner43365bd2010-12-15 19:10:17 -0800973
974static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
975{
976}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800977#endif /* CONFIG_FAIR_GROUP_SCHED */
978
Paul Turner9d85f212012-10-04 13:18:29 +0200979#ifdef CONFIG_SMP
980/*
981 * Approximate:
982 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
983 */
984static __always_inline u64 decay_load(u64 val, u64 n)
985{
986 for (; n && val; n--) {
987 val *= 4008;
988 val >>= 12;
989 }
990
991 return val;
992}
993
994/*
995 * We can represent the historical contribution to runnable average as the
996 * coefficients of a geometric series. To do this we sub-divide our runnable
997 * history into segments of approximately 1ms (1024us); label the segment that
998 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
999 *
1000 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
1001 * p0 p1 p2
1002 * (now) (~1ms ago) (~2ms ago)
1003 *
1004 * Let u_i denote the fraction of p_i that the entity was runnable.
1005 *
1006 * We then designate the fractions u_i as our co-efficients, yielding the
1007 * following representation of historical load:
1008 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
1009 *
1010 * We choose y based on the with of a reasonably scheduling period, fixing:
1011 * y^32 = 0.5
1012 *
1013 * This means that the contribution to load ~32ms ago (u_32) will be weighted
1014 * approximately half as much as the contribution to load within the last ms
1015 * (u_0).
1016 *
1017 * When a period "rolls over" and we have new u_0`, multiplying the previous
1018 * sum again by y is sufficient to update:
1019 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1020 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1021 */
1022static __always_inline int __update_entity_runnable_avg(u64 now,
1023 struct sched_avg *sa,
1024 int runnable)
1025{
1026 u64 delta;
1027 int delta_w, decayed = 0;
1028
1029 delta = now - sa->last_runnable_update;
1030 /*
1031 * This should only happen when time goes backwards, which it
1032 * unfortunately does during sched clock init when we swap over to TSC.
1033 */
1034 if ((s64)delta < 0) {
1035 sa->last_runnable_update = now;
1036 return 0;
1037 }
1038
1039 /*
1040 * Use 1024ns as the unit of measurement since it's a reasonable
1041 * approximation of 1us and fast to compute.
1042 */
1043 delta >>= 10;
1044 if (!delta)
1045 return 0;
1046 sa->last_runnable_update = now;
1047
1048 /* delta_w is the amount already accumulated against our next period */
1049 delta_w = sa->runnable_avg_period % 1024;
1050 if (delta + delta_w >= 1024) {
1051 /* period roll-over */
1052 decayed = 1;
1053
1054 /*
1055 * Now that we know we're crossing a period boundary, figure
1056 * out how much from delta we need to complete the current
1057 * period and accrue it.
1058 */
1059 delta_w = 1024 - delta_w;
1060 BUG_ON(delta_w > delta);
1061 do {
1062 if (runnable)
1063 sa->runnable_avg_sum += delta_w;
1064 sa->runnable_avg_period += delta_w;
1065
1066 /*
1067 * Remainder of delta initiates a new period, roll over
1068 * the previous.
1069 */
1070 sa->runnable_avg_sum =
1071 decay_load(sa->runnable_avg_sum, 1);
1072 sa->runnable_avg_period =
1073 decay_load(sa->runnable_avg_period, 1);
1074
1075 delta -= delta_w;
1076 /* New period is empty */
1077 delta_w = 1024;
1078 } while (delta >= 1024);
1079 }
1080
1081 /* Remainder of delta accrued against u_0` */
1082 if (runnable)
1083 sa->runnable_avg_sum += delta;
1084 sa->runnable_avg_period += delta;
1085
1086 return decayed;
1087}
1088
Paul Turner9ee474f2012-10-04 13:18:30 +02001089/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02001090static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02001091{
1092 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1093 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1094
1095 decays -= se->avg.decay_count;
1096 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02001097 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02001098
1099 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1100 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02001101
1102 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02001103}
1104
Paul Turnerc566e8e2012-10-04 13:18:30 +02001105#ifdef CONFIG_FAIR_GROUP_SCHED
1106static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1107 int force_update)
1108{
1109 struct task_group *tg = cfs_rq->tg;
1110 s64 tg_contrib;
1111
1112 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
1113 tg_contrib -= cfs_rq->tg_load_contrib;
1114
1115 if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
1116 atomic64_add(tg_contrib, &tg->load_avg);
1117 cfs_rq->tg_load_contrib += tg_contrib;
1118 }
1119}
1120#else
1121static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1122 int force_update) {}
1123#endif
1124
Paul Turner2dac7542012-10-04 13:18:30 +02001125/* Compute the current contribution to load_avg by se, return any delta */
1126static long __update_entity_load_avg_contrib(struct sched_entity *se)
1127{
1128 long old_contrib = se->avg.load_avg_contrib;
1129
1130 if (!entity_is_task(se))
1131 return 0;
1132
1133 se->avg.load_avg_contrib = div64_u64(se->avg.runnable_avg_sum *
1134 se->load.weight,
1135 se->avg.runnable_avg_period + 1);
1136
1137 return se->avg.load_avg_contrib - old_contrib;
1138}
1139
Paul Turner9ee474f2012-10-04 13:18:30 +02001140static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1141 long load_contrib)
1142{
1143 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1144 cfs_rq->blocked_load_avg -= load_contrib;
1145 else
1146 cfs_rq->blocked_load_avg = 0;
1147}
1148
Paul Turner9d85f212012-10-04 13:18:29 +02001149/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001150static inline void update_entity_load_avg(struct sched_entity *se,
1151 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001152{
Paul Turner2dac7542012-10-04 13:18:30 +02001153 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1154 long contrib_delta;
1155
1156 if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
1157 se->on_rq))
1158 return;
1159
1160 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001161
1162 if (!update_cfs_rq)
1163 return;
1164
Paul Turner2dac7542012-10-04 13:18:30 +02001165 if (se->on_rq)
1166 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001167 else
1168 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1169}
1170
1171/*
1172 * Decay the load contributed by all blocked children and account this so that
1173 * their contribution may appropriately discounted when they wake up.
1174 */
Paul Turneraff3e492012-10-04 13:18:30 +02001175static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001176{
1177 u64 now = rq_of(cfs_rq)->clock_task >> 20;
1178 u64 decays;
1179
1180 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001181 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001182 return;
1183
Paul Turneraff3e492012-10-04 13:18:30 +02001184 if (atomic64_read(&cfs_rq->removed_load)) {
1185 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1186 subtract_blocked_load_contrib(cfs_rq, removed_load);
1187 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001188
Paul Turneraff3e492012-10-04 13:18:30 +02001189 if (decays) {
1190 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1191 decays);
1192 atomic64_add(decays, &cfs_rq->decay_counter);
1193 cfs_rq->last_decay = now;
1194 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001195
1196 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02001197}
Ben Segall18bf2802012-10-04 12:51:20 +02001198
1199static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1200{
1201 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
1202}
Paul Turner2dac7542012-10-04 13:18:30 +02001203
1204/* Add the load generated by se into cfs_rq's child load-average */
1205static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001206 struct sched_entity *se,
1207 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001208{
Paul Turneraff3e492012-10-04 13:18:30 +02001209 /*
1210 * We track migrations using entity decay_count <= 0, on a wake-up
1211 * migration we use a negative decay count to track the remote decays
1212 * accumulated while sleeping.
1213 */
1214 if (unlikely(se->avg.decay_count <= 0)) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001215 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
Paul Turneraff3e492012-10-04 13:18:30 +02001216 if (se->avg.decay_count) {
1217 /*
1218 * In a wake-up migration we have to approximate the
1219 * time sleeping. This is because we can't synchronize
1220 * clock_task between the two cpus, and it is not
1221 * guaranteed to be read-safe. Instead, we can
1222 * approximate this using our carried decays, which are
1223 * explicitly atomically readable.
1224 */
1225 se->avg.last_runnable_update -= (-se->avg.decay_count)
1226 << 20;
1227 update_entity_load_avg(se, 0);
1228 /* Indicate that we're now synchronized and on-rq */
1229 se->avg.decay_count = 0;
1230 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001231 wakeup = 0;
1232 } else {
1233 __synchronize_entity_decay(se);
1234 }
1235
Paul Turneraff3e492012-10-04 13:18:30 +02001236 /* migrated tasks did not contribute to our blocked load */
1237 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001238 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02001239 update_entity_load_avg(se, 0);
1240 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001241
Paul Turner2dac7542012-10-04 13:18:30 +02001242 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02001243 /* we force update consideration on load-balancer moves */
1244 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02001245}
1246
Paul Turner9ee474f2012-10-04 13:18:30 +02001247/*
1248 * Remove se's load from this cfs_rq child load-average, if the entity is
1249 * transitioning to a blocked state we track its projected decay using
1250 * blocked_load_avg.
1251 */
Paul Turner2dac7542012-10-04 13:18:30 +02001252static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001253 struct sched_entity *se,
1254 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001255{
Paul Turner9ee474f2012-10-04 13:18:30 +02001256 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001257 /* we force update consideration on load-balancer moves */
1258 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02001259
Paul Turner2dac7542012-10-04 13:18:30 +02001260 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001261 if (sleep) {
1262 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1263 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1264 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001265}
Paul Turner9d85f212012-10-04 13:18:29 +02001266#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001267static inline void update_entity_load_avg(struct sched_entity *se,
1268 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001269static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001270static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001271 struct sched_entity *se,
1272 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001273static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001274 struct sched_entity *se,
1275 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02001276static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
1277 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001278#endif
1279
Ingo Molnar2396af62007-08-09 11:16:48 +02001280static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001281{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001282#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001283 struct task_struct *tsk = NULL;
1284
1285 if (entity_is_task(se))
1286 tsk = task_of(se);
1287
Lucas De Marchi41acab82010-03-10 23:37:45 -03001288 if (se->statistics.sleep_start) {
1289 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001290
1291 if ((s64)delta < 0)
1292 delta = 0;
1293
Lucas De Marchi41acab82010-03-10 23:37:45 -03001294 if (unlikely(delta > se->statistics.sleep_max))
1295 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001296
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001297 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001298 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001299
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001300 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001301 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001302 trace_sched_stat_sleep(tsk, delta);
1303 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001304 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001305 if (se->statistics.block_start) {
1306 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001307
1308 if ((s64)delta < 0)
1309 delta = 0;
1310
Lucas De Marchi41acab82010-03-10 23:37:45 -03001311 if (unlikely(delta > se->statistics.block_max))
1312 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001313
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001314 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001315 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001316
Peter Zijlstrae4143142009-07-23 20:13:26 +02001317 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001318 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001319 se->statistics.iowait_sum += delta;
1320 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001321 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001322 }
1323
Andrew Vaginb781a602011-11-28 12:03:35 +03001324 trace_sched_stat_blocked(tsk, delta);
1325
Peter Zijlstrae4143142009-07-23 20:13:26 +02001326 /*
1327 * Blocking time is in units of nanosecs, so shift by
1328 * 20 to get a milliseconds-range estimation of the
1329 * amount of time that the task spent sleeping:
1330 */
1331 if (unlikely(prof_on == SLEEP_PROFILING)) {
1332 profile_hits(SLEEP_PROFILING,
1333 (void *)get_wchan(tsk),
1334 delta >> 20);
1335 }
1336 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001337 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001338 }
1339#endif
1340}
1341
Peter Zijlstraddc97292007-10-15 17:00:10 +02001342static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1343{
1344#ifdef CONFIG_SCHED_DEBUG
1345 s64 d = se->vruntime - cfs_rq->min_vruntime;
1346
1347 if (d < 0)
1348 d = -d;
1349
1350 if (d > 3*sysctl_sched_latency)
1351 schedstat_inc(cfs_rq, nr_spread_over);
1352#endif
1353}
1354
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001355static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001356place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1357{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001358 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001359
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001360 /*
1361 * The 'current' period is already promised to the current tasks,
1362 * however the extra weight of the new task will slow them down a
1363 * little, place the new task so that it fits in the slot that
1364 * stays open at the end.
1365 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001366 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001367 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001368
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001369 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001370 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001371 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001372
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001373 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001374 * Halve their sleep time's effect, to allow
1375 * for a gentler effect of sleepers:
1376 */
1377 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1378 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001379
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001380 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001381 }
1382
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001383 /* ensure we never gain time by being placed backwards. */
1384 vruntime = max_vruntime(se->vruntime, vruntime);
1385
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001386 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001387}
1388
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001389static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1390
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001391static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001392enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001393{
1394 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001395 * Update the normalized vruntime before updating min_vruntime
1396 * through callig update_curr().
1397 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001398 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001399 se->vruntime += cfs_rq->min_vruntime;
1400
1401 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001402 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001403 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001404 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001405 update_cfs_load(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02001406 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001407 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001408 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001409
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001410 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001411 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001412 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001413 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001414
Ingo Molnard2417e52007-08-09 11:16:47 +02001415 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001416 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001417 if (se != cfs_rq->curr)
1418 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001419 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001420
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001421 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001422 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001423 check_enqueue_throttle(cfs_rq);
1424 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001425}
1426
Rik van Riel2c13c9192011-02-01 09:48:37 -05001427static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001428{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001429 for_each_sched_entity(se) {
1430 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1431 if (cfs_rq->last == se)
1432 cfs_rq->last = NULL;
1433 else
1434 break;
1435 }
1436}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001437
Rik van Riel2c13c9192011-02-01 09:48:37 -05001438static void __clear_buddies_next(struct sched_entity *se)
1439{
1440 for_each_sched_entity(se) {
1441 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1442 if (cfs_rq->next == se)
1443 cfs_rq->next = NULL;
1444 else
1445 break;
1446 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001447}
1448
Rik van Rielac53db52011-02-01 09:51:03 -05001449static void __clear_buddies_skip(struct sched_entity *se)
1450{
1451 for_each_sched_entity(se) {
1452 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1453 if (cfs_rq->skip == se)
1454 cfs_rq->skip = NULL;
1455 else
1456 break;
1457 }
1458}
1459
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001460static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1461{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001462 if (cfs_rq->last == se)
1463 __clear_buddies_last(se);
1464
1465 if (cfs_rq->next == se)
1466 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001467
1468 if (cfs_rq->skip == se)
1469 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001470}
1471
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001472static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001473
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001474static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001475dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001476{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001477 /*
1478 * Update run-time statistics of the 'current'.
1479 */
1480 update_curr(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02001481 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001482
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001483 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001484 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001485#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001486 if (entity_is_task(se)) {
1487 struct task_struct *tsk = task_of(se);
1488
1489 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001490 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001491 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001492 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001493 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001494#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001495 }
1496
Peter Zijlstra2002c692008-11-11 11:52:33 +01001497 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001498
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001499 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001500 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001501 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001502 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001503 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001504
1505 /*
1506 * Normalize the entity after updating the min_vruntime because the
1507 * update can refer to the ->curr item and we need to reflect this
1508 * movement in our normalized position.
1509 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001510 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001511 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001512
Paul Turnerd8b49862011-07-21 09:43:41 -07001513 /* return excess runtime on last dequeue */
1514 return_cfs_rq_runtime(cfs_rq);
1515
Peter Zijlstra1e876232011-05-17 16:21:10 -07001516 update_min_vruntime(cfs_rq);
1517 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001518}
1519
1520/*
1521 * Preempt the current task with a newly woken task if needed:
1522 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001523static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001524check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001525{
Peter Zijlstra11697832007-09-05 14:32:49 +02001526 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001527 struct sched_entity *se;
1528 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001529
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001530 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001531 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001532 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001533 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001534 /*
1535 * The current task ran long enough, ensure it doesn't get
1536 * re-elected due to buddy favours.
1537 */
1538 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001539 return;
1540 }
1541
1542 /*
1543 * Ensure that a task that missed wakeup preemption by a
1544 * narrow margin doesn't have to wait for a full slice.
1545 * This also mitigates buddy induced latencies under load.
1546 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001547 if (delta_exec < sysctl_sched_min_granularity)
1548 return;
1549
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001550 se = __pick_first_entity(cfs_rq);
1551 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001552
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001553 if (delta < 0)
1554 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001555
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001556 if (delta > ideal_runtime)
1557 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001558}
1559
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001560static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001561set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001562{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001563 /* 'current' is not kept within the tree. */
1564 if (se->on_rq) {
1565 /*
1566 * Any task has to be enqueued before it get to execute on
1567 * a CPU. So account for the time it spent waiting on the
1568 * runqueue.
1569 */
1570 update_stats_wait_end(cfs_rq, se);
1571 __dequeue_entity(cfs_rq, se);
1572 }
1573
Ingo Molnar79303e92007-08-09 11:16:47 +02001574 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001575 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001576#ifdef CONFIG_SCHEDSTATS
1577 /*
1578 * Track our maximum slice length, if the CPU's load is at
1579 * least twice that of our own weight (i.e. dont track it
1580 * when there are only lesser-weight tasks around):
1581 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001582 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001583 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001584 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1585 }
1586#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001587 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001588}
1589
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001590static int
1591wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1592
Rik van Rielac53db52011-02-01 09:51:03 -05001593/*
1594 * Pick the next process, keeping these things in mind, in this order:
1595 * 1) keep things fair between processes/task groups
1596 * 2) pick the "next" process, since someone really wants that to run
1597 * 3) pick the "last" process, for cache locality
1598 * 4) do not run the "skip" process, if something else is available
1599 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001600static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001601{
Rik van Rielac53db52011-02-01 09:51:03 -05001602 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001603 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001604
Rik van Rielac53db52011-02-01 09:51:03 -05001605 /*
1606 * Avoid running the skip buddy, if running something else can
1607 * be done without getting too unfair.
1608 */
1609 if (cfs_rq->skip == se) {
1610 struct sched_entity *second = __pick_next_entity(se);
1611 if (second && wakeup_preempt_entity(second, left) < 1)
1612 se = second;
1613 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001614
Mike Galbraithf685cea2009-10-23 23:09:22 +02001615 /*
1616 * Prefer last buddy, try to return the CPU to a preempted task.
1617 */
1618 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1619 se = cfs_rq->last;
1620
Rik van Rielac53db52011-02-01 09:51:03 -05001621 /*
1622 * Someone really wants this to run. If it's not unfair, run it.
1623 */
1624 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1625 se = cfs_rq->next;
1626
Mike Galbraithf685cea2009-10-23 23:09:22 +02001627 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001628
1629 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001630}
1631
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001632static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1633
Ingo Molnarab6cde22007-08-09 11:16:48 +02001634static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001635{
1636 /*
1637 * If still on the runqueue then deactivate_task()
1638 * was not called and update_curr() has to be done:
1639 */
1640 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001641 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001642
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001643 /* throttle cfs_rqs exceeding runtime */
1644 check_cfs_rq_runtime(cfs_rq);
1645
Peter Zijlstraddc97292007-10-15 17:00:10 +02001646 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001647 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001648 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001649 /* Put 'current' back into the tree. */
1650 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001651 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001652 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001653 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001654 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001655}
1656
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001657static void
1658entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001659{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001660 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001661 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001662 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001663 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001664
Paul Turner43365bd2010-12-15 19:10:17 -08001665 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001666 * Ensure that runnable average is periodically updated.
1667 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001668 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001669 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001670
1671 /*
Paul Turner43365bd2010-12-15 19:10:17 -08001672 * Update share accounting for long-running entities.
1673 */
1674 update_entity_shares_tick(cfs_rq);
1675
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001676#ifdef CONFIG_SCHED_HRTICK
1677 /*
1678 * queued ticks are scheduled to match the slice, so don't bother
1679 * validating it and just reschedule.
1680 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001681 if (queued) {
1682 resched_task(rq_of(cfs_rq)->curr);
1683 return;
1684 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001685 /*
1686 * don't let the period tick interfere with the hrtick preemption
1687 */
1688 if (!sched_feat(DOUBLE_TICK) &&
1689 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1690 return;
1691#endif
1692
Yong Zhang2c2efae2011-07-29 16:20:33 +08001693 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001694 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001695}
1696
Paul Turnerab84d312011-07-21 09:43:28 -07001697
1698/**************************************************
1699 * CFS bandwidth control machinery
1700 */
1701
1702#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001703
1704#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001705static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001706
1707static inline bool cfs_bandwidth_used(void)
1708{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001709 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001710}
1711
1712void account_cfs_bandwidth_used(int enabled, int was_enabled)
1713{
1714 /* only need to count groups transitioning between enabled/!enabled */
1715 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001716 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001717 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001718 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001719}
1720#else /* HAVE_JUMP_LABEL */
1721static bool cfs_bandwidth_used(void)
1722{
1723 return true;
1724}
1725
1726void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1727#endif /* HAVE_JUMP_LABEL */
1728
Paul Turnerab84d312011-07-21 09:43:28 -07001729/*
1730 * default period for cfs group bandwidth.
1731 * default: 0.1s, units: nanoseconds
1732 */
1733static inline u64 default_cfs_period(void)
1734{
1735 return 100000000ULL;
1736}
Paul Turnerec12cb72011-07-21 09:43:30 -07001737
1738static inline u64 sched_cfs_bandwidth_slice(void)
1739{
1740 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1741}
1742
Paul Turnera9cf55b2011-07-21 09:43:32 -07001743/*
1744 * Replenish runtime according to assigned quota and update expiration time.
1745 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1746 * additional synchronization around rq->lock.
1747 *
1748 * requires cfs_b->lock
1749 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001750void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001751{
1752 u64 now;
1753
1754 if (cfs_b->quota == RUNTIME_INF)
1755 return;
1756
1757 now = sched_clock_cpu(smp_processor_id());
1758 cfs_b->runtime = cfs_b->quota;
1759 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1760}
1761
Peter Zijlstra029632f2011-10-25 10:00:11 +02001762static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1763{
1764 return &tg->cfs_bandwidth;
1765}
1766
Paul Turner85dac902011-07-21 09:43:33 -07001767/* returns 0 on failure to allocate runtime */
1768static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001769{
1770 struct task_group *tg = cfs_rq->tg;
1771 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001772 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001773
1774 /* note: this is a positive sum as runtime_remaining <= 0 */
1775 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1776
1777 raw_spin_lock(&cfs_b->lock);
1778 if (cfs_b->quota == RUNTIME_INF)
1779 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001780 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001781 /*
1782 * If the bandwidth pool has become inactive, then at least one
1783 * period must have elapsed since the last consumption.
1784 * Refresh the global state and ensure bandwidth timer becomes
1785 * active.
1786 */
1787 if (!cfs_b->timer_active) {
1788 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001789 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001790 }
Paul Turner58088ad2011-07-21 09:43:31 -07001791
1792 if (cfs_b->runtime > 0) {
1793 amount = min(cfs_b->runtime, min_amount);
1794 cfs_b->runtime -= amount;
1795 cfs_b->idle = 0;
1796 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001797 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001798 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001799 raw_spin_unlock(&cfs_b->lock);
1800
1801 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001802 /*
1803 * we may have advanced our local expiration to account for allowed
1804 * spread between our sched_clock and the one on which runtime was
1805 * issued.
1806 */
1807 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1808 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001809
1810 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001811}
1812
1813/*
1814 * Note: This depends on the synchronization provided by sched_clock and the
1815 * fact that rq->clock snapshots this value.
1816 */
1817static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1818{
1819 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1820 struct rq *rq = rq_of(cfs_rq);
1821
1822 /* if the deadline is ahead of our clock, nothing to do */
1823 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1824 return;
1825
1826 if (cfs_rq->runtime_remaining < 0)
1827 return;
1828
1829 /*
1830 * If the local deadline has passed we have to consider the
1831 * possibility that our sched_clock is 'fast' and the global deadline
1832 * has not truly expired.
1833 *
1834 * Fortunately we can check determine whether this the case by checking
1835 * whether the global deadline has advanced.
1836 */
1837
1838 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1839 /* extend local deadline, drift is bounded above by 2 ticks */
1840 cfs_rq->runtime_expires += TICK_NSEC;
1841 } else {
1842 /* global deadline is ahead, expiration has passed */
1843 cfs_rq->runtime_remaining = 0;
1844 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001845}
1846
1847static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1848 unsigned long delta_exec)
1849{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001850 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001851 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001852 expire_cfs_rq_runtime(cfs_rq);
1853
1854 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001855 return;
1856
Paul Turner85dac902011-07-21 09:43:33 -07001857 /*
1858 * if we're unable to extend our runtime we resched so that the active
1859 * hierarchy can be throttled
1860 */
1861 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1862 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001863}
1864
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001865static __always_inline
1866void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001867{
Paul Turner56f570e2011-11-07 20:26:33 -08001868 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001869 return;
1870
1871 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1872}
1873
Paul Turner85dac902011-07-21 09:43:33 -07001874static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1875{
Paul Turner56f570e2011-11-07 20:26:33 -08001876 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001877}
1878
Paul Turner64660c82011-07-21 09:43:36 -07001879/* check whether cfs_rq, or any parent, is throttled */
1880static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1881{
Paul Turner56f570e2011-11-07 20:26:33 -08001882 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001883}
1884
1885/*
1886 * Ensure that neither of the group entities corresponding to src_cpu or
1887 * dest_cpu are members of a throttled hierarchy when performing group
1888 * load-balance operations.
1889 */
1890static inline int throttled_lb_pair(struct task_group *tg,
1891 int src_cpu, int dest_cpu)
1892{
1893 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1894
1895 src_cfs_rq = tg->cfs_rq[src_cpu];
1896 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1897
1898 return throttled_hierarchy(src_cfs_rq) ||
1899 throttled_hierarchy(dest_cfs_rq);
1900}
1901
1902/* updated child weight may affect parent so we have to do this bottom up */
1903static int tg_unthrottle_up(struct task_group *tg, void *data)
1904{
1905 struct rq *rq = data;
1906 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1907
1908 cfs_rq->throttle_count--;
1909#ifdef CONFIG_SMP
1910 if (!cfs_rq->throttle_count) {
1911 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1912
1913 /* leaving throttled state, advance shares averaging windows */
1914 cfs_rq->load_stamp += delta;
1915 cfs_rq->load_last += delta;
1916
1917 /* update entity weight now that we are on_rq again */
1918 update_cfs_shares(cfs_rq);
1919 }
1920#endif
1921
1922 return 0;
1923}
1924
1925static int tg_throttle_down(struct task_group *tg, void *data)
1926{
1927 struct rq *rq = data;
1928 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1929
1930 /* group is entering throttled state, record last load */
1931 if (!cfs_rq->throttle_count)
1932 update_cfs_load(cfs_rq, 0);
1933 cfs_rq->throttle_count++;
1934
1935 return 0;
1936}
1937
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001938static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001939{
1940 struct rq *rq = rq_of(cfs_rq);
1941 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1942 struct sched_entity *se;
1943 long task_delta, dequeue = 1;
1944
1945 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1946
1947 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001948 rcu_read_lock();
1949 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1950 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001951
1952 task_delta = cfs_rq->h_nr_running;
1953 for_each_sched_entity(se) {
1954 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1955 /* throttled entity or throttle-on-deactivate */
1956 if (!se->on_rq)
1957 break;
1958
1959 if (dequeue)
1960 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1961 qcfs_rq->h_nr_running -= task_delta;
1962
1963 if (qcfs_rq->load.weight)
1964 dequeue = 0;
1965 }
1966
1967 if (!se)
1968 rq->nr_running -= task_delta;
1969
1970 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001971 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001972 raw_spin_lock(&cfs_b->lock);
1973 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1974 raw_spin_unlock(&cfs_b->lock);
1975}
1976
Peter Zijlstra029632f2011-10-25 10:00:11 +02001977void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001978{
1979 struct rq *rq = rq_of(cfs_rq);
1980 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1981 struct sched_entity *se;
1982 int enqueue = 1;
1983 long task_delta;
1984
1985 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1986
1987 cfs_rq->throttled = 0;
1988 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001989 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001990 list_del_rcu(&cfs_rq->throttled_list);
1991 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001992 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001993
Paul Turner64660c82011-07-21 09:43:36 -07001994 update_rq_clock(rq);
1995 /* update hierarchical throttle state */
1996 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1997
Paul Turner671fd9d2011-07-21 09:43:34 -07001998 if (!cfs_rq->load.weight)
1999 return;
2000
2001 task_delta = cfs_rq->h_nr_running;
2002 for_each_sched_entity(se) {
2003 if (se->on_rq)
2004 enqueue = 0;
2005
2006 cfs_rq = cfs_rq_of(se);
2007 if (enqueue)
2008 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2009 cfs_rq->h_nr_running += task_delta;
2010
2011 if (cfs_rq_throttled(cfs_rq))
2012 break;
2013 }
2014
2015 if (!se)
2016 rq->nr_running += task_delta;
2017
2018 /* determine whether we need to wake up potentially idle cpu */
2019 if (rq->curr == rq->idle && rq->cfs.nr_running)
2020 resched_task(rq->curr);
2021}
2022
2023static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2024 u64 remaining, u64 expires)
2025{
2026 struct cfs_rq *cfs_rq;
2027 u64 runtime = remaining;
2028
2029 rcu_read_lock();
2030 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2031 throttled_list) {
2032 struct rq *rq = rq_of(cfs_rq);
2033
2034 raw_spin_lock(&rq->lock);
2035 if (!cfs_rq_throttled(cfs_rq))
2036 goto next;
2037
2038 runtime = -cfs_rq->runtime_remaining + 1;
2039 if (runtime > remaining)
2040 runtime = remaining;
2041 remaining -= runtime;
2042
2043 cfs_rq->runtime_remaining += runtime;
2044 cfs_rq->runtime_expires = expires;
2045
2046 /* we check whether we're throttled above */
2047 if (cfs_rq->runtime_remaining > 0)
2048 unthrottle_cfs_rq(cfs_rq);
2049
2050next:
2051 raw_spin_unlock(&rq->lock);
2052
2053 if (!remaining)
2054 break;
2055 }
2056 rcu_read_unlock();
2057
2058 return remaining;
2059}
2060
Paul Turner58088ad2011-07-21 09:43:31 -07002061/*
2062 * Responsible for refilling a task_group's bandwidth and unthrottling its
2063 * cfs_rqs as appropriate. If there has been no activity within the last
2064 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2065 * used to track this state.
2066 */
2067static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2068{
Paul Turner671fd9d2011-07-21 09:43:34 -07002069 u64 runtime, runtime_expires;
2070 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002071
2072 raw_spin_lock(&cfs_b->lock);
2073 /* no need to continue the timer with no bandwidth constraint */
2074 if (cfs_b->quota == RUNTIME_INF)
2075 goto out_unlock;
2076
Paul Turner671fd9d2011-07-21 09:43:34 -07002077 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2078 /* idle depends on !throttled (for the case of a large deficit) */
2079 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002080 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002081
Paul Turnera9cf55b2011-07-21 09:43:32 -07002082 /* if we're going inactive then everything else can be deferred */
2083 if (idle)
2084 goto out_unlock;
2085
2086 __refill_cfs_bandwidth_runtime(cfs_b);
2087
Paul Turner671fd9d2011-07-21 09:43:34 -07002088 if (!throttled) {
2089 /* mark as potentially idle for the upcoming period */
2090 cfs_b->idle = 1;
2091 goto out_unlock;
2092 }
Paul Turner58088ad2011-07-21 09:43:31 -07002093
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002094 /* account preceding periods in which throttling occurred */
2095 cfs_b->nr_throttled += overrun;
2096
Paul Turner671fd9d2011-07-21 09:43:34 -07002097 /*
2098 * There are throttled entities so we must first use the new bandwidth
2099 * to unthrottle them before making it generally available. This
2100 * ensures that all existing debts will be paid before a new cfs_rq is
2101 * allowed to run.
2102 */
2103 runtime = cfs_b->runtime;
2104 runtime_expires = cfs_b->runtime_expires;
2105 cfs_b->runtime = 0;
2106
2107 /*
2108 * This check is repeated as we are holding onto the new bandwidth
2109 * while we unthrottle. This can potentially race with an unthrottled
2110 * group trying to acquire new bandwidth from the global pool.
2111 */
2112 while (throttled && runtime > 0) {
2113 raw_spin_unlock(&cfs_b->lock);
2114 /* we can't nest cfs_b->lock while distributing bandwidth */
2115 runtime = distribute_cfs_runtime(cfs_b, runtime,
2116 runtime_expires);
2117 raw_spin_lock(&cfs_b->lock);
2118
2119 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2120 }
2121
2122 /* return (any) remaining runtime */
2123 cfs_b->runtime = runtime;
2124 /*
2125 * While we are ensured activity in the period following an
2126 * unthrottle, this also covers the case in which the new bandwidth is
2127 * insufficient to cover the existing bandwidth deficit. (Forcing the
2128 * timer to remain active while there are any throttled entities.)
2129 */
2130 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002131out_unlock:
2132 if (idle)
2133 cfs_b->timer_active = 0;
2134 raw_spin_unlock(&cfs_b->lock);
2135
2136 return idle;
2137}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002138
Paul Turnerd8b49862011-07-21 09:43:41 -07002139/* a cfs_rq won't donate quota below this amount */
2140static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2141/* minimum remaining period time to redistribute slack quota */
2142static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2143/* how long we wait to gather additional slack before distributing */
2144static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2145
2146/* are we near the end of the current quota period? */
2147static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2148{
2149 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2150 u64 remaining;
2151
2152 /* if the call-back is running a quota refresh is already occurring */
2153 if (hrtimer_callback_running(refresh_timer))
2154 return 1;
2155
2156 /* is a quota refresh about to occur? */
2157 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2158 if (remaining < min_expire)
2159 return 1;
2160
2161 return 0;
2162}
2163
2164static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2165{
2166 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2167
2168 /* if there's a quota refresh soon don't bother with slack */
2169 if (runtime_refresh_within(cfs_b, min_left))
2170 return;
2171
2172 start_bandwidth_timer(&cfs_b->slack_timer,
2173 ns_to_ktime(cfs_bandwidth_slack_period));
2174}
2175
2176/* we know any runtime found here is valid as update_curr() precedes return */
2177static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2178{
2179 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2180 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2181
2182 if (slack_runtime <= 0)
2183 return;
2184
2185 raw_spin_lock(&cfs_b->lock);
2186 if (cfs_b->quota != RUNTIME_INF &&
2187 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2188 cfs_b->runtime += slack_runtime;
2189
2190 /* we are under rq->lock, defer unthrottling using a timer */
2191 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2192 !list_empty(&cfs_b->throttled_cfs_rq))
2193 start_cfs_slack_bandwidth(cfs_b);
2194 }
2195 raw_spin_unlock(&cfs_b->lock);
2196
2197 /* even if it's not valid for return we don't want to try again */
2198 cfs_rq->runtime_remaining -= slack_runtime;
2199}
2200
2201static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2202{
Paul Turner56f570e2011-11-07 20:26:33 -08002203 if (!cfs_bandwidth_used())
2204 return;
2205
Paul Turnerfccfdc62011-11-07 20:26:34 -08002206 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002207 return;
2208
2209 __return_cfs_rq_runtime(cfs_rq);
2210}
2211
2212/*
2213 * This is done with a timer (instead of inline with bandwidth return) since
2214 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2215 */
2216static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2217{
2218 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2219 u64 expires;
2220
2221 /* confirm we're still not at a refresh boundary */
2222 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2223 return;
2224
2225 raw_spin_lock(&cfs_b->lock);
2226 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2227 runtime = cfs_b->runtime;
2228 cfs_b->runtime = 0;
2229 }
2230 expires = cfs_b->runtime_expires;
2231 raw_spin_unlock(&cfs_b->lock);
2232
2233 if (!runtime)
2234 return;
2235
2236 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2237
2238 raw_spin_lock(&cfs_b->lock);
2239 if (expires == cfs_b->runtime_expires)
2240 cfs_b->runtime = runtime;
2241 raw_spin_unlock(&cfs_b->lock);
2242}
2243
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002244/*
2245 * When a group wakes up we want to make sure that its quota is not already
2246 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2247 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2248 */
2249static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2250{
Paul Turner56f570e2011-11-07 20:26:33 -08002251 if (!cfs_bandwidth_used())
2252 return;
2253
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002254 /* an active group must be handled by the update_curr()->put() path */
2255 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2256 return;
2257
2258 /* ensure the group is not already throttled */
2259 if (cfs_rq_throttled(cfs_rq))
2260 return;
2261
2262 /* update runtime allocation */
2263 account_cfs_rq_runtime(cfs_rq, 0);
2264 if (cfs_rq->runtime_remaining <= 0)
2265 throttle_cfs_rq(cfs_rq);
2266}
2267
2268/* conditionally throttle active cfs_rq's from put_prev_entity() */
2269static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2270{
Paul Turner56f570e2011-11-07 20:26:33 -08002271 if (!cfs_bandwidth_used())
2272 return;
2273
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002274 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2275 return;
2276
2277 /*
2278 * it's possible for a throttled entity to be forced into a running
2279 * state (e.g. set_curr_task), in this case we're finished.
2280 */
2281 if (cfs_rq_throttled(cfs_rq))
2282 return;
2283
2284 throttle_cfs_rq(cfs_rq);
2285}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002286
2287static inline u64 default_cfs_period(void);
2288static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2289static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2290
2291static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2292{
2293 struct cfs_bandwidth *cfs_b =
2294 container_of(timer, struct cfs_bandwidth, slack_timer);
2295 do_sched_cfs_slack_timer(cfs_b);
2296
2297 return HRTIMER_NORESTART;
2298}
2299
2300static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2301{
2302 struct cfs_bandwidth *cfs_b =
2303 container_of(timer, struct cfs_bandwidth, period_timer);
2304 ktime_t now;
2305 int overrun;
2306 int idle = 0;
2307
2308 for (;;) {
2309 now = hrtimer_cb_get_time(timer);
2310 overrun = hrtimer_forward(timer, now, cfs_b->period);
2311
2312 if (!overrun)
2313 break;
2314
2315 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2316 }
2317
2318 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2319}
2320
2321void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2322{
2323 raw_spin_lock_init(&cfs_b->lock);
2324 cfs_b->runtime = 0;
2325 cfs_b->quota = RUNTIME_INF;
2326 cfs_b->period = ns_to_ktime(default_cfs_period());
2327
2328 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2329 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2330 cfs_b->period_timer.function = sched_cfs_period_timer;
2331 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2332 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2333}
2334
2335static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2336{
2337 cfs_rq->runtime_enabled = 0;
2338 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2339}
2340
2341/* requires cfs_b->lock, may release to reprogram timer */
2342void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2343{
2344 /*
2345 * The timer may be active because we're trying to set a new bandwidth
2346 * period or because we're racing with the tear-down path
2347 * (timer_active==0 becomes visible before the hrtimer call-back
2348 * terminates). In either case we ensure that it's re-programmed
2349 */
2350 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2351 raw_spin_unlock(&cfs_b->lock);
2352 /* ensure cfs_b->lock is available while we wait */
2353 hrtimer_cancel(&cfs_b->period_timer);
2354
2355 raw_spin_lock(&cfs_b->lock);
2356 /* if someone else restarted the timer then we're done */
2357 if (cfs_b->timer_active)
2358 return;
2359 }
2360
2361 cfs_b->timer_active = 1;
2362 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2363}
2364
2365static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2366{
2367 hrtimer_cancel(&cfs_b->period_timer);
2368 hrtimer_cancel(&cfs_b->slack_timer);
2369}
2370
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002371static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002372{
2373 struct cfs_rq *cfs_rq;
2374
2375 for_each_leaf_cfs_rq(rq, cfs_rq) {
2376 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2377
2378 if (!cfs_rq->runtime_enabled)
2379 continue;
2380
2381 /*
2382 * clock_task is not advancing so we just need to make sure
2383 * there's some valid quota amount
2384 */
2385 cfs_rq->runtime_remaining = cfs_b->quota;
2386 if (cfs_rq_throttled(cfs_rq))
2387 unthrottle_cfs_rq(cfs_rq);
2388 }
2389}
2390
2391#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002392static __always_inline
2393void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002394static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2395static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002396static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002397
2398static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2399{
2400 return 0;
2401}
Paul Turner64660c82011-07-21 09:43:36 -07002402
2403static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2404{
2405 return 0;
2406}
2407
2408static inline int throttled_lb_pair(struct task_group *tg,
2409 int src_cpu, int dest_cpu)
2410{
2411 return 0;
2412}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002413
2414void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2415
2416#ifdef CONFIG_FAIR_GROUP_SCHED
2417static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002418#endif
2419
Peter Zijlstra029632f2011-10-25 10:00:11 +02002420static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2421{
2422 return NULL;
2423}
2424static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002425static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002426
2427#endif /* CONFIG_CFS_BANDWIDTH */
2428
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002429/**************************************************
2430 * CFS operations on tasks:
2431 */
2432
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002433#ifdef CONFIG_SCHED_HRTICK
2434static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2435{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002436 struct sched_entity *se = &p->se;
2437 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2438
2439 WARN_ON(task_rq(p) != rq);
2440
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002441 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002442 u64 slice = sched_slice(cfs_rq, se);
2443 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2444 s64 delta = slice - ran;
2445
2446 if (delta < 0) {
2447 if (rq->curr == p)
2448 resched_task(p);
2449 return;
2450 }
2451
2452 /*
2453 * Don't schedule slices shorter than 10000ns, that just
2454 * doesn't make sense. Rely on vruntime for fairness.
2455 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002456 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002457 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002458
Peter Zijlstra31656512008-07-18 18:01:23 +02002459 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002460 }
2461}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002462
2463/*
2464 * called from enqueue/dequeue and updates the hrtick when the
2465 * current task is from our class and nr_running is low enough
2466 * to matter.
2467 */
2468static void hrtick_update(struct rq *rq)
2469{
2470 struct task_struct *curr = rq->curr;
2471
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002472 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002473 return;
2474
2475 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2476 hrtick_start_fair(rq, curr);
2477}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302478#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002479static inline void
2480hrtick_start_fair(struct rq *rq, struct task_struct *p)
2481{
2482}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002483
2484static inline void hrtick_update(struct rq *rq)
2485{
2486}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002487#endif
2488
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002489/*
2490 * The enqueue_task method is called before nr_running is
2491 * increased. Here we update the fair scheduling stats and
2492 * then put the task into the rbtree:
2493 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002494static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002495enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002496{
2497 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002498 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002499
2500 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002501 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002502 break;
2503 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002504 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002505
2506 /*
2507 * end evaluation on encountering a throttled cfs_rq
2508 *
2509 * note: in the case of encountering a throttled cfs_rq we will
2510 * post the final h_nr_running increment below.
2511 */
2512 if (cfs_rq_throttled(cfs_rq))
2513 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002514 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002515
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002516 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002517 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002518
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002519 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002520 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002521 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002522
Paul Turner85dac902011-07-21 09:43:33 -07002523 if (cfs_rq_throttled(cfs_rq))
2524 break;
2525
Paul Turnerd6b55912010-11-15 15:47:09 -08002526 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002527 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002528 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002529 }
2530
Ben Segall18bf2802012-10-04 12:51:20 +02002531 if (!se) {
2532 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002533 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002534 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002535 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002536}
2537
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002538static void set_next_buddy(struct sched_entity *se);
2539
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002540/*
2541 * The dequeue_task method is called before nr_running is
2542 * decreased. We remove the task from the rbtree and
2543 * update the fair scheduling stats:
2544 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002545static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002546{
2547 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002548 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002549 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002550
2551 for_each_sched_entity(se) {
2552 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002553 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002554
2555 /*
2556 * end evaluation on encountering a throttled cfs_rq
2557 *
2558 * note: in the case of encountering a throttled cfs_rq we will
2559 * post the final h_nr_running decrement below.
2560 */
2561 if (cfs_rq_throttled(cfs_rq))
2562 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002563 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002564
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002565 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002566 if (cfs_rq->load.weight) {
2567 /*
2568 * Bias pick_next to pick a task from this cfs_rq, as
2569 * p is sleeping when it is within its sched_slice.
2570 */
2571 if (task_sleep && parent_entity(se))
2572 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002573
2574 /* avoid re-evaluating load for this entity */
2575 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002576 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002577 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002578 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002579 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002580
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002581 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002582 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002583 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002584
Paul Turner85dac902011-07-21 09:43:33 -07002585 if (cfs_rq_throttled(cfs_rq))
2586 break;
2587
Paul Turnerd6b55912010-11-15 15:47:09 -08002588 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002589 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002590 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002591 }
2592
Ben Segall18bf2802012-10-04 12:51:20 +02002593 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002594 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002595 update_rq_runnable_avg(rq, 1);
2596 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002597 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002598}
2599
Gregory Haskinse7693a32008-01-25 21:08:09 +01002600#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002601/* Used instead of source_load when we know the type == 0 */
2602static unsigned long weighted_cpuload(const int cpu)
2603{
2604 return cpu_rq(cpu)->load.weight;
2605}
2606
2607/*
2608 * Return a low guess at the load of a migration-source cpu weighted
2609 * according to the scheduling class and "nice" value.
2610 *
2611 * We want to under-estimate the load of migration sources, to
2612 * balance conservatively.
2613 */
2614static unsigned long source_load(int cpu, int type)
2615{
2616 struct rq *rq = cpu_rq(cpu);
2617 unsigned long total = weighted_cpuload(cpu);
2618
2619 if (type == 0 || !sched_feat(LB_BIAS))
2620 return total;
2621
2622 return min(rq->cpu_load[type-1], total);
2623}
2624
2625/*
2626 * Return a high guess at the load of a migration-target cpu weighted
2627 * according to the scheduling class and "nice" value.
2628 */
2629static unsigned long target_load(int cpu, int type)
2630{
2631 struct rq *rq = cpu_rq(cpu);
2632 unsigned long total = weighted_cpuload(cpu);
2633
2634 if (type == 0 || !sched_feat(LB_BIAS))
2635 return total;
2636
2637 return max(rq->cpu_load[type-1], total);
2638}
2639
2640static unsigned long power_of(int cpu)
2641{
2642 return cpu_rq(cpu)->cpu_power;
2643}
2644
2645static unsigned long cpu_avg_load_per_task(int cpu)
2646{
2647 struct rq *rq = cpu_rq(cpu);
2648 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2649
2650 if (nr_running)
2651 return rq->load.weight / nr_running;
2652
2653 return 0;
2654}
2655
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002656
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002657static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002658{
2659 struct sched_entity *se = &p->se;
2660 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002661 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002662
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002663#ifndef CONFIG_64BIT
2664 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002665
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002666 do {
2667 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2668 smp_rmb();
2669 min_vruntime = cfs_rq->min_vruntime;
2670 } while (min_vruntime != min_vruntime_copy);
2671#else
2672 min_vruntime = cfs_rq->min_vruntime;
2673#endif
2674
2675 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002676}
2677
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002678#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002679/*
2680 * effective_load() calculates the load change as seen from the root_task_group
2681 *
2682 * Adding load to a group doesn't make a group heavier, but can cause movement
2683 * of group shares between cpus. Assuming the shares were perfectly aligned one
2684 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002685 *
2686 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2687 * on this @cpu and results in a total addition (subtraction) of @wg to the
2688 * total group weight.
2689 *
2690 * Given a runqueue weight distribution (rw_i) we can compute a shares
2691 * distribution (s_i) using:
2692 *
2693 * s_i = rw_i / \Sum rw_j (1)
2694 *
2695 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2696 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2697 * shares distribution (s_i):
2698 *
2699 * rw_i = { 2, 4, 1, 0 }
2700 * s_i = { 2/7, 4/7, 1/7, 0 }
2701 *
2702 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2703 * task used to run on and the CPU the waker is running on), we need to
2704 * compute the effect of waking a task on either CPU and, in case of a sync
2705 * wakeup, compute the effect of the current task going to sleep.
2706 *
2707 * So for a change of @wl to the local @cpu with an overall group weight change
2708 * of @wl we can compute the new shares distribution (s'_i) using:
2709 *
2710 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2711 *
2712 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2713 * differences in waking a task to CPU 0. The additional task changes the
2714 * weight and shares distributions like:
2715 *
2716 * rw'_i = { 3, 4, 1, 0 }
2717 * s'_i = { 3/8, 4/8, 1/8, 0 }
2718 *
2719 * We can then compute the difference in effective weight by using:
2720 *
2721 * dw_i = S * (s'_i - s_i) (3)
2722 *
2723 * Where 'S' is the group weight as seen by its parent.
2724 *
2725 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2726 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2727 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002728 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002729static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002730{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002731 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002732
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002733 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002734 return wl;
2735
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002736 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002737 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002738
Paul Turner977dda72011-01-14 17:57:50 -08002739 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002740
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002741 /*
2742 * W = @wg + \Sum rw_j
2743 */
2744 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002745
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002746 /*
2747 * w = rw_i + @wl
2748 */
2749 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002750
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002751 /*
2752 * wl = S * s'_i; see (2)
2753 */
2754 if (W > 0 && w < W)
2755 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002756 else
2757 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002758
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002759 /*
2760 * Per the above, wl is the new se->load.weight value; since
2761 * those are clipped to [MIN_SHARES, ...) do so now. See
2762 * calc_cfs_shares().
2763 */
Paul Turner977dda72011-01-14 17:57:50 -08002764 if (wl < MIN_SHARES)
2765 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002766
2767 /*
2768 * wl = dw_i = S * (s'_i - s_i); see (3)
2769 */
Paul Turner977dda72011-01-14 17:57:50 -08002770 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002771
2772 /*
2773 * Recursively apply this logic to all parent groups to compute
2774 * the final effective load change on the root group. Since
2775 * only the @tg group gets extra weight, all parent groups can
2776 * only redistribute existing shares. @wl is the shift in shares
2777 * resulting from this level per the above.
2778 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002779 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002780 }
2781
2782 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002783}
2784#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002785
Peter Zijlstra83378262008-06-27 13:41:37 +02002786static inline unsigned long effective_load(struct task_group *tg, int cpu,
2787 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002788{
Peter Zijlstra83378262008-06-27 13:41:37 +02002789 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002790}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002791
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002792#endif
2793
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002794static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002795{
Paul Turnere37b6a72011-01-21 20:44:59 -08002796 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002797 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002798 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002799 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002800 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002801 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002802
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002803 idx = sd->wake_idx;
2804 this_cpu = smp_processor_id();
2805 prev_cpu = task_cpu(p);
2806 load = source_load(prev_cpu, idx);
2807 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002808
2809 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002810 * If sync wakeup then subtract the (maximum possible)
2811 * effect of the currently running task from the load
2812 * of the current CPU:
2813 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002814 if (sync) {
2815 tg = task_group(current);
2816 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002817
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002818 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002819 load += effective_load(tg, prev_cpu, 0, -weight);
2820 }
2821
2822 tg = task_group(p);
2823 weight = p->se.load.weight;
2824
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002825 /*
2826 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002827 * due to the sync cause above having dropped this_load to 0, we'll
2828 * always have an imbalance, but there's really nothing you can do
2829 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002830 *
2831 * Otherwise check if either cpus are near enough in load to allow this
2832 * task to be woken on this_cpu.
2833 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002834 if (this_load > 0) {
2835 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002836
2837 this_eff_load = 100;
2838 this_eff_load *= power_of(prev_cpu);
2839 this_eff_load *= this_load +
2840 effective_load(tg, this_cpu, weight, weight);
2841
2842 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2843 prev_eff_load *= power_of(this_cpu);
2844 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2845
2846 balanced = this_eff_load <= prev_eff_load;
2847 } else
2848 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002849
2850 /*
2851 * If the currently running task will sleep within
2852 * a reasonable amount of time then attract this newly
2853 * woken task:
2854 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002855 if (sync && balanced)
2856 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002857
Lucas De Marchi41acab82010-03-10 23:37:45 -03002858 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002859 tl_per_task = cpu_avg_load_per_task(this_cpu);
2860
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002861 if (balanced ||
2862 (this_load <= load &&
2863 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002864 /*
2865 * This domain has SD_WAKE_AFFINE and
2866 * p is cache cold in this domain, and
2867 * there is no bad imbalance.
2868 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002869 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002870 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002871
2872 return 1;
2873 }
2874 return 0;
2875}
2876
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002877/*
2878 * find_idlest_group finds and returns the least busy CPU group within the
2879 * domain.
2880 */
2881static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002882find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002883 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002884{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002885 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002886 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002887 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002888
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002889 do {
2890 unsigned long load, avg_load;
2891 int local_group;
2892 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002893
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002894 /* Skip over this group if it has no CPUs allowed */
2895 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002896 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002897 continue;
2898
2899 local_group = cpumask_test_cpu(this_cpu,
2900 sched_group_cpus(group));
2901
2902 /* Tally up the load of all CPUs in the group */
2903 avg_load = 0;
2904
2905 for_each_cpu(i, sched_group_cpus(group)) {
2906 /* Bias balancing toward cpus of our domain */
2907 if (local_group)
2908 load = source_load(i, load_idx);
2909 else
2910 load = target_load(i, load_idx);
2911
2912 avg_load += load;
2913 }
2914
2915 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002916 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002917
2918 if (local_group) {
2919 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002920 } else if (avg_load < min_load) {
2921 min_load = avg_load;
2922 idlest = group;
2923 }
2924 } while (group = group->next, group != sd->groups);
2925
2926 if (!idlest || 100*this_load < imbalance*min_load)
2927 return NULL;
2928 return idlest;
2929}
2930
2931/*
2932 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2933 */
2934static int
2935find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2936{
2937 unsigned long load, min_load = ULONG_MAX;
2938 int idlest = -1;
2939 int i;
2940
2941 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002942 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002943 load = weighted_cpuload(i);
2944
2945 if (load < min_load || (load == min_load && i == this_cpu)) {
2946 min_load = load;
2947 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002948 }
2949 }
2950
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002951 return idlest;
2952}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002953
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002954/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002955 * Try and locate an idle CPU in the sched_domain.
2956 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002957static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002958{
2959 int cpu = smp_processor_id();
2960 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002961 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07002962 struct sched_group *sg;
2963 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002964
2965 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002966 * If the task is going to be woken-up on this cpu and if it is
2967 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002968 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002969 if (target == cpu && idle_cpu(cpu))
2970 return cpu;
2971
2972 /*
2973 * If the task is going to be woken-up on the cpu where it previously
2974 * ran and if it is currently idle, then it the right target.
2975 */
2976 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002977 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002978
2979 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07002980 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002981 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002982 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002983 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07002984 sg = sd->groups;
2985 do {
2986 if (!cpumask_intersects(sched_group_cpus(sg),
2987 tsk_cpus_allowed(p)))
2988 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02002989
Linus Torvalds37407ea2012-09-16 12:29:43 -07002990 for_each_cpu(i, sched_group_cpus(sg)) {
2991 if (!idle_cpu(i))
2992 goto next;
2993 }
2994
2995 target = cpumask_first_and(sched_group_cpus(sg),
2996 tsk_cpus_allowed(p));
2997 goto done;
2998next:
2999 sg = sg->next;
3000 } while (sg != sd->groups);
3001 }
3002done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003003 return target;
3004}
3005
3006/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003007 * sched_balance_self: balance the current task (running on cpu) in domains
3008 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3009 * SD_BALANCE_EXEC.
3010 *
3011 * Balance, ie. select the least loaded group.
3012 *
3013 * Returns the target CPU number, or the same CPU if no balancing is needed.
3014 *
3015 * preempt must be disabled.
3016 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003017static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003018select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003019{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003020 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003021 int cpu = smp_processor_id();
3022 int prev_cpu = task_cpu(p);
3023 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003024 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003025 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003026
Peter Zijlstra29baa742012-04-23 12:11:21 +02003027 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003028 return prev_cpu;
3029
Peter Zijlstra0763a662009-09-14 19:37:39 +02003030 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003031 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003032 want_affine = 1;
3033 new_cpu = prev_cpu;
3034 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003035
Peter Zijlstradce840a2011-04-07 14:09:50 +02003036 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003037 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01003038 if (!(tmp->flags & SD_LOAD_BALANCE))
3039 continue;
3040
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003041 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003042 * If both cpu and prev_cpu are part of this domain,
3043 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003044 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003045 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3046 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3047 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003048 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003049 }
3050
Alex Shif03542a2012-07-26 08:55:34 +08003051 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003052 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003053 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003054
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003055 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003056 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003057 prev_cpu = cpu;
3058
3059 new_cpu = select_idle_sibling(p, prev_cpu);
3060 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003061 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003062
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003063 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003064 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003065 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003066 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003067
Peter Zijlstra0763a662009-09-14 19:37:39 +02003068 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003069 sd = sd->child;
3070 continue;
3071 }
3072
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003073 if (sd_flag & SD_BALANCE_WAKE)
3074 load_idx = sd->wake_idx;
3075
3076 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003077 if (!group) {
3078 sd = sd->child;
3079 continue;
3080 }
3081
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003082 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003083 if (new_cpu == -1 || new_cpu == cpu) {
3084 /* Now try balancing at a lower domain level of cpu */
3085 sd = sd->child;
3086 continue;
3087 }
3088
3089 /* Now try balancing at a lower domain level of new_cpu */
3090 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003091 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003092 sd = NULL;
3093 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003094 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003095 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003096 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003097 sd = tmp;
3098 }
3099 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003100 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003101unlock:
3102 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003103
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003104 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003105}
Paul Turner0a74bef2012-10-04 13:18:30 +02003106
3107/*
3108 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3109 * cfs_rq_of(p) references at time of call are still valid and identify the
3110 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3111 * other assumptions, including the state of rq->lock, should be made.
3112 */
3113static void
3114migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3115{
Paul Turneraff3e492012-10-04 13:18:30 +02003116 struct sched_entity *se = &p->se;
3117 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3118
3119 /*
3120 * Load tracking: accumulate removed load so that it can be processed
3121 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3122 * to blocked load iff they have a positive decay-count. It can never
3123 * be negative here since on-rq tasks have decay-count == 0.
3124 */
3125 if (se->avg.decay_count) {
3126 se->avg.decay_count = -__synchronize_entity_decay(se);
3127 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3128 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003129}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003130#endif /* CONFIG_SMP */
3131
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003132static unsigned long
3133wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003134{
3135 unsigned long gran = sysctl_sched_wakeup_granularity;
3136
3137 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003138 * Since its curr running now, convert the gran from real-time
3139 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003140 *
3141 * By using 'se' instead of 'curr' we penalize light tasks, so
3142 * they get preempted easier. That is, if 'se' < 'curr' then
3143 * the resulting gran will be larger, therefore penalizing the
3144 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3145 * be smaller, again penalizing the lighter task.
3146 *
3147 * This is especially important for buddies when the leftmost
3148 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003149 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003150 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003151}
3152
3153/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003154 * Should 'se' preempt 'curr'.
3155 *
3156 * |s1
3157 * |s2
3158 * |s3
3159 * g
3160 * |<--->|c
3161 *
3162 * w(c, s1) = -1
3163 * w(c, s2) = 0
3164 * w(c, s3) = 1
3165 *
3166 */
3167static int
3168wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3169{
3170 s64 gran, vdiff = curr->vruntime - se->vruntime;
3171
3172 if (vdiff <= 0)
3173 return -1;
3174
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003175 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003176 if (vdiff > gran)
3177 return 1;
3178
3179 return 0;
3180}
3181
Peter Zijlstra02479092008-11-04 21:25:10 +01003182static void set_last_buddy(struct sched_entity *se)
3183{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003184 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3185 return;
3186
3187 for_each_sched_entity(se)
3188 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003189}
3190
3191static void set_next_buddy(struct sched_entity *se)
3192{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003193 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3194 return;
3195
3196 for_each_sched_entity(se)
3197 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003198}
3199
Rik van Rielac53db52011-02-01 09:51:03 -05003200static void set_skip_buddy(struct sched_entity *se)
3201{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003202 for_each_sched_entity(se)
3203 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003204}
3205
Peter Zijlstra464b7522008-10-24 11:06:15 +02003206/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003207 * Preempt the current task with a newly woken task if needed:
3208 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003209static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003210{
3211 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003212 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003213 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003214 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003215 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003216
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003217 if (unlikely(se == pse))
3218 return;
3219
Paul Turner5238cdd2011-07-21 09:43:37 -07003220 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003221 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003222 * unconditionally check_prempt_curr() after an enqueue (which may have
3223 * lead to a throttle). This both saves work and prevents false
3224 * next-buddy nomination below.
3225 */
3226 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3227 return;
3228
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003229 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003230 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003231 next_buddy_marked = 1;
3232 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003233
Bharata B Raoaec0a512008-08-28 14:42:49 +05303234 /*
3235 * We can come here with TIF_NEED_RESCHED already set from new task
3236 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003237 *
3238 * Note: this also catches the edge-case of curr being in a throttled
3239 * group (e.g. via set_curr_task), since update_curr() (in the
3240 * enqueue of curr) will have resulted in resched being set. This
3241 * prevents us from potentially nominating it as a false LAST_BUDDY
3242 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303243 */
3244 if (test_tsk_need_resched(curr))
3245 return;
3246
Darren Harta2f5c9a2011-02-22 13:04:33 -08003247 /* Idle tasks are by definition preempted by non-idle tasks. */
3248 if (unlikely(curr->policy == SCHED_IDLE) &&
3249 likely(p->policy != SCHED_IDLE))
3250 goto preempt;
3251
Ingo Molnar91c234b2007-10-15 17:00:18 +02003252 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003253 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3254 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003255 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003256 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003257 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003258
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003259 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003260 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003261 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003262 if (wakeup_preempt_entity(se, pse) == 1) {
3263 /*
3264 * Bias pick_next to pick the sched entity that is
3265 * triggering this preemption.
3266 */
3267 if (!next_buddy_marked)
3268 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003269 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003270 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003271
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003272 return;
3273
3274preempt:
3275 resched_task(curr);
3276 /*
3277 * Only set the backward buddy when the current task is still
3278 * on the rq. This can happen when a wakeup gets interleaved
3279 * with schedule on the ->pre_schedule() or idle_balance()
3280 * point, either of which can * drop the rq lock.
3281 *
3282 * Also, during early boot the idle thread is in the fair class,
3283 * for obvious reasons its a bad idea to schedule back to it.
3284 */
3285 if (unlikely(!se->on_rq || curr == rq->idle))
3286 return;
3287
3288 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3289 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003290}
3291
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003292static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003293{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003294 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003295 struct cfs_rq *cfs_rq = &rq->cfs;
3296 struct sched_entity *se;
3297
Tim Blechmann36ace272009-11-24 11:55:45 +01003298 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003299 return NULL;
3300
3301 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003302 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003303 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003304 cfs_rq = group_cfs_rq(se);
3305 } while (cfs_rq);
3306
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003307 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003308 if (hrtick_enabled(rq))
3309 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003310
3311 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003312}
3313
3314/*
3315 * Account for a descheduled task:
3316 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003317static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003318{
3319 struct sched_entity *se = &prev->se;
3320 struct cfs_rq *cfs_rq;
3321
3322 for_each_sched_entity(se) {
3323 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003324 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003325 }
3326}
3327
Rik van Rielac53db52011-02-01 09:51:03 -05003328/*
3329 * sched_yield() is very simple
3330 *
3331 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3332 */
3333static void yield_task_fair(struct rq *rq)
3334{
3335 struct task_struct *curr = rq->curr;
3336 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3337 struct sched_entity *se = &curr->se;
3338
3339 /*
3340 * Are we the only task in the tree?
3341 */
3342 if (unlikely(rq->nr_running == 1))
3343 return;
3344
3345 clear_buddies(cfs_rq, se);
3346
3347 if (curr->policy != SCHED_BATCH) {
3348 update_rq_clock(rq);
3349 /*
3350 * Update run-time statistics of the 'current'.
3351 */
3352 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003353 /*
3354 * Tell update_rq_clock() that we've just updated,
3355 * so we don't do microscopic update in schedule()
3356 * and double the fastpath cost.
3357 */
3358 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003359 }
3360
3361 set_skip_buddy(se);
3362}
3363
Mike Galbraithd95f4122011-02-01 09:50:51 -05003364static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3365{
3366 struct sched_entity *se = &p->se;
3367
Paul Turner5238cdd2011-07-21 09:43:37 -07003368 /* throttled hierarchies are not runnable */
3369 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003370 return false;
3371
3372 /* Tell the scheduler that we'd really like pse to run next. */
3373 set_next_buddy(se);
3374
Mike Galbraithd95f4122011-02-01 09:50:51 -05003375 yield_task_fair(rq);
3376
3377 return true;
3378}
3379
Peter Williams681f3e62007-10-24 18:23:51 +02003380#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003381/**************************************************
3382 * Fair scheduling class load-balancing methods:
3383 */
3384
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003385static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3386
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003387#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003388#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303389#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003390
3391struct lb_env {
3392 struct sched_domain *sd;
3393
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003394 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303395 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003396
3397 int dst_cpu;
3398 struct rq *dst_rq;
3399
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303400 struct cpumask *dst_grpmask;
3401 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003402 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003403 long imbalance;
Michael Wangb9403132012-07-12 16:10:13 +08003404 /* The set of CPUs under consideration for load-balancing */
3405 struct cpumask *cpus;
3406
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003407 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003408
3409 unsigned int loop;
3410 unsigned int loop_break;
3411 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003412};
3413
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003414/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003415 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003416 * Both runqueues must be locked.
3417 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003418static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003419{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003420 deactivate_task(env->src_rq, p, 0);
3421 set_task_cpu(p, env->dst_cpu);
3422 activate_task(env->dst_rq, p, 0);
3423 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003424}
3425
3426/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003427 * Is this task likely cache-hot:
3428 */
3429static int
3430task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3431{
3432 s64 delta;
3433
3434 if (p->sched_class != &fair_sched_class)
3435 return 0;
3436
3437 if (unlikely(p->policy == SCHED_IDLE))
3438 return 0;
3439
3440 /*
3441 * Buddy candidates are cache hot:
3442 */
3443 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3444 (&p->se == cfs_rq_of(&p->se)->next ||
3445 &p->se == cfs_rq_of(&p->se)->last))
3446 return 1;
3447
3448 if (sysctl_sched_migration_cost == -1)
3449 return 1;
3450 if (sysctl_sched_migration_cost == 0)
3451 return 0;
3452
3453 delta = now - p->se.exec_start;
3454
3455 return delta < (s64)sysctl_sched_migration_cost;
3456}
3457
3458/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003459 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3460 */
3461static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003462int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003463{
3464 int tsk_cache_hot = 0;
3465 /*
3466 * We do not migrate tasks that are:
3467 * 1) running (obviously), or
3468 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3469 * 3) are cache-hot on their current CPU.
3470 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003471 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303472 int new_dst_cpu;
3473
Lucas De Marchi41acab82010-03-10 23:37:45 -03003474 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303475
3476 /*
3477 * Remember if this task can be migrated to any other cpu in
3478 * our sched_group. We may want to revisit it if we couldn't
3479 * meet load balance goals by pulling other tasks on src_cpu.
3480 *
3481 * Also avoid computing new_dst_cpu if we have already computed
3482 * one in current iteration.
3483 */
3484 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3485 return 0;
3486
3487 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3488 tsk_cpus_allowed(p));
3489 if (new_dst_cpu < nr_cpu_ids) {
3490 env->flags |= LBF_SOME_PINNED;
3491 env->new_dst_cpu = new_dst_cpu;
3492 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003493 return 0;
3494 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303495
3496 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003497 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003498
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003499 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003500 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003501 return 0;
3502 }
3503
3504 /*
3505 * Aggressive migration if:
3506 * 1) task is cache cold, or
3507 * 2) too many balance attempts have failed.
3508 */
3509
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003510 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003511 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003512 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003513#ifdef CONFIG_SCHEDSTATS
3514 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003515 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003516 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003517 }
3518#endif
3519 return 1;
3520 }
3521
3522 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003523 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003524 return 0;
3525 }
3526 return 1;
3527}
3528
Peter Zijlstra897c3952009-12-17 17:45:42 +01003529/*
3530 * move_one_task tries to move exactly one task from busiest to this_rq, as
3531 * part of active balancing operations within "domain".
3532 * Returns 1 if successful and 0 otherwise.
3533 *
3534 * Called with both runqueues locked.
3535 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003536static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003537{
3538 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003539
Peter Zijlstra367456c2012-02-20 21:49:09 +01003540 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3541 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3542 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003543
Peter Zijlstra367456c2012-02-20 21:49:09 +01003544 if (!can_migrate_task(p, env))
3545 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003546
Peter Zijlstra367456c2012-02-20 21:49:09 +01003547 move_task(p, env);
3548 /*
3549 * Right now, this is only the second place move_task()
3550 * is called, so we can safely collect move_task()
3551 * stats here rather than inside move_task().
3552 */
3553 schedstat_inc(env->sd, lb_gained[env->idle]);
3554 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003555 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003556 return 0;
3557}
3558
Peter Zijlstra367456c2012-02-20 21:49:09 +01003559static unsigned long task_h_load(struct task_struct *p);
3560
Peter Zijlstraeb953082012-04-17 13:38:40 +02003561static const unsigned int sched_nr_migrate_break = 32;
3562
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003563/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003564 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003565 * this_rq, as part of a balancing operation within domain "sd".
3566 * Returns 1 if successful and 0 otherwise.
3567 *
3568 * Called with both runqueues locked.
3569 */
3570static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003571{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003572 struct list_head *tasks = &env->src_rq->cfs_tasks;
3573 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003574 unsigned long load;
3575 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003576
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003577 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003578 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003579
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003580 while (!list_empty(tasks)) {
3581 p = list_first_entry(tasks, struct task_struct, se.group_node);
3582
Peter Zijlstra367456c2012-02-20 21:49:09 +01003583 env->loop++;
3584 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003585 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003586 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003587
3588 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003589 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003590 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003591 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003592 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003593 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003594
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003595 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003596 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003597
Peter Zijlstra367456c2012-02-20 21:49:09 +01003598 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003599
Peter Zijlstraeb953082012-04-17 13:38:40 +02003600 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003601 goto next;
3602
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003603 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003604 goto next;
3605
3606 if (!can_migrate_task(p, env))
3607 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003608
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003609 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003610 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003611 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003612
3613#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003614 /*
3615 * NEWIDLE balancing is a source of latency, so preemptible
3616 * kernels will stop after the first task is pulled to minimize
3617 * the critical section.
3618 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003619 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003620 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003621#endif
3622
Peter Zijlstraee00e662009-12-17 17:25:20 +01003623 /*
3624 * We only want to steal up to the prescribed amount of
3625 * weighted load.
3626 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003627 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003628 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003629
Peter Zijlstra367456c2012-02-20 21:49:09 +01003630 continue;
3631next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003632 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003633 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003634
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003635 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003636 * Right now, this is one of only two places move_task() is called,
3637 * so we can safely collect move_task() stats here rather than
3638 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003639 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003640 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003641
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003642 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003643}
3644
Peter Zijlstra230059de2009-12-17 17:47:12 +01003645#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003646/*
3647 * update tg->load_weight by folding this cpu's load_avg
3648 */
Paul Turner67e86252010-11-15 15:47:05 -08003649static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003650{
3651 struct cfs_rq *cfs_rq;
3652 unsigned long flags;
3653 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003654
3655 if (!tg->se[cpu])
3656 return 0;
3657
3658 rq = cpu_rq(cpu);
3659 cfs_rq = tg->cfs_rq[cpu];
3660
3661 raw_spin_lock_irqsave(&rq->lock, flags);
3662
3663 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003664 update_cfs_load(cfs_rq, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02003665 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003666
3667 /*
3668 * We need to update shares after updating tg->load_weight in
3669 * order to adjust the weight of groups with long running tasks.
3670 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003671 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003672
3673 raw_spin_unlock_irqrestore(&rq->lock, flags);
3674
3675 return 0;
3676}
3677
3678static void update_shares(int cpu)
3679{
3680 struct cfs_rq *cfs_rq;
3681 struct rq *rq = cpu_rq(cpu);
3682
3683 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003684 /*
3685 * Iterates the task_group tree in a bottom up fashion, see
3686 * list_add_leaf_cfs_rq() for details.
3687 */
Paul Turner64660c82011-07-21 09:43:36 -07003688 for_each_leaf_cfs_rq(rq, cfs_rq) {
3689 /* throttled entities do not contribute to load */
3690 if (throttled_hierarchy(cfs_rq))
3691 continue;
3692
Paul Turner67e86252010-11-15 15:47:05 -08003693 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003694 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003695 rcu_read_unlock();
3696}
3697
Peter Zijlstra9763b672011-07-13 13:09:25 +02003698/*
3699 * Compute the cpu's hierarchical load factor for each task group.
3700 * This needs to be done in a top-down fashion because the load of a child
3701 * group is a fraction of its parents load.
3702 */
3703static int tg_load_down(struct task_group *tg, void *data)
3704{
3705 unsigned long load;
3706 long cpu = (long)data;
3707
3708 if (!tg->parent) {
3709 load = cpu_rq(cpu)->load.weight;
3710 } else {
3711 load = tg->parent->cfs_rq[cpu]->h_load;
3712 load *= tg->se[cpu]->load.weight;
3713 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3714 }
3715
3716 tg->cfs_rq[cpu]->h_load = load;
3717
3718 return 0;
3719}
3720
3721static void update_h_load(long cpu)
3722{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003723 struct rq *rq = cpu_rq(cpu);
3724 unsigned long now = jiffies;
3725
3726 if (rq->h_load_throttle == now)
3727 return;
3728
3729 rq->h_load_throttle = now;
3730
Peter Zijlstra367456c2012-02-20 21:49:09 +01003731 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003732 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003733 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003734}
3735
Peter Zijlstra367456c2012-02-20 21:49:09 +01003736static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003737{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003738 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3739 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003740
Peter Zijlstra367456c2012-02-20 21:49:09 +01003741 load = p->se.load.weight;
3742 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003743
Peter Zijlstra367456c2012-02-20 21:49:09 +01003744 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003745}
3746#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003747static inline void update_shares(int cpu)
3748{
3749}
3750
Peter Zijlstra367456c2012-02-20 21:49:09 +01003751static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003752{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003753}
3754
3755static unsigned long task_h_load(struct task_struct *p)
3756{
3757 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003758}
3759#endif
3760
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003761/********** Helpers for find_busiest_group ************************/
3762/*
3763 * sd_lb_stats - Structure to store the statistics of a sched_domain
3764 * during load balancing.
3765 */
3766struct sd_lb_stats {
3767 struct sched_group *busiest; /* Busiest group in this sd */
3768 struct sched_group *this; /* Local group in this sd */
3769 unsigned long total_load; /* Total load of all groups in sd */
3770 unsigned long total_pwr; /* Total power of all groups in sd */
3771 unsigned long avg_load; /* Average load across all groups in sd */
3772
3773 /** Statistics of this group */
3774 unsigned long this_load;
3775 unsigned long this_load_per_task;
3776 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003777 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003778 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003779
3780 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003781 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003782 unsigned long max_load;
3783 unsigned long busiest_load_per_task;
3784 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003785 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003786 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003787 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003788
3789 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003790};
3791
3792/*
3793 * sg_lb_stats - stats of a sched_group required for load_balancing
3794 */
3795struct sg_lb_stats {
3796 unsigned long avg_load; /*Avg load across the CPUs of the group */
3797 unsigned long group_load; /* Total load over the CPUs of the group */
3798 unsigned long sum_nr_running; /* Nr tasks running in the group */
3799 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3800 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003801 unsigned long idle_cpus;
3802 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003803 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003804 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003805};
3806
3807/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003808 * get_sd_load_idx - Obtain the load index for a given sched domain.
3809 * @sd: The sched_domain whose load_idx is to be obtained.
3810 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3811 */
3812static inline int get_sd_load_idx(struct sched_domain *sd,
3813 enum cpu_idle_type idle)
3814{
3815 int load_idx;
3816
3817 switch (idle) {
3818 case CPU_NOT_IDLE:
3819 load_idx = sd->busy_idx;
3820 break;
3821
3822 case CPU_NEWLY_IDLE:
3823 load_idx = sd->newidle_idx;
3824 break;
3825 default:
3826 load_idx = sd->idle_idx;
3827 break;
3828 }
3829
3830 return load_idx;
3831}
3832
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003833unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3834{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003835 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003836}
3837
3838unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3839{
3840 return default_scale_freq_power(sd, cpu);
3841}
3842
3843unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3844{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003845 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003846 unsigned long smt_gain = sd->smt_gain;
3847
3848 smt_gain /= weight;
3849
3850 return smt_gain;
3851}
3852
3853unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3854{
3855 return default_scale_smt_power(sd, cpu);
3856}
3857
3858unsigned long scale_rt_power(int cpu)
3859{
3860 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003861 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003862
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003863 /*
3864 * Since we're reading these variables without serialization make sure
3865 * we read them once before doing sanity checks on them.
3866 */
3867 age_stamp = ACCESS_ONCE(rq->age_stamp);
3868 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003869
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003870 total = sched_avg_period() + (rq->clock - age_stamp);
3871
3872 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003873 /* Ensures that power won't end up being negative */
3874 available = 0;
3875 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003876 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003877 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003878
Nikhil Rao1399fa72011-05-18 10:09:39 -07003879 if (unlikely((s64)total < SCHED_POWER_SCALE))
3880 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003881
Nikhil Rao1399fa72011-05-18 10:09:39 -07003882 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003883
3884 return div_u64(available, total);
3885}
3886
3887static void update_cpu_power(struct sched_domain *sd, int cpu)
3888{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003889 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003890 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003891 struct sched_group *sdg = sd->groups;
3892
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003893 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3894 if (sched_feat(ARCH_POWER))
3895 power *= arch_scale_smt_power(sd, cpu);
3896 else
3897 power *= default_scale_smt_power(sd, cpu);
3898
Nikhil Rao1399fa72011-05-18 10:09:39 -07003899 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003900 }
3901
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003902 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003903
3904 if (sched_feat(ARCH_POWER))
3905 power *= arch_scale_freq_power(sd, cpu);
3906 else
3907 power *= default_scale_freq_power(sd, cpu);
3908
Nikhil Rao1399fa72011-05-18 10:09:39 -07003909 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003910
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003911 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003912 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003913
3914 if (!power)
3915 power = 1;
3916
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003917 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003918 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003919}
3920
Peter Zijlstra029632f2011-10-25 10:00:11 +02003921void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003922{
3923 struct sched_domain *child = sd->child;
3924 struct sched_group *group, *sdg = sd->groups;
3925 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003926 unsigned long interval;
3927
3928 interval = msecs_to_jiffies(sd->balance_interval);
3929 interval = clamp(interval, 1UL, max_load_balance_interval);
3930 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003931
3932 if (!child) {
3933 update_cpu_power(sd, cpu);
3934 return;
3935 }
3936
3937 power = 0;
3938
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003939 if (child->flags & SD_OVERLAP) {
3940 /*
3941 * SD_OVERLAP domains cannot assume that child groups
3942 * span the current group.
3943 */
3944
3945 for_each_cpu(cpu, sched_group_cpus(sdg))
3946 power += power_of(cpu);
3947 } else {
3948 /*
3949 * !SD_OVERLAP domains can assume that child groups
3950 * span the current group.
3951 */
3952
3953 group = child->groups;
3954 do {
3955 power += group->sgp->power;
3956 group = group->next;
3957 } while (group != child->groups);
3958 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003959
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003960 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003961}
3962
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003963/*
3964 * Try and fix up capacity for tiny siblings, this is needed when
3965 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3966 * which on its own isn't powerful enough.
3967 *
3968 * See update_sd_pick_busiest() and check_asym_packing().
3969 */
3970static inline int
3971fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3972{
3973 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003974 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003975 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003976 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003977 return 0;
3978
3979 /*
3980 * If ~90% of the cpu_power is still there, we're good.
3981 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003982 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003983 return 1;
3984
3985 return 0;
3986}
3987
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003988/**
3989 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003990 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003991 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003992 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003993 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003994 * @balance: Should we balance.
3995 * @sgs: variable to hold the statistics for this group.
3996 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003997static inline void update_sg_lb_stats(struct lb_env *env,
3998 struct sched_group *group, int load_idx,
Michael Wangb9403132012-07-12 16:10:13 +08003999 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004000{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004001 unsigned long nr_running, max_nr_running, min_nr_running;
4002 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004003 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004004 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004005 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004006
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004007 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004008 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004009
4010 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004011 max_cpu_load = 0;
4012 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004013 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004014 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004015
Michael Wangb9403132012-07-12 16:10:13 +08004016 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004017 struct rq *rq = cpu_rq(i);
4018
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004019 nr_running = rq->nr_running;
4020
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004021 /* Bias balancing toward cpus of our domain */
4022 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004023 if (idle_cpu(i) && !first_idle_cpu &&
4024 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004025 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004026 balance_cpu = i;
4027 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004028
4029 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004030 } else {
4031 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004032 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004033 max_cpu_load = load;
4034 if (min_cpu_load > load)
4035 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004036
4037 if (nr_running > max_nr_running)
4038 max_nr_running = nr_running;
4039 if (min_nr_running > nr_running)
4040 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004041 }
4042
4043 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004044 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004045 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004046 if (idle_cpu(i))
4047 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004048 }
4049
4050 /*
4051 * First idle cpu or the first cpu(busiest) in this sched group
4052 * is eligible for doing load balancing at this and above
4053 * domains. In the newly idle case, we will allow all the cpu's
4054 * to do the newly idle load balance.
4055 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004056 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004057 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004058 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004059 *balance = 0;
4060 return;
4061 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004062 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004063 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004064 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004065 }
4066
4067 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004068 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004069
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004070 /*
4071 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004072 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004073 *
4074 * APZ: with cgroup the avg task weight can vary wildly and
4075 * might not be a suitable number - should we keep a
4076 * normalized nr_running number somewhere that negates
4077 * the hierarchy?
4078 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004079 if (sgs->sum_nr_running)
4080 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004081
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004082 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4083 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004084 sgs->group_imb = 1;
4085
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004086 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004087 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004088 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004089 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004090 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004091
4092 if (sgs->group_capacity > sgs->sum_nr_running)
4093 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094}
4095
4096/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004097 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004098 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004099 * @sds: sched_domain statistics
4100 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004101 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004102 *
4103 * Determine if @sg is a busier group than the previously selected
4104 * busiest group.
4105 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004106static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004107 struct sd_lb_stats *sds,
4108 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004109 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004110{
4111 if (sgs->avg_load <= sds->max_load)
4112 return false;
4113
4114 if (sgs->sum_nr_running > sgs->group_capacity)
4115 return true;
4116
4117 if (sgs->group_imb)
4118 return true;
4119
4120 /*
4121 * ASYM_PACKING needs to move all the work to the lowest
4122 * numbered CPUs in the group, therefore mark all groups
4123 * higher than ourself as busy.
4124 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004125 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4126 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004127 if (!sds->busiest)
4128 return true;
4129
4130 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4131 return true;
4132 }
4133
4134 return false;
4135}
4136
4137/**
Hui Kang461819a2011-10-11 23:00:59 -04004138 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004139 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004140 * @balance: Should we balance.
4141 * @sds: variable to hold the statistics for this sched_domain.
4142 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004143static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004144 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004145{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004146 struct sched_domain *child = env->sd->child;
4147 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004148 struct sg_lb_stats sgs;
4149 int load_idx, prefer_sibling = 0;
4150
4151 if (child && child->flags & SD_PREFER_SIBLING)
4152 prefer_sibling = 1;
4153
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004154 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004155
4156 do {
4157 int local_group;
4158
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004159 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004160 memset(&sgs, 0, sizeof(sgs));
Michael Wangb9403132012-07-12 16:10:13 +08004161 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004162
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004163 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004164 return;
4165
4166 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004167 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004168
4169 /*
4170 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004171 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004172 * and move all the excess tasks away. We lower the capacity
4173 * of a group only if the local group has the capacity to fit
4174 * these excess tasks, i.e. nr_running < group_capacity. The
4175 * extra check prevents the case where you always pull from the
4176 * heaviest group when it is already under-utilized (possible
4177 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004178 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004179 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004180 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4181
4182 if (local_group) {
4183 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004184 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004185 sds->this_nr_running = sgs.sum_nr_running;
4186 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004187 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004188 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004189 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004190 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004191 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004192 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004193 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004194 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004195 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004196 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004197 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004198 sds->group_imb = sgs.group_imb;
4199 }
4200
Michael Neuling532cb4c2010-06-08 14:57:02 +10004201 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004202 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004203}
4204
Michael Neuling532cb4c2010-06-08 14:57:02 +10004205/**
4206 * check_asym_packing - Check to see if the group is packed into the
4207 * sched doman.
4208 *
4209 * This is primarily intended to used at the sibling level. Some
4210 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4211 * case of POWER7, it can move to lower SMT modes only when higher
4212 * threads are idle. When in lower SMT modes, the threads will
4213 * perform better since they share less core resources. Hence when we
4214 * have idle threads, we want them to be the higher ones.
4215 *
4216 * This packing function is run on idle threads. It checks to see if
4217 * the busiest CPU in this domain (core in the P7 case) has a higher
4218 * CPU number than the packing function is being run on. Here we are
4219 * assuming lower CPU number will be equivalent to lower a SMT thread
4220 * number.
4221 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004222 * Returns 1 when packing is required and a task should be moved to
4223 * this CPU. The amount of the imbalance is returned in *imbalance.
4224 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004225 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004226 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004227 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004228static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004229{
4230 int busiest_cpu;
4231
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004232 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004233 return 0;
4234
4235 if (!sds->busiest)
4236 return 0;
4237
4238 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004239 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004240 return 0;
4241
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004242 env->imbalance = DIV_ROUND_CLOSEST(
4243 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4244
Michael Neuling532cb4c2010-06-08 14:57:02 +10004245 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004246}
4247
4248/**
4249 * fix_small_imbalance - Calculate the minor imbalance that exists
4250 * amongst the groups of a sched_domain, during
4251 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004252 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004253 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004254 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004255static inline
4256void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004257{
4258 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4259 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004260 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004261
4262 if (sds->this_nr_running) {
4263 sds->this_load_per_task /= sds->this_nr_running;
4264 if (sds->busiest_load_per_task >
4265 sds->this_load_per_task)
4266 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004267 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004268 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004269 cpu_avg_load_per_task(env->dst_cpu);
4270 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004271
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004272 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004273 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004274 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004275
4276 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4277 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004278 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004279 return;
4280 }
4281
4282 /*
4283 * OK, we don't have enough imbalance to justify moving tasks,
4284 * however we may be able to increase total CPU power used by
4285 * moving them.
4286 */
4287
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004288 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004289 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004290 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004291 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004292 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004293
4294 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004295 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004296 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004297 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004298 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004299 min(sds->busiest_load_per_task, sds->max_load - tmp);
4300
4301 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004302 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004303 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004304 tmp = (sds->max_load * sds->busiest->sgp->power) /
4305 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004306 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004307 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004308 sds->this->sgp->power;
4309 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004310 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004311 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004312
4313 /* Move if we gain throughput */
4314 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004315 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004316}
4317
4318/**
4319 * calculate_imbalance - Calculate the amount of imbalance present within the
4320 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004321 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004322 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004323 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004324static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004325{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004326 unsigned long max_pull, load_above_capacity = ~0UL;
4327
4328 sds->busiest_load_per_task /= sds->busiest_nr_running;
4329 if (sds->group_imb) {
4330 sds->busiest_load_per_task =
4331 min(sds->busiest_load_per_task, sds->avg_load);
4332 }
4333
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004334 /*
4335 * In the presence of smp nice balancing, certain scenarios can have
4336 * max load less than avg load(as we skip the groups at or below
4337 * its cpu_power, while calculating max_load..)
4338 */
4339 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004340 env->imbalance = 0;
4341 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004342 }
4343
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004344 if (!sds->group_imb) {
4345 /*
4346 * Don't want to pull so many tasks that a group would go idle.
4347 */
4348 load_above_capacity = (sds->busiest_nr_running -
4349 sds->busiest_group_capacity);
4350
Nikhil Rao1399fa72011-05-18 10:09:39 -07004351 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004352
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004353 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004354 }
4355
4356 /*
4357 * We're trying to get all the cpus to the average_load, so we don't
4358 * want to push ourselves above the average load, nor do we wish to
4359 * reduce the max loaded cpu below the average load. At the same time,
4360 * we also don't want to reduce the group load below the group capacity
4361 * (so that we can implement power-savings policies etc). Thus we look
4362 * for the minimum possible imbalance.
4363 * Be careful of negative numbers as they'll appear as very large values
4364 * with unsigned longs.
4365 */
4366 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004367
4368 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004369 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004370 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004371 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004372
4373 /*
4374 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004375 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004376 * a think about bumping its value to force at least one task to be
4377 * moved
4378 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004379 if (env->imbalance < sds->busiest_load_per_task)
4380 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004381
4382}
Nikhil Raofab47622010-10-15 13:12:29 -07004383
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004384/******* find_busiest_group() helpers end here *********************/
4385
4386/**
4387 * find_busiest_group - Returns the busiest group within the sched_domain
4388 * if there is an imbalance. If there isn't an imbalance, and
4389 * the user has opted for power-savings, it returns a group whose
4390 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4391 * such a group exists.
4392 *
4393 * Also calculates the amount of weighted load which should be moved
4394 * to restore balance.
4395 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004396 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004397 * @balance: Pointer to a variable indicating if this_cpu
4398 * is the appropriate cpu to perform load balancing at this_level.
4399 *
4400 * Returns: - the busiest group if imbalance exists.
4401 * - If no imbalance and user has opted for power-savings balance,
4402 * return the least loaded group whose CPUs can be
4403 * put to idle by rebalancing its tasks onto our group.
4404 */
4405static struct sched_group *
Michael Wangb9403132012-07-12 16:10:13 +08004406find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004407{
4408 struct sd_lb_stats sds;
4409
4410 memset(&sds, 0, sizeof(sds));
4411
4412 /*
4413 * Compute the various statistics relavent for load balancing at
4414 * this level.
4415 */
Michael Wangb9403132012-07-12 16:10:13 +08004416 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004417
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004418 /*
4419 * this_cpu is not the appropriate cpu to perform load balancing at
4420 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004421 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004422 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004423 goto ret;
4424
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004425 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4426 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004427 return sds.busiest;
4428
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004429 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004430 if (!sds.busiest || sds.busiest_nr_running == 0)
4431 goto out_balanced;
4432
Nikhil Rao1399fa72011-05-18 10:09:39 -07004433 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004434
Peter Zijlstra866ab432011-02-21 18:56:47 +01004435 /*
4436 * If the busiest group is imbalanced the below checks don't
4437 * work because they assumes all things are equal, which typically
4438 * isn't true due to cpus_allowed constraints and the like.
4439 */
4440 if (sds.group_imb)
4441 goto force_balance;
4442
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004443 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004444 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004445 !sds.busiest_has_capacity)
4446 goto force_balance;
4447
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004448 /*
4449 * If the local group is more busy than the selected busiest group
4450 * don't try and pull any tasks.
4451 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004452 if (sds.this_load >= sds.max_load)
4453 goto out_balanced;
4454
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004455 /*
4456 * Don't pull any tasks if this group is already above the domain
4457 * average load.
4458 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004459 if (sds.this_load >= sds.avg_load)
4460 goto out_balanced;
4461
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004462 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004463 /*
4464 * This cpu is idle. If the busiest group load doesn't
4465 * have more tasks than the number of available cpu's and
4466 * there is no imbalance between this and busiest group
4467 * wrt to idle cpu's, it is balanced.
4468 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004469 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004470 sds.busiest_nr_running <= sds.busiest_group_weight)
4471 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004472 } else {
4473 /*
4474 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4475 * imbalance_pct to be conservative.
4476 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004477 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004478 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004479 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004480
Nikhil Raofab47622010-10-15 13:12:29 -07004481force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004482 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004483 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004484 return sds.busiest;
4485
4486out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004487ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004488 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004489 return NULL;
4490}
4491
4492/*
4493 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4494 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004495static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004496 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004497{
4498 struct rq *busiest = NULL, *rq;
4499 unsigned long max_load = 0;
4500 int i;
4501
4502 for_each_cpu(i, sched_group_cpus(group)) {
4503 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004504 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4505 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004506 unsigned long wl;
4507
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004508 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004509 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004510
Michael Wangb9403132012-07-12 16:10:13 +08004511 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004512 continue;
4513
4514 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004515 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004516
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004517 /*
4518 * When comparing with imbalance, use weighted_cpuload()
4519 * which is not scaled with the cpu power.
4520 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004521 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004522 continue;
4523
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004524 /*
4525 * For the load comparisons with the other cpu's, consider
4526 * the weighted_cpuload() scaled with the cpu power, so that
4527 * the load can be moved away from the cpu that is potentially
4528 * running at a lower capacity.
4529 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004530 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004531
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004532 if (wl > max_load) {
4533 max_load = wl;
4534 busiest = rq;
4535 }
4536 }
4537
4538 return busiest;
4539}
4540
4541/*
4542 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4543 * so long as it is large enough.
4544 */
4545#define MAX_PINNED_INTERVAL 512
4546
4547/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004548DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004549
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004550static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004551{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004552 struct sched_domain *sd = env->sd;
4553
4554 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004555
4556 /*
4557 * ASYM_PACKING needs to force migrate tasks from busy but
4558 * higher numbered CPUs in order to pack all tasks in the
4559 * lowest numbered CPUs.
4560 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004561 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004562 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004563 }
4564
4565 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4566}
4567
Tejun Heo969c7922010-05-06 18:49:21 +02004568static int active_load_balance_cpu_stop(void *data);
4569
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004570/*
4571 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4572 * tasks if there is an imbalance.
4573 */
4574static int load_balance(int this_cpu, struct rq *this_rq,
4575 struct sched_domain *sd, enum cpu_idle_type idle,
4576 int *balance)
4577{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304578 int ld_moved, cur_ld_moved, active_balance = 0;
4579 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004580 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004581 struct rq *busiest;
4582 unsigned long flags;
4583 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4584
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004585 struct lb_env env = {
4586 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004587 .dst_cpu = this_cpu,
4588 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304589 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004590 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004591 .loop_break = sched_nr_migrate_break,
Michael Wangb9403132012-07-12 16:10:13 +08004592 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004593 };
4594
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004595 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304596 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004597
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004598 schedstat_inc(sd, lb_count[idle]);
4599
4600redo:
Michael Wangb9403132012-07-12 16:10:13 +08004601 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004602
4603 if (*balance == 0)
4604 goto out_balanced;
4605
4606 if (!group) {
4607 schedstat_inc(sd, lb_nobusyg[idle]);
4608 goto out_balanced;
4609 }
4610
Michael Wangb9403132012-07-12 16:10:13 +08004611 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004612 if (!busiest) {
4613 schedstat_inc(sd, lb_nobusyq[idle]);
4614 goto out_balanced;
4615 }
4616
Michael Wang78feefc2012-08-06 16:41:59 +08004617 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004618
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004619 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004620
4621 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304622 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004623 if (busiest->nr_running > 1) {
4624 /*
4625 * Attempt to move tasks. If find_busiest_group has found
4626 * an imbalance but busiest->nr_running <= 1, the group is
4627 * still unbalanced. ld_moved simply stays zero, so it is
4628 * correctly treated as an imbalance.
4629 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004630 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004631 env.src_cpu = busiest->cpu;
4632 env.src_rq = busiest;
4633 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004634
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004635 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004636more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004637 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004638 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304639
4640 /*
4641 * cur_ld_moved - load moved in current iteration
4642 * ld_moved - cumulative load moved across iterations
4643 */
4644 cur_ld_moved = move_tasks(&env);
4645 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004646 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647 local_irq_restore(flags);
4648
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004649 if (env.flags & LBF_NEED_BREAK) {
4650 env.flags &= ~LBF_NEED_BREAK;
4651 goto more_balance;
4652 }
4653
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004654 /*
4655 * some other cpu did the load balance for us.
4656 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304657 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4658 resched_cpu(env.dst_cpu);
4659
4660 /*
4661 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4662 * us and move them to an alternate dst_cpu in our sched_group
4663 * where they can run. The upper limit on how many times we
4664 * iterate on same src_cpu is dependent on number of cpus in our
4665 * sched_group.
4666 *
4667 * This changes load balance semantics a bit on who can move
4668 * load to a given_cpu. In addition to the given_cpu itself
4669 * (or a ilb_cpu acting on its behalf where given_cpu is
4670 * nohz-idle), we now have balance_cpu in a position to move
4671 * load to given_cpu. In rare situations, this may cause
4672 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4673 * _independently_ and at _same_ time to move some load to
4674 * given_cpu) causing exceess load to be moved to given_cpu.
4675 * This however should not happen so much in practice and
4676 * moreover subsequent load balance cycles should correct the
4677 * excess load moved.
4678 */
4679 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4680 lb_iterations++ < max_lb_iterations) {
4681
Michael Wang78feefc2012-08-06 16:41:59 +08004682 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304683 env.dst_cpu = env.new_dst_cpu;
4684 env.flags &= ~LBF_SOME_PINNED;
4685 env.loop = 0;
4686 env.loop_break = sched_nr_migrate_break;
4687 /*
4688 * Go back to "more_balance" rather than "redo" since we
4689 * need to continue with same src_cpu.
4690 */
4691 goto more_balance;
4692 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004693
4694 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004695 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004696 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304697 if (!cpumask_empty(cpus)) {
4698 env.loop = 0;
4699 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004700 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304701 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004702 goto out_balanced;
4703 }
4704 }
4705
4706 if (!ld_moved) {
4707 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004708 /*
4709 * Increment the failure counter only on periodic balance.
4710 * We do not want newidle balance, which can be very
4711 * frequent, pollute the failure counter causing
4712 * excessive cache_hot migrations and active balances.
4713 */
4714 if (idle != CPU_NEWLY_IDLE)
4715 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004716
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004717 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004718 raw_spin_lock_irqsave(&busiest->lock, flags);
4719
Tejun Heo969c7922010-05-06 18:49:21 +02004720 /* don't kick the active_load_balance_cpu_stop,
4721 * if the curr task on busiest cpu can't be
4722 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004723 */
4724 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004725 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004726 raw_spin_unlock_irqrestore(&busiest->lock,
4727 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004728 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004729 goto out_one_pinned;
4730 }
4731
Tejun Heo969c7922010-05-06 18:49:21 +02004732 /*
4733 * ->active_balance synchronizes accesses to
4734 * ->active_balance_work. Once set, it's cleared
4735 * only after active load balance is finished.
4736 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004737 if (!busiest->active_balance) {
4738 busiest->active_balance = 1;
4739 busiest->push_cpu = this_cpu;
4740 active_balance = 1;
4741 }
4742 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004743
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004744 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004745 stop_one_cpu_nowait(cpu_of(busiest),
4746 active_load_balance_cpu_stop, busiest,
4747 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004748 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004749
4750 /*
4751 * We've kicked active balancing, reset the failure
4752 * counter.
4753 */
4754 sd->nr_balance_failed = sd->cache_nice_tries+1;
4755 }
4756 } else
4757 sd->nr_balance_failed = 0;
4758
4759 if (likely(!active_balance)) {
4760 /* We were unbalanced, so reset the balancing interval */
4761 sd->balance_interval = sd->min_interval;
4762 } else {
4763 /*
4764 * If we've begun active balancing, start to back off. This
4765 * case may not be covered by the all_pinned logic if there
4766 * is only 1 task on the busy runqueue (because we don't call
4767 * move_tasks).
4768 */
4769 if (sd->balance_interval < sd->max_interval)
4770 sd->balance_interval *= 2;
4771 }
4772
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004773 goto out;
4774
4775out_balanced:
4776 schedstat_inc(sd, lb_balanced[idle]);
4777
4778 sd->nr_balance_failed = 0;
4779
4780out_one_pinned:
4781 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004782 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004783 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004784 (sd->balance_interval < sd->max_interval))
4785 sd->balance_interval *= 2;
4786
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004787 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004788out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004789 return ld_moved;
4790}
4791
4792/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004793 * idle_balance is called by schedule() if this_cpu is about to become
4794 * idle. Attempts to pull tasks from other CPUs.
4795 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004796void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004797{
4798 struct sched_domain *sd;
4799 int pulled_task = 0;
4800 unsigned long next_balance = jiffies + HZ;
4801
4802 this_rq->idle_stamp = this_rq->clock;
4803
4804 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4805 return;
4806
Ben Segall18bf2802012-10-04 12:51:20 +02004807 update_rq_runnable_avg(this_rq, 1);
4808
Peter Zijlstraf492e122009-12-23 15:29:42 +01004809 /*
4810 * Drop the rq->lock, but keep IRQ/preempt disabled.
4811 */
4812 raw_spin_unlock(&this_rq->lock);
4813
Paul Turnerc66eaf62010-11-15 15:47:07 -08004814 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004815 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004816 for_each_domain(this_cpu, sd) {
4817 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004818 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004819
4820 if (!(sd->flags & SD_LOAD_BALANCE))
4821 continue;
4822
Peter Zijlstraf492e122009-12-23 15:29:42 +01004823 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004824 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004825 pulled_task = load_balance(this_cpu, this_rq,
4826 sd, CPU_NEWLY_IDLE, &balance);
4827 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004828
4829 interval = msecs_to_jiffies(sd->balance_interval);
4830 if (time_after(next_balance, sd->last_balance + interval))
4831 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004832 if (pulled_task) {
4833 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004834 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004835 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004836 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004837 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004838
4839 raw_spin_lock(&this_rq->lock);
4840
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004841 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4842 /*
4843 * We are going idle. next_balance may be set based on
4844 * a busy processor. So reset next_balance.
4845 */
4846 this_rq->next_balance = next_balance;
4847 }
4848}
4849
4850/*
Tejun Heo969c7922010-05-06 18:49:21 +02004851 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4852 * running tasks off the busiest CPU onto idle CPUs. It requires at
4853 * least 1 task to be running on each physical CPU where possible, and
4854 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004855 */
Tejun Heo969c7922010-05-06 18:49:21 +02004856static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004857{
Tejun Heo969c7922010-05-06 18:49:21 +02004858 struct rq *busiest_rq = data;
4859 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004860 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004861 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004862 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004863
4864 raw_spin_lock_irq(&busiest_rq->lock);
4865
4866 /* make sure the requested cpu hasn't gone down in the meantime */
4867 if (unlikely(busiest_cpu != smp_processor_id() ||
4868 !busiest_rq->active_balance))
4869 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004870
4871 /* Is there any task to move? */
4872 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004873 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004874
4875 /*
4876 * This condition is "impossible", if it occurs
4877 * we need to fix it. Originally reported by
4878 * Bjorn Helgaas on a 128-cpu setup.
4879 */
4880 BUG_ON(busiest_rq == target_rq);
4881
4882 /* move a task from busiest_rq to target_rq */
4883 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004884
4885 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004886 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004887 for_each_domain(target_cpu, sd) {
4888 if ((sd->flags & SD_LOAD_BALANCE) &&
4889 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4890 break;
4891 }
4892
4893 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004894 struct lb_env env = {
4895 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004896 .dst_cpu = target_cpu,
4897 .dst_rq = target_rq,
4898 .src_cpu = busiest_rq->cpu,
4899 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004900 .idle = CPU_IDLE,
4901 };
4902
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004903 schedstat_inc(sd, alb_count);
4904
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004905 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004906 schedstat_inc(sd, alb_pushed);
4907 else
4908 schedstat_inc(sd, alb_failed);
4909 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004910 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004911 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004912out_unlock:
4913 busiest_rq->active_balance = 0;
4914 raw_spin_unlock_irq(&busiest_rq->lock);
4915 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004916}
4917
4918#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004919/*
4920 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004921 * - When one of the busy CPUs notice that there may be an idle rebalancing
4922 * needed, they will kick the idle load balancer, which then does idle
4923 * load balancing for all the idle CPUs.
4924 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004925static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004926 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004927 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004928 unsigned long next_balance; /* in jiffy units */
4929} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004930
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004931static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004932{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004933 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004934
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004935 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4936 return ilb;
4937
4938 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004939}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004940
4941/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004942 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4943 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4944 * CPU (if there is one).
4945 */
4946static void nohz_balancer_kick(int cpu)
4947{
4948 int ilb_cpu;
4949
4950 nohz.next_balance++;
4951
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004952 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004953
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004954 if (ilb_cpu >= nr_cpu_ids)
4955 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004956
Suresh Siddhacd490c52011-12-06 11:26:34 -08004957 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004958 return;
4959 /*
4960 * Use smp_send_reschedule() instead of resched_cpu().
4961 * This way we generate a sched IPI on the target cpu which
4962 * is idle. And the softirq performing nohz idle load balance
4963 * will be run before returning from the IPI.
4964 */
4965 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004966 return;
4967}
4968
Alex Shic1cc0172012-09-10 15:10:58 +08004969static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004970{
4971 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4972 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4973 atomic_dec(&nohz.nr_cpus);
4974 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4975 }
4976}
4977
Suresh Siddha69e1e812011-12-01 17:07:33 -08004978static inline void set_cpu_sd_state_busy(void)
4979{
4980 struct sched_domain *sd;
4981 int cpu = smp_processor_id();
4982
4983 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4984 return;
4985 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4986
4987 rcu_read_lock();
4988 for_each_domain(cpu, sd)
4989 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4990 rcu_read_unlock();
4991}
4992
4993void set_cpu_sd_state_idle(void)
4994{
4995 struct sched_domain *sd;
4996 int cpu = smp_processor_id();
4997
4998 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4999 return;
5000 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5001
5002 rcu_read_lock();
5003 for_each_domain(cpu, sd)
5004 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5005 rcu_read_unlock();
5006}
5007
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005008/*
Alex Shic1cc0172012-09-10 15:10:58 +08005009 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005010 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005011 */
Alex Shic1cc0172012-09-10 15:10:58 +08005012void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005013{
Suresh Siddha71325962012-01-19 18:28:57 -08005014 /*
5015 * If this cpu is going down, then nothing needs to be done.
5016 */
5017 if (!cpu_active(cpu))
5018 return;
5019
Alex Shic1cc0172012-09-10 15:10:58 +08005020 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5021 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005022
Alex Shic1cc0172012-09-10 15:10:58 +08005023 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5024 atomic_inc(&nohz.nr_cpus);
5025 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005026}
Suresh Siddha71325962012-01-19 18:28:57 -08005027
5028static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5029 unsigned long action, void *hcpu)
5030{
5031 switch (action & ~CPU_TASKS_FROZEN) {
5032 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005033 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005034 return NOTIFY_OK;
5035 default:
5036 return NOTIFY_DONE;
5037 }
5038}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005039#endif
5040
5041static DEFINE_SPINLOCK(balancing);
5042
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005043/*
5044 * Scale the max load_balance interval with the number of CPUs in the system.
5045 * This trades load-balance latency on larger machines for less cross talk.
5046 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005047void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005048{
5049 max_load_balance_interval = HZ*num_online_cpus()/10;
5050}
5051
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005052/*
5053 * It checks each scheduling domain to see if it is due to be balanced,
5054 * and initiates a balancing operation if so.
5055 *
5056 * Balancing parameters are set up in arch_init_sched_domains.
5057 */
5058static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5059{
5060 int balance = 1;
5061 struct rq *rq = cpu_rq(cpu);
5062 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005063 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005064 /* Earliest time when we have to do rebalance again */
5065 unsigned long next_balance = jiffies + 60*HZ;
5066 int update_next_balance = 0;
5067 int need_serialize;
5068
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005069 update_shares(cpu);
5070
Peter Zijlstradce840a2011-04-07 14:09:50 +02005071 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005072 for_each_domain(cpu, sd) {
5073 if (!(sd->flags & SD_LOAD_BALANCE))
5074 continue;
5075
5076 interval = sd->balance_interval;
5077 if (idle != CPU_IDLE)
5078 interval *= sd->busy_factor;
5079
5080 /* scale ms to jiffies */
5081 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005082 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005083
5084 need_serialize = sd->flags & SD_SERIALIZE;
5085
5086 if (need_serialize) {
5087 if (!spin_trylock(&balancing))
5088 goto out;
5089 }
5090
5091 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5092 if (load_balance(cpu, rq, sd, idle, &balance)) {
5093 /*
5094 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005095 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005096 */
5097 idle = CPU_NOT_IDLE;
5098 }
5099 sd->last_balance = jiffies;
5100 }
5101 if (need_serialize)
5102 spin_unlock(&balancing);
5103out:
5104 if (time_after(next_balance, sd->last_balance + interval)) {
5105 next_balance = sd->last_balance + interval;
5106 update_next_balance = 1;
5107 }
5108
5109 /*
5110 * Stop the load balance at this level. There is another
5111 * CPU in our sched group which is doing load balancing more
5112 * actively.
5113 */
5114 if (!balance)
5115 break;
5116 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005117 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005118
5119 /*
5120 * next_balance will be updated only when there is a need.
5121 * When the cpu is attached to null domain for ex, it will not be
5122 * updated.
5123 */
5124 if (likely(update_next_balance))
5125 rq->next_balance = next_balance;
5126}
5127
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005128#ifdef CONFIG_NO_HZ
5129/*
5130 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5131 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5132 */
5133static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5134{
5135 struct rq *this_rq = cpu_rq(this_cpu);
5136 struct rq *rq;
5137 int balance_cpu;
5138
Suresh Siddha1c792db2011-12-01 17:07:32 -08005139 if (idle != CPU_IDLE ||
5140 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5141 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005142
5143 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005144 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005145 continue;
5146
5147 /*
5148 * If this cpu gets work to do, stop the load balancing
5149 * work being done for other cpus. Next load
5150 * balancing owner will pick it up.
5151 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005152 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005153 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005154
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005155 rq = cpu_rq(balance_cpu);
5156
5157 raw_spin_lock_irq(&rq->lock);
5158 update_rq_clock(rq);
5159 update_idle_cpu_load(rq);
5160 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005161
5162 rebalance_domains(balance_cpu, CPU_IDLE);
5163
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005164 if (time_after(this_rq->next_balance, rq->next_balance))
5165 this_rq->next_balance = rq->next_balance;
5166 }
5167 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005168end:
5169 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005170}
5171
5172/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005173 * Current heuristic for kicking the idle load balancer in the presence
5174 * of an idle cpu is the system.
5175 * - This rq has more than one task.
5176 * - At any scheduler domain level, this cpu's scheduler group has multiple
5177 * busy cpu's exceeding the group's power.
5178 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5179 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005180 */
5181static inline int nohz_kick_needed(struct rq *rq, int cpu)
5182{
5183 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005184 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005185
Suresh Siddha1c792db2011-12-01 17:07:32 -08005186 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005187 return 0;
5188
Suresh Siddha1c792db2011-12-01 17:07:32 -08005189 /*
5190 * We may be recently in ticked or tickless idle mode. At the first
5191 * busy tick after returning from idle, we will update the busy stats.
5192 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005193 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005194 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005195
5196 /*
5197 * None are in tickless mode and hence no need for NOHZ idle load
5198 * balancing.
5199 */
5200 if (likely(!atomic_read(&nohz.nr_cpus)))
5201 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005202
5203 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005204 return 0;
5205
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005206 if (rq->nr_running >= 2)
5207 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005208
Peter Zijlstra067491b2011-12-07 14:32:08 +01005209 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005210 for_each_domain(cpu, sd) {
5211 struct sched_group *sg = sd->groups;
5212 struct sched_group_power *sgp = sg->sgp;
5213 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005214
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005215 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005216 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005217
5218 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5219 && (cpumask_first_and(nohz.idle_cpus_mask,
5220 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005221 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005222
5223 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5224 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005225 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005226 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005227 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005228
5229need_kick_unlock:
5230 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005231need_kick:
5232 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005233}
5234#else
5235static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5236#endif
5237
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005238/*
5239 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005240 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005241 */
5242static void run_rebalance_domains(struct softirq_action *h)
5243{
5244 int this_cpu = smp_processor_id();
5245 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005246 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005247 CPU_IDLE : CPU_NOT_IDLE;
5248
5249 rebalance_domains(this_cpu, idle);
5250
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005251 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005252 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005253 * balancing on behalf of the other idle cpus whose ticks are
5254 * stopped.
5255 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005256 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005257}
5258
5259static inline int on_null_domain(int cpu)
5260{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005261 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005262}
5263
5264/*
5265 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005266 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005267void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005268{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005269 /* Don't need to rebalance while attached to NULL domain */
5270 if (time_after_eq(jiffies, rq->next_balance) &&
5271 likely(!on_null_domain(cpu)))
5272 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005273#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005274 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005275 nohz_balancer_kick(cpu);
5276#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005277}
5278
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005279static void rq_online_fair(struct rq *rq)
5280{
5281 update_sysctl();
5282}
5283
5284static void rq_offline_fair(struct rq *rq)
5285{
5286 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005287
5288 /* Ensure any throttled groups are reachable by pick_next_task */
5289 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005290}
5291
Dhaval Giani55e12e52008-06-24 23:39:43 +05305292#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005293
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005294/*
5295 * scheduler tick hitting a task of our scheduling class:
5296 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005297static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005298{
5299 struct cfs_rq *cfs_rq;
5300 struct sched_entity *se = &curr->se;
5301
5302 for_each_sched_entity(se) {
5303 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005304 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005305 }
Ben Segall18bf2802012-10-04 12:51:20 +02005306
5307 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005308}
5309
5310/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005311 * called on fork with the child task as argument from the parent's context
5312 * - child not yet on the tasklist
5313 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005314 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005315static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005316{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005317 struct cfs_rq *cfs_rq;
5318 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005319 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005320 struct rq *rq = this_rq();
5321 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005322
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005323 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005324
Peter Zijlstra861d0342010-08-19 13:31:43 +02005325 update_rq_clock(rq);
5326
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005327 cfs_rq = task_cfs_rq(current);
5328 curr = cfs_rq->curr;
5329
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005330 if (unlikely(task_cpu(p) != this_cpu)) {
5331 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005332 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005333 rcu_read_unlock();
5334 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005335
Ting Yang7109c442007-08-28 12:53:24 +02005336 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005337
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005338 if (curr)
5339 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005340 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005341
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005342 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005343 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005344 * Upon rescheduling, sched_class::put_prev_task() will place
5345 * 'current' within the tree based on its new key value.
5346 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005347 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305348 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005349 }
5350
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005351 se->vruntime -= cfs_rq->min_vruntime;
5352
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005353 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005354}
5355
Steven Rostedtcb469842008-01-25 21:08:22 +01005356/*
5357 * Priority of the task has changed. Check to see if we preempt
5358 * the current task.
5359 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005360static void
5361prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005362{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005363 if (!p->se.on_rq)
5364 return;
5365
Steven Rostedtcb469842008-01-25 21:08:22 +01005366 /*
5367 * Reschedule if we are currently running on this runqueue and
5368 * our priority decreased, or if we are not currently running on
5369 * this runqueue and our priority is higher than the current's
5370 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005371 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005372 if (p->prio > oldprio)
5373 resched_task(rq->curr);
5374 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005375 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005376}
5377
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005378static void switched_from_fair(struct rq *rq, struct task_struct *p)
5379{
5380 struct sched_entity *se = &p->se;
5381 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5382
5383 /*
5384 * Ensure the task's vruntime is normalized, so that when its
5385 * switched back to the fair class the enqueue_entity(.flags=0) will
5386 * do the right thing.
5387 *
5388 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5389 * have normalized the vruntime, if it was !on_rq, then only when
5390 * the task is sleeping will it still have non-normalized vruntime.
5391 */
5392 if (!se->on_rq && p->state != TASK_RUNNING) {
5393 /*
5394 * Fix up our vruntime so that the current sleep doesn't
5395 * cause 'unlimited' sleep bonus.
5396 */
5397 place_entity(cfs_rq, se, 0);
5398 se->vruntime -= cfs_rq->min_vruntime;
5399 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005400
5401#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5402 /*
5403 * Remove our load from contribution when we leave sched_fair
5404 * and ensure we don't carry in an old decay_count if we
5405 * switch back.
5406 */
5407 if (p->se.avg.decay_count) {
5408 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5409 __synchronize_entity_decay(&p->se);
5410 subtract_blocked_load_contrib(cfs_rq,
5411 p->se.avg.load_avg_contrib);
5412 }
5413#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005414}
5415
Steven Rostedtcb469842008-01-25 21:08:22 +01005416/*
5417 * We switched to the sched_fair class.
5418 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005419static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005420{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005421 if (!p->se.on_rq)
5422 return;
5423
Steven Rostedtcb469842008-01-25 21:08:22 +01005424 /*
5425 * We were most likely switched from sched_rt, so
5426 * kick off the schedule if running, otherwise just see
5427 * if we can still preempt the current task.
5428 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005429 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005430 resched_task(rq->curr);
5431 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005432 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005433}
5434
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005435/* Account for a task changing its policy or group.
5436 *
5437 * This routine is mostly called to set cfs_rq->curr field when a task
5438 * migrates between groups/classes.
5439 */
5440static void set_curr_task_fair(struct rq *rq)
5441{
5442 struct sched_entity *se = &rq->curr->se;
5443
Paul Turnerec12cb72011-07-21 09:43:30 -07005444 for_each_sched_entity(se) {
5445 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5446
5447 set_next_entity(cfs_rq, se);
5448 /* ensure bandwidth has been allocated on our new cfs_rq */
5449 account_cfs_rq_runtime(cfs_rq, 0);
5450 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005451}
5452
Peter Zijlstra029632f2011-10-25 10:00:11 +02005453void init_cfs_rq(struct cfs_rq *cfs_rq)
5454{
5455 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005456 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5457#ifndef CONFIG_64BIT
5458 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5459#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005460#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5461 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005462 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005463#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005464}
5465
Peter Zijlstra810b3812008-02-29 15:21:01 -05005466#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005467static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005468{
Paul Turneraff3e492012-10-04 13:18:30 +02005469 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005470 /*
5471 * If the task was not on the rq at the time of this cgroup movement
5472 * it must have been asleep, sleeping tasks keep their ->vruntime
5473 * absolute on their old rq until wakeup (needed for the fair sleeper
5474 * bonus in place_entity()).
5475 *
5476 * If it was on the rq, we've just 'preempted' it, which does convert
5477 * ->vruntime to a relative base.
5478 *
5479 * Make sure both cases convert their relative position when migrating
5480 * to another cgroup's rq. This does somewhat interfere with the
5481 * fair sleeper stuff for the first placement, but who cares.
5482 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005483 /*
5484 * When !on_rq, vruntime of the task has usually NOT been normalized.
5485 * But there are some cases where it has already been normalized:
5486 *
5487 * - Moving a forked child which is waiting for being woken up by
5488 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005489 * - Moving a task which has been woken up by try_to_wake_up() and
5490 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005491 *
5492 * To prevent boost or penalty in the new cfs_rq caused by delta
5493 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5494 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005495 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005496 on_rq = 1;
5497
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005498 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005499 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5500 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005501 if (!on_rq) {
5502 cfs_rq = cfs_rq_of(&p->se);
5503 p->se.vruntime += cfs_rq->min_vruntime;
5504#ifdef CONFIG_SMP
5505 /*
5506 * migrate_task_rq_fair() will have removed our previous
5507 * contribution, but we must synchronize for ongoing future
5508 * decay.
5509 */
5510 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5511 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5512#endif
5513 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005514}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005515
5516void free_fair_sched_group(struct task_group *tg)
5517{
5518 int i;
5519
5520 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5521
5522 for_each_possible_cpu(i) {
5523 if (tg->cfs_rq)
5524 kfree(tg->cfs_rq[i]);
5525 if (tg->se)
5526 kfree(tg->se[i]);
5527 }
5528
5529 kfree(tg->cfs_rq);
5530 kfree(tg->se);
5531}
5532
5533int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5534{
5535 struct cfs_rq *cfs_rq;
5536 struct sched_entity *se;
5537 int i;
5538
5539 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5540 if (!tg->cfs_rq)
5541 goto err;
5542 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5543 if (!tg->se)
5544 goto err;
5545
5546 tg->shares = NICE_0_LOAD;
5547
5548 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5549
5550 for_each_possible_cpu(i) {
5551 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5552 GFP_KERNEL, cpu_to_node(i));
5553 if (!cfs_rq)
5554 goto err;
5555
5556 se = kzalloc_node(sizeof(struct sched_entity),
5557 GFP_KERNEL, cpu_to_node(i));
5558 if (!se)
5559 goto err_free_rq;
5560
5561 init_cfs_rq(cfs_rq);
5562 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5563 }
5564
5565 return 1;
5566
5567err_free_rq:
5568 kfree(cfs_rq);
5569err:
5570 return 0;
5571}
5572
5573void unregister_fair_sched_group(struct task_group *tg, int cpu)
5574{
5575 struct rq *rq = cpu_rq(cpu);
5576 unsigned long flags;
5577
5578 /*
5579 * Only empty task groups can be destroyed; so we can speculatively
5580 * check on_list without danger of it being re-added.
5581 */
5582 if (!tg->cfs_rq[cpu]->on_list)
5583 return;
5584
5585 raw_spin_lock_irqsave(&rq->lock, flags);
5586 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5587 raw_spin_unlock_irqrestore(&rq->lock, flags);
5588}
5589
5590void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5591 struct sched_entity *se, int cpu,
5592 struct sched_entity *parent)
5593{
5594 struct rq *rq = cpu_rq(cpu);
5595
5596 cfs_rq->tg = tg;
5597 cfs_rq->rq = rq;
5598#ifdef CONFIG_SMP
5599 /* allow initial update_cfs_load() to truncate */
5600 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005601#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005602 init_cfs_rq_runtime(cfs_rq);
5603
5604 tg->cfs_rq[cpu] = cfs_rq;
5605 tg->se[cpu] = se;
5606
5607 /* se could be NULL for root_task_group */
5608 if (!se)
5609 return;
5610
5611 if (!parent)
5612 se->cfs_rq = &rq->cfs;
5613 else
5614 se->cfs_rq = parent->my_q;
5615
5616 se->my_q = cfs_rq;
5617 update_load_set(&se->load, 0);
5618 se->parent = parent;
5619}
5620
5621static DEFINE_MUTEX(shares_mutex);
5622
5623int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5624{
5625 int i;
5626 unsigned long flags;
5627
5628 /*
5629 * We can't change the weight of the root cgroup.
5630 */
5631 if (!tg->se[0])
5632 return -EINVAL;
5633
5634 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5635
5636 mutex_lock(&shares_mutex);
5637 if (tg->shares == shares)
5638 goto done;
5639
5640 tg->shares = shares;
5641 for_each_possible_cpu(i) {
5642 struct rq *rq = cpu_rq(i);
5643 struct sched_entity *se;
5644
5645 se = tg->se[i];
5646 /* Propagate contribution to hierarchy */
5647 raw_spin_lock_irqsave(&rq->lock, flags);
5648 for_each_sched_entity(se)
5649 update_cfs_shares(group_cfs_rq(se));
5650 raw_spin_unlock_irqrestore(&rq->lock, flags);
5651 }
5652
5653done:
5654 mutex_unlock(&shares_mutex);
5655 return 0;
5656}
5657#else /* CONFIG_FAIR_GROUP_SCHED */
5658
5659void free_fair_sched_group(struct task_group *tg) { }
5660
5661int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5662{
5663 return 1;
5664}
5665
5666void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5667
5668#endif /* CONFIG_FAIR_GROUP_SCHED */
5669
Peter Zijlstra810b3812008-02-29 15:21:01 -05005670
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005671static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005672{
5673 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005674 unsigned int rr_interval = 0;
5675
5676 /*
5677 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5678 * idle runqueue:
5679 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005680 if (rq->cfs.load.weight)
5681 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005682
5683 return rr_interval;
5684}
5685
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005686/*
5687 * All the scheduling class methods:
5688 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005689const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005690 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005691 .enqueue_task = enqueue_task_fair,
5692 .dequeue_task = dequeue_task_fair,
5693 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005694 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005695
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005696 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005697
5698 .pick_next_task = pick_next_task_fair,
5699 .put_prev_task = put_prev_task_fair,
5700
Peter Williams681f3e62007-10-24 18:23:51 +02005701#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005702 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005703 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005704
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005705 .rq_online = rq_online_fair,
5706 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005707
5708 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005709#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005710
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005711 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005712 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005713 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005714
5715 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005716 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005717 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005718
Peter Williams0d721ce2009-09-21 01:31:53 +00005719 .get_rr_interval = get_rr_interval_fair,
5720
Peter Zijlstra810b3812008-02-29 15:21:01 -05005721#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005722 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005723#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005724};
5725
5726#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005727void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005728{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005729 struct cfs_rq *cfs_rq;
5730
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005731 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005732 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005733 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005734 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005735}
5736#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005737
5738__init void init_sched_fair_class(void)
5739{
5740#ifdef CONFIG_SMP
5741 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5742
5743#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005744 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005745 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005746 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005747#endif
5748#endif /* SMP */
5749
5750}