blob: 5e602e6ba0c3f0fa3e26c8aa05cebb791448014b [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 Turner9ee474f2012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq);
263
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800264static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
265{
266 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800267 /*
268 * Ensure we either appear before our parent (if already
269 * enqueued) or force our parent to appear after us when it is
270 * enqueued. The fact that we always enqueue bottom-up
271 * reduces this to two cases.
272 */
273 if (cfs_rq->tg->parent &&
274 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
275 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800276 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800277 } else {
278 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
279 &rq_of(cfs_rq)->leaf_cfs_rq_list);
280 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800281
282 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200283 /* We should have no load, but we need to update last_decay. */
284 update_cfs_rq_blocked_load(cfs_rq);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800285 }
286}
287
288static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
289{
290 if (cfs_rq->on_list) {
291 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
292 cfs_rq->on_list = 0;
293 }
294}
295
Peter Zijlstrab7581492008-04-19 19:45:00 +0200296/* Iterate thr' all leaf cfs_rq's on a runqueue */
297#define for_each_leaf_cfs_rq(rq, cfs_rq) \
298 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
299
300/* Do the two (enqueued) entities belong to the same group ? */
301static inline int
302is_same_group(struct sched_entity *se, struct sched_entity *pse)
303{
304 if (se->cfs_rq == pse->cfs_rq)
305 return 1;
306
307 return 0;
308}
309
310static inline struct sched_entity *parent_entity(struct sched_entity *se)
311{
312 return se->parent;
313}
314
Peter Zijlstra464b7522008-10-24 11:06:15 +0200315/* return depth at which a sched entity is present in the hierarchy */
316static inline int depth_se(struct sched_entity *se)
317{
318 int depth = 0;
319
320 for_each_sched_entity(se)
321 depth++;
322
323 return depth;
324}
325
326static void
327find_matching_se(struct sched_entity **se, struct sched_entity **pse)
328{
329 int se_depth, pse_depth;
330
331 /*
332 * preemption test can be made between sibling entities who are in the
333 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
334 * both tasks until we find their ancestors who are siblings of common
335 * parent.
336 */
337
338 /* First walk up until both entities are at same depth */
339 se_depth = depth_se(*se);
340 pse_depth = depth_se(*pse);
341
342 while (se_depth > pse_depth) {
343 se_depth--;
344 *se = parent_entity(*se);
345 }
346
347 while (pse_depth > se_depth) {
348 pse_depth--;
349 *pse = parent_entity(*pse);
350 }
351
352 while (!is_same_group(*se, *pse)) {
353 *se = parent_entity(*se);
354 *pse = parent_entity(*pse);
355 }
356}
357
Peter Zijlstra8f488942009-07-24 12:25:30 +0200358#else /* !CONFIG_FAIR_GROUP_SCHED */
359
360static inline struct task_struct *task_of(struct sched_entity *se)
361{
362 return container_of(se, struct task_struct, se);
363}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200364
365static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
366{
367 return container_of(cfs_rq, struct rq, cfs);
368}
369
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200370#define entity_is_task(se) 1
371
Peter Zijlstrab7581492008-04-19 19:45:00 +0200372#define for_each_sched_entity(se) \
373 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374
Peter Zijlstrab7581492008-04-19 19:45:00 +0200375static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200376{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200377 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200378}
379
Peter Zijlstrab7581492008-04-19 19:45:00 +0200380static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
381{
382 struct task_struct *p = task_of(se);
383 struct rq *rq = task_rq(p);
384
385 return &rq->cfs;
386}
387
388/* runqueue "owned" by this group */
389static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
390{
391 return NULL;
392}
393
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800394static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
395{
396}
397
398static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
399{
400}
401
Peter Zijlstrab7581492008-04-19 19:45:00 +0200402#define for_each_leaf_cfs_rq(rq, cfs_rq) \
403 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
404
405static inline int
406is_same_group(struct sched_entity *se, struct sched_entity *pse)
407{
408 return 1;
409}
410
411static inline struct sched_entity *parent_entity(struct sched_entity *se)
412{
413 return NULL;
414}
415
Peter Zijlstra464b7522008-10-24 11:06:15 +0200416static inline void
417find_matching_se(struct sched_entity **se, struct sched_entity **pse)
418{
419}
420
Peter Zijlstrab7581492008-04-19 19:45:00 +0200421#endif /* CONFIG_FAIR_GROUP_SCHED */
422
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700423static __always_inline
424void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200425
426/**************************************************************
427 * Scheduling class tree data structure manipulation methods:
428 */
429
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200430static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200431{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200432 s64 delta = (s64)(vruntime - min_vruntime);
433 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200434 min_vruntime = vruntime;
435
436 return min_vruntime;
437}
438
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200439static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200440{
441 s64 delta = (s64)(vruntime - min_vruntime);
442 if (delta < 0)
443 min_vruntime = vruntime;
444
445 return min_vruntime;
446}
447
Fabio Checconi54fdc582009-07-16 12:32:27 +0200448static inline int entity_before(struct sched_entity *a,
449 struct sched_entity *b)
450{
451 return (s64)(a->vruntime - b->vruntime) < 0;
452}
453
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200454static void update_min_vruntime(struct cfs_rq *cfs_rq)
455{
456 u64 vruntime = cfs_rq->min_vruntime;
457
458 if (cfs_rq->curr)
459 vruntime = cfs_rq->curr->vruntime;
460
461 if (cfs_rq->rb_leftmost) {
462 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
463 struct sched_entity,
464 run_node);
465
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100466 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200467 vruntime = se->vruntime;
468 else
469 vruntime = min_vruntime(vruntime, se->vruntime);
470 }
471
472 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200473#ifndef CONFIG_64BIT
474 smp_wmb();
475 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
476#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200477}
478
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200479/*
480 * Enqueue an entity into the rb-tree:
481 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200482static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200483{
484 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
485 struct rb_node *parent = NULL;
486 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200487 int leftmost = 1;
488
489 /*
490 * Find the right place in the rbtree:
491 */
492 while (*link) {
493 parent = *link;
494 entry = rb_entry(parent, struct sched_entity, run_node);
495 /*
496 * We dont care about collisions. Nodes with
497 * the same key stay together.
498 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200499 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200500 link = &parent->rb_left;
501 } else {
502 link = &parent->rb_right;
503 leftmost = 0;
504 }
505 }
506
507 /*
508 * Maintain a cache of leftmost tree entries (it is frequently
509 * used):
510 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200511 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200512 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200513
514 rb_link_node(&se->run_node, parent, link);
515 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200516}
517
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200518static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200519{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100520 if (cfs_rq->rb_leftmost == &se->run_node) {
521 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100522
523 next_node = rb_next(&se->run_node);
524 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100525 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200526
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200527 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528}
529
Peter Zijlstra029632f2011-10-25 10:00:11 +0200530struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100532 struct rb_node *left = cfs_rq->rb_leftmost;
533
534 if (!left)
535 return NULL;
536
537 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200538}
539
Rik van Rielac53db52011-02-01 09:51:03 -0500540static struct sched_entity *__pick_next_entity(struct sched_entity *se)
541{
542 struct rb_node *next = rb_next(&se->run_node);
543
544 if (!next)
545 return NULL;
546
547 return rb_entry(next, struct sched_entity, run_node);
548}
549
550#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200551struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200552{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100553 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200554
Balbir Singh70eee742008-02-22 13:25:53 +0530555 if (!last)
556 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100557
558 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200559}
560
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200561/**************************************************************
562 * Scheduling class statistics methods:
563 */
564
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100565int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700566 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100567 loff_t *ppos)
568{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700569 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100570 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100571
572 if (ret || !write)
573 return ret;
574
575 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
576 sysctl_sched_min_granularity);
577
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100578#define WRT_SYSCTL(name) \
579 (normalized_sysctl_##name = sysctl_##name / (factor))
580 WRT_SYSCTL(sched_min_granularity);
581 WRT_SYSCTL(sched_latency);
582 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100583#undef WRT_SYSCTL
584
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100585 return 0;
586}
587#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200588
589/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200590 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200591 */
592static inline unsigned long
593calc_delta_fair(unsigned long delta, struct sched_entity *se)
594{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200595 if (unlikely(se->load.weight != NICE_0_LOAD))
596 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200597
598 return delta;
599}
600
601/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200602 * The idea is to set a period in which each task runs once.
603 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200604 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200605 * this period because otherwise the slices get too small.
606 *
607 * p = (nr <= nl) ? l : l*nr/nl
608 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200609static u64 __sched_period(unsigned long nr_running)
610{
611 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100612 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200613
614 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100615 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200616 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 }
618
619 return period;
620}
621
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200622/*
623 * We calculate the wall-time slice from the period by taking a part
624 * proportional to the weight.
625 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200626 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200627 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200628static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200629{
Mike Galbraith0a582442009-01-02 12:16:42 +0100630 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200631
Mike Galbraith0a582442009-01-02 12:16:42 +0100632 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100633 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200634 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100635
636 cfs_rq = cfs_rq_of(se);
637 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200638
Mike Galbraith0a582442009-01-02 12:16:42 +0100639 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200640 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100641
642 update_load_add(&lw, se->load.weight);
643 load = &lw;
644 }
645 slice = calc_delta_mine(slice, se->load.weight, load);
646 }
647 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200648}
649
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200650/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200651 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200652 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200653 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200654 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200655static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200656{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200657 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200658}
659
Paul Turnerd6b55912010-11-15 15:47:09 -0800660static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800661static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800662
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200663/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200664 * Update the current task's runtime statistics. Skip current tasks that
665 * are not in our scheduling class.
666 */
667static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200668__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
669 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200670{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200671 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200672
Lucas De Marchi41acab82010-03-10 23:37:45 -0300673 schedstat_set(curr->statistics.exec_max,
674 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200675
676 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200677 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200678 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100679
Ingo Molnare9acbff2007-10-15 17:00:04 +0200680 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200681 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800682
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100683#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800684 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800685#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200686}
687
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200688static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200689{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200690 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700691 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200692 unsigned long delta_exec;
693
694 if (unlikely(!curr))
695 return;
696
697 /*
698 * Get the amount of time the current task was running
699 * since the last time we changed load (this cannot
700 * overflow on 32 bits):
701 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200702 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100703 if (!delta_exec)
704 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200705
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200706 __update_curr(cfs_rq, curr, delta_exec);
707 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100708
709 if (entity_is_task(curr)) {
710 struct task_struct *curtask = task_of(curr);
711
Ingo Molnarf977bb42009-09-13 18:15:54 +0200712 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100713 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700714 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100715 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700716
717 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200718}
719
720static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200721update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200722{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300723 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200724}
725
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726/*
727 * Task is being enqueued - update stats:
728 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200729static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200730{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731 /*
732 * Are we enqueueing a waiting task? (for current tasks
733 * a dequeue/enqueue event is a NOP)
734 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200735 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200736 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200737}
738
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200739static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200740update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200741{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300742 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
743 rq_of(cfs_rq)->clock - se->statistics.wait_start));
744 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
745 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
746 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200747#ifdef CONFIG_SCHEDSTATS
748 if (entity_is_task(se)) {
749 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300750 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200751 }
752#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300753 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200754}
755
756static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200757update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200758{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200759 /*
760 * Mark the end of the wait period if dequeueing a
761 * waiting task:
762 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200763 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200764 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200765}
766
767/*
768 * We are picking a new current task - update its stats:
769 */
770static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200771update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200772{
773 /*
774 * We are starting a new run period:
775 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700776 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200777}
778
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200779/**************************************************
780 * Scheduling class queueing methods:
781 */
782
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200783static void
784account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
785{
786 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200787 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200788 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100789#ifdef CONFIG_SMP
790 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200791 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100792#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200793 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200794}
795
796static void
797account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
798{
799 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200800 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200801 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100802 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530803 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200804 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200805}
806
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800807#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700808/* we need this in update_cfs_load and load-balance functions below */
809static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800810# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800811static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
812 int global_update)
813{
814 struct task_group *tg = cfs_rq->tg;
815 long load_avg;
816
817 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
818 load_avg -= cfs_rq->load_contribution;
819
820 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
821 atomic_add(load_avg, &tg->load_weight);
822 cfs_rq->load_contribution += load_avg;
823 }
824}
825
826static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800827{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800828 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800829 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800830 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800831
Paul Turner64660c82011-07-21 09:43:36 -0700832 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800833 return;
834
Paul Turner05ca62c2011-01-21 20:45:02 -0800835 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800836 delta = now - cfs_rq->load_stamp;
837
Paul Turnere33078b2010-11-15 15:47:04 -0800838 /* truncate load history at 4 idle periods */
839 if (cfs_rq->load_stamp > cfs_rq->load_last &&
840 now - cfs_rq->load_last > 4 * period) {
841 cfs_rq->load_period = 0;
842 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800843 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800844 }
845
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800846 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800847 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800848 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800849 if (load) {
850 cfs_rq->load_last = now;
851 cfs_rq->load_avg += delta * load;
852 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800853
Paul Turnerd6b55912010-11-15 15:47:09 -0800854 /* consider updating load contribution on each fold or truncate */
855 if (global_update || cfs_rq->load_period > period
856 || !cfs_rq->load_period)
857 update_cfs_rq_load_contribution(cfs_rq, global_update);
858
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800859 while (cfs_rq->load_period > period) {
860 /*
861 * Inline assembly required to prevent the compiler
862 * optimising this loop into a divmod call.
863 * See __iter_div_u64_rem() for another example of this.
864 */
865 asm("" : "+rm" (cfs_rq->load_period));
866 cfs_rq->load_period /= 2;
867 cfs_rq->load_avg /= 2;
868 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800869
Paul Turnere33078b2010-11-15 15:47:04 -0800870 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
871 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800872}
873
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200874static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
875{
876 long tg_weight;
877
878 /*
879 * Use this CPU's actual weight instead of the last load_contribution
880 * to gain a more accurate current total weight. See
881 * update_cfs_rq_load_contribution().
882 */
883 tg_weight = atomic_read(&tg->load_weight);
884 tg_weight -= cfs_rq->load_contribution;
885 tg_weight += cfs_rq->load.weight;
886
887 return tg_weight;
888}
889
Paul Turner6d5ab292011-01-21 20:45:01 -0800890static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800891{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200892 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800893
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200894 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800895 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800896
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200898 if (tg_weight)
899 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800900
901 if (shares < MIN_SHARES)
902 shares = MIN_SHARES;
903 if (shares > tg->shares)
904 shares = tg->shares;
905
906 return shares;
907}
908
909static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
910{
911 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
912 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800913 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800914 }
915}
916# else /* CONFIG_SMP */
917static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
918{
919}
920
Paul Turner6d5ab292011-01-21 20:45:01 -0800921static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800922{
923 return tg->shares;
924}
925
926static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
927{
928}
929# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800930static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
931 unsigned long weight)
932{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800933 if (se->on_rq) {
934 /* commit outstanding execution time */
935 if (cfs_rq->curr == se)
936 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800937 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800938 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800939
940 update_load_set(&se->load, weight);
941
942 if (se->on_rq)
943 account_entity_enqueue(cfs_rq, se);
944}
945
Paul Turner6d5ab292011-01-21 20:45:01 -0800946static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800947{
948 struct task_group *tg;
949 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800950 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800951
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800952 tg = cfs_rq->tg;
953 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700954 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800955 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800956#ifndef CONFIG_SMP
957 if (likely(se->load.weight == tg->shares))
958 return;
959#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800960 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800961
962 reweight_entity(cfs_rq_of(se), se, shares);
963}
964#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800965static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800966{
967}
968
Paul Turner6d5ab292011-01-21 20:45:01 -0800969static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800970{
971}
Paul Turner43365bd2010-12-15 19:10:17 -0800972
973static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
974{
975}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800976#endif /* CONFIG_FAIR_GROUP_SCHED */
977
Paul Turner9d85f212012-10-04 13:18:29 +0200978#ifdef CONFIG_SMP
979/*
980 * Approximate:
981 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
982 */
983static __always_inline u64 decay_load(u64 val, u64 n)
984{
985 for (; n && val; n--) {
986 val *= 4008;
987 val >>= 12;
988 }
989
990 return val;
991}
992
993/*
994 * We can represent the historical contribution to runnable average as the
995 * coefficients of a geometric series. To do this we sub-divide our runnable
996 * history into segments of approximately 1ms (1024us); label the segment that
997 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
998 *
999 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
1000 * p0 p1 p2
1001 * (now) (~1ms ago) (~2ms ago)
1002 *
1003 * Let u_i denote the fraction of p_i that the entity was runnable.
1004 *
1005 * We then designate the fractions u_i as our co-efficients, yielding the
1006 * following representation of historical load:
1007 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
1008 *
1009 * We choose y based on the with of a reasonably scheduling period, fixing:
1010 * y^32 = 0.5
1011 *
1012 * This means that the contribution to load ~32ms ago (u_32) will be weighted
1013 * approximately half as much as the contribution to load within the last ms
1014 * (u_0).
1015 *
1016 * When a period "rolls over" and we have new u_0`, multiplying the previous
1017 * sum again by y is sufficient to update:
1018 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1019 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1020 */
1021static __always_inline int __update_entity_runnable_avg(u64 now,
1022 struct sched_avg *sa,
1023 int runnable)
1024{
1025 u64 delta;
1026 int delta_w, decayed = 0;
1027
1028 delta = now - sa->last_runnable_update;
1029 /*
1030 * This should only happen when time goes backwards, which it
1031 * unfortunately does during sched clock init when we swap over to TSC.
1032 */
1033 if ((s64)delta < 0) {
1034 sa->last_runnable_update = now;
1035 return 0;
1036 }
1037
1038 /*
1039 * Use 1024ns as the unit of measurement since it's a reasonable
1040 * approximation of 1us and fast to compute.
1041 */
1042 delta >>= 10;
1043 if (!delta)
1044 return 0;
1045 sa->last_runnable_update = now;
1046
1047 /* delta_w is the amount already accumulated against our next period */
1048 delta_w = sa->runnable_avg_period % 1024;
1049 if (delta + delta_w >= 1024) {
1050 /* period roll-over */
1051 decayed = 1;
1052
1053 /*
1054 * Now that we know we're crossing a period boundary, figure
1055 * out how much from delta we need to complete the current
1056 * period and accrue it.
1057 */
1058 delta_w = 1024 - delta_w;
1059 BUG_ON(delta_w > delta);
1060 do {
1061 if (runnable)
1062 sa->runnable_avg_sum += delta_w;
1063 sa->runnable_avg_period += delta_w;
1064
1065 /*
1066 * Remainder of delta initiates a new period, roll over
1067 * the previous.
1068 */
1069 sa->runnable_avg_sum =
1070 decay_load(sa->runnable_avg_sum, 1);
1071 sa->runnable_avg_period =
1072 decay_load(sa->runnable_avg_period, 1);
1073
1074 delta -= delta_w;
1075 /* New period is empty */
1076 delta_w = 1024;
1077 } while (delta >= 1024);
1078 }
1079
1080 /* Remainder of delta accrued against u_0` */
1081 if (runnable)
1082 sa->runnable_avg_sum += delta;
1083 sa->runnable_avg_period += delta;
1084
1085 return decayed;
1086}
1087
Paul Turner9ee474f2012-10-04 13:18:30 +02001088/* Synchronize an entity's decay with its parenting cfs_rq.*/
1089static inline void __synchronize_entity_decay(struct sched_entity *se)
1090{
1091 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1092 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1093
1094 decays -= se->avg.decay_count;
1095 if (!decays)
1096 return;
1097
1098 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1099 se->avg.decay_count = 0;
1100}
1101
Paul Turner2dac7542012-10-04 13:18:30 +02001102/* Compute the current contribution to load_avg by se, return any delta */
1103static long __update_entity_load_avg_contrib(struct sched_entity *se)
1104{
1105 long old_contrib = se->avg.load_avg_contrib;
1106
1107 if (!entity_is_task(se))
1108 return 0;
1109
1110 se->avg.load_avg_contrib = div64_u64(se->avg.runnable_avg_sum *
1111 se->load.weight,
1112 se->avg.runnable_avg_period + 1);
1113
1114 return se->avg.load_avg_contrib - old_contrib;
1115}
1116
Paul Turner9ee474f2012-10-04 13:18:30 +02001117static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1118 long load_contrib)
1119{
1120 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1121 cfs_rq->blocked_load_avg -= load_contrib;
1122 else
1123 cfs_rq->blocked_load_avg = 0;
1124}
1125
Paul Turner9d85f212012-10-04 13:18:29 +02001126/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001127static inline void update_entity_load_avg(struct sched_entity *se,
1128 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001129{
Paul Turner2dac7542012-10-04 13:18:30 +02001130 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1131 long contrib_delta;
1132
1133 if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
1134 se->on_rq))
1135 return;
1136
1137 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001138
1139 if (!update_cfs_rq)
1140 return;
1141
Paul Turner2dac7542012-10-04 13:18:30 +02001142 if (se->on_rq)
1143 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001144 else
1145 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1146}
1147
1148/*
1149 * Decay the load contributed by all blocked children and account this so that
1150 * their contribution may appropriately discounted when they wake up.
1151 */
1152static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq)
1153{
1154 u64 now = rq_of(cfs_rq)->clock_task >> 20;
1155 u64 decays;
1156
1157 decays = now - cfs_rq->last_decay;
1158 if (!decays)
1159 return;
1160
1161 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1162 decays);
1163 atomic64_add(decays, &cfs_rq->decay_counter);
1164
1165 cfs_rq->last_decay = now;
Paul Turner9d85f212012-10-04 13:18:29 +02001166}
Ben Segall18bf2802012-10-04 12:51:20 +02001167
1168static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1169{
1170 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
1171}
Paul Turner2dac7542012-10-04 13:18:30 +02001172
1173/* Add the load generated by se into cfs_rq's child load-average */
1174static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001175 struct sched_entity *se,
1176 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001177{
Paul Turner9ee474f2012-10-04 13:18:30 +02001178 /* we track migrations using entity decay_count == 0 */
1179 if (unlikely(!se->avg.decay_count)) {
1180 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
1181 wakeup = 0;
1182 } else {
1183 __synchronize_entity_decay(se);
1184 }
1185
1186 if (wakeup)
1187 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
1188
1189 update_entity_load_avg(se, 0);
Paul Turner2dac7542012-10-04 13:18:30 +02001190 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001191 update_cfs_rq_blocked_load(cfs_rq);
Paul Turner2dac7542012-10-04 13:18:30 +02001192}
1193
Paul Turner9ee474f2012-10-04 13:18:30 +02001194/*
1195 * Remove se's load from this cfs_rq child load-average, if the entity is
1196 * transitioning to a blocked state we track its projected decay using
1197 * blocked_load_avg.
1198 */
Paul Turner2dac7542012-10-04 13:18:30 +02001199static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001200 struct sched_entity *se,
1201 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001202{
Paul Turner9ee474f2012-10-04 13:18:30 +02001203 update_entity_load_avg(se, 1);
1204
Paul Turner2dac7542012-10-04 13:18:30 +02001205 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001206 if (sleep) {
1207 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1208 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1209 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001210}
Paul Turner9d85f212012-10-04 13:18:29 +02001211#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001212static inline void update_entity_load_avg(struct sched_entity *se,
1213 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001214static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001215static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001216 struct sched_entity *se,
1217 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001218static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001219 struct sched_entity *se,
1220 int sleep) {}
1221static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001222#endif
1223
Ingo Molnar2396af62007-08-09 11:16:48 +02001224static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001225{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001226#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001227 struct task_struct *tsk = NULL;
1228
1229 if (entity_is_task(se))
1230 tsk = task_of(se);
1231
Lucas De Marchi41acab82010-03-10 23:37:45 -03001232 if (se->statistics.sleep_start) {
1233 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001234
1235 if ((s64)delta < 0)
1236 delta = 0;
1237
Lucas De Marchi41acab82010-03-10 23:37:45 -03001238 if (unlikely(delta > se->statistics.sleep_max))
1239 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001240
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001241 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001242 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001243
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001244 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001245 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001246 trace_sched_stat_sleep(tsk, delta);
1247 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001248 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001249 if (se->statistics.block_start) {
1250 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001251
1252 if ((s64)delta < 0)
1253 delta = 0;
1254
Lucas De Marchi41acab82010-03-10 23:37:45 -03001255 if (unlikely(delta > se->statistics.block_max))
1256 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001257
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001258 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001259 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001260
Peter Zijlstrae4143142009-07-23 20:13:26 +02001261 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001262 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001263 se->statistics.iowait_sum += delta;
1264 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001265 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001266 }
1267
Andrew Vaginb781a602011-11-28 12:03:35 +03001268 trace_sched_stat_blocked(tsk, delta);
1269
Peter Zijlstrae4143142009-07-23 20:13:26 +02001270 /*
1271 * Blocking time is in units of nanosecs, so shift by
1272 * 20 to get a milliseconds-range estimation of the
1273 * amount of time that the task spent sleeping:
1274 */
1275 if (unlikely(prof_on == SLEEP_PROFILING)) {
1276 profile_hits(SLEEP_PROFILING,
1277 (void *)get_wchan(tsk),
1278 delta >> 20);
1279 }
1280 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001281 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001282 }
1283#endif
1284}
1285
Peter Zijlstraddc97292007-10-15 17:00:10 +02001286static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1287{
1288#ifdef CONFIG_SCHED_DEBUG
1289 s64 d = se->vruntime - cfs_rq->min_vruntime;
1290
1291 if (d < 0)
1292 d = -d;
1293
1294 if (d > 3*sysctl_sched_latency)
1295 schedstat_inc(cfs_rq, nr_spread_over);
1296#endif
1297}
1298
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001299static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001300place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1301{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001302 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001303
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001304 /*
1305 * The 'current' period is already promised to the current tasks,
1306 * however the extra weight of the new task will slow them down a
1307 * little, place the new task so that it fits in the slot that
1308 * stays open at the end.
1309 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001310 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001311 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001312
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001313 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001314 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001315 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001316
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001317 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001318 * Halve their sleep time's effect, to allow
1319 * for a gentler effect of sleepers:
1320 */
1321 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1322 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001323
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001324 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001325 }
1326
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001327 /* ensure we never gain time by being placed backwards. */
1328 vruntime = max_vruntime(se->vruntime, vruntime);
1329
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001330 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001331}
1332
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001333static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1334
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001335static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001336enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001337{
1338 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001339 * Update the normalized vruntime before updating min_vruntime
1340 * through callig update_curr().
1341 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001342 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001343 se->vruntime += cfs_rq->min_vruntime;
1344
1345 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001346 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001347 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001348 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001349 update_cfs_load(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02001350 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001351 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001352 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001353
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001354 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001355 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001356 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001357 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001358
Ingo Molnard2417e52007-08-09 11:16:47 +02001359 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001360 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001361 if (se != cfs_rq->curr)
1362 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001363 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001364
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001365 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001366 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001367 check_enqueue_throttle(cfs_rq);
1368 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001369}
1370
Rik van Riel2c13c9192011-02-01 09:48:37 -05001371static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001372{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001373 for_each_sched_entity(se) {
1374 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1375 if (cfs_rq->last == se)
1376 cfs_rq->last = NULL;
1377 else
1378 break;
1379 }
1380}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001381
Rik van Riel2c13c9192011-02-01 09:48:37 -05001382static void __clear_buddies_next(struct sched_entity *se)
1383{
1384 for_each_sched_entity(se) {
1385 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1386 if (cfs_rq->next == se)
1387 cfs_rq->next = NULL;
1388 else
1389 break;
1390 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001391}
1392
Rik van Rielac53db52011-02-01 09:51:03 -05001393static void __clear_buddies_skip(struct sched_entity *se)
1394{
1395 for_each_sched_entity(se) {
1396 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1397 if (cfs_rq->skip == se)
1398 cfs_rq->skip = NULL;
1399 else
1400 break;
1401 }
1402}
1403
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001404static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1405{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001406 if (cfs_rq->last == se)
1407 __clear_buddies_last(se);
1408
1409 if (cfs_rq->next == se)
1410 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001411
1412 if (cfs_rq->skip == se)
1413 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001414}
1415
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001416static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001417
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001418static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001419dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001420{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001421 /*
1422 * Update run-time statistics of the 'current'.
1423 */
1424 update_curr(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02001425 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001426
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001427 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001428 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001429#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001430 if (entity_is_task(se)) {
1431 struct task_struct *tsk = task_of(se);
1432
1433 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001434 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001435 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001436 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001437 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001438#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001439 }
1440
Peter Zijlstra2002c692008-11-11 11:52:33 +01001441 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001442
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001443 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001444 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001445 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001446 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001447 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001448
1449 /*
1450 * Normalize the entity after updating the min_vruntime because the
1451 * update can refer to the ->curr item and we need to reflect this
1452 * movement in our normalized position.
1453 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001454 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001455 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001456
Paul Turnerd8b49862011-07-21 09:43:41 -07001457 /* return excess runtime on last dequeue */
1458 return_cfs_rq_runtime(cfs_rq);
1459
Peter Zijlstra1e876232011-05-17 16:21:10 -07001460 update_min_vruntime(cfs_rq);
1461 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001462}
1463
1464/*
1465 * Preempt the current task with a newly woken task if needed:
1466 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001467static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001468check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001469{
Peter Zijlstra11697832007-09-05 14:32:49 +02001470 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001471 struct sched_entity *se;
1472 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001473
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001474 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001475 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001476 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001477 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001478 /*
1479 * The current task ran long enough, ensure it doesn't get
1480 * re-elected due to buddy favours.
1481 */
1482 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001483 return;
1484 }
1485
1486 /*
1487 * Ensure that a task that missed wakeup preemption by a
1488 * narrow margin doesn't have to wait for a full slice.
1489 * This also mitigates buddy induced latencies under load.
1490 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001491 if (delta_exec < sysctl_sched_min_granularity)
1492 return;
1493
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001494 se = __pick_first_entity(cfs_rq);
1495 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001496
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001497 if (delta < 0)
1498 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001499
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001500 if (delta > ideal_runtime)
1501 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001502}
1503
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001504static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001505set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001506{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001507 /* 'current' is not kept within the tree. */
1508 if (se->on_rq) {
1509 /*
1510 * Any task has to be enqueued before it get to execute on
1511 * a CPU. So account for the time it spent waiting on the
1512 * runqueue.
1513 */
1514 update_stats_wait_end(cfs_rq, se);
1515 __dequeue_entity(cfs_rq, se);
1516 }
1517
Ingo Molnar79303e92007-08-09 11:16:47 +02001518 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001519 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001520#ifdef CONFIG_SCHEDSTATS
1521 /*
1522 * Track our maximum slice length, if the CPU's load is at
1523 * least twice that of our own weight (i.e. dont track it
1524 * when there are only lesser-weight tasks around):
1525 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001526 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001527 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001528 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1529 }
1530#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001531 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001532}
1533
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001534static int
1535wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1536
Rik van Rielac53db52011-02-01 09:51:03 -05001537/*
1538 * Pick the next process, keeping these things in mind, in this order:
1539 * 1) keep things fair between processes/task groups
1540 * 2) pick the "next" process, since someone really wants that to run
1541 * 3) pick the "last" process, for cache locality
1542 * 4) do not run the "skip" process, if something else is available
1543 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001544static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001545{
Rik van Rielac53db52011-02-01 09:51:03 -05001546 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001547 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001548
Rik van Rielac53db52011-02-01 09:51:03 -05001549 /*
1550 * Avoid running the skip buddy, if running something else can
1551 * be done without getting too unfair.
1552 */
1553 if (cfs_rq->skip == se) {
1554 struct sched_entity *second = __pick_next_entity(se);
1555 if (second && wakeup_preempt_entity(second, left) < 1)
1556 se = second;
1557 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001558
Mike Galbraithf685cea2009-10-23 23:09:22 +02001559 /*
1560 * Prefer last buddy, try to return the CPU to a preempted task.
1561 */
1562 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1563 se = cfs_rq->last;
1564
Rik van Rielac53db52011-02-01 09:51:03 -05001565 /*
1566 * Someone really wants this to run. If it's not unfair, run it.
1567 */
1568 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1569 se = cfs_rq->next;
1570
Mike Galbraithf685cea2009-10-23 23:09:22 +02001571 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001572
1573 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001574}
1575
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001576static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1577
Ingo Molnarab6cde22007-08-09 11:16:48 +02001578static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001579{
1580 /*
1581 * If still on the runqueue then deactivate_task()
1582 * was not called and update_curr() has to be done:
1583 */
1584 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001585 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001586
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001587 /* throttle cfs_rqs exceeding runtime */
1588 check_cfs_rq_runtime(cfs_rq);
1589
Peter Zijlstraddc97292007-10-15 17:00:10 +02001590 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001591 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001592 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001593 /* Put 'current' back into the tree. */
1594 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001595 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001596 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001597 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001598 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001599}
1600
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001601static void
1602entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001603{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001604 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001605 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001606 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001607 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001608
Paul Turner43365bd2010-12-15 19:10:17 -08001609 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001610 * Ensure that runnable average is periodically updated.
1611 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001612 update_entity_load_avg(curr, 1);
1613 update_cfs_rq_blocked_load(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02001614
1615 /*
Paul Turner43365bd2010-12-15 19:10:17 -08001616 * Update share accounting for long-running entities.
1617 */
1618 update_entity_shares_tick(cfs_rq);
1619
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001620#ifdef CONFIG_SCHED_HRTICK
1621 /*
1622 * queued ticks are scheduled to match the slice, so don't bother
1623 * validating it and just reschedule.
1624 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001625 if (queued) {
1626 resched_task(rq_of(cfs_rq)->curr);
1627 return;
1628 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001629 /*
1630 * don't let the period tick interfere with the hrtick preemption
1631 */
1632 if (!sched_feat(DOUBLE_TICK) &&
1633 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1634 return;
1635#endif
1636
Yong Zhang2c2efae2011-07-29 16:20:33 +08001637 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001638 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001639}
1640
Paul Turnerab84d312011-07-21 09:43:28 -07001641
1642/**************************************************
1643 * CFS bandwidth control machinery
1644 */
1645
1646#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001647
1648#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001649static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001650
1651static inline bool cfs_bandwidth_used(void)
1652{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001653 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001654}
1655
1656void account_cfs_bandwidth_used(int enabled, int was_enabled)
1657{
1658 /* only need to count groups transitioning between enabled/!enabled */
1659 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001660 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001661 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001662 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001663}
1664#else /* HAVE_JUMP_LABEL */
1665static bool cfs_bandwidth_used(void)
1666{
1667 return true;
1668}
1669
1670void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1671#endif /* HAVE_JUMP_LABEL */
1672
Paul Turnerab84d312011-07-21 09:43:28 -07001673/*
1674 * default period for cfs group bandwidth.
1675 * default: 0.1s, units: nanoseconds
1676 */
1677static inline u64 default_cfs_period(void)
1678{
1679 return 100000000ULL;
1680}
Paul Turnerec12cb72011-07-21 09:43:30 -07001681
1682static inline u64 sched_cfs_bandwidth_slice(void)
1683{
1684 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1685}
1686
Paul Turnera9cf55b2011-07-21 09:43:32 -07001687/*
1688 * Replenish runtime according to assigned quota and update expiration time.
1689 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1690 * additional synchronization around rq->lock.
1691 *
1692 * requires cfs_b->lock
1693 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001694void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001695{
1696 u64 now;
1697
1698 if (cfs_b->quota == RUNTIME_INF)
1699 return;
1700
1701 now = sched_clock_cpu(smp_processor_id());
1702 cfs_b->runtime = cfs_b->quota;
1703 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1704}
1705
Peter Zijlstra029632f2011-10-25 10:00:11 +02001706static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1707{
1708 return &tg->cfs_bandwidth;
1709}
1710
Paul Turner85dac902011-07-21 09:43:33 -07001711/* returns 0 on failure to allocate runtime */
1712static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001713{
1714 struct task_group *tg = cfs_rq->tg;
1715 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001716 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001717
1718 /* note: this is a positive sum as runtime_remaining <= 0 */
1719 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1720
1721 raw_spin_lock(&cfs_b->lock);
1722 if (cfs_b->quota == RUNTIME_INF)
1723 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001724 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001725 /*
1726 * If the bandwidth pool has become inactive, then at least one
1727 * period must have elapsed since the last consumption.
1728 * Refresh the global state and ensure bandwidth timer becomes
1729 * active.
1730 */
1731 if (!cfs_b->timer_active) {
1732 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001733 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001734 }
Paul Turner58088ad2011-07-21 09:43:31 -07001735
1736 if (cfs_b->runtime > 0) {
1737 amount = min(cfs_b->runtime, min_amount);
1738 cfs_b->runtime -= amount;
1739 cfs_b->idle = 0;
1740 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001741 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001742 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001743 raw_spin_unlock(&cfs_b->lock);
1744
1745 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001746 /*
1747 * we may have advanced our local expiration to account for allowed
1748 * spread between our sched_clock and the one on which runtime was
1749 * issued.
1750 */
1751 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1752 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001753
1754 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001755}
1756
1757/*
1758 * Note: This depends on the synchronization provided by sched_clock and the
1759 * fact that rq->clock snapshots this value.
1760 */
1761static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1762{
1763 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1764 struct rq *rq = rq_of(cfs_rq);
1765
1766 /* if the deadline is ahead of our clock, nothing to do */
1767 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1768 return;
1769
1770 if (cfs_rq->runtime_remaining < 0)
1771 return;
1772
1773 /*
1774 * If the local deadline has passed we have to consider the
1775 * possibility that our sched_clock is 'fast' and the global deadline
1776 * has not truly expired.
1777 *
1778 * Fortunately we can check determine whether this the case by checking
1779 * whether the global deadline has advanced.
1780 */
1781
1782 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1783 /* extend local deadline, drift is bounded above by 2 ticks */
1784 cfs_rq->runtime_expires += TICK_NSEC;
1785 } else {
1786 /* global deadline is ahead, expiration has passed */
1787 cfs_rq->runtime_remaining = 0;
1788 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001789}
1790
1791static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1792 unsigned long delta_exec)
1793{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001794 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001795 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001796 expire_cfs_rq_runtime(cfs_rq);
1797
1798 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001799 return;
1800
Paul Turner85dac902011-07-21 09:43:33 -07001801 /*
1802 * if we're unable to extend our runtime we resched so that the active
1803 * hierarchy can be throttled
1804 */
1805 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1806 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001807}
1808
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001809static __always_inline
1810void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001811{
Paul Turner56f570e2011-11-07 20:26:33 -08001812 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001813 return;
1814
1815 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1816}
1817
Paul Turner85dac902011-07-21 09:43:33 -07001818static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1819{
Paul Turner56f570e2011-11-07 20:26:33 -08001820 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001821}
1822
Paul Turner64660c82011-07-21 09:43:36 -07001823/* check whether cfs_rq, or any parent, is throttled */
1824static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1825{
Paul Turner56f570e2011-11-07 20:26:33 -08001826 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001827}
1828
1829/*
1830 * Ensure that neither of the group entities corresponding to src_cpu or
1831 * dest_cpu are members of a throttled hierarchy when performing group
1832 * load-balance operations.
1833 */
1834static inline int throttled_lb_pair(struct task_group *tg,
1835 int src_cpu, int dest_cpu)
1836{
1837 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1838
1839 src_cfs_rq = tg->cfs_rq[src_cpu];
1840 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1841
1842 return throttled_hierarchy(src_cfs_rq) ||
1843 throttled_hierarchy(dest_cfs_rq);
1844}
1845
1846/* updated child weight may affect parent so we have to do this bottom up */
1847static int tg_unthrottle_up(struct task_group *tg, void *data)
1848{
1849 struct rq *rq = data;
1850 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1851
1852 cfs_rq->throttle_count--;
1853#ifdef CONFIG_SMP
1854 if (!cfs_rq->throttle_count) {
1855 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1856
1857 /* leaving throttled state, advance shares averaging windows */
1858 cfs_rq->load_stamp += delta;
1859 cfs_rq->load_last += delta;
1860
1861 /* update entity weight now that we are on_rq again */
1862 update_cfs_shares(cfs_rq);
1863 }
1864#endif
1865
1866 return 0;
1867}
1868
1869static int tg_throttle_down(struct task_group *tg, void *data)
1870{
1871 struct rq *rq = data;
1872 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1873
1874 /* group is entering throttled state, record last load */
1875 if (!cfs_rq->throttle_count)
1876 update_cfs_load(cfs_rq, 0);
1877 cfs_rq->throttle_count++;
1878
1879 return 0;
1880}
1881
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001882static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001883{
1884 struct rq *rq = rq_of(cfs_rq);
1885 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1886 struct sched_entity *se;
1887 long task_delta, dequeue = 1;
1888
1889 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1890
1891 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001892 rcu_read_lock();
1893 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1894 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001895
1896 task_delta = cfs_rq->h_nr_running;
1897 for_each_sched_entity(se) {
1898 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1899 /* throttled entity or throttle-on-deactivate */
1900 if (!se->on_rq)
1901 break;
1902
1903 if (dequeue)
1904 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1905 qcfs_rq->h_nr_running -= task_delta;
1906
1907 if (qcfs_rq->load.weight)
1908 dequeue = 0;
1909 }
1910
1911 if (!se)
1912 rq->nr_running -= task_delta;
1913
1914 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001915 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001916 raw_spin_lock(&cfs_b->lock);
1917 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1918 raw_spin_unlock(&cfs_b->lock);
1919}
1920
Peter Zijlstra029632f2011-10-25 10:00:11 +02001921void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001922{
1923 struct rq *rq = rq_of(cfs_rq);
1924 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1925 struct sched_entity *se;
1926 int enqueue = 1;
1927 long task_delta;
1928
1929 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1930
1931 cfs_rq->throttled = 0;
1932 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001933 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001934 list_del_rcu(&cfs_rq->throttled_list);
1935 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001936 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001937
Paul Turner64660c82011-07-21 09:43:36 -07001938 update_rq_clock(rq);
1939 /* update hierarchical throttle state */
1940 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1941
Paul Turner671fd9d2011-07-21 09:43:34 -07001942 if (!cfs_rq->load.weight)
1943 return;
1944
1945 task_delta = cfs_rq->h_nr_running;
1946 for_each_sched_entity(se) {
1947 if (se->on_rq)
1948 enqueue = 0;
1949
1950 cfs_rq = cfs_rq_of(se);
1951 if (enqueue)
1952 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1953 cfs_rq->h_nr_running += task_delta;
1954
1955 if (cfs_rq_throttled(cfs_rq))
1956 break;
1957 }
1958
1959 if (!se)
1960 rq->nr_running += task_delta;
1961
1962 /* determine whether we need to wake up potentially idle cpu */
1963 if (rq->curr == rq->idle && rq->cfs.nr_running)
1964 resched_task(rq->curr);
1965}
1966
1967static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1968 u64 remaining, u64 expires)
1969{
1970 struct cfs_rq *cfs_rq;
1971 u64 runtime = remaining;
1972
1973 rcu_read_lock();
1974 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1975 throttled_list) {
1976 struct rq *rq = rq_of(cfs_rq);
1977
1978 raw_spin_lock(&rq->lock);
1979 if (!cfs_rq_throttled(cfs_rq))
1980 goto next;
1981
1982 runtime = -cfs_rq->runtime_remaining + 1;
1983 if (runtime > remaining)
1984 runtime = remaining;
1985 remaining -= runtime;
1986
1987 cfs_rq->runtime_remaining += runtime;
1988 cfs_rq->runtime_expires = expires;
1989
1990 /* we check whether we're throttled above */
1991 if (cfs_rq->runtime_remaining > 0)
1992 unthrottle_cfs_rq(cfs_rq);
1993
1994next:
1995 raw_spin_unlock(&rq->lock);
1996
1997 if (!remaining)
1998 break;
1999 }
2000 rcu_read_unlock();
2001
2002 return remaining;
2003}
2004
Paul Turner58088ad2011-07-21 09:43:31 -07002005/*
2006 * Responsible for refilling a task_group's bandwidth and unthrottling its
2007 * cfs_rqs as appropriate. If there has been no activity within the last
2008 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2009 * used to track this state.
2010 */
2011static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2012{
Paul Turner671fd9d2011-07-21 09:43:34 -07002013 u64 runtime, runtime_expires;
2014 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002015
2016 raw_spin_lock(&cfs_b->lock);
2017 /* no need to continue the timer with no bandwidth constraint */
2018 if (cfs_b->quota == RUNTIME_INF)
2019 goto out_unlock;
2020
Paul Turner671fd9d2011-07-21 09:43:34 -07002021 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2022 /* idle depends on !throttled (for the case of a large deficit) */
2023 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002024 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002025
Paul Turnera9cf55b2011-07-21 09:43:32 -07002026 /* if we're going inactive then everything else can be deferred */
2027 if (idle)
2028 goto out_unlock;
2029
2030 __refill_cfs_bandwidth_runtime(cfs_b);
2031
Paul Turner671fd9d2011-07-21 09:43:34 -07002032 if (!throttled) {
2033 /* mark as potentially idle for the upcoming period */
2034 cfs_b->idle = 1;
2035 goto out_unlock;
2036 }
Paul Turner58088ad2011-07-21 09:43:31 -07002037
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002038 /* account preceding periods in which throttling occurred */
2039 cfs_b->nr_throttled += overrun;
2040
Paul Turner671fd9d2011-07-21 09:43:34 -07002041 /*
2042 * There are throttled entities so we must first use the new bandwidth
2043 * to unthrottle them before making it generally available. This
2044 * ensures that all existing debts will be paid before a new cfs_rq is
2045 * allowed to run.
2046 */
2047 runtime = cfs_b->runtime;
2048 runtime_expires = cfs_b->runtime_expires;
2049 cfs_b->runtime = 0;
2050
2051 /*
2052 * This check is repeated as we are holding onto the new bandwidth
2053 * while we unthrottle. This can potentially race with an unthrottled
2054 * group trying to acquire new bandwidth from the global pool.
2055 */
2056 while (throttled && runtime > 0) {
2057 raw_spin_unlock(&cfs_b->lock);
2058 /* we can't nest cfs_b->lock while distributing bandwidth */
2059 runtime = distribute_cfs_runtime(cfs_b, runtime,
2060 runtime_expires);
2061 raw_spin_lock(&cfs_b->lock);
2062
2063 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2064 }
2065
2066 /* return (any) remaining runtime */
2067 cfs_b->runtime = runtime;
2068 /*
2069 * While we are ensured activity in the period following an
2070 * unthrottle, this also covers the case in which the new bandwidth is
2071 * insufficient to cover the existing bandwidth deficit. (Forcing the
2072 * timer to remain active while there are any throttled entities.)
2073 */
2074 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002075out_unlock:
2076 if (idle)
2077 cfs_b->timer_active = 0;
2078 raw_spin_unlock(&cfs_b->lock);
2079
2080 return idle;
2081}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002082
Paul Turnerd8b49862011-07-21 09:43:41 -07002083/* a cfs_rq won't donate quota below this amount */
2084static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2085/* minimum remaining period time to redistribute slack quota */
2086static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2087/* how long we wait to gather additional slack before distributing */
2088static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2089
2090/* are we near the end of the current quota period? */
2091static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2092{
2093 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2094 u64 remaining;
2095
2096 /* if the call-back is running a quota refresh is already occurring */
2097 if (hrtimer_callback_running(refresh_timer))
2098 return 1;
2099
2100 /* is a quota refresh about to occur? */
2101 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2102 if (remaining < min_expire)
2103 return 1;
2104
2105 return 0;
2106}
2107
2108static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2109{
2110 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2111
2112 /* if there's a quota refresh soon don't bother with slack */
2113 if (runtime_refresh_within(cfs_b, min_left))
2114 return;
2115
2116 start_bandwidth_timer(&cfs_b->slack_timer,
2117 ns_to_ktime(cfs_bandwidth_slack_period));
2118}
2119
2120/* we know any runtime found here is valid as update_curr() precedes return */
2121static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2122{
2123 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2124 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2125
2126 if (slack_runtime <= 0)
2127 return;
2128
2129 raw_spin_lock(&cfs_b->lock);
2130 if (cfs_b->quota != RUNTIME_INF &&
2131 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2132 cfs_b->runtime += slack_runtime;
2133
2134 /* we are under rq->lock, defer unthrottling using a timer */
2135 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2136 !list_empty(&cfs_b->throttled_cfs_rq))
2137 start_cfs_slack_bandwidth(cfs_b);
2138 }
2139 raw_spin_unlock(&cfs_b->lock);
2140
2141 /* even if it's not valid for return we don't want to try again */
2142 cfs_rq->runtime_remaining -= slack_runtime;
2143}
2144
2145static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2146{
Paul Turner56f570e2011-11-07 20:26:33 -08002147 if (!cfs_bandwidth_used())
2148 return;
2149
Paul Turnerfccfdc62011-11-07 20:26:34 -08002150 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002151 return;
2152
2153 __return_cfs_rq_runtime(cfs_rq);
2154}
2155
2156/*
2157 * This is done with a timer (instead of inline with bandwidth return) since
2158 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2159 */
2160static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2161{
2162 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2163 u64 expires;
2164
2165 /* confirm we're still not at a refresh boundary */
2166 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2167 return;
2168
2169 raw_spin_lock(&cfs_b->lock);
2170 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2171 runtime = cfs_b->runtime;
2172 cfs_b->runtime = 0;
2173 }
2174 expires = cfs_b->runtime_expires;
2175 raw_spin_unlock(&cfs_b->lock);
2176
2177 if (!runtime)
2178 return;
2179
2180 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2181
2182 raw_spin_lock(&cfs_b->lock);
2183 if (expires == cfs_b->runtime_expires)
2184 cfs_b->runtime = runtime;
2185 raw_spin_unlock(&cfs_b->lock);
2186}
2187
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002188/*
2189 * When a group wakes up we want to make sure that its quota is not already
2190 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2191 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2192 */
2193static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2194{
Paul Turner56f570e2011-11-07 20:26:33 -08002195 if (!cfs_bandwidth_used())
2196 return;
2197
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002198 /* an active group must be handled by the update_curr()->put() path */
2199 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2200 return;
2201
2202 /* ensure the group is not already throttled */
2203 if (cfs_rq_throttled(cfs_rq))
2204 return;
2205
2206 /* update runtime allocation */
2207 account_cfs_rq_runtime(cfs_rq, 0);
2208 if (cfs_rq->runtime_remaining <= 0)
2209 throttle_cfs_rq(cfs_rq);
2210}
2211
2212/* conditionally throttle active cfs_rq's from put_prev_entity() */
2213static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2214{
Paul Turner56f570e2011-11-07 20:26:33 -08002215 if (!cfs_bandwidth_used())
2216 return;
2217
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002218 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2219 return;
2220
2221 /*
2222 * it's possible for a throttled entity to be forced into a running
2223 * state (e.g. set_curr_task), in this case we're finished.
2224 */
2225 if (cfs_rq_throttled(cfs_rq))
2226 return;
2227
2228 throttle_cfs_rq(cfs_rq);
2229}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002230
2231static inline u64 default_cfs_period(void);
2232static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2233static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2234
2235static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2236{
2237 struct cfs_bandwidth *cfs_b =
2238 container_of(timer, struct cfs_bandwidth, slack_timer);
2239 do_sched_cfs_slack_timer(cfs_b);
2240
2241 return HRTIMER_NORESTART;
2242}
2243
2244static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2245{
2246 struct cfs_bandwidth *cfs_b =
2247 container_of(timer, struct cfs_bandwidth, period_timer);
2248 ktime_t now;
2249 int overrun;
2250 int idle = 0;
2251
2252 for (;;) {
2253 now = hrtimer_cb_get_time(timer);
2254 overrun = hrtimer_forward(timer, now, cfs_b->period);
2255
2256 if (!overrun)
2257 break;
2258
2259 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2260 }
2261
2262 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2263}
2264
2265void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2266{
2267 raw_spin_lock_init(&cfs_b->lock);
2268 cfs_b->runtime = 0;
2269 cfs_b->quota = RUNTIME_INF;
2270 cfs_b->period = ns_to_ktime(default_cfs_period());
2271
2272 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2273 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2274 cfs_b->period_timer.function = sched_cfs_period_timer;
2275 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2276 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2277}
2278
2279static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2280{
2281 cfs_rq->runtime_enabled = 0;
2282 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2283}
2284
2285/* requires cfs_b->lock, may release to reprogram timer */
2286void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2287{
2288 /*
2289 * The timer may be active because we're trying to set a new bandwidth
2290 * period or because we're racing with the tear-down path
2291 * (timer_active==0 becomes visible before the hrtimer call-back
2292 * terminates). In either case we ensure that it's re-programmed
2293 */
2294 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2295 raw_spin_unlock(&cfs_b->lock);
2296 /* ensure cfs_b->lock is available while we wait */
2297 hrtimer_cancel(&cfs_b->period_timer);
2298
2299 raw_spin_lock(&cfs_b->lock);
2300 /* if someone else restarted the timer then we're done */
2301 if (cfs_b->timer_active)
2302 return;
2303 }
2304
2305 cfs_b->timer_active = 1;
2306 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2307}
2308
2309static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2310{
2311 hrtimer_cancel(&cfs_b->period_timer);
2312 hrtimer_cancel(&cfs_b->slack_timer);
2313}
2314
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002315static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002316{
2317 struct cfs_rq *cfs_rq;
2318
2319 for_each_leaf_cfs_rq(rq, cfs_rq) {
2320 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2321
2322 if (!cfs_rq->runtime_enabled)
2323 continue;
2324
2325 /*
2326 * clock_task is not advancing so we just need to make sure
2327 * there's some valid quota amount
2328 */
2329 cfs_rq->runtime_remaining = cfs_b->quota;
2330 if (cfs_rq_throttled(cfs_rq))
2331 unthrottle_cfs_rq(cfs_rq);
2332 }
2333}
2334
2335#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002336static __always_inline
2337void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002338static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2339static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002340static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002341
2342static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2343{
2344 return 0;
2345}
Paul Turner64660c82011-07-21 09:43:36 -07002346
2347static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2348{
2349 return 0;
2350}
2351
2352static inline int throttled_lb_pair(struct task_group *tg,
2353 int src_cpu, int dest_cpu)
2354{
2355 return 0;
2356}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002357
2358void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2359
2360#ifdef CONFIG_FAIR_GROUP_SCHED
2361static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002362#endif
2363
Peter Zijlstra029632f2011-10-25 10:00:11 +02002364static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2365{
2366 return NULL;
2367}
2368static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002369static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002370
2371#endif /* CONFIG_CFS_BANDWIDTH */
2372
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002373/**************************************************
2374 * CFS operations on tasks:
2375 */
2376
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002377#ifdef CONFIG_SCHED_HRTICK
2378static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2379{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002380 struct sched_entity *se = &p->se;
2381 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2382
2383 WARN_ON(task_rq(p) != rq);
2384
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002385 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002386 u64 slice = sched_slice(cfs_rq, se);
2387 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2388 s64 delta = slice - ran;
2389
2390 if (delta < 0) {
2391 if (rq->curr == p)
2392 resched_task(p);
2393 return;
2394 }
2395
2396 /*
2397 * Don't schedule slices shorter than 10000ns, that just
2398 * doesn't make sense. Rely on vruntime for fairness.
2399 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002400 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002401 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002402
Peter Zijlstra31656512008-07-18 18:01:23 +02002403 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002404 }
2405}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002406
2407/*
2408 * called from enqueue/dequeue and updates the hrtick when the
2409 * current task is from our class and nr_running is low enough
2410 * to matter.
2411 */
2412static void hrtick_update(struct rq *rq)
2413{
2414 struct task_struct *curr = rq->curr;
2415
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002416 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002417 return;
2418
2419 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2420 hrtick_start_fair(rq, curr);
2421}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302422#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002423static inline void
2424hrtick_start_fair(struct rq *rq, struct task_struct *p)
2425{
2426}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002427
2428static inline void hrtick_update(struct rq *rq)
2429{
2430}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002431#endif
2432
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002433/*
2434 * The enqueue_task method is called before nr_running is
2435 * increased. Here we update the fair scheduling stats and
2436 * then put the task into the rbtree:
2437 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002438static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002439enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002440{
2441 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002442 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002443
2444 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002445 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002446 break;
2447 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002448 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002449
2450 /*
2451 * end evaluation on encountering a throttled cfs_rq
2452 *
2453 * note: in the case of encountering a throttled cfs_rq we will
2454 * post the final h_nr_running increment below.
2455 */
2456 if (cfs_rq_throttled(cfs_rq))
2457 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002458 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002459
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002460 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002461 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002462
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002463 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002464 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002465 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002466
Paul Turner85dac902011-07-21 09:43:33 -07002467 if (cfs_rq_throttled(cfs_rq))
2468 break;
2469
Paul Turnerd6b55912010-11-15 15:47:09 -08002470 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002471 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002472 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002473 }
2474
Ben Segall18bf2802012-10-04 12:51:20 +02002475 if (!se) {
2476 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002477 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002478 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002479 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002480}
2481
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002482static void set_next_buddy(struct sched_entity *se);
2483
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002484/*
2485 * The dequeue_task method is called before nr_running is
2486 * decreased. We remove the task from the rbtree and
2487 * update the fair scheduling stats:
2488 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002489static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002490{
2491 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002492 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002493 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002494
2495 for_each_sched_entity(se) {
2496 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002497 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002498
2499 /*
2500 * end evaluation on encountering a throttled cfs_rq
2501 *
2502 * note: in the case of encountering a throttled cfs_rq we will
2503 * post the final h_nr_running decrement below.
2504 */
2505 if (cfs_rq_throttled(cfs_rq))
2506 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002507 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002508
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002509 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002510 if (cfs_rq->load.weight) {
2511 /*
2512 * Bias pick_next to pick a task from this cfs_rq, as
2513 * p is sleeping when it is within its sched_slice.
2514 */
2515 if (task_sleep && parent_entity(se))
2516 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002517
2518 /* avoid re-evaluating load for this entity */
2519 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002520 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002521 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002522 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002523 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002524
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002525 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002526 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002527 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002528
Paul Turner85dac902011-07-21 09:43:33 -07002529 if (cfs_rq_throttled(cfs_rq))
2530 break;
2531
Paul Turnerd6b55912010-11-15 15:47:09 -08002532 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002533 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002534 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002535 }
2536
Ben Segall18bf2802012-10-04 12:51:20 +02002537 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002538 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002539 update_rq_runnable_avg(rq, 1);
2540 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002541 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002542}
2543
Gregory Haskinse7693a32008-01-25 21:08:09 +01002544#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002545/* Used instead of source_load when we know the type == 0 */
2546static unsigned long weighted_cpuload(const int cpu)
2547{
2548 return cpu_rq(cpu)->load.weight;
2549}
2550
2551/*
2552 * Return a low guess at the load of a migration-source cpu weighted
2553 * according to the scheduling class and "nice" value.
2554 *
2555 * We want to under-estimate the load of migration sources, to
2556 * balance conservatively.
2557 */
2558static unsigned long source_load(int cpu, int type)
2559{
2560 struct rq *rq = cpu_rq(cpu);
2561 unsigned long total = weighted_cpuload(cpu);
2562
2563 if (type == 0 || !sched_feat(LB_BIAS))
2564 return total;
2565
2566 return min(rq->cpu_load[type-1], total);
2567}
2568
2569/*
2570 * Return a high guess at the load of a migration-target cpu weighted
2571 * according to the scheduling class and "nice" value.
2572 */
2573static unsigned long target_load(int cpu, int type)
2574{
2575 struct rq *rq = cpu_rq(cpu);
2576 unsigned long total = weighted_cpuload(cpu);
2577
2578 if (type == 0 || !sched_feat(LB_BIAS))
2579 return total;
2580
2581 return max(rq->cpu_load[type-1], total);
2582}
2583
2584static unsigned long power_of(int cpu)
2585{
2586 return cpu_rq(cpu)->cpu_power;
2587}
2588
2589static unsigned long cpu_avg_load_per_task(int cpu)
2590{
2591 struct rq *rq = cpu_rq(cpu);
2592 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2593
2594 if (nr_running)
2595 return rq->load.weight / nr_running;
2596
2597 return 0;
2598}
2599
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002600
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002601static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002602{
2603 struct sched_entity *se = &p->se;
2604 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002605 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002606
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002607#ifndef CONFIG_64BIT
2608 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002609
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002610 do {
2611 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2612 smp_rmb();
2613 min_vruntime = cfs_rq->min_vruntime;
2614 } while (min_vruntime != min_vruntime_copy);
2615#else
2616 min_vruntime = cfs_rq->min_vruntime;
2617#endif
2618
2619 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002620}
2621
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002622#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002623/*
2624 * effective_load() calculates the load change as seen from the root_task_group
2625 *
2626 * Adding load to a group doesn't make a group heavier, but can cause movement
2627 * of group shares between cpus. Assuming the shares were perfectly aligned one
2628 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002629 *
2630 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2631 * on this @cpu and results in a total addition (subtraction) of @wg to the
2632 * total group weight.
2633 *
2634 * Given a runqueue weight distribution (rw_i) we can compute a shares
2635 * distribution (s_i) using:
2636 *
2637 * s_i = rw_i / \Sum rw_j (1)
2638 *
2639 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2640 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2641 * shares distribution (s_i):
2642 *
2643 * rw_i = { 2, 4, 1, 0 }
2644 * s_i = { 2/7, 4/7, 1/7, 0 }
2645 *
2646 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2647 * task used to run on and the CPU the waker is running on), we need to
2648 * compute the effect of waking a task on either CPU and, in case of a sync
2649 * wakeup, compute the effect of the current task going to sleep.
2650 *
2651 * So for a change of @wl to the local @cpu with an overall group weight change
2652 * of @wl we can compute the new shares distribution (s'_i) using:
2653 *
2654 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2655 *
2656 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2657 * differences in waking a task to CPU 0. The additional task changes the
2658 * weight and shares distributions like:
2659 *
2660 * rw'_i = { 3, 4, 1, 0 }
2661 * s'_i = { 3/8, 4/8, 1/8, 0 }
2662 *
2663 * We can then compute the difference in effective weight by using:
2664 *
2665 * dw_i = S * (s'_i - s_i) (3)
2666 *
2667 * Where 'S' is the group weight as seen by its parent.
2668 *
2669 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2670 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2671 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002672 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002673static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002674{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002675 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002676
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002677 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002678 return wl;
2679
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002680 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002681 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002682
Paul Turner977dda72011-01-14 17:57:50 -08002683 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002684
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002685 /*
2686 * W = @wg + \Sum rw_j
2687 */
2688 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002689
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002690 /*
2691 * w = rw_i + @wl
2692 */
2693 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002694
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002695 /*
2696 * wl = S * s'_i; see (2)
2697 */
2698 if (W > 0 && w < W)
2699 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002700 else
2701 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002702
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002703 /*
2704 * Per the above, wl is the new se->load.weight value; since
2705 * those are clipped to [MIN_SHARES, ...) do so now. See
2706 * calc_cfs_shares().
2707 */
Paul Turner977dda72011-01-14 17:57:50 -08002708 if (wl < MIN_SHARES)
2709 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002710
2711 /*
2712 * wl = dw_i = S * (s'_i - s_i); see (3)
2713 */
Paul Turner977dda72011-01-14 17:57:50 -08002714 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002715
2716 /*
2717 * Recursively apply this logic to all parent groups to compute
2718 * the final effective load change on the root group. Since
2719 * only the @tg group gets extra weight, all parent groups can
2720 * only redistribute existing shares. @wl is the shift in shares
2721 * resulting from this level per the above.
2722 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002723 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002724 }
2725
2726 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002727}
2728#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002729
Peter Zijlstra83378262008-06-27 13:41:37 +02002730static inline unsigned long effective_load(struct task_group *tg, int cpu,
2731 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002732{
Peter Zijlstra83378262008-06-27 13:41:37 +02002733 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002734}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002735
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002736#endif
2737
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002738static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002739{
Paul Turnere37b6a72011-01-21 20:44:59 -08002740 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002741 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002742 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002743 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002744 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002745 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002746
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002747 idx = sd->wake_idx;
2748 this_cpu = smp_processor_id();
2749 prev_cpu = task_cpu(p);
2750 load = source_load(prev_cpu, idx);
2751 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002752
2753 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002754 * If sync wakeup then subtract the (maximum possible)
2755 * effect of the currently running task from the load
2756 * of the current CPU:
2757 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002758 if (sync) {
2759 tg = task_group(current);
2760 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002761
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002762 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002763 load += effective_load(tg, prev_cpu, 0, -weight);
2764 }
2765
2766 tg = task_group(p);
2767 weight = p->se.load.weight;
2768
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002769 /*
2770 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002771 * due to the sync cause above having dropped this_load to 0, we'll
2772 * always have an imbalance, but there's really nothing you can do
2773 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002774 *
2775 * Otherwise check if either cpus are near enough in load to allow this
2776 * task to be woken on this_cpu.
2777 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002778 if (this_load > 0) {
2779 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002780
2781 this_eff_load = 100;
2782 this_eff_load *= power_of(prev_cpu);
2783 this_eff_load *= this_load +
2784 effective_load(tg, this_cpu, weight, weight);
2785
2786 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2787 prev_eff_load *= power_of(this_cpu);
2788 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2789
2790 balanced = this_eff_load <= prev_eff_load;
2791 } else
2792 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002793
2794 /*
2795 * If the currently running task will sleep within
2796 * a reasonable amount of time then attract this newly
2797 * woken task:
2798 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002799 if (sync && balanced)
2800 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002801
Lucas De Marchi41acab82010-03-10 23:37:45 -03002802 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002803 tl_per_task = cpu_avg_load_per_task(this_cpu);
2804
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002805 if (balanced ||
2806 (this_load <= load &&
2807 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002808 /*
2809 * This domain has SD_WAKE_AFFINE and
2810 * p is cache cold in this domain, and
2811 * there is no bad imbalance.
2812 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002813 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002814 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002815
2816 return 1;
2817 }
2818 return 0;
2819}
2820
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002821/*
2822 * find_idlest_group finds and returns the least busy CPU group within the
2823 * domain.
2824 */
2825static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002826find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002827 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002828{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002829 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002830 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002831 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002832
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002833 do {
2834 unsigned long load, avg_load;
2835 int local_group;
2836 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002837
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002838 /* Skip over this group if it has no CPUs allowed */
2839 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002840 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002841 continue;
2842
2843 local_group = cpumask_test_cpu(this_cpu,
2844 sched_group_cpus(group));
2845
2846 /* Tally up the load of all CPUs in the group */
2847 avg_load = 0;
2848
2849 for_each_cpu(i, sched_group_cpus(group)) {
2850 /* Bias balancing toward cpus of our domain */
2851 if (local_group)
2852 load = source_load(i, load_idx);
2853 else
2854 load = target_load(i, load_idx);
2855
2856 avg_load += load;
2857 }
2858
2859 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002860 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002861
2862 if (local_group) {
2863 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002864 } else if (avg_load < min_load) {
2865 min_load = avg_load;
2866 idlest = group;
2867 }
2868 } while (group = group->next, group != sd->groups);
2869
2870 if (!idlest || 100*this_load < imbalance*min_load)
2871 return NULL;
2872 return idlest;
2873}
2874
2875/*
2876 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2877 */
2878static int
2879find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2880{
2881 unsigned long load, min_load = ULONG_MAX;
2882 int idlest = -1;
2883 int i;
2884
2885 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002886 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002887 load = weighted_cpuload(i);
2888
2889 if (load < min_load || (load == min_load && i == this_cpu)) {
2890 min_load = load;
2891 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002892 }
2893 }
2894
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002895 return idlest;
2896}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002897
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002898/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002899 * Try and locate an idle CPU in the sched_domain.
2900 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002901static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002902{
2903 int cpu = smp_processor_id();
2904 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002905 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07002906 struct sched_group *sg;
2907 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002908
2909 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002910 * If the task is going to be woken-up on this cpu and if it is
2911 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002912 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002913 if (target == cpu && idle_cpu(cpu))
2914 return cpu;
2915
2916 /*
2917 * If the task is going to be woken-up on the cpu where it previously
2918 * ran and if it is currently idle, then it the right target.
2919 */
2920 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002921 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002922
2923 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07002924 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002925 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002926 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002927 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07002928 sg = sd->groups;
2929 do {
2930 if (!cpumask_intersects(sched_group_cpus(sg),
2931 tsk_cpus_allowed(p)))
2932 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02002933
Linus Torvalds37407ea2012-09-16 12:29:43 -07002934 for_each_cpu(i, sched_group_cpus(sg)) {
2935 if (!idle_cpu(i))
2936 goto next;
2937 }
2938
2939 target = cpumask_first_and(sched_group_cpus(sg),
2940 tsk_cpus_allowed(p));
2941 goto done;
2942next:
2943 sg = sg->next;
2944 } while (sg != sd->groups);
2945 }
2946done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002947 return target;
2948}
2949
2950/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002951 * sched_balance_self: balance the current task (running on cpu) in domains
2952 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2953 * SD_BALANCE_EXEC.
2954 *
2955 * Balance, ie. select the least loaded group.
2956 *
2957 * Returns the target CPU number, or the same CPU if no balancing is needed.
2958 *
2959 * preempt must be disabled.
2960 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002961static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002962select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002963{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002964 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002965 int cpu = smp_processor_id();
2966 int prev_cpu = task_cpu(p);
2967 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002968 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002969 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002970
Peter Zijlstra29baa742012-04-23 12:11:21 +02002971 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01002972 return prev_cpu;
2973
Peter Zijlstra0763a662009-09-14 19:37:39 +02002974 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002975 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002976 want_affine = 1;
2977 new_cpu = prev_cpu;
2978 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002979
Peter Zijlstradce840a2011-04-07 14:09:50 +02002980 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002981 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01002982 if (!(tmp->flags & SD_LOAD_BALANCE))
2983 continue;
2984
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002985 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002986 * If both cpu and prev_cpu are part of this domain,
2987 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002988 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002989 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2990 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2991 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08002992 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002993 }
2994
Alex Shif03542a2012-07-26 08:55:34 +08002995 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002996 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002997 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002998
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002999 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003000 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003001 prev_cpu = cpu;
3002
3003 new_cpu = select_idle_sibling(p, prev_cpu);
3004 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003005 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003006
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003007 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003008 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003009 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003010 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003011
Peter Zijlstra0763a662009-09-14 19:37:39 +02003012 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003013 sd = sd->child;
3014 continue;
3015 }
3016
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003017 if (sd_flag & SD_BALANCE_WAKE)
3018 load_idx = sd->wake_idx;
3019
3020 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003021 if (!group) {
3022 sd = sd->child;
3023 continue;
3024 }
3025
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003026 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003027 if (new_cpu == -1 || new_cpu == cpu) {
3028 /* Now try balancing at a lower domain level of cpu */
3029 sd = sd->child;
3030 continue;
3031 }
3032
3033 /* Now try balancing at a lower domain level of new_cpu */
3034 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003035 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003036 sd = NULL;
3037 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003038 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003039 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003040 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003041 sd = tmp;
3042 }
3043 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003044 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003045unlock:
3046 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003047
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003048 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003049}
Paul Turner0a74bef2012-10-04 13:18:30 +02003050
3051/*
3052 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3053 * cfs_rq_of(p) references at time of call are still valid and identify the
3054 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3055 * other assumptions, including the state of rq->lock, should be made.
3056 */
3057static void
3058migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3059{
3060}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003061#endif /* CONFIG_SMP */
3062
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003063static unsigned long
3064wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003065{
3066 unsigned long gran = sysctl_sched_wakeup_granularity;
3067
3068 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003069 * Since its curr running now, convert the gran from real-time
3070 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003071 *
3072 * By using 'se' instead of 'curr' we penalize light tasks, so
3073 * they get preempted easier. That is, if 'se' < 'curr' then
3074 * the resulting gran will be larger, therefore penalizing the
3075 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3076 * be smaller, again penalizing the lighter task.
3077 *
3078 * This is especially important for buddies when the leftmost
3079 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003080 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003081 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003082}
3083
3084/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003085 * Should 'se' preempt 'curr'.
3086 *
3087 * |s1
3088 * |s2
3089 * |s3
3090 * g
3091 * |<--->|c
3092 *
3093 * w(c, s1) = -1
3094 * w(c, s2) = 0
3095 * w(c, s3) = 1
3096 *
3097 */
3098static int
3099wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3100{
3101 s64 gran, vdiff = curr->vruntime - se->vruntime;
3102
3103 if (vdiff <= 0)
3104 return -1;
3105
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003106 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003107 if (vdiff > gran)
3108 return 1;
3109
3110 return 0;
3111}
3112
Peter Zijlstra02479092008-11-04 21:25:10 +01003113static void set_last_buddy(struct sched_entity *se)
3114{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003115 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3116 return;
3117
3118 for_each_sched_entity(se)
3119 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003120}
3121
3122static void set_next_buddy(struct sched_entity *se)
3123{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003124 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3125 return;
3126
3127 for_each_sched_entity(se)
3128 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003129}
3130
Rik van Rielac53db52011-02-01 09:51:03 -05003131static void set_skip_buddy(struct sched_entity *se)
3132{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003133 for_each_sched_entity(se)
3134 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003135}
3136
Peter Zijlstra464b7522008-10-24 11:06:15 +02003137/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003138 * Preempt the current task with a newly woken task if needed:
3139 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003140static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003141{
3142 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003143 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003144 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003145 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003146 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003147
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003148 if (unlikely(se == pse))
3149 return;
3150
Paul Turner5238cdd2011-07-21 09:43:37 -07003151 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003152 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003153 * unconditionally check_prempt_curr() after an enqueue (which may have
3154 * lead to a throttle). This both saves work and prevents false
3155 * next-buddy nomination below.
3156 */
3157 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3158 return;
3159
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003160 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003161 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003162 next_buddy_marked = 1;
3163 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003164
Bharata B Raoaec0a512008-08-28 14:42:49 +05303165 /*
3166 * We can come here with TIF_NEED_RESCHED already set from new task
3167 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003168 *
3169 * Note: this also catches the edge-case of curr being in a throttled
3170 * group (e.g. via set_curr_task), since update_curr() (in the
3171 * enqueue of curr) will have resulted in resched being set. This
3172 * prevents us from potentially nominating it as a false LAST_BUDDY
3173 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303174 */
3175 if (test_tsk_need_resched(curr))
3176 return;
3177
Darren Harta2f5c9a2011-02-22 13:04:33 -08003178 /* Idle tasks are by definition preempted by non-idle tasks. */
3179 if (unlikely(curr->policy == SCHED_IDLE) &&
3180 likely(p->policy != SCHED_IDLE))
3181 goto preempt;
3182
Ingo Molnar91c234b2007-10-15 17:00:18 +02003183 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003184 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3185 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003186 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003187 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003188 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003189
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003190 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003191 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003192 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003193 if (wakeup_preempt_entity(se, pse) == 1) {
3194 /*
3195 * Bias pick_next to pick the sched entity that is
3196 * triggering this preemption.
3197 */
3198 if (!next_buddy_marked)
3199 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003200 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003201 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003202
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003203 return;
3204
3205preempt:
3206 resched_task(curr);
3207 /*
3208 * Only set the backward buddy when the current task is still
3209 * on the rq. This can happen when a wakeup gets interleaved
3210 * with schedule on the ->pre_schedule() or idle_balance()
3211 * point, either of which can * drop the rq lock.
3212 *
3213 * Also, during early boot the idle thread is in the fair class,
3214 * for obvious reasons its a bad idea to schedule back to it.
3215 */
3216 if (unlikely(!se->on_rq || curr == rq->idle))
3217 return;
3218
3219 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3220 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003221}
3222
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003223static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003224{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003225 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003226 struct cfs_rq *cfs_rq = &rq->cfs;
3227 struct sched_entity *se;
3228
Tim Blechmann36ace272009-11-24 11:55:45 +01003229 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003230 return NULL;
3231
3232 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003233 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003234 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003235 cfs_rq = group_cfs_rq(se);
3236 } while (cfs_rq);
3237
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003238 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003239 if (hrtick_enabled(rq))
3240 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003241
3242 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003243}
3244
3245/*
3246 * Account for a descheduled task:
3247 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003248static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003249{
3250 struct sched_entity *se = &prev->se;
3251 struct cfs_rq *cfs_rq;
3252
3253 for_each_sched_entity(se) {
3254 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003255 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003256 }
3257}
3258
Rik van Rielac53db52011-02-01 09:51:03 -05003259/*
3260 * sched_yield() is very simple
3261 *
3262 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3263 */
3264static void yield_task_fair(struct rq *rq)
3265{
3266 struct task_struct *curr = rq->curr;
3267 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3268 struct sched_entity *se = &curr->se;
3269
3270 /*
3271 * Are we the only task in the tree?
3272 */
3273 if (unlikely(rq->nr_running == 1))
3274 return;
3275
3276 clear_buddies(cfs_rq, se);
3277
3278 if (curr->policy != SCHED_BATCH) {
3279 update_rq_clock(rq);
3280 /*
3281 * Update run-time statistics of the 'current'.
3282 */
3283 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003284 /*
3285 * Tell update_rq_clock() that we've just updated,
3286 * so we don't do microscopic update in schedule()
3287 * and double the fastpath cost.
3288 */
3289 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003290 }
3291
3292 set_skip_buddy(se);
3293}
3294
Mike Galbraithd95f4122011-02-01 09:50:51 -05003295static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3296{
3297 struct sched_entity *se = &p->se;
3298
Paul Turner5238cdd2011-07-21 09:43:37 -07003299 /* throttled hierarchies are not runnable */
3300 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003301 return false;
3302
3303 /* Tell the scheduler that we'd really like pse to run next. */
3304 set_next_buddy(se);
3305
Mike Galbraithd95f4122011-02-01 09:50:51 -05003306 yield_task_fair(rq);
3307
3308 return true;
3309}
3310
Peter Williams681f3e62007-10-24 18:23:51 +02003311#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003312/**************************************************
3313 * Fair scheduling class load-balancing methods:
3314 */
3315
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003316static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3317
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003318#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003319#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303320#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003321
3322struct lb_env {
3323 struct sched_domain *sd;
3324
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003325 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303326 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003327
3328 int dst_cpu;
3329 struct rq *dst_rq;
3330
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303331 struct cpumask *dst_grpmask;
3332 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003333 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003334 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08003335 /* The set of CPUs under consideration for load-balancing */
3336 struct cpumask *cpus;
3337
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003338 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003339
3340 unsigned int loop;
3341 unsigned int loop_break;
3342 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003343};
3344
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003345/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003346 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003347 * Both runqueues must be locked.
3348 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003349static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003350{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003351 deactivate_task(env->src_rq, p, 0);
3352 set_task_cpu(p, env->dst_cpu);
3353 activate_task(env->dst_rq, p, 0);
3354 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003355}
3356
3357/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003358 * Is this task likely cache-hot:
3359 */
3360static int
3361task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3362{
3363 s64 delta;
3364
3365 if (p->sched_class != &fair_sched_class)
3366 return 0;
3367
3368 if (unlikely(p->policy == SCHED_IDLE))
3369 return 0;
3370
3371 /*
3372 * Buddy candidates are cache hot:
3373 */
3374 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3375 (&p->se == cfs_rq_of(&p->se)->next ||
3376 &p->se == cfs_rq_of(&p->se)->last))
3377 return 1;
3378
3379 if (sysctl_sched_migration_cost == -1)
3380 return 1;
3381 if (sysctl_sched_migration_cost == 0)
3382 return 0;
3383
3384 delta = now - p->se.exec_start;
3385
3386 return delta < (s64)sysctl_sched_migration_cost;
3387}
3388
3389/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003390 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3391 */
3392static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003393int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003394{
3395 int tsk_cache_hot = 0;
3396 /*
3397 * We do not migrate tasks that are:
3398 * 1) running (obviously), or
3399 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3400 * 3) are cache-hot on their current CPU.
3401 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003402 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303403 int new_dst_cpu;
3404
Lucas De Marchi41acab82010-03-10 23:37:45 -03003405 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303406
3407 /*
3408 * Remember if this task can be migrated to any other cpu in
3409 * our sched_group. We may want to revisit it if we couldn't
3410 * meet load balance goals by pulling other tasks on src_cpu.
3411 *
3412 * Also avoid computing new_dst_cpu if we have already computed
3413 * one in current iteration.
3414 */
3415 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3416 return 0;
3417
3418 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3419 tsk_cpus_allowed(p));
3420 if (new_dst_cpu < nr_cpu_ids) {
3421 env->flags |= LBF_SOME_PINNED;
3422 env->new_dst_cpu = new_dst_cpu;
3423 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003424 return 0;
3425 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303426
3427 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003428 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003429
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003430 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003431 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003432 return 0;
3433 }
3434
3435 /*
3436 * Aggressive migration if:
3437 * 1) task is cache cold, or
3438 * 2) too many balance attempts have failed.
3439 */
3440
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003441 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003442 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003443 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003444#ifdef CONFIG_SCHEDSTATS
3445 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003446 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003447 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003448 }
3449#endif
3450 return 1;
3451 }
3452
3453 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003454 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003455 return 0;
3456 }
3457 return 1;
3458}
3459
Peter Zijlstra897c3952009-12-17 17:45:42 +01003460/*
3461 * move_one_task tries to move exactly one task from busiest to this_rq, as
3462 * part of active balancing operations within "domain".
3463 * Returns 1 if successful and 0 otherwise.
3464 *
3465 * Called with both runqueues locked.
3466 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003467static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003468{
3469 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003470
Peter Zijlstra367456c2012-02-20 21:49:09 +01003471 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3472 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3473 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003474
Peter Zijlstra367456c2012-02-20 21:49:09 +01003475 if (!can_migrate_task(p, env))
3476 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003477
Peter Zijlstra367456c2012-02-20 21:49:09 +01003478 move_task(p, env);
3479 /*
3480 * Right now, this is only the second place move_task()
3481 * is called, so we can safely collect move_task()
3482 * stats here rather than inside move_task().
3483 */
3484 schedstat_inc(env->sd, lb_gained[env->idle]);
3485 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003486 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003487 return 0;
3488}
3489
Peter Zijlstra367456c2012-02-20 21:49:09 +01003490static unsigned long task_h_load(struct task_struct *p);
3491
Peter Zijlstraeb953082012-04-17 13:38:40 +02003492static const unsigned int sched_nr_migrate_break = 32;
3493
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003494/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003495 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003496 * this_rq, as part of a balancing operation within domain "sd".
3497 * Returns 1 if successful and 0 otherwise.
3498 *
3499 * Called with both runqueues locked.
3500 */
3501static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003502{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003503 struct list_head *tasks = &env->src_rq->cfs_tasks;
3504 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003505 unsigned long load;
3506 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003507
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003508 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003509 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003510
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003511 while (!list_empty(tasks)) {
3512 p = list_first_entry(tasks, struct task_struct, se.group_node);
3513
Peter Zijlstra367456c2012-02-20 21:49:09 +01003514 env->loop++;
3515 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003516 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003517 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003518
3519 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003520 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003521 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003522 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003523 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003524 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003525
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003526 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003527 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003528
Peter Zijlstra367456c2012-02-20 21:49:09 +01003529 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003530
Peter Zijlstraeb953082012-04-17 13:38:40 +02003531 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003532 goto next;
3533
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003534 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003535 goto next;
3536
3537 if (!can_migrate_task(p, env))
3538 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003539
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003540 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003541 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003542 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003543
3544#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003545 /*
3546 * NEWIDLE balancing is a source of latency, so preemptible
3547 * kernels will stop after the first task is pulled to minimize
3548 * the critical section.
3549 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003550 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003551 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003552#endif
3553
Peter Zijlstraee00e662009-12-17 17:25:20 +01003554 /*
3555 * We only want to steal up to the prescribed amount of
3556 * weighted load.
3557 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003558 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003559 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003560
Peter Zijlstra367456c2012-02-20 21:49:09 +01003561 continue;
3562next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003563 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003564 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003565
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003566 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003567 * Right now, this is one of only two places move_task() is called,
3568 * so we can safely collect move_task() stats here rather than
3569 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003570 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003571 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003572
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003573 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003574}
3575
Peter Zijlstra230059de2009-12-17 17:47:12 +01003576#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003577/*
3578 * update tg->load_weight by folding this cpu's load_avg
3579 */
Paul Turner67e86252010-11-15 15:47:05 -08003580static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003581{
3582 struct cfs_rq *cfs_rq;
3583 unsigned long flags;
3584 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003585
3586 if (!tg->se[cpu])
3587 return 0;
3588
3589 rq = cpu_rq(cpu);
3590 cfs_rq = tg->cfs_rq[cpu];
3591
3592 raw_spin_lock_irqsave(&rq->lock, flags);
3593
3594 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003595 update_cfs_load(cfs_rq, 1);
Paul Turner9ee474f2012-10-04 13:18:30 +02003596 update_cfs_rq_blocked_load(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003597
3598 /*
3599 * We need to update shares after updating tg->load_weight in
3600 * order to adjust the weight of groups with long running tasks.
3601 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003602 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003603
3604 raw_spin_unlock_irqrestore(&rq->lock, flags);
3605
3606 return 0;
3607}
3608
3609static void update_shares(int cpu)
3610{
3611 struct cfs_rq *cfs_rq;
3612 struct rq *rq = cpu_rq(cpu);
3613
3614 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003615 /*
3616 * Iterates the task_group tree in a bottom up fashion, see
3617 * list_add_leaf_cfs_rq() for details.
3618 */
Paul Turner64660c82011-07-21 09:43:36 -07003619 for_each_leaf_cfs_rq(rq, cfs_rq) {
3620 /* throttled entities do not contribute to load */
3621 if (throttled_hierarchy(cfs_rq))
3622 continue;
3623
Paul Turner67e86252010-11-15 15:47:05 -08003624 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003625 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003626 rcu_read_unlock();
3627}
3628
Peter Zijlstra9763b672011-07-13 13:09:25 +02003629/*
3630 * Compute the cpu's hierarchical load factor for each task group.
3631 * This needs to be done in a top-down fashion because the load of a child
3632 * group is a fraction of its parents load.
3633 */
3634static int tg_load_down(struct task_group *tg, void *data)
3635{
3636 unsigned long load;
3637 long cpu = (long)data;
3638
3639 if (!tg->parent) {
3640 load = cpu_rq(cpu)->load.weight;
3641 } else {
3642 load = tg->parent->cfs_rq[cpu]->h_load;
3643 load *= tg->se[cpu]->load.weight;
3644 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3645 }
3646
3647 tg->cfs_rq[cpu]->h_load = load;
3648
3649 return 0;
3650}
3651
3652static void update_h_load(long cpu)
3653{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003654 struct rq *rq = cpu_rq(cpu);
3655 unsigned long now = jiffies;
3656
3657 if (rq->h_load_throttle == now)
3658 return;
3659
3660 rq->h_load_throttle = now;
3661
Peter Zijlstra367456c2012-02-20 21:49:09 +01003662 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003663 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003664 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003665}
3666
Peter Zijlstra367456c2012-02-20 21:49:09 +01003667static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003668{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003669 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3670 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003671
Peter Zijlstra367456c2012-02-20 21:49:09 +01003672 load = p->se.load.weight;
3673 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003674
Peter Zijlstra367456c2012-02-20 21:49:09 +01003675 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003676}
3677#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003678static inline void update_shares(int cpu)
3679{
3680}
3681
Peter Zijlstra367456c2012-02-20 21:49:09 +01003682static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003683{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003684}
3685
3686static unsigned long task_h_load(struct task_struct *p)
3687{
3688 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003689}
3690#endif
3691
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003692/********** Helpers for find_busiest_group ************************/
3693/*
3694 * sd_lb_stats - Structure to store the statistics of a sched_domain
3695 * during load balancing.
3696 */
3697struct sd_lb_stats {
3698 struct sched_group *busiest; /* Busiest group in this sd */
3699 struct sched_group *this; /* Local group in this sd */
3700 unsigned long total_load; /* Total load of all groups in sd */
3701 unsigned long total_pwr; /* Total power of all groups in sd */
3702 unsigned long avg_load; /* Average load across all groups in sd */
3703
3704 /** Statistics of this group */
3705 unsigned long this_load;
3706 unsigned long this_load_per_task;
3707 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003708 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003709 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003710
3711 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003712 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003713 unsigned long max_load;
3714 unsigned long busiest_load_per_task;
3715 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003716 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003717 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003718 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003719
3720 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003721};
3722
3723/*
3724 * sg_lb_stats - stats of a sched_group required for load_balancing
3725 */
3726struct sg_lb_stats {
3727 unsigned long avg_load; /*Avg load across the CPUs of the group */
3728 unsigned long group_load; /* Total load over the CPUs of the group */
3729 unsigned long sum_nr_running; /* Nr tasks running in the group */
3730 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3731 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003732 unsigned long idle_cpus;
3733 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003734 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003735 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003736};
3737
3738/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003739 * get_sd_load_idx - Obtain the load index for a given sched domain.
3740 * @sd: The sched_domain whose load_idx is to be obtained.
3741 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3742 */
3743static inline int get_sd_load_idx(struct sched_domain *sd,
3744 enum cpu_idle_type idle)
3745{
3746 int load_idx;
3747
3748 switch (idle) {
3749 case CPU_NOT_IDLE:
3750 load_idx = sd->busy_idx;
3751 break;
3752
3753 case CPU_NEWLY_IDLE:
3754 load_idx = sd->newidle_idx;
3755 break;
3756 default:
3757 load_idx = sd->idle_idx;
3758 break;
3759 }
3760
3761 return load_idx;
3762}
3763
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003764unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3765{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003766 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003767}
3768
3769unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3770{
3771 return default_scale_freq_power(sd, cpu);
3772}
3773
3774unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3775{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003776 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003777 unsigned long smt_gain = sd->smt_gain;
3778
3779 smt_gain /= weight;
3780
3781 return smt_gain;
3782}
3783
3784unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3785{
3786 return default_scale_smt_power(sd, cpu);
3787}
3788
3789unsigned long scale_rt_power(int cpu)
3790{
3791 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003792 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003793
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003794 /*
3795 * Since we're reading these variables without serialization make sure
3796 * we read them once before doing sanity checks on them.
3797 */
3798 age_stamp = ACCESS_ONCE(rq->age_stamp);
3799 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003800
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003801 total = sched_avg_period() + (rq->clock - age_stamp);
3802
3803 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003804 /* Ensures that power won't end up being negative */
3805 available = 0;
3806 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003807 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003808 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003809
Nikhil Rao1399fa72011-05-18 10:09:39 -07003810 if (unlikely((s64)total < SCHED_POWER_SCALE))
3811 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003812
Nikhil Rao1399fa72011-05-18 10:09:39 -07003813 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003814
3815 return div_u64(available, total);
3816}
3817
3818static void update_cpu_power(struct sched_domain *sd, int cpu)
3819{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003820 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003821 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003822 struct sched_group *sdg = sd->groups;
3823
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003824 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3825 if (sched_feat(ARCH_POWER))
3826 power *= arch_scale_smt_power(sd, cpu);
3827 else
3828 power *= default_scale_smt_power(sd, cpu);
3829
Nikhil Rao1399fa72011-05-18 10:09:39 -07003830 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003831 }
3832
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003833 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003834
3835 if (sched_feat(ARCH_POWER))
3836 power *= arch_scale_freq_power(sd, cpu);
3837 else
3838 power *= default_scale_freq_power(sd, cpu);
3839
Nikhil Rao1399fa72011-05-18 10:09:39 -07003840 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003841
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003842 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003843 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003844
3845 if (!power)
3846 power = 1;
3847
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003848 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003849 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003850}
3851
Peter Zijlstra029632f2011-10-25 10:00:11 +02003852void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003853{
3854 struct sched_domain *child = sd->child;
3855 struct sched_group *group, *sdg = sd->groups;
3856 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003857 unsigned long interval;
3858
3859 interval = msecs_to_jiffies(sd->balance_interval);
3860 interval = clamp(interval, 1UL, max_load_balance_interval);
3861 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003862
3863 if (!child) {
3864 update_cpu_power(sd, cpu);
3865 return;
3866 }
3867
3868 power = 0;
3869
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003870 if (child->flags & SD_OVERLAP) {
3871 /*
3872 * SD_OVERLAP domains cannot assume that child groups
3873 * span the current group.
3874 */
3875
3876 for_each_cpu(cpu, sched_group_cpus(sdg))
3877 power += power_of(cpu);
3878 } else {
3879 /*
3880 * !SD_OVERLAP domains can assume that child groups
3881 * span the current group.
3882 */
3883
3884 group = child->groups;
3885 do {
3886 power += group->sgp->power;
3887 group = group->next;
3888 } while (group != child->groups);
3889 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003890
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003891 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003892}
3893
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003894/*
3895 * Try and fix up capacity for tiny siblings, this is needed when
3896 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3897 * which on its own isn't powerful enough.
3898 *
3899 * See update_sd_pick_busiest() and check_asym_packing().
3900 */
3901static inline int
3902fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3903{
3904 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003905 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003906 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003907 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003908 return 0;
3909
3910 /*
3911 * If ~90% of the cpu_power is still there, we're good.
3912 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003913 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003914 return 1;
3915
3916 return 0;
3917}
3918
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003919/**
3920 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003921 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003922 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003923 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003924 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003925 * @balance: Should we balance.
3926 * @sgs: variable to hold the statistics for this group.
3927 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003928static inline void update_sg_lb_stats(struct lb_env *env,
3929 struct sched_group *group, int load_idx,
Michael Wangb94031302012-07-12 16:10:13 +08003930 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003931{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003932 unsigned long nr_running, max_nr_running, min_nr_running;
3933 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003934 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003935 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003936 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003937
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003938 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02003939 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940
3941 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003942 max_cpu_load = 0;
3943 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003944 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003945 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003946
Michael Wangb94031302012-07-12 16:10:13 +08003947 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003948 struct rq *rq = cpu_rq(i);
3949
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003950 nr_running = rq->nr_running;
3951
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003952 /* Bias balancing toward cpus of our domain */
3953 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02003954 if (idle_cpu(i) && !first_idle_cpu &&
3955 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003956 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003957 balance_cpu = i;
3958 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003959
3960 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003961 } else {
3962 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003963 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003964 max_cpu_load = load;
3965 if (min_cpu_load > load)
3966 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003967
3968 if (nr_running > max_nr_running)
3969 max_nr_running = nr_running;
3970 if (min_nr_running > nr_running)
3971 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003972 }
3973
3974 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003975 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003976 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003977 if (idle_cpu(i))
3978 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003979 }
3980
3981 /*
3982 * First idle cpu or the first cpu(busiest) in this sched group
3983 * is eligible for doing load balancing at this and above
3984 * domains. In the newly idle case, we will allow all the cpu's
3985 * to do the newly idle load balance.
3986 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003987 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003988 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003989 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01003990 *balance = 0;
3991 return;
3992 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003993 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01003994 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003995 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003996 }
3997
3998 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003999 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004000
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004001 /*
4002 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004003 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004004 *
4005 * APZ: with cgroup the avg task weight can vary wildly and
4006 * might not be a suitable number - should we keep a
4007 * normalized nr_running number somewhere that negates
4008 * the hierarchy?
4009 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004010 if (sgs->sum_nr_running)
4011 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004012
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004013 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4014 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004015 sgs->group_imb = 1;
4016
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004017 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004018 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004019 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004020 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004021 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004022
4023 if (sgs->group_capacity > sgs->sum_nr_running)
4024 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004025}
4026
4027/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004028 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004029 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004030 * @sds: sched_domain statistics
4031 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004032 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004033 *
4034 * Determine if @sg is a busier group than the previously selected
4035 * busiest group.
4036 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004037static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004038 struct sd_lb_stats *sds,
4039 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004040 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004041{
4042 if (sgs->avg_load <= sds->max_load)
4043 return false;
4044
4045 if (sgs->sum_nr_running > sgs->group_capacity)
4046 return true;
4047
4048 if (sgs->group_imb)
4049 return true;
4050
4051 /*
4052 * ASYM_PACKING needs to move all the work to the lowest
4053 * numbered CPUs in the group, therefore mark all groups
4054 * higher than ourself as busy.
4055 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004056 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4057 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004058 if (!sds->busiest)
4059 return true;
4060
4061 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4062 return true;
4063 }
4064
4065 return false;
4066}
4067
4068/**
Hui Kang461819a2011-10-11 23:00:59 -04004069 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004070 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004071 * @balance: Should we balance.
4072 * @sds: variable to hold the statistics for this sched_domain.
4073 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004074static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004075 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004076{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004077 struct sched_domain *child = env->sd->child;
4078 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004079 struct sg_lb_stats sgs;
4080 int load_idx, prefer_sibling = 0;
4081
4082 if (child && child->flags & SD_PREFER_SIBLING)
4083 prefer_sibling = 1;
4084
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004085 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004086
4087 do {
4088 int local_group;
4089
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004090 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004091 memset(&sgs, 0, sizeof(sgs));
Michael Wangb94031302012-07-12 16:10:13 +08004092 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004093
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004094 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004095 return;
4096
4097 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004098 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004099
4100 /*
4101 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004102 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004103 * and move all the excess tasks away. We lower the capacity
4104 * of a group only if the local group has the capacity to fit
4105 * these excess tasks, i.e. nr_running < group_capacity. The
4106 * extra check prevents the case where you always pull from the
4107 * heaviest group when it is already under-utilized (possible
4108 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004109 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004110 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004111 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4112
4113 if (local_group) {
4114 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004115 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004116 sds->this_nr_running = sgs.sum_nr_running;
4117 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004118 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004119 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004120 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004121 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004122 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004123 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004124 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004125 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004126 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004127 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004128 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004129 sds->group_imb = sgs.group_imb;
4130 }
4131
Michael Neuling532cb4c2010-06-08 14:57:02 +10004132 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004133 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004134}
4135
Michael Neuling532cb4c2010-06-08 14:57:02 +10004136/**
4137 * check_asym_packing - Check to see if the group is packed into the
4138 * sched doman.
4139 *
4140 * This is primarily intended to used at the sibling level. Some
4141 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4142 * case of POWER7, it can move to lower SMT modes only when higher
4143 * threads are idle. When in lower SMT modes, the threads will
4144 * perform better since they share less core resources. Hence when we
4145 * have idle threads, we want them to be the higher ones.
4146 *
4147 * This packing function is run on idle threads. It checks to see if
4148 * the busiest CPU in this domain (core in the P7 case) has a higher
4149 * CPU number than the packing function is being run on. Here we are
4150 * assuming lower CPU number will be equivalent to lower a SMT thread
4151 * number.
4152 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004153 * Returns 1 when packing is required and a task should be moved to
4154 * this CPU. The amount of the imbalance is returned in *imbalance.
4155 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004156 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004157 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004158 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004159static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004160{
4161 int busiest_cpu;
4162
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004163 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004164 return 0;
4165
4166 if (!sds->busiest)
4167 return 0;
4168
4169 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004170 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004171 return 0;
4172
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004173 env->imbalance = DIV_ROUND_CLOSEST(
4174 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4175
Michael Neuling532cb4c2010-06-08 14:57:02 +10004176 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004177}
4178
4179/**
4180 * fix_small_imbalance - Calculate the minor imbalance that exists
4181 * amongst the groups of a sched_domain, during
4182 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004183 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004184 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004185 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004186static inline
4187void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004188{
4189 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4190 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004191 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004192
4193 if (sds->this_nr_running) {
4194 sds->this_load_per_task /= sds->this_nr_running;
4195 if (sds->busiest_load_per_task >
4196 sds->this_load_per_task)
4197 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004198 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004199 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004200 cpu_avg_load_per_task(env->dst_cpu);
4201 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004202
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004203 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004204 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004205 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004206
4207 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4208 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004209 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004210 return;
4211 }
4212
4213 /*
4214 * OK, we don't have enough imbalance to justify moving tasks,
4215 * however we may be able to increase total CPU power used by
4216 * moving them.
4217 */
4218
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004219 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004220 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004221 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004222 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004223 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004224
4225 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004226 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004227 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004228 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004229 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004230 min(sds->busiest_load_per_task, sds->max_load - tmp);
4231
4232 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004233 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004234 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004235 tmp = (sds->max_load * sds->busiest->sgp->power) /
4236 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004237 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004238 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004239 sds->this->sgp->power;
4240 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004241 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004242 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004243
4244 /* Move if we gain throughput */
4245 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004246 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004247}
4248
4249/**
4250 * calculate_imbalance - Calculate the amount of imbalance present within the
4251 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004252 * @env: load balance 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 void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004256{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004257 unsigned long max_pull, load_above_capacity = ~0UL;
4258
4259 sds->busiest_load_per_task /= sds->busiest_nr_running;
4260 if (sds->group_imb) {
4261 sds->busiest_load_per_task =
4262 min(sds->busiest_load_per_task, sds->avg_load);
4263 }
4264
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004265 /*
4266 * In the presence of smp nice balancing, certain scenarios can have
4267 * max load less than avg load(as we skip the groups at or below
4268 * its cpu_power, while calculating max_load..)
4269 */
4270 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004271 env->imbalance = 0;
4272 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004273 }
4274
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004275 if (!sds->group_imb) {
4276 /*
4277 * Don't want to pull so many tasks that a group would go idle.
4278 */
4279 load_above_capacity = (sds->busiest_nr_running -
4280 sds->busiest_group_capacity);
4281
Nikhil Rao1399fa72011-05-18 10:09:39 -07004282 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004283
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004284 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004285 }
4286
4287 /*
4288 * We're trying to get all the cpus to the average_load, so we don't
4289 * want to push ourselves above the average load, nor do we wish to
4290 * reduce the max loaded cpu below the average load. At the same time,
4291 * we also don't want to reduce the group load below the group capacity
4292 * (so that we can implement power-savings policies etc). Thus we look
4293 * for the minimum possible imbalance.
4294 * Be careful of negative numbers as they'll appear as very large values
4295 * with unsigned longs.
4296 */
4297 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004298
4299 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004300 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004301 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004302 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004303
4304 /*
4305 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004306 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004307 * a think about bumping its value to force at least one task to be
4308 * moved
4309 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004310 if (env->imbalance < sds->busiest_load_per_task)
4311 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004312
4313}
Nikhil Raofab47622010-10-15 13:12:29 -07004314
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004315/******* find_busiest_group() helpers end here *********************/
4316
4317/**
4318 * find_busiest_group - Returns the busiest group within the sched_domain
4319 * if there is an imbalance. If there isn't an imbalance, and
4320 * the user has opted for power-savings, it returns a group whose
4321 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4322 * such a group exists.
4323 *
4324 * Also calculates the amount of weighted load which should be moved
4325 * to restore balance.
4326 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004327 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004328 * @balance: Pointer to a variable indicating if this_cpu
4329 * is the appropriate cpu to perform load balancing at this_level.
4330 *
4331 * Returns: - the busiest group if imbalance exists.
4332 * - If no imbalance and user has opted for power-savings balance,
4333 * return the least loaded group whose CPUs can be
4334 * put to idle by rebalancing its tasks onto our group.
4335 */
4336static struct sched_group *
Michael Wangb94031302012-07-12 16:10:13 +08004337find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004338{
4339 struct sd_lb_stats sds;
4340
4341 memset(&sds, 0, sizeof(sds));
4342
4343 /*
4344 * Compute the various statistics relavent for load balancing at
4345 * this level.
4346 */
Michael Wangb94031302012-07-12 16:10:13 +08004347 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004348
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004349 /*
4350 * this_cpu is not the appropriate cpu to perform load balancing at
4351 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004352 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004353 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004354 goto ret;
4355
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004356 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4357 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004358 return sds.busiest;
4359
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004360 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004361 if (!sds.busiest || sds.busiest_nr_running == 0)
4362 goto out_balanced;
4363
Nikhil Rao1399fa72011-05-18 10:09:39 -07004364 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004365
Peter Zijlstra866ab432011-02-21 18:56:47 +01004366 /*
4367 * If the busiest group is imbalanced the below checks don't
4368 * work because they assumes all things are equal, which typically
4369 * isn't true due to cpus_allowed constraints and the like.
4370 */
4371 if (sds.group_imb)
4372 goto force_balance;
4373
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004374 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004375 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004376 !sds.busiest_has_capacity)
4377 goto force_balance;
4378
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004379 /*
4380 * If the local group is more busy than the selected busiest group
4381 * don't try and pull any tasks.
4382 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004383 if (sds.this_load >= sds.max_load)
4384 goto out_balanced;
4385
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004386 /*
4387 * Don't pull any tasks if this group is already above the domain
4388 * average load.
4389 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004390 if (sds.this_load >= sds.avg_load)
4391 goto out_balanced;
4392
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004393 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004394 /*
4395 * This cpu is idle. If the busiest group load doesn't
4396 * have more tasks than the number of available cpu's and
4397 * there is no imbalance between this and busiest group
4398 * wrt to idle cpu's, it is balanced.
4399 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004400 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004401 sds.busiest_nr_running <= sds.busiest_group_weight)
4402 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004403 } else {
4404 /*
4405 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4406 * imbalance_pct to be conservative.
4407 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004408 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004409 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004410 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004411
Nikhil Raofab47622010-10-15 13:12:29 -07004412force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004413 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004414 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004415 return sds.busiest;
4416
4417out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004418ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004419 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004420 return NULL;
4421}
4422
4423/*
4424 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4425 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004426static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004427 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004428{
4429 struct rq *busiest = NULL, *rq;
4430 unsigned long max_load = 0;
4431 int i;
4432
4433 for_each_cpu(i, sched_group_cpus(group)) {
4434 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004435 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4436 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004437 unsigned long wl;
4438
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004439 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004440 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004441
Michael Wangb94031302012-07-12 16:10:13 +08004442 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004443 continue;
4444
4445 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004446 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004447
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004448 /*
4449 * When comparing with imbalance, use weighted_cpuload()
4450 * which is not scaled with the cpu power.
4451 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004452 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004453 continue;
4454
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004455 /*
4456 * For the load comparisons with the other cpu's, consider
4457 * the weighted_cpuload() scaled with the cpu power, so that
4458 * the load can be moved away from the cpu that is potentially
4459 * running at a lower capacity.
4460 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004461 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004462
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004463 if (wl > max_load) {
4464 max_load = wl;
4465 busiest = rq;
4466 }
4467 }
4468
4469 return busiest;
4470}
4471
4472/*
4473 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4474 * so long as it is large enough.
4475 */
4476#define MAX_PINNED_INTERVAL 512
4477
4478/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004479DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004480
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004481static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004482{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004483 struct sched_domain *sd = env->sd;
4484
4485 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004486
4487 /*
4488 * ASYM_PACKING needs to force migrate tasks from busy but
4489 * higher numbered CPUs in order to pack all tasks in the
4490 * lowest numbered CPUs.
4491 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004492 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004493 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004494 }
4495
4496 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4497}
4498
Tejun Heo969c7922010-05-06 18:49:21 +02004499static int active_load_balance_cpu_stop(void *data);
4500
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004501/*
4502 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4503 * tasks if there is an imbalance.
4504 */
4505static int load_balance(int this_cpu, struct rq *this_rq,
4506 struct sched_domain *sd, enum cpu_idle_type idle,
4507 int *balance)
4508{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304509 int ld_moved, cur_ld_moved, active_balance = 0;
4510 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004511 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004512 struct rq *busiest;
4513 unsigned long flags;
4514 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4515
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004516 struct lb_env env = {
4517 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004518 .dst_cpu = this_cpu,
4519 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304520 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004521 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004522 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08004523 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004524 };
4525
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004526 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304527 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004528
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004529 schedstat_inc(sd, lb_count[idle]);
4530
4531redo:
Michael Wangb94031302012-07-12 16:10:13 +08004532 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004533
4534 if (*balance == 0)
4535 goto out_balanced;
4536
4537 if (!group) {
4538 schedstat_inc(sd, lb_nobusyg[idle]);
4539 goto out_balanced;
4540 }
4541
Michael Wangb94031302012-07-12 16:10:13 +08004542 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004543 if (!busiest) {
4544 schedstat_inc(sd, lb_nobusyq[idle]);
4545 goto out_balanced;
4546 }
4547
Michael Wang78feefc2012-08-06 16:41:59 +08004548 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004549
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004550 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004551
4552 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304553 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004554 if (busiest->nr_running > 1) {
4555 /*
4556 * Attempt to move tasks. If find_busiest_group has found
4557 * an imbalance but busiest->nr_running <= 1, the group is
4558 * still unbalanced. ld_moved simply stays zero, so it is
4559 * correctly treated as an imbalance.
4560 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004561 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004562 env.src_cpu = busiest->cpu;
4563 env.src_rq = busiest;
4564 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004565
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004566 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004567more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004568 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004569 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304570
4571 /*
4572 * cur_ld_moved - load moved in current iteration
4573 * ld_moved - cumulative load moved across iterations
4574 */
4575 cur_ld_moved = move_tasks(&env);
4576 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004577 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004578 local_irq_restore(flags);
4579
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004580 if (env.flags & LBF_NEED_BREAK) {
4581 env.flags &= ~LBF_NEED_BREAK;
4582 goto more_balance;
4583 }
4584
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004585 /*
4586 * some other cpu did the load balance for us.
4587 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304588 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4589 resched_cpu(env.dst_cpu);
4590
4591 /*
4592 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4593 * us and move them to an alternate dst_cpu in our sched_group
4594 * where they can run. The upper limit on how many times we
4595 * iterate on same src_cpu is dependent on number of cpus in our
4596 * sched_group.
4597 *
4598 * This changes load balance semantics a bit on who can move
4599 * load to a given_cpu. In addition to the given_cpu itself
4600 * (or a ilb_cpu acting on its behalf where given_cpu is
4601 * nohz-idle), we now have balance_cpu in a position to move
4602 * load to given_cpu. In rare situations, this may cause
4603 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4604 * _independently_ and at _same_ time to move some load to
4605 * given_cpu) causing exceess load to be moved to given_cpu.
4606 * This however should not happen so much in practice and
4607 * moreover subsequent load balance cycles should correct the
4608 * excess load moved.
4609 */
4610 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4611 lb_iterations++ < max_lb_iterations) {
4612
Michael Wang78feefc2012-08-06 16:41:59 +08004613 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304614 env.dst_cpu = env.new_dst_cpu;
4615 env.flags &= ~LBF_SOME_PINNED;
4616 env.loop = 0;
4617 env.loop_break = sched_nr_migrate_break;
4618 /*
4619 * Go back to "more_balance" rather than "redo" since we
4620 * need to continue with same src_cpu.
4621 */
4622 goto more_balance;
4623 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004624
4625 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004626 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004627 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304628 if (!cpumask_empty(cpus)) {
4629 env.loop = 0;
4630 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004631 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304632 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004633 goto out_balanced;
4634 }
4635 }
4636
4637 if (!ld_moved) {
4638 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004639 /*
4640 * Increment the failure counter only on periodic balance.
4641 * We do not want newidle balance, which can be very
4642 * frequent, pollute the failure counter causing
4643 * excessive cache_hot migrations and active balances.
4644 */
4645 if (idle != CPU_NEWLY_IDLE)
4646 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004648 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004649 raw_spin_lock_irqsave(&busiest->lock, flags);
4650
Tejun Heo969c7922010-05-06 18:49:21 +02004651 /* don't kick the active_load_balance_cpu_stop,
4652 * if the curr task on busiest cpu can't be
4653 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004654 */
4655 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004656 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004657 raw_spin_unlock_irqrestore(&busiest->lock,
4658 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004659 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004660 goto out_one_pinned;
4661 }
4662
Tejun Heo969c7922010-05-06 18:49:21 +02004663 /*
4664 * ->active_balance synchronizes accesses to
4665 * ->active_balance_work. Once set, it's cleared
4666 * only after active load balance is finished.
4667 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004668 if (!busiest->active_balance) {
4669 busiest->active_balance = 1;
4670 busiest->push_cpu = this_cpu;
4671 active_balance = 1;
4672 }
4673 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004674
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004675 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004676 stop_one_cpu_nowait(cpu_of(busiest),
4677 active_load_balance_cpu_stop, busiest,
4678 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004679 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004680
4681 /*
4682 * We've kicked active balancing, reset the failure
4683 * counter.
4684 */
4685 sd->nr_balance_failed = sd->cache_nice_tries+1;
4686 }
4687 } else
4688 sd->nr_balance_failed = 0;
4689
4690 if (likely(!active_balance)) {
4691 /* We were unbalanced, so reset the balancing interval */
4692 sd->balance_interval = sd->min_interval;
4693 } else {
4694 /*
4695 * If we've begun active balancing, start to back off. This
4696 * case may not be covered by the all_pinned logic if there
4697 * is only 1 task on the busy runqueue (because we don't call
4698 * move_tasks).
4699 */
4700 if (sd->balance_interval < sd->max_interval)
4701 sd->balance_interval *= 2;
4702 }
4703
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004704 goto out;
4705
4706out_balanced:
4707 schedstat_inc(sd, lb_balanced[idle]);
4708
4709 sd->nr_balance_failed = 0;
4710
4711out_one_pinned:
4712 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004713 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004714 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004715 (sd->balance_interval < sd->max_interval))
4716 sd->balance_interval *= 2;
4717
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004718 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004719out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004720 return ld_moved;
4721}
4722
4723/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004724 * idle_balance is called by schedule() if this_cpu is about to become
4725 * idle. Attempts to pull tasks from other CPUs.
4726 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004727void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004728{
4729 struct sched_domain *sd;
4730 int pulled_task = 0;
4731 unsigned long next_balance = jiffies + HZ;
4732
4733 this_rq->idle_stamp = this_rq->clock;
4734
4735 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4736 return;
4737
Ben Segall18bf2802012-10-04 12:51:20 +02004738 update_rq_runnable_avg(this_rq, 1);
4739
Peter Zijlstraf492e122009-12-23 15:29:42 +01004740 /*
4741 * Drop the rq->lock, but keep IRQ/preempt disabled.
4742 */
4743 raw_spin_unlock(&this_rq->lock);
4744
Paul Turnerc66eaf62010-11-15 15:47:07 -08004745 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004746 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004747 for_each_domain(this_cpu, sd) {
4748 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004749 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004750
4751 if (!(sd->flags & SD_LOAD_BALANCE))
4752 continue;
4753
Peter Zijlstraf492e122009-12-23 15:29:42 +01004754 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004755 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004756 pulled_task = load_balance(this_cpu, this_rq,
4757 sd, CPU_NEWLY_IDLE, &balance);
4758 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004759
4760 interval = msecs_to_jiffies(sd->balance_interval);
4761 if (time_after(next_balance, sd->last_balance + interval))
4762 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004763 if (pulled_task) {
4764 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004765 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004766 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004767 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004768 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004769
4770 raw_spin_lock(&this_rq->lock);
4771
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004772 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4773 /*
4774 * We are going idle. next_balance may be set based on
4775 * a busy processor. So reset next_balance.
4776 */
4777 this_rq->next_balance = next_balance;
4778 }
4779}
4780
4781/*
Tejun Heo969c7922010-05-06 18:49:21 +02004782 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4783 * running tasks off the busiest CPU onto idle CPUs. It requires at
4784 * least 1 task to be running on each physical CPU where possible, and
4785 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004786 */
Tejun Heo969c7922010-05-06 18:49:21 +02004787static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004788{
Tejun Heo969c7922010-05-06 18:49:21 +02004789 struct rq *busiest_rq = data;
4790 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004791 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004792 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004793 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004794
4795 raw_spin_lock_irq(&busiest_rq->lock);
4796
4797 /* make sure the requested cpu hasn't gone down in the meantime */
4798 if (unlikely(busiest_cpu != smp_processor_id() ||
4799 !busiest_rq->active_balance))
4800 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004801
4802 /* Is there any task to move? */
4803 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004804 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004805
4806 /*
4807 * This condition is "impossible", if it occurs
4808 * we need to fix it. Originally reported by
4809 * Bjorn Helgaas on a 128-cpu setup.
4810 */
4811 BUG_ON(busiest_rq == target_rq);
4812
4813 /* move a task from busiest_rq to target_rq */
4814 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004815
4816 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004817 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004818 for_each_domain(target_cpu, sd) {
4819 if ((sd->flags & SD_LOAD_BALANCE) &&
4820 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4821 break;
4822 }
4823
4824 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004825 struct lb_env env = {
4826 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004827 .dst_cpu = target_cpu,
4828 .dst_rq = target_rq,
4829 .src_cpu = busiest_rq->cpu,
4830 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004831 .idle = CPU_IDLE,
4832 };
4833
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004834 schedstat_inc(sd, alb_count);
4835
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004836 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004837 schedstat_inc(sd, alb_pushed);
4838 else
4839 schedstat_inc(sd, alb_failed);
4840 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004841 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004842 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004843out_unlock:
4844 busiest_rq->active_balance = 0;
4845 raw_spin_unlock_irq(&busiest_rq->lock);
4846 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004847}
4848
4849#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004850/*
4851 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004852 * - When one of the busy CPUs notice that there may be an idle rebalancing
4853 * needed, they will kick the idle load balancer, which then does idle
4854 * load balancing for all the idle CPUs.
4855 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004856static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004857 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004858 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004859 unsigned long next_balance; /* in jiffy units */
4860} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004861
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004862static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004863{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004864 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004865
Suresh Siddha786d6dc72011-12-01 17:07:35 -08004866 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4867 return ilb;
4868
4869 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004870}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004871
4872/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004873 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4874 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4875 * CPU (if there is one).
4876 */
4877static void nohz_balancer_kick(int cpu)
4878{
4879 int ilb_cpu;
4880
4881 nohz.next_balance++;
4882
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004883 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004884
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004885 if (ilb_cpu >= nr_cpu_ids)
4886 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004887
Suresh Siddhacd490c52011-12-06 11:26:34 -08004888 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004889 return;
4890 /*
4891 * Use smp_send_reschedule() instead of resched_cpu().
4892 * This way we generate a sched IPI on the target cpu which
4893 * is idle. And the softirq performing nohz idle load balance
4894 * will be run before returning from the IPI.
4895 */
4896 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004897 return;
4898}
4899
Alex Shic1cc0172012-09-10 15:10:58 +08004900static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004901{
4902 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4903 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4904 atomic_dec(&nohz.nr_cpus);
4905 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4906 }
4907}
4908
Suresh Siddha69e1e812011-12-01 17:07:33 -08004909static inline void set_cpu_sd_state_busy(void)
4910{
4911 struct sched_domain *sd;
4912 int cpu = smp_processor_id();
4913
4914 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4915 return;
4916 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4917
4918 rcu_read_lock();
4919 for_each_domain(cpu, sd)
4920 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4921 rcu_read_unlock();
4922}
4923
4924void set_cpu_sd_state_idle(void)
4925{
4926 struct sched_domain *sd;
4927 int cpu = smp_processor_id();
4928
4929 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4930 return;
4931 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4932
4933 rcu_read_lock();
4934 for_each_domain(cpu, sd)
4935 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4936 rcu_read_unlock();
4937}
4938
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004939/*
Alex Shic1cc0172012-09-10 15:10:58 +08004940 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004941 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004942 */
Alex Shic1cc0172012-09-10 15:10:58 +08004943void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004944{
Suresh Siddha71325962012-01-19 18:28:57 -08004945 /*
4946 * If this cpu is going down, then nothing needs to be done.
4947 */
4948 if (!cpu_active(cpu))
4949 return;
4950
Alex Shic1cc0172012-09-10 15:10:58 +08004951 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
4952 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004953
Alex Shic1cc0172012-09-10 15:10:58 +08004954 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
4955 atomic_inc(&nohz.nr_cpus);
4956 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004957}
Suresh Siddha71325962012-01-19 18:28:57 -08004958
4959static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4960 unsigned long action, void *hcpu)
4961{
4962 switch (action & ~CPU_TASKS_FROZEN) {
4963 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08004964 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08004965 return NOTIFY_OK;
4966 default:
4967 return NOTIFY_DONE;
4968 }
4969}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004970#endif
4971
4972static DEFINE_SPINLOCK(balancing);
4973
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004974/*
4975 * Scale the max load_balance interval with the number of CPUs in the system.
4976 * This trades load-balance latency on larger machines for less cross talk.
4977 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004978void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004979{
4980 max_load_balance_interval = HZ*num_online_cpus()/10;
4981}
4982
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004983/*
4984 * It checks each scheduling domain to see if it is due to be balanced,
4985 * and initiates a balancing operation if so.
4986 *
4987 * Balancing parameters are set up in arch_init_sched_domains.
4988 */
4989static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4990{
4991 int balance = 1;
4992 struct rq *rq = cpu_rq(cpu);
4993 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004994 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004995 /* Earliest time when we have to do rebalance again */
4996 unsigned long next_balance = jiffies + 60*HZ;
4997 int update_next_balance = 0;
4998 int need_serialize;
4999
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005000 update_shares(cpu);
5001
Peter Zijlstradce840a2011-04-07 14:09:50 +02005002 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005003 for_each_domain(cpu, sd) {
5004 if (!(sd->flags & SD_LOAD_BALANCE))
5005 continue;
5006
5007 interval = sd->balance_interval;
5008 if (idle != CPU_IDLE)
5009 interval *= sd->busy_factor;
5010
5011 /* scale ms to jiffies */
5012 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005013 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005014
5015 need_serialize = sd->flags & SD_SERIALIZE;
5016
5017 if (need_serialize) {
5018 if (!spin_trylock(&balancing))
5019 goto out;
5020 }
5021
5022 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5023 if (load_balance(cpu, rq, sd, idle, &balance)) {
5024 /*
5025 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005026 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005027 */
5028 idle = CPU_NOT_IDLE;
5029 }
5030 sd->last_balance = jiffies;
5031 }
5032 if (need_serialize)
5033 spin_unlock(&balancing);
5034out:
5035 if (time_after(next_balance, sd->last_balance + interval)) {
5036 next_balance = sd->last_balance + interval;
5037 update_next_balance = 1;
5038 }
5039
5040 /*
5041 * Stop the load balance at this level. There is another
5042 * CPU in our sched group which is doing load balancing more
5043 * actively.
5044 */
5045 if (!balance)
5046 break;
5047 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005048 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005049
5050 /*
5051 * next_balance will be updated only when there is a need.
5052 * When the cpu is attached to null domain for ex, it will not be
5053 * updated.
5054 */
5055 if (likely(update_next_balance))
5056 rq->next_balance = next_balance;
5057}
5058
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005059#ifdef CONFIG_NO_HZ
5060/*
5061 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5062 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5063 */
5064static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5065{
5066 struct rq *this_rq = cpu_rq(this_cpu);
5067 struct rq *rq;
5068 int balance_cpu;
5069
Suresh Siddha1c792db2011-12-01 17:07:32 -08005070 if (idle != CPU_IDLE ||
5071 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5072 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005073
5074 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005075 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005076 continue;
5077
5078 /*
5079 * If this cpu gets work to do, stop the load balancing
5080 * work being done for other cpus. Next load
5081 * balancing owner will pick it up.
5082 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005083 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005085
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005086 rq = cpu_rq(balance_cpu);
5087
5088 raw_spin_lock_irq(&rq->lock);
5089 update_rq_clock(rq);
5090 update_idle_cpu_load(rq);
5091 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005092
5093 rebalance_domains(balance_cpu, CPU_IDLE);
5094
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005095 if (time_after(this_rq->next_balance, rq->next_balance))
5096 this_rq->next_balance = rq->next_balance;
5097 }
5098 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005099end:
5100 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005101}
5102
5103/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005104 * Current heuristic for kicking the idle load balancer in the presence
5105 * of an idle cpu is the system.
5106 * - This rq has more than one task.
5107 * - At any scheduler domain level, this cpu's scheduler group has multiple
5108 * busy cpu's exceeding the group's power.
5109 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5110 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005111 */
5112static inline int nohz_kick_needed(struct rq *rq, int cpu)
5113{
5114 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005115 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005116
Suresh Siddha1c792db2011-12-01 17:07:32 -08005117 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005118 return 0;
5119
Suresh Siddha1c792db2011-12-01 17:07:32 -08005120 /*
5121 * We may be recently in ticked or tickless idle mode. At the first
5122 * busy tick after returning from idle, we will update the busy stats.
5123 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005124 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005125 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005126
5127 /*
5128 * None are in tickless mode and hence no need for NOHZ idle load
5129 * balancing.
5130 */
5131 if (likely(!atomic_read(&nohz.nr_cpus)))
5132 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005133
5134 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005135 return 0;
5136
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005137 if (rq->nr_running >= 2)
5138 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005139
Peter Zijlstra067491b2011-12-07 14:32:08 +01005140 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005141 for_each_domain(cpu, sd) {
5142 struct sched_group *sg = sd->groups;
5143 struct sched_group_power *sgp = sg->sgp;
5144 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005145
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005146 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005147 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005148
5149 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5150 && (cpumask_first_and(nohz.idle_cpus_mask,
5151 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005152 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005153
5154 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5155 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005156 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005157 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005158 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005159
5160need_kick_unlock:
5161 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005162need_kick:
5163 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005164}
5165#else
5166static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5167#endif
5168
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005169/*
5170 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005171 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005172 */
5173static void run_rebalance_domains(struct softirq_action *h)
5174{
5175 int this_cpu = smp_processor_id();
5176 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005177 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005178 CPU_IDLE : CPU_NOT_IDLE;
5179
5180 rebalance_domains(this_cpu, idle);
5181
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005182 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005183 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005184 * balancing on behalf of the other idle cpus whose ticks are
5185 * stopped.
5186 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005187 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005188}
5189
5190static inline int on_null_domain(int cpu)
5191{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005192 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005193}
5194
5195/*
5196 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005197 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005198void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005199{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005200 /* Don't need to rebalance while attached to NULL domain */
5201 if (time_after_eq(jiffies, rq->next_balance) &&
5202 likely(!on_null_domain(cpu)))
5203 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005204#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005205 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005206 nohz_balancer_kick(cpu);
5207#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005208}
5209
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005210static void rq_online_fair(struct rq *rq)
5211{
5212 update_sysctl();
5213}
5214
5215static void rq_offline_fair(struct rq *rq)
5216{
5217 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005218
5219 /* Ensure any throttled groups are reachable by pick_next_task */
5220 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005221}
5222
Dhaval Giani55e12e52008-06-24 23:39:43 +05305223#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005224
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005225/*
5226 * scheduler tick hitting a task of our scheduling class:
5227 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005228static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005229{
5230 struct cfs_rq *cfs_rq;
5231 struct sched_entity *se = &curr->se;
5232
5233 for_each_sched_entity(se) {
5234 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005235 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005236 }
Ben Segall18bf2802012-10-04 12:51:20 +02005237
5238 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005239}
5240
5241/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005242 * called on fork with the child task as argument from the parent's context
5243 * - child not yet on the tasklist
5244 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005245 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005246static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005247{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005248 struct cfs_rq *cfs_rq;
5249 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005250 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005251 struct rq *rq = this_rq();
5252 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005253
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005254 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005255
Peter Zijlstra861d0342010-08-19 13:31:43 +02005256 update_rq_clock(rq);
5257
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005258 cfs_rq = task_cfs_rq(current);
5259 curr = cfs_rq->curr;
5260
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005261 if (unlikely(task_cpu(p) != this_cpu)) {
5262 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005263 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005264 rcu_read_unlock();
5265 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005266
Ting Yang7109c442007-08-28 12:53:24 +02005267 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005268
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005269 if (curr)
5270 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005271 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005272
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005273 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005274 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005275 * Upon rescheduling, sched_class::put_prev_task() will place
5276 * 'current' within the tree based on its new key value.
5277 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005278 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305279 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005280 }
5281
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005282 se->vruntime -= cfs_rq->min_vruntime;
5283
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005284 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005285}
5286
Steven Rostedtcb469842008-01-25 21:08:22 +01005287/*
5288 * Priority of the task has changed. Check to see if we preempt
5289 * the current task.
5290 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005291static void
5292prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005293{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005294 if (!p->se.on_rq)
5295 return;
5296
Steven Rostedtcb469842008-01-25 21:08:22 +01005297 /*
5298 * Reschedule if we are currently running on this runqueue and
5299 * our priority decreased, or if we are not currently running on
5300 * this runqueue and our priority is higher than the current's
5301 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005302 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005303 if (p->prio > oldprio)
5304 resched_task(rq->curr);
5305 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005306 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005307}
5308
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005309static void switched_from_fair(struct rq *rq, struct task_struct *p)
5310{
5311 struct sched_entity *se = &p->se;
5312 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5313
5314 /*
5315 * Ensure the task's vruntime is normalized, so that when its
5316 * switched back to the fair class the enqueue_entity(.flags=0) will
5317 * do the right thing.
5318 *
5319 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5320 * have normalized the vruntime, if it was !on_rq, then only when
5321 * the task is sleeping will it still have non-normalized vruntime.
5322 */
5323 if (!se->on_rq && p->state != TASK_RUNNING) {
5324 /*
5325 * Fix up our vruntime so that the current sleep doesn't
5326 * cause 'unlimited' sleep bonus.
5327 */
5328 place_entity(cfs_rq, se, 0);
5329 se->vruntime -= cfs_rq->min_vruntime;
5330 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005331
5332#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5333 /*
5334 * Remove our load from contribution when we leave sched_fair
5335 * and ensure we don't carry in an old decay_count if we
5336 * switch back.
5337 */
5338 if (p->se.avg.decay_count) {
5339 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5340 __synchronize_entity_decay(&p->se);
5341 subtract_blocked_load_contrib(cfs_rq,
5342 p->se.avg.load_avg_contrib);
5343 }
5344#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005345}
5346
Steven Rostedtcb469842008-01-25 21:08:22 +01005347/*
5348 * We switched to the sched_fair class.
5349 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005350static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005351{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005352 if (!p->se.on_rq)
5353 return;
5354
Steven Rostedtcb469842008-01-25 21:08:22 +01005355 /*
5356 * We were most likely switched from sched_rt, so
5357 * kick off the schedule if running, otherwise just see
5358 * if we can still preempt the current task.
5359 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005360 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005361 resched_task(rq->curr);
5362 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005363 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005364}
5365
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005366/* Account for a task changing its policy or group.
5367 *
5368 * This routine is mostly called to set cfs_rq->curr field when a task
5369 * migrates between groups/classes.
5370 */
5371static void set_curr_task_fair(struct rq *rq)
5372{
5373 struct sched_entity *se = &rq->curr->se;
5374
Paul Turnerec12cb72011-07-21 09:43:30 -07005375 for_each_sched_entity(se) {
5376 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5377
5378 set_next_entity(cfs_rq, se);
5379 /* ensure bandwidth has been allocated on our new cfs_rq */
5380 account_cfs_rq_runtime(cfs_rq, 0);
5381 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005382}
5383
Peter Zijlstra029632f2011-10-25 10:00:11 +02005384void init_cfs_rq(struct cfs_rq *cfs_rq)
5385{
5386 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005387 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5388#ifndef CONFIG_64BIT
5389 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5390#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005391#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5392 atomic64_set(&cfs_rq->decay_counter, 1);
5393#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005394}
5395
Peter Zijlstra810b3812008-02-29 15:21:01 -05005396#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005397static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005398{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005399 /*
5400 * If the task was not on the rq at the time of this cgroup movement
5401 * it must have been asleep, sleeping tasks keep their ->vruntime
5402 * absolute on their old rq until wakeup (needed for the fair sleeper
5403 * bonus in place_entity()).
5404 *
5405 * If it was on the rq, we've just 'preempted' it, which does convert
5406 * ->vruntime to a relative base.
5407 *
5408 * Make sure both cases convert their relative position when migrating
5409 * to another cgroup's rq. This does somewhat interfere with the
5410 * fair sleeper stuff for the first placement, but who cares.
5411 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005412 /*
5413 * When !on_rq, vruntime of the task has usually NOT been normalized.
5414 * But there are some cases where it has already been normalized:
5415 *
5416 * - Moving a forked child which is waiting for being woken up by
5417 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005418 * - Moving a task which has been woken up by try_to_wake_up() and
5419 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005420 *
5421 * To prevent boost or penalty in the new cfs_rq caused by delta
5422 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5423 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005424 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005425 on_rq = 1;
5426
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005427 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005428 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5429 set_task_rq(p, task_cpu(p));
5430 if (!on_rq)
5431 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005432}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005433
5434void free_fair_sched_group(struct task_group *tg)
5435{
5436 int i;
5437
5438 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5439
5440 for_each_possible_cpu(i) {
5441 if (tg->cfs_rq)
5442 kfree(tg->cfs_rq[i]);
5443 if (tg->se)
5444 kfree(tg->se[i]);
5445 }
5446
5447 kfree(tg->cfs_rq);
5448 kfree(tg->se);
5449}
5450
5451int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5452{
5453 struct cfs_rq *cfs_rq;
5454 struct sched_entity *se;
5455 int i;
5456
5457 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5458 if (!tg->cfs_rq)
5459 goto err;
5460 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5461 if (!tg->se)
5462 goto err;
5463
5464 tg->shares = NICE_0_LOAD;
5465
5466 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5467
5468 for_each_possible_cpu(i) {
5469 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5470 GFP_KERNEL, cpu_to_node(i));
5471 if (!cfs_rq)
5472 goto err;
5473
5474 se = kzalloc_node(sizeof(struct sched_entity),
5475 GFP_KERNEL, cpu_to_node(i));
5476 if (!se)
5477 goto err_free_rq;
5478
5479 init_cfs_rq(cfs_rq);
5480 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5481 }
5482
5483 return 1;
5484
5485err_free_rq:
5486 kfree(cfs_rq);
5487err:
5488 return 0;
5489}
5490
5491void unregister_fair_sched_group(struct task_group *tg, int cpu)
5492{
5493 struct rq *rq = cpu_rq(cpu);
5494 unsigned long flags;
5495
5496 /*
5497 * Only empty task groups can be destroyed; so we can speculatively
5498 * check on_list without danger of it being re-added.
5499 */
5500 if (!tg->cfs_rq[cpu]->on_list)
5501 return;
5502
5503 raw_spin_lock_irqsave(&rq->lock, flags);
5504 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5505 raw_spin_unlock_irqrestore(&rq->lock, flags);
5506}
5507
5508void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5509 struct sched_entity *se, int cpu,
5510 struct sched_entity *parent)
5511{
5512 struct rq *rq = cpu_rq(cpu);
5513
5514 cfs_rq->tg = tg;
5515 cfs_rq->rq = rq;
5516#ifdef CONFIG_SMP
5517 /* allow initial update_cfs_load() to truncate */
5518 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005519#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005520 init_cfs_rq_runtime(cfs_rq);
5521
5522 tg->cfs_rq[cpu] = cfs_rq;
5523 tg->se[cpu] = se;
5524
5525 /* se could be NULL for root_task_group */
5526 if (!se)
5527 return;
5528
5529 if (!parent)
5530 se->cfs_rq = &rq->cfs;
5531 else
5532 se->cfs_rq = parent->my_q;
5533
5534 se->my_q = cfs_rq;
5535 update_load_set(&se->load, 0);
5536 se->parent = parent;
5537}
5538
5539static DEFINE_MUTEX(shares_mutex);
5540
5541int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5542{
5543 int i;
5544 unsigned long flags;
5545
5546 /*
5547 * We can't change the weight of the root cgroup.
5548 */
5549 if (!tg->se[0])
5550 return -EINVAL;
5551
5552 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5553
5554 mutex_lock(&shares_mutex);
5555 if (tg->shares == shares)
5556 goto done;
5557
5558 tg->shares = shares;
5559 for_each_possible_cpu(i) {
5560 struct rq *rq = cpu_rq(i);
5561 struct sched_entity *se;
5562
5563 se = tg->se[i];
5564 /* Propagate contribution to hierarchy */
5565 raw_spin_lock_irqsave(&rq->lock, flags);
5566 for_each_sched_entity(se)
5567 update_cfs_shares(group_cfs_rq(se));
5568 raw_spin_unlock_irqrestore(&rq->lock, flags);
5569 }
5570
5571done:
5572 mutex_unlock(&shares_mutex);
5573 return 0;
5574}
5575#else /* CONFIG_FAIR_GROUP_SCHED */
5576
5577void free_fair_sched_group(struct task_group *tg) { }
5578
5579int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5580{
5581 return 1;
5582}
5583
5584void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5585
5586#endif /* CONFIG_FAIR_GROUP_SCHED */
5587
Peter Zijlstra810b3812008-02-29 15:21:01 -05005588
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005589static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005590{
5591 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005592 unsigned int rr_interval = 0;
5593
5594 /*
5595 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5596 * idle runqueue:
5597 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005598 if (rq->cfs.load.weight)
5599 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005600
5601 return rr_interval;
5602}
5603
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005604/*
5605 * All the scheduling class methods:
5606 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005607const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005608 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005609 .enqueue_task = enqueue_task_fair,
5610 .dequeue_task = dequeue_task_fair,
5611 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005612 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005613
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005614 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005615
5616 .pick_next_task = pick_next_task_fair,
5617 .put_prev_task = put_prev_task_fair,
5618
Peter Williams681f3e62007-10-24 18:23:51 +02005619#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005620 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005621 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005622
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005623 .rq_online = rq_online_fair,
5624 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005625
5626 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005627#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005628
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005629 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005630 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005631 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005632
5633 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005634 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005635 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005636
Peter Williams0d721ce2009-09-21 01:31:53 +00005637 .get_rr_interval = get_rr_interval_fair,
5638
Peter Zijlstra810b3812008-02-29 15:21:01 -05005639#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005640 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005641#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005642};
5643
5644#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005645void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005646{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005647 struct cfs_rq *cfs_rq;
5648
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005649 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005650 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005651 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005652 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005653}
5654#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005655
5656__init void init_sched_fair_class(void)
5657{
5658#ifdef CONFIG_SMP
5659 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5660
5661#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005662 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005663 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005664 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005665#endif
5666#endif /* SMP */
5667
5668}