blob: 83194175e841b74ea76f2d2ad177a88a16114ae1 [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 Zijlstra6d0f0eb2007-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 Zijlstra6d0f0eb2007-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 Zijlstrae4f42882009-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}
3050#endif /* CONFIG_SMP */
3051
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003052static unsigned long
3053wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003054{
3055 unsigned long gran = sysctl_sched_wakeup_granularity;
3056
3057 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003058 * Since its curr running now, convert the gran from real-time
3059 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003060 *
3061 * By using 'se' instead of 'curr' we penalize light tasks, so
3062 * they get preempted easier. That is, if 'se' < 'curr' then
3063 * the resulting gran will be larger, therefore penalizing the
3064 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3065 * be smaller, again penalizing the lighter task.
3066 *
3067 * This is especially important for buddies when the leftmost
3068 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003069 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003070 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003071}
3072
3073/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003074 * Should 'se' preempt 'curr'.
3075 *
3076 * |s1
3077 * |s2
3078 * |s3
3079 * g
3080 * |<--->|c
3081 *
3082 * w(c, s1) = -1
3083 * w(c, s2) = 0
3084 * w(c, s3) = 1
3085 *
3086 */
3087static int
3088wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3089{
3090 s64 gran, vdiff = curr->vruntime - se->vruntime;
3091
3092 if (vdiff <= 0)
3093 return -1;
3094
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003095 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003096 if (vdiff > gran)
3097 return 1;
3098
3099 return 0;
3100}
3101
Peter Zijlstra02479092008-11-04 21:25:10 +01003102static void set_last_buddy(struct sched_entity *se)
3103{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003104 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3105 return;
3106
3107 for_each_sched_entity(se)
3108 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003109}
3110
3111static void set_next_buddy(struct sched_entity *se)
3112{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003113 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3114 return;
3115
3116 for_each_sched_entity(se)
3117 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003118}
3119
Rik van Rielac53db52011-02-01 09:51:03 -05003120static void set_skip_buddy(struct sched_entity *se)
3121{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003122 for_each_sched_entity(se)
3123 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003124}
3125
Peter Zijlstra464b7522008-10-24 11:06:15 +02003126/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003127 * Preempt the current task with a newly woken task if needed:
3128 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003129static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003130{
3131 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003132 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003133 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003134 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003135 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003136
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003137 if (unlikely(se == pse))
3138 return;
3139
Paul Turner5238cdd2011-07-21 09:43:37 -07003140 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003141 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003142 * unconditionally check_prempt_curr() after an enqueue (which may have
3143 * lead to a throttle). This both saves work and prevents false
3144 * next-buddy nomination below.
3145 */
3146 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3147 return;
3148
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003149 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003150 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003151 next_buddy_marked = 1;
3152 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003153
Bharata B Raoaec0a512008-08-28 14:42:49 +05303154 /*
3155 * We can come here with TIF_NEED_RESCHED already set from new task
3156 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003157 *
3158 * Note: this also catches the edge-case of curr being in a throttled
3159 * group (e.g. via set_curr_task), since update_curr() (in the
3160 * enqueue of curr) will have resulted in resched being set. This
3161 * prevents us from potentially nominating it as a false LAST_BUDDY
3162 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303163 */
3164 if (test_tsk_need_resched(curr))
3165 return;
3166
Darren Harta2f5c9a2011-02-22 13:04:33 -08003167 /* Idle tasks are by definition preempted by non-idle tasks. */
3168 if (unlikely(curr->policy == SCHED_IDLE) &&
3169 likely(p->policy != SCHED_IDLE))
3170 goto preempt;
3171
Ingo Molnar91c234b2007-10-15 17:00:18 +02003172 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003173 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3174 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003175 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003176 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003177 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003178
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003179 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003180 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003181 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003182 if (wakeup_preempt_entity(se, pse) == 1) {
3183 /*
3184 * Bias pick_next to pick the sched entity that is
3185 * triggering this preemption.
3186 */
3187 if (!next_buddy_marked)
3188 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003189 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003190 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003191
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003192 return;
3193
3194preempt:
3195 resched_task(curr);
3196 /*
3197 * Only set the backward buddy when the current task is still
3198 * on the rq. This can happen when a wakeup gets interleaved
3199 * with schedule on the ->pre_schedule() or idle_balance()
3200 * point, either of which can * drop the rq lock.
3201 *
3202 * Also, during early boot the idle thread is in the fair class,
3203 * for obvious reasons its a bad idea to schedule back to it.
3204 */
3205 if (unlikely(!se->on_rq || curr == rq->idle))
3206 return;
3207
3208 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3209 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003210}
3211
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003212static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003213{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003214 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003215 struct cfs_rq *cfs_rq = &rq->cfs;
3216 struct sched_entity *se;
3217
Tim Blechmann36ace272009-11-24 11:55:45 +01003218 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003219 return NULL;
3220
3221 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003222 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003223 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003224 cfs_rq = group_cfs_rq(se);
3225 } while (cfs_rq);
3226
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003227 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003228 if (hrtick_enabled(rq))
3229 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003230
3231 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003232}
3233
3234/*
3235 * Account for a descheduled task:
3236 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003237static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003238{
3239 struct sched_entity *se = &prev->se;
3240 struct cfs_rq *cfs_rq;
3241
3242 for_each_sched_entity(se) {
3243 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003244 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003245 }
3246}
3247
Rik van Rielac53db52011-02-01 09:51:03 -05003248/*
3249 * sched_yield() is very simple
3250 *
3251 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3252 */
3253static void yield_task_fair(struct rq *rq)
3254{
3255 struct task_struct *curr = rq->curr;
3256 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3257 struct sched_entity *se = &curr->se;
3258
3259 /*
3260 * Are we the only task in the tree?
3261 */
3262 if (unlikely(rq->nr_running == 1))
3263 return;
3264
3265 clear_buddies(cfs_rq, se);
3266
3267 if (curr->policy != SCHED_BATCH) {
3268 update_rq_clock(rq);
3269 /*
3270 * Update run-time statistics of the 'current'.
3271 */
3272 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003273 /*
3274 * Tell update_rq_clock() that we've just updated,
3275 * so we don't do microscopic update in schedule()
3276 * and double the fastpath cost.
3277 */
3278 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003279 }
3280
3281 set_skip_buddy(se);
3282}
3283
Mike Galbraithd95f4122011-02-01 09:50:51 -05003284static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3285{
3286 struct sched_entity *se = &p->se;
3287
Paul Turner5238cdd2011-07-21 09:43:37 -07003288 /* throttled hierarchies are not runnable */
3289 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003290 return false;
3291
3292 /* Tell the scheduler that we'd really like pse to run next. */
3293 set_next_buddy(se);
3294
Mike Galbraithd95f4122011-02-01 09:50:51 -05003295 yield_task_fair(rq);
3296
3297 return true;
3298}
3299
Peter Williams681f3e62007-10-24 18:23:51 +02003300#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003301/**************************************************
3302 * Fair scheduling class load-balancing methods:
3303 */
3304
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003305static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3306
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003307#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003308#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303309#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003310
3311struct lb_env {
3312 struct sched_domain *sd;
3313
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003314 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303315 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003316
3317 int dst_cpu;
3318 struct rq *dst_rq;
3319
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303320 struct cpumask *dst_grpmask;
3321 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003322 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003323 long imbalance;
Michael Wangb9403132012-07-12 16:10:13 +08003324 /* The set of CPUs under consideration for load-balancing */
3325 struct cpumask *cpus;
3326
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003327 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003328
3329 unsigned int loop;
3330 unsigned int loop_break;
3331 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003332};
3333
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003334/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003335 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003336 * Both runqueues must be locked.
3337 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003338static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003339{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003340 deactivate_task(env->src_rq, p, 0);
3341 set_task_cpu(p, env->dst_cpu);
3342 activate_task(env->dst_rq, p, 0);
3343 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003344}
3345
3346/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003347 * Is this task likely cache-hot:
3348 */
3349static int
3350task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3351{
3352 s64 delta;
3353
3354 if (p->sched_class != &fair_sched_class)
3355 return 0;
3356
3357 if (unlikely(p->policy == SCHED_IDLE))
3358 return 0;
3359
3360 /*
3361 * Buddy candidates are cache hot:
3362 */
3363 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3364 (&p->se == cfs_rq_of(&p->se)->next ||
3365 &p->se == cfs_rq_of(&p->se)->last))
3366 return 1;
3367
3368 if (sysctl_sched_migration_cost == -1)
3369 return 1;
3370 if (sysctl_sched_migration_cost == 0)
3371 return 0;
3372
3373 delta = now - p->se.exec_start;
3374
3375 return delta < (s64)sysctl_sched_migration_cost;
3376}
3377
3378/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003379 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3380 */
3381static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003382int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003383{
3384 int tsk_cache_hot = 0;
3385 /*
3386 * We do not migrate tasks that are:
3387 * 1) running (obviously), or
3388 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3389 * 3) are cache-hot on their current CPU.
3390 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003391 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303392 int new_dst_cpu;
3393
Lucas De Marchi41acab82010-03-10 23:37:45 -03003394 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303395
3396 /*
3397 * Remember if this task can be migrated to any other cpu in
3398 * our sched_group. We may want to revisit it if we couldn't
3399 * meet load balance goals by pulling other tasks on src_cpu.
3400 *
3401 * Also avoid computing new_dst_cpu if we have already computed
3402 * one in current iteration.
3403 */
3404 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3405 return 0;
3406
3407 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3408 tsk_cpus_allowed(p));
3409 if (new_dst_cpu < nr_cpu_ids) {
3410 env->flags |= LBF_SOME_PINNED;
3411 env->new_dst_cpu = new_dst_cpu;
3412 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003413 return 0;
3414 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303415
3416 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003417 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003418
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003419 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003420 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003421 return 0;
3422 }
3423
3424 /*
3425 * Aggressive migration if:
3426 * 1) task is cache cold, or
3427 * 2) too many balance attempts have failed.
3428 */
3429
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003430 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003431 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003432 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003433#ifdef CONFIG_SCHEDSTATS
3434 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003435 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003436 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003437 }
3438#endif
3439 return 1;
3440 }
3441
3442 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003443 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003444 return 0;
3445 }
3446 return 1;
3447}
3448
Peter Zijlstra897c3952009-12-17 17:45:42 +01003449/*
3450 * move_one_task tries to move exactly one task from busiest to this_rq, as
3451 * part of active balancing operations within "domain".
3452 * Returns 1 if successful and 0 otherwise.
3453 *
3454 * Called with both runqueues locked.
3455 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003456static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003457{
3458 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003459
Peter Zijlstra367456c2012-02-20 21:49:09 +01003460 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3461 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3462 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003463
Peter Zijlstra367456c2012-02-20 21:49:09 +01003464 if (!can_migrate_task(p, env))
3465 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003466
Peter Zijlstra367456c2012-02-20 21:49:09 +01003467 move_task(p, env);
3468 /*
3469 * Right now, this is only the second place move_task()
3470 * is called, so we can safely collect move_task()
3471 * stats here rather than inside move_task().
3472 */
3473 schedstat_inc(env->sd, lb_gained[env->idle]);
3474 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003475 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003476 return 0;
3477}
3478
Peter Zijlstra367456c2012-02-20 21:49:09 +01003479static unsigned long task_h_load(struct task_struct *p);
3480
Peter Zijlstraeb953082012-04-17 13:38:40 +02003481static const unsigned int sched_nr_migrate_break = 32;
3482
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003483/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003484 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003485 * this_rq, as part of a balancing operation within domain "sd".
3486 * Returns 1 if successful and 0 otherwise.
3487 *
3488 * Called with both runqueues locked.
3489 */
3490static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003491{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003492 struct list_head *tasks = &env->src_rq->cfs_tasks;
3493 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003494 unsigned long load;
3495 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003496
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003497 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003498 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003499
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003500 while (!list_empty(tasks)) {
3501 p = list_first_entry(tasks, struct task_struct, se.group_node);
3502
Peter Zijlstra367456c2012-02-20 21:49:09 +01003503 env->loop++;
3504 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003505 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003506 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003507
3508 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003509 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003510 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003511 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003512 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003513 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003514
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003515 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003516 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003517
Peter Zijlstra367456c2012-02-20 21:49:09 +01003518 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003519
Peter Zijlstraeb953082012-04-17 13:38:40 +02003520 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003521 goto next;
3522
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003523 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003524 goto next;
3525
3526 if (!can_migrate_task(p, env))
3527 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003528
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003529 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003530 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003531 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003532
3533#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003534 /*
3535 * NEWIDLE balancing is a source of latency, so preemptible
3536 * kernels will stop after the first task is pulled to minimize
3537 * the critical section.
3538 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003539 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003540 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003541#endif
3542
Peter Zijlstraee00e662009-12-17 17:25:20 +01003543 /*
3544 * We only want to steal up to the prescribed amount of
3545 * weighted load.
3546 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003547 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003548 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003549
Peter Zijlstra367456c2012-02-20 21:49:09 +01003550 continue;
3551next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003552 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003553 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003554
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003555 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003556 * Right now, this is one of only two places move_task() is called,
3557 * so we can safely collect move_task() stats here rather than
3558 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003559 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003560 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003561
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003562 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003563}
3564
Peter Zijlstra230059de2009-12-17 17:47:12 +01003565#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003566/*
3567 * update tg->load_weight by folding this cpu's load_avg
3568 */
Paul Turner67e86252010-11-15 15:47:05 -08003569static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003570{
3571 struct cfs_rq *cfs_rq;
3572 unsigned long flags;
3573 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003574
3575 if (!tg->se[cpu])
3576 return 0;
3577
3578 rq = cpu_rq(cpu);
3579 cfs_rq = tg->cfs_rq[cpu];
3580
3581 raw_spin_lock_irqsave(&rq->lock, flags);
3582
3583 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003584 update_cfs_load(cfs_rq, 1);
Paul Turner9ee474f2012-10-04 13:18:30 +02003585 update_cfs_rq_blocked_load(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003586
3587 /*
3588 * We need to update shares after updating tg->load_weight in
3589 * order to adjust the weight of groups with long running tasks.
3590 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003591 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003592
3593 raw_spin_unlock_irqrestore(&rq->lock, flags);
3594
3595 return 0;
3596}
3597
3598static void update_shares(int cpu)
3599{
3600 struct cfs_rq *cfs_rq;
3601 struct rq *rq = cpu_rq(cpu);
3602
3603 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003604 /*
3605 * Iterates the task_group tree in a bottom up fashion, see
3606 * list_add_leaf_cfs_rq() for details.
3607 */
Paul Turner64660c82011-07-21 09:43:36 -07003608 for_each_leaf_cfs_rq(rq, cfs_rq) {
3609 /* throttled entities do not contribute to load */
3610 if (throttled_hierarchy(cfs_rq))
3611 continue;
3612
Paul Turner67e86252010-11-15 15:47:05 -08003613 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003614 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003615 rcu_read_unlock();
3616}
3617
Peter Zijlstra9763b672011-07-13 13:09:25 +02003618/*
3619 * Compute the cpu's hierarchical load factor for each task group.
3620 * This needs to be done in a top-down fashion because the load of a child
3621 * group is a fraction of its parents load.
3622 */
3623static int tg_load_down(struct task_group *tg, void *data)
3624{
3625 unsigned long load;
3626 long cpu = (long)data;
3627
3628 if (!tg->parent) {
3629 load = cpu_rq(cpu)->load.weight;
3630 } else {
3631 load = tg->parent->cfs_rq[cpu]->h_load;
3632 load *= tg->se[cpu]->load.weight;
3633 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3634 }
3635
3636 tg->cfs_rq[cpu]->h_load = load;
3637
3638 return 0;
3639}
3640
3641static void update_h_load(long cpu)
3642{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003643 struct rq *rq = cpu_rq(cpu);
3644 unsigned long now = jiffies;
3645
3646 if (rq->h_load_throttle == now)
3647 return;
3648
3649 rq->h_load_throttle = now;
3650
Peter Zijlstra367456c2012-02-20 21:49:09 +01003651 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003652 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003653 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003654}
3655
Peter Zijlstra367456c2012-02-20 21:49:09 +01003656static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003657{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003658 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3659 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003660
Peter Zijlstra367456c2012-02-20 21:49:09 +01003661 load = p->se.load.weight;
3662 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003663
Peter Zijlstra367456c2012-02-20 21:49:09 +01003664 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003665}
3666#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003667static inline void update_shares(int cpu)
3668{
3669}
3670
Peter Zijlstra367456c2012-02-20 21:49:09 +01003671static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003672{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003673}
3674
3675static unsigned long task_h_load(struct task_struct *p)
3676{
3677 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003678}
3679#endif
3680
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003681/********** Helpers for find_busiest_group ************************/
3682/*
3683 * sd_lb_stats - Structure to store the statistics of a sched_domain
3684 * during load balancing.
3685 */
3686struct sd_lb_stats {
3687 struct sched_group *busiest; /* Busiest group in this sd */
3688 struct sched_group *this; /* Local group in this sd */
3689 unsigned long total_load; /* Total load of all groups in sd */
3690 unsigned long total_pwr; /* Total power of all groups in sd */
3691 unsigned long avg_load; /* Average load across all groups in sd */
3692
3693 /** Statistics of this group */
3694 unsigned long this_load;
3695 unsigned long this_load_per_task;
3696 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003697 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003698 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003699
3700 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003701 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003702 unsigned long max_load;
3703 unsigned long busiest_load_per_task;
3704 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003705 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003706 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003707 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003708
3709 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003710};
3711
3712/*
3713 * sg_lb_stats - stats of a sched_group required for load_balancing
3714 */
3715struct sg_lb_stats {
3716 unsigned long avg_load; /*Avg load across the CPUs of the group */
3717 unsigned long group_load; /* Total load over the CPUs of the group */
3718 unsigned long sum_nr_running; /* Nr tasks running in the group */
3719 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3720 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003721 unsigned long idle_cpus;
3722 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003723 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003724 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003725};
3726
3727/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003728 * get_sd_load_idx - Obtain the load index for a given sched domain.
3729 * @sd: The sched_domain whose load_idx is to be obtained.
3730 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3731 */
3732static inline int get_sd_load_idx(struct sched_domain *sd,
3733 enum cpu_idle_type idle)
3734{
3735 int load_idx;
3736
3737 switch (idle) {
3738 case CPU_NOT_IDLE:
3739 load_idx = sd->busy_idx;
3740 break;
3741
3742 case CPU_NEWLY_IDLE:
3743 load_idx = sd->newidle_idx;
3744 break;
3745 default:
3746 load_idx = sd->idle_idx;
3747 break;
3748 }
3749
3750 return load_idx;
3751}
3752
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003753unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3754{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003755 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003756}
3757
3758unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3759{
3760 return default_scale_freq_power(sd, cpu);
3761}
3762
3763unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3764{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003765 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003766 unsigned long smt_gain = sd->smt_gain;
3767
3768 smt_gain /= weight;
3769
3770 return smt_gain;
3771}
3772
3773unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3774{
3775 return default_scale_smt_power(sd, cpu);
3776}
3777
3778unsigned long scale_rt_power(int cpu)
3779{
3780 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003781 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003782
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003783 /*
3784 * Since we're reading these variables without serialization make sure
3785 * we read them once before doing sanity checks on them.
3786 */
3787 age_stamp = ACCESS_ONCE(rq->age_stamp);
3788 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003789
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003790 total = sched_avg_period() + (rq->clock - age_stamp);
3791
3792 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003793 /* Ensures that power won't end up being negative */
3794 available = 0;
3795 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003796 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003797 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003798
Nikhil Rao1399fa72011-05-18 10:09:39 -07003799 if (unlikely((s64)total < SCHED_POWER_SCALE))
3800 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003801
Nikhil Rao1399fa72011-05-18 10:09:39 -07003802 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003803
3804 return div_u64(available, total);
3805}
3806
3807static void update_cpu_power(struct sched_domain *sd, int cpu)
3808{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003809 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003810 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003811 struct sched_group *sdg = sd->groups;
3812
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003813 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3814 if (sched_feat(ARCH_POWER))
3815 power *= arch_scale_smt_power(sd, cpu);
3816 else
3817 power *= default_scale_smt_power(sd, cpu);
3818
Nikhil Rao1399fa72011-05-18 10:09:39 -07003819 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003820 }
3821
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003822 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003823
3824 if (sched_feat(ARCH_POWER))
3825 power *= arch_scale_freq_power(sd, cpu);
3826 else
3827 power *= default_scale_freq_power(sd, cpu);
3828
Nikhil Rao1399fa72011-05-18 10:09:39 -07003829 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003830
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003831 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003832 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003833
3834 if (!power)
3835 power = 1;
3836
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003837 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003838 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003839}
3840
Peter Zijlstra029632f2011-10-25 10:00:11 +02003841void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003842{
3843 struct sched_domain *child = sd->child;
3844 struct sched_group *group, *sdg = sd->groups;
3845 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003846 unsigned long interval;
3847
3848 interval = msecs_to_jiffies(sd->balance_interval);
3849 interval = clamp(interval, 1UL, max_load_balance_interval);
3850 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003851
3852 if (!child) {
3853 update_cpu_power(sd, cpu);
3854 return;
3855 }
3856
3857 power = 0;
3858
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003859 if (child->flags & SD_OVERLAP) {
3860 /*
3861 * SD_OVERLAP domains cannot assume that child groups
3862 * span the current group.
3863 */
3864
3865 for_each_cpu(cpu, sched_group_cpus(sdg))
3866 power += power_of(cpu);
3867 } else {
3868 /*
3869 * !SD_OVERLAP domains can assume that child groups
3870 * span the current group.
3871 */
3872
3873 group = child->groups;
3874 do {
3875 power += group->sgp->power;
3876 group = group->next;
3877 } while (group != child->groups);
3878 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003879
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003880 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003881}
3882
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003883/*
3884 * Try and fix up capacity for tiny siblings, this is needed when
3885 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3886 * which on its own isn't powerful enough.
3887 *
3888 * See update_sd_pick_busiest() and check_asym_packing().
3889 */
3890static inline int
3891fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3892{
3893 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003894 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003895 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003896 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003897 return 0;
3898
3899 /*
3900 * If ~90% of the cpu_power is still there, we're good.
3901 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003902 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003903 return 1;
3904
3905 return 0;
3906}
3907
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003908/**
3909 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003910 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003911 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003912 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003913 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003914 * @balance: Should we balance.
3915 * @sgs: variable to hold the statistics for this group.
3916 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003917static inline void update_sg_lb_stats(struct lb_env *env,
3918 struct sched_group *group, int load_idx,
Michael Wangb9403132012-07-12 16:10:13 +08003919 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003920{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003921 unsigned long nr_running, max_nr_running, min_nr_running;
3922 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003923 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003924 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003925 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003926
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003927 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02003928 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003929
3930 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003931 max_cpu_load = 0;
3932 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003933 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003934 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003935
Michael Wangb9403132012-07-12 16:10:13 +08003936 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003937 struct rq *rq = cpu_rq(i);
3938
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003939 nr_running = rq->nr_running;
3940
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003941 /* Bias balancing toward cpus of our domain */
3942 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02003943 if (idle_cpu(i) && !first_idle_cpu &&
3944 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003945 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003946 balance_cpu = i;
3947 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003948
3949 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003950 } else {
3951 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003952 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003953 max_cpu_load = load;
3954 if (min_cpu_load > load)
3955 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003956
3957 if (nr_running > max_nr_running)
3958 max_nr_running = nr_running;
3959 if (min_nr_running > nr_running)
3960 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003961 }
3962
3963 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003964 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003965 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003966 if (idle_cpu(i))
3967 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003968 }
3969
3970 /*
3971 * First idle cpu or the first cpu(busiest) in this sched group
3972 * is eligible for doing load balancing at this and above
3973 * domains. In the newly idle case, we will allow all the cpu's
3974 * to do the newly idle load balance.
3975 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003976 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003977 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003978 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01003979 *balance = 0;
3980 return;
3981 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003982 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01003983 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003984 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003985 }
3986
3987 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003988 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003989
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003990 /*
3991 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003992 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003993 *
3994 * APZ: with cgroup the avg task weight can vary wildly and
3995 * might not be a suitable number - should we keep a
3996 * normalized nr_running number somewhere that negates
3997 * the hierarchy?
3998 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003999 if (sgs->sum_nr_running)
4000 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004001
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004002 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4003 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004004 sgs->group_imb = 1;
4005
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004006 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004007 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004008 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004009 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004010 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004011
4012 if (sgs->group_capacity > sgs->sum_nr_running)
4013 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004014}
4015
4016/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004017 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004018 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004019 * @sds: sched_domain statistics
4020 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004021 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004022 *
4023 * Determine if @sg is a busier group than the previously selected
4024 * busiest group.
4025 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004026static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004027 struct sd_lb_stats *sds,
4028 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004029 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004030{
4031 if (sgs->avg_load <= sds->max_load)
4032 return false;
4033
4034 if (sgs->sum_nr_running > sgs->group_capacity)
4035 return true;
4036
4037 if (sgs->group_imb)
4038 return true;
4039
4040 /*
4041 * ASYM_PACKING needs to move all the work to the lowest
4042 * numbered CPUs in the group, therefore mark all groups
4043 * higher than ourself as busy.
4044 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004045 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4046 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004047 if (!sds->busiest)
4048 return true;
4049
4050 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4051 return true;
4052 }
4053
4054 return false;
4055}
4056
4057/**
Hui Kang461819a2011-10-11 23:00:59 -04004058 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004059 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004060 * @balance: Should we balance.
4061 * @sds: variable to hold the statistics for this sched_domain.
4062 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004063static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004064 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004065{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004066 struct sched_domain *child = env->sd->child;
4067 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004068 struct sg_lb_stats sgs;
4069 int load_idx, prefer_sibling = 0;
4070
4071 if (child && child->flags & SD_PREFER_SIBLING)
4072 prefer_sibling = 1;
4073
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004074 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004075
4076 do {
4077 int local_group;
4078
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004079 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004080 memset(&sgs, 0, sizeof(sgs));
Michael Wangb9403132012-07-12 16:10:13 +08004081 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004082
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004083 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004084 return;
4085
4086 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004087 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004088
4089 /*
4090 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004091 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004092 * and move all the excess tasks away. We lower the capacity
4093 * of a group only if the local group has the capacity to fit
4094 * these excess tasks, i.e. nr_running < group_capacity. The
4095 * extra check prevents the case where you always pull from the
4096 * heaviest group when it is already under-utilized (possible
4097 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004099 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004100 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4101
4102 if (local_group) {
4103 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004104 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004105 sds->this_nr_running = sgs.sum_nr_running;
4106 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004107 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004108 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004109 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004110 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004111 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004112 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004113 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004114 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004115 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004116 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004117 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004118 sds->group_imb = sgs.group_imb;
4119 }
4120
Michael Neuling532cb4c2010-06-08 14:57:02 +10004121 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004122 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004123}
4124
Michael Neuling532cb4c2010-06-08 14:57:02 +10004125/**
4126 * check_asym_packing - Check to see if the group is packed into the
4127 * sched doman.
4128 *
4129 * This is primarily intended to used at the sibling level. Some
4130 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4131 * case of POWER7, it can move to lower SMT modes only when higher
4132 * threads are idle. When in lower SMT modes, the threads will
4133 * perform better since they share less core resources. Hence when we
4134 * have idle threads, we want them to be the higher ones.
4135 *
4136 * This packing function is run on idle threads. It checks to see if
4137 * the busiest CPU in this domain (core in the P7 case) has a higher
4138 * CPU number than the packing function is being run on. Here we are
4139 * assuming lower CPU number will be equivalent to lower a SMT thread
4140 * number.
4141 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004142 * Returns 1 when packing is required and a task should be moved to
4143 * this CPU. The amount of the imbalance is returned in *imbalance.
4144 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004145 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004146 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004147 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004148static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004149{
4150 int busiest_cpu;
4151
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004152 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004153 return 0;
4154
4155 if (!sds->busiest)
4156 return 0;
4157
4158 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004159 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004160 return 0;
4161
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004162 env->imbalance = DIV_ROUND_CLOSEST(
4163 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4164
Michael Neuling532cb4c2010-06-08 14:57:02 +10004165 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004166}
4167
4168/**
4169 * fix_small_imbalance - Calculate the minor imbalance that exists
4170 * amongst the groups of a sched_domain, during
4171 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004172 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004173 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004174 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004175static inline
4176void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004177{
4178 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4179 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004180 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004181
4182 if (sds->this_nr_running) {
4183 sds->this_load_per_task /= sds->this_nr_running;
4184 if (sds->busiest_load_per_task >
4185 sds->this_load_per_task)
4186 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004187 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004188 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004189 cpu_avg_load_per_task(env->dst_cpu);
4190 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004191
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004192 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004193 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004194 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004195
4196 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4197 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004198 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004199 return;
4200 }
4201
4202 /*
4203 * OK, we don't have enough imbalance to justify moving tasks,
4204 * however we may be able to increase total CPU power used by
4205 * moving them.
4206 */
4207
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004208 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004209 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004210 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004211 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004212 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004213
4214 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004215 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004216 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004217 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004218 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004219 min(sds->busiest_load_per_task, sds->max_load - tmp);
4220
4221 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004222 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004223 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004224 tmp = (sds->max_load * sds->busiest->sgp->power) /
4225 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004226 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004227 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004228 sds->this->sgp->power;
4229 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004230 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004231 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004232
4233 /* Move if we gain throughput */
4234 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004235 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004236}
4237
4238/**
4239 * calculate_imbalance - Calculate the amount of imbalance present within the
4240 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004241 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004242 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004243 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004244static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004245{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004246 unsigned long max_pull, load_above_capacity = ~0UL;
4247
4248 sds->busiest_load_per_task /= sds->busiest_nr_running;
4249 if (sds->group_imb) {
4250 sds->busiest_load_per_task =
4251 min(sds->busiest_load_per_task, sds->avg_load);
4252 }
4253
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004254 /*
4255 * In the presence of smp nice balancing, certain scenarios can have
4256 * max load less than avg load(as we skip the groups at or below
4257 * its cpu_power, while calculating max_load..)
4258 */
4259 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004260 env->imbalance = 0;
4261 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004262 }
4263
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004264 if (!sds->group_imb) {
4265 /*
4266 * Don't want to pull so many tasks that a group would go idle.
4267 */
4268 load_above_capacity = (sds->busiest_nr_running -
4269 sds->busiest_group_capacity);
4270
Nikhil Rao1399fa72011-05-18 10:09:39 -07004271 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004272
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004273 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004274 }
4275
4276 /*
4277 * We're trying to get all the cpus to the average_load, so we don't
4278 * want to push ourselves above the average load, nor do we wish to
4279 * reduce the max loaded cpu below the average load. At the same time,
4280 * we also don't want to reduce the group load below the group capacity
4281 * (so that we can implement power-savings policies etc). Thus we look
4282 * for the minimum possible imbalance.
4283 * Be careful of negative numbers as they'll appear as very large values
4284 * with unsigned longs.
4285 */
4286 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004287
4288 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004289 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004290 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004291 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004292
4293 /*
4294 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004295 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004296 * a think about bumping its value to force at least one task to be
4297 * moved
4298 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004299 if (env->imbalance < sds->busiest_load_per_task)
4300 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004301
4302}
Nikhil Raofab47622010-10-15 13:12:29 -07004303
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004304/******* find_busiest_group() helpers end here *********************/
4305
4306/**
4307 * find_busiest_group - Returns the busiest group within the sched_domain
4308 * if there is an imbalance. If there isn't an imbalance, and
4309 * the user has opted for power-savings, it returns a group whose
4310 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4311 * such a group exists.
4312 *
4313 * Also calculates the amount of weighted load which should be moved
4314 * to restore balance.
4315 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004316 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004317 * @balance: Pointer to a variable indicating if this_cpu
4318 * is the appropriate cpu to perform load balancing at this_level.
4319 *
4320 * Returns: - the busiest group if imbalance exists.
4321 * - If no imbalance and user has opted for power-savings balance,
4322 * return the least loaded group whose CPUs can be
4323 * put to idle by rebalancing its tasks onto our group.
4324 */
4325static struct sched_group *
Michael Wangb9403132012-07-12 16:10:13 +08004326find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004327{
4328 struct sd_lb_stats sds;
4329
4330 memset(&sds, 0, sizeof(sds));
4331
4332 /*
4333 * Compute the various statistics relavent for load balancing at
4334 * this level.
4335 */
Michael Wangb9403132012-07-12 16:10:13 +08004336 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004337
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004338 /*
4339 * this_cpu is not the appropriate cpu to perform load balancing at
4340 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004341 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004342 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004343 goto ret;
4344
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004345 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4346 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004347 return sds.busiest;
4348
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004349 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004350 if (!sds.busiest || sds.busiest_nr_running == 0)
4351 goto out_balanced;
4352
Nikhil Rao1399fa72011-05-18 10:09:39 -07004353 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004354
Peter Zijlstra866ab432011-02-21 18:56:47 +01004355 /*
4356 * If the busiest group is imbalanced the below checks don't
4357 * work because they assumes all things are equal, which typically
4358 * isn't true due to cpus_allowed constraints and the like.
4359 */
4360 if (sds.group_imb)
4361 goto force_balance;
4362
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004363 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004364 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004365 !sds.busiest_has_capacity)
4366 goto force_balance;
4367
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004368 /*
4369 * If the local group is more busy than the selected busiest group
4370 * don't try and pull any tasks.
4371 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004372 if (sds.this_load >= sds.max_load)
4373 goto out_balanced;
4374
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004375 /*
4376 * Don't pull any tasks if this group is already above the domain
4377 * average load.
4378 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004379 if (sds.this_load >= sds.avg_load)
4380 goto out_balanced;
4381
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004382 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004383 /*
4384 * This cpu is idle. If the busiest group load doesn't
4385 * have more tasks than the number of available cpu's and
4386 * there is no imbalance between this and busiest group
4387 * wrt to idle cpu's, it is balanced.
4388 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004389 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004390 sds.busiest_nr_running <= sds.busiest_group_weight)
4391 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004392 } else {
4393 /*
4394 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4395 * imbalance_pct to be conservative.
4396 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004397 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004398 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004399 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004400
Nikhil Raofab47622010-10-15 13:12:29 -07004401force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004402 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004403 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004404 return sds.busiest;
4405
4406out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004407ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004408 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004409 return NULL;
4410}
4411
4412/*
4413 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4414 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004415static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004416 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004417{
4418 struct rq *busiest = NULL, *rq;
4419 unsigned long max_load = 0;
4420 int i;
4421
4422 for_each_cpu(i, sched_group_cpus(group)) {
4423 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004424 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4425 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004426 unsigned long wl;
4427
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004428 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004429 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004430
Michael Wangb9403132012-07-12 16:10:13 +08004431 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004432 continue;
4433
4434 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004435 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004436
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004437 /*
4438 * When comparing with imbalance, use weighted_cpuload()
4439 * which is not scaled with the cpu power.
4440 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004441 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004442 continue;
4443
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004444 /*
4445 * For the load comparisons with the other cpu's, consider
4446 * the weighted_cpuload() scaled with the cpu power, so that
4447 * the load can be moved away from the cpu that is potentially
4448 * running at a lower capacity.
4449 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004450 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004451
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004452 if (wl > max_load) {
4453 max_load = wl;
4454 busiest = rq;
4455 }
4456 }
4457
4458 return busiest;
4459}
4460
4461/*
4462 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4463 * so long as it is large enough.
4464 */
4465#define MAX_PINNED_INTERVAL 512
4466
4467/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004468DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004469
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004470static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004471{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004472 struct sched_domain *sd = env->sd;
4473
4474 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004475
4476 /*
4477 * ASYM_PACKING needs to force migrate tasks from busy but
4478 * higher numbered CPUs in order to pack all tasks in the
4479 * lowest numbered CPUs.
4480 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004481 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004482 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004483 }
4484
4485 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4486}
4487
Tejun Heo969c7922010-05-06 18:49:21 +02004488static int active_load_balance_cpu_stop(void *data);
4489
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004490/*
4491 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4492 * tasks if there is an imbalance.
4493 */
4494static int load_balance(int this_cpu, struct rq *this_rq,
4495 struct sched_domain *sd, enum cpu_idle_type idle,
4496 int *balance)
4497{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304498 int ld_moved, cur_ld_moved, active_balance = 0;
4499 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004500 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004501 struct rq *busiest;
4502 unsigned long flags;
4503 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4504
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004505 struct lb_env env = {
4506 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004507 .dst_cpu = this_cpu,
4508 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304509 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004510 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004511 .loop_break = sched_nr_migrate_break,
Michael Wangb9403132012-07-12 16:10:13 +08004512 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004513 };
4514
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004515 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304516 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004517
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004518 schedstat_inc(sd, lb_count[idle]);
4519
4520redo:
Michael Wangb9403132012-07-12 16:10:13 +08004521 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004522
4523 if (*balance == 0)
4524 goto out_balanced;
4525
4526 if (!group) {
4527 schedstat_inc(sd, lb_nobusyg[idle]);
4528 goto out_balanced;
4529 }
4530
Michael Wangb9403132012-07-12 16:10:13 +08004531 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004532 if (!busiest) {
4533 schedstat_inc(sd, lb_nobusyq[idle]);
4534 goto out_balanced;
4535 }
4536
Michael Wang78feefc2012-08-06 16:41:59 +08004537 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004538
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004539 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004540
4541 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304542 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004543 if (busiest->nr_running > 1) {
4544 /*
4545 * Attempt to move tasks. If find_busiest_group has found
4546 * an imbalance but busiest->nr_running <= 1, the group is
4547 * still unbalanced. ld_moved simply stays zero, so it is
4548 * correctly treated as an imbalance.
4549 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004550 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004551 env.src_cpu = busiest->cpu;
4552 env.src_rq = busiest;
4553 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004554
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004555 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004556more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004557 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004558 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304559
4560 /*
4561 * cur_ld_moved - load moved in current iteration
4562 * ld_moved - cumulative load moved across iterations
4563 */
4564 cur_ld_moved = move_tasks(&env);
4565 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004566 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004567 local_irq_restore(flags);
4568
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004569 if (env.flags & LBF_NEED_BREAK) {
4570 env.flags &= ~LBF_NEED_BREAK;
4571 goto more_balance;
4572 }
4573
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004574 /*
4575 * some other cpu did the load balance for us.
4576 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304577 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4578 resched_cpu(env.dst_cpu);
4579
4580 /*
4581 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4582 * us and move them to an alternate dst_cpu in our sched_group
4583 * where they can run. The upper limit on how many times we
4584 * iterate on same src_cpu is dependent on number of cpus in our
4585 * sched_group.
4586 *
4587 * This changes load balance semantics a bit on who can move
4588 * load to a given_cpu. In addition to the given_cpu itself
4589 * (or a ilb_cpu acting on its behalf where given_cpu is
4590 * nohz-idle), we now have balance_cpu in a position to move
4591 * load to given_cpu. In rare situations, this may cause
4592 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4593 * _independently_ and at _same_ time to move some load to
4594 * given_cpu) causing exceess load to be moved to given_cpu.
4595 * This however should not happen so much in practice and
4596 * moreover subsequent load balance cycles should correct the
4597 * excess load moved.
4598 */
4599 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4600 lb_iterations++ < max_lb_iterations) {
4601
Michael Wang78feefc2012-08-06 16:41:59 +08004602 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304603 env.dst_cpu = env.new_dst_cpu;
4604 env.flags &= ~LBF_SOME_PINNED;
4605 env.loop = 0;
4606 env.loop_break = sched_nr_migrate_break;
4607 /*
4608 * Go back to "more_balance" rather than "redo" since we
4609 * need to continue with same src_cpu.
4610 */
4611 goto more_balance;
4612 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004613
4614 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004615 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004616 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304617 if (!cpumask_empty(cpus)) {
4618 env.loop = 0;
4619 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004620 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304621 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004622 goto out_balanced;
4623 }
4624 }
4625
4626 if (!ld_moved) {
4627 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004628 /*
4629 * Increment the failure counter only on periodic balance.
4630 * We do not want newidle balance, which can be very
4631 * frequent, pollute the failure counter causing
4632 * excessive cache_hot migrations and active balances.
4633 */
4634 if (idle != CPU_NEWLY_IDLE)
4635 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004636
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004637 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004638 raw_spin_lock_irqsave(&busiest->lock, flags);
4639
Tejun Heo969c7922010-05-06 18:49:21 +02004640 /* don't kick the active_load_balance_cpu_stop,
4641 * if the curr task on busiest cpu can't be
4642 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004643 */
4644 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004645 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004646 raw_spin_unlock_irqrestore(&busiest->lock,
4647 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004648 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004649 goto out_one_pinned;
4650 }
4651
Tejun Heo969c7922010-05-06 18:49:21 +02004652 /*
4653 * ->active_balance synchronizes accesses to
4654 * ->active_balance_work. Once set, it's cleared
4655 * only after active load balance is finished.
4656 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004657 if (!busiest->active_balance) {
4658 busiest->active_balance = 1;
4659 busiest->push_cpu = this_cpu;
4660 active_balance = 1;
4661 }
4662 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004663
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004664 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004665 stop_one_cpu_nowait(cpu_of(busiest),
4666 active_load_balance_cpu_stop, busiest,
4667 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004668 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004669
4670 /*
4671 * We've kicked active balancing, reset the failure
4672 * counter.
4673 */
4674 sd->nr_balance_failed = sd->cache_nice_tries+1;
4675 }
4676 } else
4677 sd->nr_balance_failed = 0;
4678
4679 if (likely(!active_balance)) {
4680 /* We were unbalanced, so reset the balancing interval */
4681 sd->balance_interval = sd->min_interval;
4682 } else {
4683 /*
4684 * If we've begun active balancing, start to back off. This
4685 * case may not be covered by the all_pinned logic if there
4686 * is only 1 task on the busy runqueue (because we don't call
4687 * move_tasks).
4688 */
4689 if (sd->balance_interval < sd->max_interval)
4690 sd->balance_interval *= 2;
4691 }
4692
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004693 goto out;
4694
4695out_balanced:
4696 schedstat_inc(sd, lb_balanced[idle]);
4697
4698 sd->nr_balance_failed = 0;
4699
4700out_one_pinned:
4701 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004702 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004703 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004704 (sd->balance_interval < sd->max_interval))
4705 sd->balance_interval *= 2;
4706
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004707 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004708out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004709 return ld_moved;
4710}
4711
4712/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004713 * idle_balance is called by schedule() if this_cpu is about to become
4714 * idle. Attempts to pull tasks from other CPUs.
4715 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004716void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004717{
4718 struct sched_domain *sd;
4719 int pulled_task = 0;
4720 unsigned long next_balance = jiffies + HZ;
4721
4722 this_rq->idle_stamp = this_rq->clock;
4723
4724 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4725 return;
4726
Ben Segall18bf2802012-10-04 12:51:20 +02004727 update_rq_runnable_avg(this_rq, 1);
4728
Peter Zijlstraf492e122009-12-23 15:29:42 +01004729 /*
4730 * Drop the rq->lock, but keep IRQ/preempt disabled.
4731 */
4732 raw_spin_unlock(&this_rq->lock);
4733
Paul Turnerc66eaf62010-11-15 15:47:07 -08004734 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004735 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004736 for_each_domain(this_cpu, sd) {
4737 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004738 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004739
4740 if (!(sd->flags & SD_LOAD_BALANCE))
4741 continue;
4742
Peter Zijlstraf492e122009-12-23 15:29:42 +01004743 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004744 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004745 pulled_task = load_balance(this_cpu, this_rq,
4746 sd, CPU_NEWLY_IDLE, &balance);
4747 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004748
4749 interval = msecs_to_jiffies(sd->balance_interval);
4750 if (time_after(next_balance, sd->last_balance + interval))
4751 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004752 if (pulled_task) {
4753 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004754 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004755 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004756 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004757 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004758
4759 raw_spin_lock(&this_rq->lock);
4760
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004761 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4762 /*
4763 * We are going idle. next_balance may be set based on
4764 * a busy processor. So reset next_balance.
4765 */
4766 this_rq->next_balance = next_balance;
4767 }
4768}
4769
4770/*
Tejun Heo969c7922010-05-06 18:49:21 +02004771 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4772 * running tasks off the busiest CPU onto idle CPUs. It requires at
4773 * least 1 task to be running on each physical CPU where possible, and
4774 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004775 */
Tejun Heo969c7922010-05-06 18:49:21 +02004776static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004777{
Tejun Heo969c7922010-05-06 18:49:21 +02004778 struct rq *busiest_rq = data;
4779 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004780 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004781 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004782 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004783
4784 raw_spin_lock_irq(&busiest_rq->lock);
4785
4786 /* make sure the requested cpu hasn't gone down in the meantime */
4787 if (unlikely(busiest_cpu != smp_processor_id() ||
4788 !busiest_rq->active_balance))
4789 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004790
4791 /* Is there any task to move? */
4792 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004793 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004794
4795 /*
4796 * This condition is "impossible", if it occurs
4797 * we need to fix it. Originally reported by
4798 * Bjorn Helgaas on a 128-cpu setup.
4799 */
4800 BUG_ON(busiest_rq == target_rq);
4801
4802 /* move a task from busiest_rq to target_rq */
4803 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004804
4805 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004806 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004807 for_each_domain(target_cpu, sd) {
4808 if ((sd->flags & SD_LOAD_BALANCE) &&
4809 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4810 break;
4811 }
4812
4813 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004814 struct lb_env env = {
4815 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004816 .dst_cpu = target_cpu,
4817 .dst_rq = target_rq,
4818 .src_cpu = busiest_rq->cpu,
4819 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004820 .idle = CPU_IDLE,
4821 };
4822
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004823 schedstat_inc(sd, alb_count);
4824
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004825 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004826 schedstat_inc(sd, alb_pushed);
4827 else
4828 schedstat_inc(sd, alb_failed);
4829 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004830 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004831 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004832out_unlock:
4833 busiest_rq->active_balance = 0;
4834 raw_spin_unlock_irq(&busiest_rq->lock);
4835 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004836}
4837
4838#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004839/*
4840 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004841 * - When one of the busy CPUs notice that there may be an idle rebalancing
4842 * needed, they will kick the idle load balancer, which then does idle
4843 * load balancing for all the idle CPUs.
4844 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004845static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004846 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004847 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004848 unsigned long next_balance; /* in jiffy units */
4849} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004850
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004851static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004852{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004853 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004854
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004855 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4856 return ilb;
4857
4858 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004859}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004860
4861/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004862 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4863 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4864 * CPU (if there is one).
4865 */
4866static void nohz_balancer_kick(int cpu)
4867{
4868 int ilb_cpu;
4869
4870 nohz.next_balance++;
4871
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004872 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004873
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004874 if (ilb_cpu >= nr_cpu_ids)
4875 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004876
Suresh Siddhacd490c52011-12-06 11:26:34 -08004877 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004878 return;
4879 /*
4880 * Use smp_send_reschedule() instead of resched_cpu().
4881 * This way we generate a sched IPI on the target cpu which
4882 * is idle. And the softirq performing nohz idle load balance
4883 * will be run before returning from the IPI.
4884 */
4885 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004886 return;
4887}
4888
Alex Shic1cc0172012-09-10 15:10:58 +08004889static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004890{
4891 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4892 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4893 atomic_dec(&nohz.nr_cpus);
4894 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4895 }
4896}
4897
Suresh Siddha69e1e812011-12-01 17:07:33 -08004898static inline void set_cpu_sd_state_busy(void)
4899{
4900 struct sched_domain *sd;
4901 int cpu = smp_processor_id();
4902
4903 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4904 return;
4905 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4906
4907 rcu_read_lock();
4908 for_each_domain(cpu, sd)
4909 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4910 rcu_read_unlock();
4911}
4912
4913void set_cpu_sd_state_idle(void)
4914{
4915 struct sched_domain *sd;
4916 int cpu = smp_processor_id();
4917
4918 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4919 return;
4920 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4921
4922 rcu_read_lock();
4923 for_each_domain(cpu, sd)
4924 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4925 rcu_read_unlock();
4926}
4927
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004928/*
Alex Shic1cc0172012-09-10 15:10:58 +08004929 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004930 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004931 */
Alex Shic1cc0172012-09-10 15:10:58 +08004932void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004933{
Suresh Siddha71325962012-01-19 18:28:57 -08004934 /*
4935 * If this cpu is going down, then nothing needs to be done.
4936 */
4937 if (!cpu_active(cpu))
4938 return;
4939
Alex Shic1cc0172012-09-10 15:10:58 +08004940 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
4941 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004942
Alex Shic1cc0172012-09-10 15:10:58 +08004943 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
4944 atomic_inc(&nohz.nr_cpus);
4945 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004946}
Suresh Siddha71325962012-01-19 18:28:57 -08004947
4948static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4949 unsigned long action, void *hcpu)
4950{
4951 switch (action & ~CPU_TASKS_FROZEN) {
4952 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08004953 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08004954 return NOTIFY_OK;
4955 default:
4956 return NOTIFY_DONE;
4957 }
4958}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004959#endif
4960
4961static DEFINE_SPINLOCK(balancing);
4962
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004963/*
4964 * Scale the max load_balance interval with the number of CPUs in the system.
4965 * This trades load-balance latency on larger machines for less cross talk.
4966 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004967void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004968{
4969 max_load_balance_interval = HZ*num_online_cpus()/10;
4970}
4971
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004972/*
4973 * It checks each scheduling domain to see if it is due to be balanced,
4974 * and initiates a balancing operation if so.
4975 *
4976 * Balancing parameters are set up in arch_init_sched_domains.
4977 */
4978static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4979{
4980 int balance = 1;
4981 struct rq *rq = cpu_rq(cpu);
4982 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004983 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004984 /* Earliest time when we have to do rebalance again */
4985 unsigned long next_balance = jiffies + 60*HZ;
4986 int update_next_balance = 0;
4987 int need_serialize;
4988
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004989 update_shares(cpu);
4990
Peter Zijlstradce840a2011-04-07 14:09:50 +02004991 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004992 for_each_domain(cpu, sd) {
4993 if (!(sd->flags & SD_LOAD_BALANCE))
4994 continue;
4995
4996 interval = sd->balance_interval;
4997 if (idle != CPU_IDLE)
4998 interval *= sd->busy_factor;
4999
5000 /* scale ms to jiffies */
5001 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005002 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005003
5004 need_serialize = sd->flags & SD_SERIALIZE;
5005
5006 if (need_serialize) {
5007 if (!spin_trylock(&balancing))
5008 goto out;
5009 }
5010
5011 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5012 if (load_balance(cpu, rq, sd, idle, &balance)) {
5013 /*
5014 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005015 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005016 */
5017 idle = CPU_NOT_IDLE;
5018 }
5019 sd->last_balance = jiffies;
5020 }
5021 if (need_serialize)
5022 spin_unlock(&balancing);
5023out:
5024 if (time_after(next_balance, sd->last_balance + interval)) {
5025 next_balance = sd->last_balance + interval;
5026 update_next_balance = 1;
5027 }
5028
5029 /*
5030 * Stop the load balance at this level. There is another
5031 * CPU in our sched group which is doing load balancing more
5032 * actively.
5033 */
5034 if (!balance)
5035 break;
5036 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005037 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005038
5039 /*
5040 * next_balance will be updated only when there is a need.
5041 * When the cpu is attached to null domain for ex, it will not be
5042 * updated.
5043 */
5044 if (likely(update_next_balance))
5045 rq->next_balance = next_balance;
5046}
5047
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005048#ifdef CONFIG_NO_HZ
5049/*
5050 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5051 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5052 */
5053static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5054{
5055 struct rq *this_rq = cpu_rq(this_cpu);
5056 struct rq *rq;
5057 int balance_cpu;
5058
Suresh Siddha1c792db2011-12-01 17:07:32 -08005059 if (idle != CPU_IDLE ||
5060 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5061 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005062
5063 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005064 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005065 continue;
5066
5067 /*
5068 * If this cpu gets work to do, stop the load balancing
5069 * work being done for other cpus. Next load
5070 * balancing owner will pick it up.
5071 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005072 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005073 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005074
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005075 rq = cpu_rq(balance_cpu);
5076
5077 raw_spin_lock_irq(&rq->lock);
5078 update_rq_clock(rq);
5079 update_idle_cpu_load(rq);
5080 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005081
5082 rebalance_domains(balance_cpu, CPU_IDLE);
5083
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084 if (time_after(this_rq->next_balance, rq->next_balance))
5085 this_rq->next_balance = rq->next_balance;
5086 }
5087 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005088end:
5089 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005090}
5091
5092/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005093 * Current heuristic for kicking the idle load balancer in the presence
5094 * of an idle cpu is the system.
5095 * - This rq has more than one task.
5096 * - At any scheduler domain level, this cpu's scheduler group has multiple
5097 * busy cpu's exceeding the group's power.
5098 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5099 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005100 */
5101static inline int nohz_kick_needed(struct rq *rq, int cpu)
5102{
5103 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005104 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005105
Suresh Siddha1c792db2011-12-01 17:07:32 -08005106 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005107 return 0;
5108
Suresh Siddha1c792db2011-12-01 17:07:32 -08005109 /*
5110 * We may be recently in ticked or tickless idle mode. At the first
5111 * busy tick after returning from idle, we will update the busy stats.
5112 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005113 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005114 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005115
5116 /*
5117 * None are in tickless mode and hence no need for NOHZ idle load
5118 * balancing.
5119 */
5120 if (likely(!atomic_read(&nohz.nr_cpus)))
5121 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005122
5123 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005124 return 0;
5125
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005126 if (rq->nr_running >= 2)
5127 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005128
Peter Zijlstra067491b2011-12-07 14:32:08 +01005129 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005130 for_each_domain(cpu, sd) {
5131 struct sched_group *sg = sd->groups;
5132 struct sched_group_power *sgp = sg->sgp;
5133 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005134
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005135 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005136 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005137
5138 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5139 && (cpumask_first_and(nohz.idle_cpus_mask,
5140 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005141 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005142
5143 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5144 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005145 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005146 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005147 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005148
5149need_kick_unlock:
5150 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005151need_kick:
5152 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005153}
5154#else
5155static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5156#endif
5157
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005158/*
5159 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005160 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005161 */
5162static void run_rebalance_domains(struct softirq_action *h)
5163{
5164 int this_cpu = smp_processor_id();
5165 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005166 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005167 CPU_IDLE : CPU_NOT_IDLE;
5168
5169 rebalance_domains(this_cpu, idle);
5170
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005171 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005172 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005173 * balancing on behalf of the other idle cpus whose ticks are
5174 * stopped.
5175 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005176 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005177}
5178
5179static inline int on_null_domain(int cpu)
5180{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005181 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005182}
5183
5184/*
5185 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005186 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005187void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005188{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005189 /* Don't need to rebalance while attached to NULL domain */
5190 if (time_after_eq(jiffies, rq->next_balance) &&
5191 likely(!on_null_domain(cpu)))
5192 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005193#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005194 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005195 nohz_balancer_kick(cpu);
5196#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005197}
5198
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005199static void rq_online_fair(struct rq *rq)
5200{
5201 update_sysctl();
5202}
5203
5204static void rq_offline_fair(struct rq *rq)
5205{
5206 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005207
5208 /* Ensure any throttled groups are reachable by pick_next_task */
5209 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005210}
5211
Dhaval Giani55e12e52008-06-24 23:39:43 +05305212#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005213
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005214/*
5215 * scheduler tick hitting a task of our scheduling class:
5216 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005217static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005218{
5219 struct cfs_rq *cfs_rq;
5220 struct sched_entity *se = &curr->se;
5221
5222 for_each_sched_entity(se) {
5223 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005224 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005225 }
Ben Segall18bf2802012-10-04 12:51:20 +02005226
5227 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005228}
5229
5230/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005231 * called on fork with the child task as argument from the parent's context
5232 * - child not yet on the tasklist
5233 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005234 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005235static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005236{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005237 struct cfs_rq *cfs_rq;
5238 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005239 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005240 struct rq *rq = this_rq();
5241 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005242
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005243 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005244
Peter Zijlstra861d0342010-08-19 13:31:43 +02005245 update_rq_clock(rq);
5246
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005247 cfs_rq = task_cfs_rq(current);
5248 curr = cfs_rq->curr;
5249
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005250 if (unlikely(task_cpu(p) != this_cpu)) {
5251 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005252 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005253 rcu_read_unlock();
5254 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005255
Ting Yang7109c442007-08-28 12:53:24 +02005256 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005257
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005258 if (curr)
5259 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005260 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005261
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005262 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005263 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005264 * Upon rescheduling, sched_class::put_prev_task() will place
5265 * 'current' within the tree based on its new key value.
5266 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005267 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305268 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005269 }
5270
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005271 se->vruntime -= cfs_rq->min_vruntime;
5272
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005273 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005274}
5275
Steven Rostedtcb469842008-01-25 21:08:22 +01005276/*
5277 * Priority of the task has changed. Check to see if we preempt
5278 * the current task.
5279 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005280static void
5281prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005282{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005283 if (!p->se.on_rq)
5284 return;
5285
Steven Rostedtcb469842008-01-25 21:08:22 +01005286 /*
5287 * Reschedule if we are currently running on this runqueue and
5288 * our priority decreased, or if we are not currently running on
5289 * this runqueue and our priority is higher than the current's
5290 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005291 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005292 if (p->prio > oldprio)
5293 resched_task(rq->curr);
5294 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005295 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005296}
5297
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005298static void switched_from_fair(struct rq *rq, struct task_struct *p)
5299{
5300 struct sched_entity *se = &p->se;
5301 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5302
5303 /*
5304 * Ensure the task's vruntime is normalized, so that when its
5305 * switched back to the fair class the enqueue_entity(.flags=0) will
5306 * do the right thing.
5307 *
5308 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5309 * have normalized the vruntime, if it was !on_rq, then only when
5310 * the task is sleeping will it still have non-normalized vruntime.
5311 */
5312 if (!se->on_rq && p->state != TASK_RUNNING) {
5313 /*
5314 * Fix up our vruntime so that the current sleep doesn't
5315 * cause 'unlimited' sleep bonus.
5316 */
5317 place_entity(cfs_rq, se, 0);
5318 se->vruntime -= cfs_rq->min_vruntime;
5319 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005320
5321#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5322 /*
5323 * Remove our load from contribution when we leave sched_fair
5324 * and ensure we don't carry in an old decay_count if we
5325 * switch back.
5326 */
5327 if (p->se.avg.decay_count) {
5328 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5329 __synchronize_entity_decay(&p->se);
5330 subtract_blocked_load_contrib(cfs_rq,
5331 p->se.avg.load_avg_contrib);
5332 }
5333#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005334}
5335
Steven Rostedtcb469842008-01-25 21:08:22 +01005336/*
5337 * We switched to the sched_fair class.
5338 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005339static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005340{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005341 if (!p->se.on_rq)
5342 return;
5343
Steven Rostedtcb469842008-01-25 21:08:22 +01005344 /*
5345 * We were most likely switched from sched_rt, so
5346 * kick off the schedule if running, otherwise just see
5347 * if we can still preempt the current task.
5348 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005349 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005350 resched_task(rq->curr);
5351 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005352 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005353}
5354
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005355/* Account for a task changing its policy or group.
5356 *
5357 * This routine is mostly called to set cfs_rq->curr field when a task
5358 * migrates between groups/classes.
5359 */
5360static void set_curr_task_fair(struct rq *rq)
5361{
5362 struct sched_entity *se = &rq->curr->se;
5363
Paul Turnerec12cb72011-07-21 09:43:30 -07005364 for_each_sched_entity(se) {
5365 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5366
5367 set_next_entity(cfs_rq, se);
5368 /* ensure bandwidth has been allocated on our new cfs_rq */
5369 account_cfs_rq_runtime(cfs_rq, 0);
5370 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005371}
5372
Peter Zijlstra029632f2011-10-25 10:00:11 +02005373void init_cfs_rq(struct cfs_rq *cfs_rq)
5374{
5375 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005376 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5377#ifndef CONFIG_64BIT
5378 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5379#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005380#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5381 atomic64_set(&cfs_rq->decay_counter, 1);
5382#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005383}
5384
Peter Zijlstra810b3812008-02-29 15:21:01 -05005385#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005386static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005387{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005388 /*
5389 * If the task was not on the rq at the time of this cgroup movement
5390 * it must have been asleep, sleeping tasks keep their ->vruntime
5391 * absolute on their old rq until wakeup (needed for the fair sleeper
5392 * bonus in place_entity()).
5393 *
5394 * If it was on the rq, we've just 'preempted' it, which does convert
5395 * ->vruntime to a relative base.
5396 *
5397 * Make sure both cases convert their relative position when migrating
5398 * to another cgroup's rq. This does somewhat interfere with the
5399 * fair sleeper stuff for the first placement, but who cares.
5400 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005401 /*
5402 * When !on_rq, vruntime of the task has usually NOT been normalized.
5403 * But there are some cases where it has already been normalized:
5404 *
5405 * - Moving a forked child which is waiting for being woken up by
5406 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005407 * - Moving a task which has been woken up by try_to_wake_up() and
5408 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005409 *
5410 * To prevent boost or penalty in the new cfs_rq caused by delta
5411 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5412 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005413 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005414 on_rq = 1;
5415
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005416 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005417 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5418 set_task_rq(p, task_cpu(p));
5419 if (!on_rq)
5420 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005421}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005422
5423void free_fair_sched_group(struct task_group *tg)
5424{
5425 int i;
5426
5427 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5428
5429 for_each_possible_cpu(i) {
5430 if (tg->cfs_rq)
5431 kfree(tg->cfs_rq[i]);
5432 if (tg->se)
5433 kfree(tg->se[i]);
5434 }
5435
5436 kfree(tg->cfs_rq);
5437 kfree(tg->se);
5438}
5439
5440int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5441{
5442 struct cfs_rq *cfs_rq;
5443 struct sched_entity *se;
5444 int i;
5445
5446 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5447 if (!tg->cfs_rq)
5448 goto err;
5449 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5450 if (!tg->se)
5451 goto err;
5452
5453 tg->shares = NICE_0_LOAD;
5454
5455 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5456
5457 for_each_possible_cpu(i) {
5458 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5459 GFP_KERNEL, cpu_to_node(i));
5460 if (!cfs_rq)
5461 goto err;
5462
5463 se = kzalloc_node(sizeof(struct sched_entity),
5464 GFP_KERNEL, cpu_to_node(i));
5465 if (!se)
5466 goto err_free_rq;
5467
5468 init_cfs_rq(cfs_rq);
5469 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5470 }
5471
5472 return 1;
5473
5474err_free_rq:
5475 kfree(cfs_rq);
5476err:
5477 return 0;
5478}
5479
5480void unregister_fair_sched_group(struct task_group *tg, int cpu)
5481{
5482 struct rq *rq = cpu_rq(cpu);
5483 unsigned long flags;
5484
5485 /*
5486 * Only empty task groups can be destroyed; so we can speculatively
5487 * check on_list without danger of it being re-added.
5488 */
5489 if (!tg->cfs_rq[cpu]->on_list)
5490 return;
5491
5492 raw_spin_lock_irqsave(&rq->lock, flags);
5493 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5494 raw_spin_unlock_irqrestore(&rq->lock, flags);
5495}
5496
5497void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5498 struct sched_entity *se, int cpu,
5499 struct sched_entity *parent)
5500{
5501 struct rq *rq = cpu_rq(cpu);
5502
5503 cfs_rq->tg = tg;
5504 cfs_rq->rq = rq;
5505#ifdef CONFIG_SMP
5506 /* allow initial update_cfs_load() to truncate */
5507 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005508#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005509 init_cfs_rq_runtime(cfs_rq);
5510
5511 tg->cfs_rq[cpu] = cfs_rq;
5512 tg->se[cpu] = se;
5513
5514 /* se could be NULL for root_task_group */
5515 if (!se)
5516 return;
5517
5518 if (!parent)
5519 se->cfs_rq = &rq->cfs;
5520 else
5521 se->cfs_rq = parent->my_q;
5522
5523 se->my_q = cfs_rq;
5524 update_load_set(&se->load, 0);
5525 se->parent = parent;
5526}
5527
5528static DEFINE_MUTEX(shares_mutex);
5529
5530int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5531{
5532 int i;
5533 unsigned long flags;
5534
5535 /*
5536 * We can't change the weight of the root cgroup.
5537 */
5538 if (!tg->se[0])
5539 return -EINVAL;
5540
5541 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5542
5543 mutex_lock(&shares_mutex);
5544 if (tg->shares == shares)
5545 goto done;
5546
5547 tg->shares = shares;
5548 for_each_possible_cpu(i) {
5549 struct rq *rq = cpu_rq(i);
5550 struct sched_entity *se;
5551
5552 se = tg->se[i];
5553 /* Propagate contribution to hierarchy */
5554 raw_spin_lock_irqsave(&rq->lock, flags);
5555 for_each_sched_entity(se)
5556 update_cfs_shares(group_cfs_rq(se));
5557 raw_spin_unlock_irqrestore(&rq->lock, flags);
5558 }
5559
5560done:
5561 mutex_unlock(&shares_mutex);
5562 return 0;
5563}
5564#else /* CONFIG_FAIR_GROUP_SCHED */
5565
5566void free_fair_sched_group(struct task_group *tg) { }
5567
5568int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5569{
5570 return 1;
5571}
5572
5573void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5574
5575#endif /* CONFIG_FAIR_GROUP_SCHED */
5576
Peter Zijlstra810b3812008-02-29 15:21:01 -05005577
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005578static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005579{
5580 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005581 unsigned int rr_interval = 0;
5582
5583 /*
5584 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5585 * idle runqueue:
5586 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005587 if (rq->cfs.load.weight)
5588 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005589
5590 return rr_interval;
5591}
5592
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005593/*
5594 * All the scheduling class methods:
5595 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005596const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005597 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005598 .enqueue_task = enqueue_task_fair,
5599 .dequeue_task = dequeue_task_fair,
5600 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005601 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005602
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005603 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005604
5605 .pick_next_task = pick_next_task_fair,
5606 .put_prev_task = put_prev_task_fair,
5607
Peter Williams681f3e62007-10-24 18:23:51 +02005608#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005609 .select_task_rq = select_task_rq_fair,
5610
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005611 .rq_online = rq_online_fair,
5612 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005613
5614 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005615#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005616
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005617 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005618 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005619 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005620
5621 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005622 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005623 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005624
Peter Williams0d721ce2009-09-21 01:31:53 +00005625 .get_rr_interval = get_rr_interval_fair,
5626
Peter Zijlstra810b3812008-02-29 15:21:01 -05005627#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005628 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005629#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005630};
5631
5632#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005633void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005634{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005635 struct cfs_rq *cfs_rq;
5636
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005637 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005638 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005639 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005640 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005641}
5642#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005643
5644__init void init_sched_fair_class(void)
5645{
5646#ifdef CONFIG_SMP
5647 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5648
5649#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005650 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005651 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005652 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005653#endif
5654#endif /* SMP */
5655
5656}