blob: 756f9f9e85422759e4c391ac1e5404d93ee9a836 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Paul Turneraff3e492012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
263 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200264
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800265static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
266{
267 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800268 /*
269 * Ensure we either appear before our parent (if already
270 * enqueued) or force our parent to appear after us when it is
271 * enqueued. The fact that we always enqueue bottom-up
272 * reduces this to two cases.
273 */
274 if (cfs_rq->tg->parent &&
275 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
276 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800278 } else {
279 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
280 &rq_of(cfs_rq)->leaf_cfs_rq_list);
281 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282
283 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200284 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200285 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800286 }
287}
288
289static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (cfs_rq->on_list) {
292 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
293 cfs_rq->on_list = 0;
294 }
295}
296
Peter Zijlstrab7581492008-04-19 19:45:00 +0200297/* Iterate thr' all leaf cfs_rq's on a runqueue */
298#define for_each_leaf_cfs_rq(rq, cfs_rq) \
299 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
300
301/* Do the two (enqueued) entities belong to the same group ? */
302static inline int
303is_same_group(struct sched_entity *se, struct sched_entity *pse)
304{
305 if (se->cfs_rq == pse->cfs_rq)
306 return 1;
307
308 return 0;
309}
310
311static inline struct sched_entity *parent_entity(struct sched_entity *se)
312{
313 return se->parent;
314}
315
Peter Zijlstra464b7522008-10-24 11:06:15 +0200316/* return depth at which a sched entity is present in the hierarchy */
317static inline int depth_se(struct sched_entity *se)
318{
319 int depth = 0;
320
321 for_each_sched_entity(se)
322 depth++;
323
324 return depth;
325}
326
327static void
328find_matching_se(struct sched_entity **se, struct sched_entity **pse)
329{
330 int se_depth, pse_depth;
331
332 /*
333 * preemption test can be made between sibling entities who are in the
334 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
335 * both tasks until we find their ancestors who are siblings of common
336 * parent.
337 */
338
339 /* First walk up until both entities are at same depth */
340 se_depth = depth_se(*se);
341 pse_depth = depth_se(*pse);
342
343 while (se_depth > pse_depth) {
344 se_depth--;
345 *se = parent_entity(*se);
346 }
347
348 while (pse_depth > se_depth) {
349 pse_depth--;
350 *pse = parent_entity(*pse);
351 }
352
353 while (!is_same_group(*se, *pse)) {
354 *se = parent_entity(*se);
355 *pse = parent_entity(*pse);
356 }
357}
358
Peter Zijlstra8f488942009-07-24 12:25:30 +0200359#else /* !CONFIG_FAIR_GROUP_SCHED */
360
361static inline struct task_struct *task_of(struct sched_entity *se)
362{
363 return container_of(se, struct task_struct, se);
364}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365
366static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
367{
368 return container_of(cfs_rq, struct rq, cfs);
369}
370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200371#define entity_is_task(se) 1
372
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373#define for_each_sched_entity(se) \
374 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200379}
380
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
382{
383 struct task_struct *p = task_of(se);
384 struct rq *rq = task_rq(p);
385
386 return &rq->cfs;
387}
388
389/* runqueue "owned" by this group */
390static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
391{
392 return NULL;
393}
394
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800395static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
396{
397}
398
399static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
400{
401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403#define for_each_leaf_cfs_rq(rq, cfs_rq) \
404 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
405
406static inline int
407is_same_group(struct sched_entity *se, struct sched_entity *pse)
408{
409 return 1;
410}
411
412static inline struct sched_entity *parent_entity(struct sched_entity *se)
413{
414 return NULL;
415}
416
Peter Zijlstra464b7522008-10-24 11:06:15 +0200417static inline void
418find_matching_se(struct sched_entity **se, struct sched_entity **pse)
419{
420}
421
Peter Zijlstrab7581492008-04-19 19:45:00 +0200422#endif /* CONFIG_FAIR_GROUP_SCHED */
423
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700424static __always_inline
425void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427/**************************************************************
428 * Scheduling class tree data structure manipulation methods:
429 */
430
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200431static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200432{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200433 s64 delta = (s64)(vruntime - min_vruntime);
434 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435 min_vruntime = vruntime;
436
437 return min_vruntime;
438}
439
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200440static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200441{
442 s64 delta = (s64)(vruntime - min_vruntime);
443 if (delta < 0)
444 min_vruntime = vruntime;
445
446 return min_vruntime;
447}
448
Fabio Checconi54fdc582009-07-16 12:32:27 +0200449static inline int entity_before(struct sched_entity *a,
450 struct sched_entity *b)
451{
452 return (s64)(a->vruntime - b->vruntime) < 0;
453}
454
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200455static void update_min_vruntime(struct cfs_rq *cfs_rq)
456{
457 u64 vruntime = cfs_rq->min_vruntime;
458
459 if (cfs_rq->curr)
460 vruntime = cfs_rq->curr->vruntime;
461
462 if (cfs_rq->rb_leftmost) {
463 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
464 struct sched_entity,
465 run_node);
466
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100467 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200468 vruntime = se->vruntime;
469 else
470 vruntime = min_vruntime(vruntime, se->vruntime);
471 }
472
473 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200474#ifndef CONFIG_64BIT
475 smp_wmb();
476 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
477#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478}
479
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200480/*
481 * Enqueue an entity into the rb-tree:
482 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200483static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484{
485 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
486 struct rb_node *parent = NULL;
487 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488 int leftmost = 1;
489
490 /*
491 * Find the right place in the rbtree:
492 */
493 while (*link) {
494 parent = *link;
495 entry = rb_entry(parent, struct sched_entity, run_node);
496 /*
497 * We dont care about collisions. Nodes with
498 * the same key stay together.
499 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200500 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200501 link = &parent->rb_left;
502 } else {
503 link = &parent->rb_right;
504 leftmost = 0;
505 }
506 }
507
508 /*
509 * Maintain a cache of leftmost tree entries (it is frequently
510 * used):
511 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200512 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200513 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200514
515 rb_link_node(&se->run_node, parent, link);
516 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517}
518
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200519static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 if (cfs_rq->rb_leftmost == &se->run_node) {
522 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100523
524 next_node = rb_next(&se->run_node);
525 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200527
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200529}
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100533 struct rb_node *left = cfs_rq->rb_leftmost;
534
535 if (!left)
536 return NULL;
537
538 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200539}
540
Rik van Rielac53db52011-02-01 09:51:03 -0500541static struct sched_entity *__pick_next_entity(struct sched_entity *se)
542{
543 struct rb_node *next = rb_next(&se->run_node);
544
545 if (!next)
546 return NULL;
547
548 return rb_entry(next, struct sched_entity, run_node);
549}
550
551#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100554 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555
Balbir Singh70eee742008-02-22 13:25:53 +0530556 if (!last)
557 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100558
559 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200560}
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562/**************************************************************
563 * Scheduling class statistics methods:
564 */
565
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700567 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100568 loff_t *ppos)
569{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100571 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100572
573 if (ret || !write)
574 return ret;
575
576 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
577 sysctl_sched_min_granularity);
578
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#define WRT_SYSCTL(name) \
580 (normalized_sysctl_##name = sysctl_##name / (factor))
581 WRT_SYSCTL(sched_min_granularity);
582 WRT_SYSCTL(sched_latency);
583 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100584#undef WRT_SYSCTL
585
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586 return 0;
587}
588#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200589
590/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200592 */
593static inline unsigned long
594calc_delta_fair(unsigned long delta, struct sched_entity *se)
595{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200596 if (unlikely(se->load.weight != NICE_0_LOAD))
597 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200598
599 return delta;
600}
601
602/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603 * The idea is to set a period in which each task runs once.
604 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200605 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * this period because otherwise the slices get too small.
607 *
608 * p = (nr <= nl) ? l : l*nr/nl
609 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200610static u64 __sched_period(unsigned long nr_running)
611{
612 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100613 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200614
615 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100616 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200618 }
619
620 return period;
621}
622
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623/*
624 * We calculate the wall-time slice from the period by taking a part
625 * proportional to the weight.
626 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200629static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200630{
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200632
Mike Galbraith0a582442009-01-02 12:16:42 +0100633 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100634 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200635 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100636
637 cfs_rq = cfs_rq_of(se);
638 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200641 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100642
643 update_load_add(&lw, se->load.weight);
644 load = &lw;
645 }
646 slice = calc_delta_mine(slice, se->load.weight, load);
647 }
648 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200649}
650
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200652 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200658 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659}
660
661/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200662 * Update the current task's runtime statistics. Skip current tasks that
663 * are not in our scheduling class.
664 */
665static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200666__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
667 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200668{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200669 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200670
Lucas De Marchi41acab82010-03-10 23:37:45 -0300671 schedstat_set(curr->statistics.exec_max,
672 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200673
674 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200675 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200676 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100677
Ingo Molnare9acbff2007-10-15 17:00:04 +0200678 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200679 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200680}
681
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200682static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200683{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200684 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700685 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200686 unsigned long delta_exec;
687
688 if (unlikely(!curr))
689 return;
690
691 /*
692 * Get the amount of time the current task was running
693 * since the last time we changed load (this cannot
694 * overflow on 32 bits):
695 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200696 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100697 if (!delta_exec)
698 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200699
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200700 __update_curr(cfs_rq, curr, delta_exec);
701 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100702
703 if (entity_is_task(curr)) {
704 struct task_struct *curtask = task_of(curr);
705
Ingo Molnarf977bb42009-09-13 18:15:54 +0200706 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100707 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700708 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100709 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700710
711 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200712}
713
714static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200715update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200716{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300717 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200718}
719
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200720/*
721 * Task is being enqueued - update stats:
722 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200723static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200724{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200725 /*
726 * Are we enqueueing a waiting task? (for current tasks
727 * a dequeue/enqueue event is a NOP)
728 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200729 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200730 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731}
732
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200733static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200734update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300736 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
737 rq_of(cfs_rq)->clock - se->statistics.wait_start));
738 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
739 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
740 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200741#ifdef CONFIG_SCHEDSTATS
742 if (entity_is_task(se)) {
743 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300744 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200745 }
746#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300747 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200748}
749
750static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200751update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200752{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200753 /*
754 * Mark the end of the wait period if dequeueing a
755 * waiting task:
756 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200757 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200758 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200759}
760
761/*
762 * We are picking a new current task - update its stats:
763 */
764static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200765update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200766{
767 /*
768 * We are starting a new run period:
769 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700770 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200771}
772
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200773/**************************************************
774 * Scheduling class queueing methods:
775 */
776
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200777static void
778account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
779{
780 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200781 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200782 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100783#ifdef CONFIG_SMP
784 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200785 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100786#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200787 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200788}
789
790static void
791account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
792{
793 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200794 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200795 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100796 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530797 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200798 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200799}
800
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800801#ifdef CONFIG_FAIR_GROUP_SCHED
802# ifdef CONFIG_SMP
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200803static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
804{
805 long tg_weight;
806
807 /*
808 * Use this CPU's actual weight instead of the last load_contribution
809 * to gain a more accurate current total weight. See
810 * update_cfs_rq_load_contribution().
811 */
Paul Turner82958362012-10-04 13:18:31 +0200812 tg_weight = atomic64_read(&tg->load_avg);
813 tg_weight -= cfs_rq->tg_load_contrib;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200814 tg_weight += cfs_rq->load.weight;
815
816 return tg_weight;
817}
818
Paul Turner6d5ab292011-01-21 20:45:01 -0800819static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800820{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200821 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800822
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200823 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800824 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800825
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800826 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200827 if (tg_weight)
828 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800829
830 if (shares < MIN_SHARES)
831 shares = MIN_SHARES;
832 if (shares > tg->shares)
833 shares = tg->shares;
834
835 return shares;
836}
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800837# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -0800838static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800839{
840 return tg->shares;
841}
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800842# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800843static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
844 unsigned long weight)
845{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800846 if (se->on_rq) {
847 /* commit outstanding execution time */
848 if (cfs_rq->curr == se)
849 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800850 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800851 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800852
853 update_load_set(&se->load, weight);
854
855 if (se->on_rq)
856 account_entity_enqueue(cfs_rq, se);
857}
858
Paul Turner82958362012-10-04 13:18:31 +0200859static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
860
Paul Turner6d5ab292011-01-21 20:45:01 -0800861static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800862{
863 struct task_group *tg;
864 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800865 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800866
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800867 tg = cfs_rq->tg;
868 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700869 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800870 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800871#ifndef CONFIG_SMP
872 if (likely(se->load.weight == tg->shares))
873 return;
874#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800875 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800876
877 reweight_entity(cfs_rq_of(se), se, shares);
878}
879#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -0800880static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800881{
882}
883#endif /* CONFIG_FAIR_GROUP_SCHED */
884
Paul Turnerf4e26b12012-10-04 13:18:32 +0200885/* Only depends on SMP, FAIR_GROUP_SCHED may be removed when useful in lb */
886#if defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)
Paul Turner9d85f212012-10-04 13:18:29 +0200887/*
Paul Turner5b51f2f2012-10-04 13:18:32 +0200888 * We choose a half-life close to 1 scheduling period.
889 * Note: The tables below are dependent on this value.
890 */
891#define LOAD_AVG_PERIOD 32
892#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
893#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */
894
895/* Precomputed fixed inverse multiplies for multiplication by y^n */
896static const u32 runnable_avg_yN_inv[] = {
897 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
898 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
899 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
900 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
901 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
902 0x85aac367, 0x82cd8698,
903};
904
905/*
906 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
907 * over-estimates when re-combining.
908 */
909static const u32 runnable_avg_yN_sum[] = {
910 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
911 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
912 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
913};
914
915/*
Paul Turner9d85f212012-10-04 13:18:29 +0200916 * Approximate:
917 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
918 */
919static __always_inline u64 decay_load(u64 val, u64 n)
920{
Paul Turner5b51f2f2012-10-04 13:18:32 +0200921 unsigned int local_n;
922
923 if (!n)
924 return val;
925 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
926 return 0;
927
928 /* after bounds checking we can collapse to 32-bit */
929 local_n = n;
930
931 /*
932 * As y^PERIOD = 1/2, we can combine
933 * y^n = 1/2^(n/PERIOD) * k^(n%PERIOD)
934 * With a look-up table which covers k^n (n<PERIOD)
935 *
936 * To achieve constant time decay_load.
937 */
938 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
939 val >>= local_n / LOAD_AVG_PERIOD;
940 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +0200941 }
942
Paul Turner5b51f2f2012-10-04 13:18:32 +0200943 val *= runnable_avg_yN_inv[local_n];
944 /* We don't use SRR here since we always want to round down. */
945 return val >> 32;
946}
947
948/*
949 * For updates fully spanning n periods, the contribution to runnable
950 * average will be: \Sum 1024*y^n
951 *
952 * We can compute this reasonably efficiently by combining:
953 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
954 */
955static u32 __compute_runnable_contrib(u64 n)
956{
957 u32 contrib = 0;
958
959 if (likely(n <= LOAD_AVG_PERIOD))
960 return runnable_avg_yN_sum[n];
961 else if (unlikely(n >= LOAD_AVG_MAX_N))
962 return LOAD_AVG_MAX;
963
964 /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */
965 do {
966 contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */
967 contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD];
968
969 n -= LOAD_AVG_PERIOD;
970 } while (n > LOAD_AVG_PERIOD);
971
972 contrib = decay_load(contrib, n);
973 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +0200974}
975
976/*
977 * We can represent the historical contribution to runnable average as the
978 * coefficients of a geometric series. To do this we sub-divide our runnable
979 * history into segments of approximately 1ms (1024us); label the segment that
980 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
981 *
982 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
983 * p0 p1 p2
984 * (now) (~1ms ago) (~2ms ago)
985 *
986 * Let u_i denote the fraction of p_i that the entity was runnable.
987 *
988 * We then designate the fractions u_i as our co-efficients, yielding the
989 * following representation of historical load:
990 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
991 *
992 * We choose y based on the with of a reasonably scheduling period, fixing:
993 * y^32 = 0.5
994 *
995 * This means that the contribution to load ~32ms ago (u_32) will be weighted
996 * approximately half as much as the contribution to load within the last ms
997 * (u_0).
998 *
999 * When a period "rolls over" and we have new u_0`, multiplying the previous
1000 * sum again by y is sufficient to update:
1001 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1002 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1003 */
1004static __always_inline int __update_entity_runnable_avg(u64 now,
1005 struct sched_avg *sa,
1006 int runnable)
1007{
Paul Turner5b51f2f2012-10-04 13:18:32 +02001008 u64 delta, periods;
1009 u32 runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02001010 int delta_w, decayed = 0;
1011
1012 delta = now - sa->last_runnable_update;
1013 /*
1014 * This should only happen when time goes backwards, which it
1015 * unfortunately does during sched clock init when we swap over to TSC.
1016 */
1017 if ((s64)delta < 0) {
1018 sa->last_runnable_update = now;
1019 return 0;
1020 }
1021
1022 /*
1023 * Use 1024ns as the unit of measurement since it's a reasonable
1024 * approximation of 1us and fast to compute.
1025 */
1026 delta >>= 10;
1027 if (!delta)
1028 return 0;
1029 sa->last_runnable_update = now;
1030
1031 /* delta_w is the amount already accumulated against our next period */
1032 delta_w = sa->runnable_avg_period % 1024;
1033 if (delta + delta_w >= 1024) {
1034 /* period roll-over */
1035 decayed = 1;
1036
1037 /*
1038 * Now that we know we're crossing a period boundary, figure
1039 * out how much from delta we need to complete the current
1040 * period and accrue it.
1041 */
1042 delta_w = 1024 - delta_w;
Paul Turner5b51f2f2012-10-04 13:18:32 +02001043 if (runnable)
1044 sa->runnable_avg_sum += delta_w;
1045 sa->runnable_avg_period += delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02001046
Paul Turner5b51f2f2012-10-04 13:18:32 +02001047 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02001048
Paul Turner5b51f2f2012-10-04 13:18:32 +02001049 /* Figure out how many additional periods this update spans */
1050 periods = delta / 1024;
1051 delta %= 1024;
1052
1053 sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum,
1054 periods + 1);
1055 sa->runnable_avg_period = decay_load(sa->runnable_avg_period,
1056 periods + 1);
1057
1058 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
1059 runnable_contrib = __compute_runnable_contrib(periods);
1060 if (runnable)
1061 sa->runnable_avg_sum += runnable_contrib;
1062 sa->runnable_avg_period += runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02001063 }
1064
1065 /* Remainder of delta accrued against u_0` */
1066 if (runnable)
1067 sa->runnable_avg_sum += delta;
1068 sa->runnable_avg_period += delta;
1069
1070 return decayed;
1071}
1072
Paul Turner9ee474f2012-10-04 13:18:30 +02001073/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02001074static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02001075{
1076 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1077 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1078
1079 decays -= se->avg.decay_count;
1080 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02001081 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02001082
1083 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1084 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02001085
1086 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02001087}
1088
Paul Turnerc566e8e2012-10-04 13:18:30 +02001089#ifdef CONFIG_FAIR_GROUP_SCHED
1090static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1091 int force_update)
1092{
1093 struct task_group *tg = cfs_rq->tg;
1094 s64 tg_contrib;
1095
1096 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
1097 tg_contrib -= cfs_rq->tg_load_contrib;
1098
1099 if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
1100 atomic64_add(tg_contrib, &tg->load_avg);
1101 cfs_rq->tg_load_contrib += tg_contrib;
1102 }
1103}
Paul Turner8165e142012-10-04 13:18:31 +02001104
Paul Turnerbb17f652012-10-04 13:18:31 +02001105/*
1106 * Aggregate cfs_rq runnable averages into an equivalent task_group
1107 * representation for computing load contributions.
1108 */
1109static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1110 struct cfs_rq *cfs_rq)
1111{
1112 struct task_group *tg = cfs_rq->tg;
1113 long contrib;
1114
1115 /* The fraction of a cpu used by this cfs_rq */
1116 contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
1117 sa->runnable_avg_period + 1);
1118 contrib -= cfs_rq->tg_runnable_contrib;
1119
1120 if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
1121 atomic_add(contrib, &tg->runnable_avg);
1122 cfs_rq->tg_runnable_contrib += contrib;
1123 }
1124}
1125
Paul Turner8165e142012-10-04 13:18:31 +02001126static inline void __update_group_entity_contrib(struct sched_entity *se)
1127{
1128 struct cfs_rq *cfs_rq = group_cfs_rq(se);
1129 struct task_group *tg = cfs_rq->tg;
Paul Turnerbb17f652012-10-04 13:18:31 +02001130 int runnable_avg;
1131
Paul Turner8165e142012-10-04 13:18:31 +02001132 u64 contrib;
1133
1134 contrib = cfs_rq->tg_load_contrib * tg->shares;
1135 se->avg.load_avg_contrib = div64_u64(contrib,
1136 atomic64_read(&tg->load_avg) + 1);
Paul Turnerbb17f652012-10-04 13:18:31 +02001137
1138 /*
1139 * For group entities we need to compute a correction term in the case
1140 * that they are consuming <1 cpu so that we would contribute the same
1141 * load as a task of equal weight.
1142 *
1143 * Explicitly co-ordinating this measurement would be expensive, but
1144 * fortunately the sum of each cpus contribution forms a usable
1145 * lower-bound on the true value.
1146 *
1147 * Consider the aggregate of 2 contributions. Either they are disjoint
1148 * (and the sum represents true value) or they are disjoint and we are
1149 * understating by the aggregate of their overlap.
1150 *
1151 * Extending this to N cpus, for a given overlap, the maximum amount we
1152 * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
1153 * cpus that overlap for this interval and w_i is the interval width.
1154 *
1155 * On a small machine; the first term is well-bounded which bounds the
1156 * total error since w_i is a subset of the period. Whereas on a
1157 * larger machine, while this first term can be larger, if w_i is the
1158 * of consequential size guaranteed to see n_i*w_i quickly converge to
1159 * our upper bound of 1-cpu.
1160 */
1161 runnable_avg = atomic_read(&tg->runnable_avg);
1162 if (runnable_avg < NICE_0_LOAD) {
1163 se->avg.load_avg_contrib *= runnable_avg;
1164 se->avg.load_avg_contrib >>= NICE_0_SHIFT;
1165 }
Paul Turner8165e142012-10-04 13:18:31 +02001166}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001167#else
1168static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1169 int force_update) {}
Paul Turnerbb17f652012-10-04 13:18:31 +02001170static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1171 struct cfs_rq *cfs_rq) {}
Paul Turner8165e142012-10-04 13:18:31 +02001172static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001173#endif
1174
Paul Turner8165e142012-10-04 13:18:31 +02001175static inline void __update_task_entity_contrib(struct sched_entity *se)
1176{
1177 u32 contrib;
1178
1179 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
1180 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
1181 contrib /= (se->avg.runnable_avg_period + 1);
1182 se->avg.load_avg_contrib = scale_load(contrib);
1183}
1184
Paul Turner2dac7542012-10-04 13:18:30 +02001185/* Compute the current contribution to load_avg by se, return any delta */
1186static long __update_entity_load_avg_contrib(struct sched_entity *se)
1187{
1188 long old_contrib = se->avg.load_avg_contrib;
1189
Paul Turner8165e142012-10-04 13:18:31 +02001190 if (entity_is_task(se)) {
1191 __update_task_entity_contrib(se);
1192 } else {
Paul Turnerbb17f652012-10-04 13:18:31 +02001193 __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
Paul Turner8165e142012-10-04 13:18:31 +02001194 __update_group_entity_contrib(se);
1195 }
Paul Turner2dac7542012-10-04 13:18:30 +02001196
1197 return se->avg.load_avg_contrib - old_contrib;
1198}
1199
Paul Turner9ee474f2012-10-04 13:18:30 +02001200static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1201 long load_contrib)
1202{
1203 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1204 cfs_rq->blocked_load_avg -= load_contrib;
1205 else
1206 cfs_rq->blocked_load_avg = 0;
1207}
1208
Paul Turnerf1b17282012-10-04 13:18:31 +02001209static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
1210
Paul Turner9d85f212012-10-04 13:18:29 +02001211/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001212static inline void update_entity_load_avg(struct sched_entity *se,
1213 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001214{
Paul Turner2dac7542012-10-04 13:18:30 +02001215 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1216 long contrib_delta;
Paul Turnerf1b17282012-10-04 13:18:31 +02001217 u64 now;
Paul Turner2dac7542012-10-04 13:18:30 +02001218
Paul Turnerf1b17282012-10-04 13:18:31 +02001219 /*
1220 * For a group entity we need to use their owned cfs_rq_clock_task() in
1221 * case they are the parent of a throttled hierarchy.
1222 */
1223 if (entity_is_task(se))
1224 now = cfs_rq_clock_task(cfs_rq);
1225 else
1226 now = cfs_rq_clock_task(group_cfs_rq(se));
1227
1228 if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
Paul Turner2dac7542012-10-04 13:18:30 +02001229 return;
1230
1231 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001232
1233 if (!update_cfs_rq)
1234 return;
1235
Paul Turner2dac7542012-10-04 13:18:30 +02001236 if (se->on_rq)
1237 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001238 else
1239 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1240}
1241
1242/*
1243 * Decay the load contributed by all blocked children and account this so that
1244 * their contribution may appropriately discounted when they wake up.
1245 */
Paul Turneraff3e492012-10-04 13:18:30 +02001246static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001247{
Paul Turnerf1b17282012-10-04 13:18:31 +02001248 u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
Paul Turner9ee474f2012-10-04 13:18:30 +02001249 u64 decays;
1250
1251 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001252 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001253 return;
1254
Paul Turneraff3e492012-10-04 13:18:30 +02001255 if (atomic64_read(&cfs_rq->removed_load)) {
1256 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1257 subtract_blocked_load_contrib(cfs_rq, removed_load);
1258 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001259
Paul Turneraff3e492012-10-04 13:18:30 +02001260 if (decays) {
1261 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1262 decays);
1263 atomic64_add(decays, &cfs_rq->decay_counter);
1264 cfs_rq->last_decay = now;
1265 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001266
1267 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02001268}
Ben Segall18bf2802012-10-04 12:51:20 +02001269
1270static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1271{
1272 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
Paul Turnerbb17f652012-10-04 13:18:31 +02001273 __update_tg_runnable_avg(&rq->avg, &rq->cfs);
Ben Segall18bf2802012-10-04 12:51:20 +02001274}
Paul Turner2dac7542012-10-04 13:18:30 +02001275
1276/* Add the load generated by se into cfs_rq's child load-average */
1277static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001278 struct sched_entity *se,
1279 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001280{
Paul Turneraff3e492012-10-04 13:18:30 +02001281 /*
1282 * We track migrations using entity decay_count <= 0, on a wake-up
1283 * migration we use a negative decay count to track the remote decays
1284 * accumulated while sleeping.
1285 */
1286 if (unlikely(se->avg.decay_count <= 0)) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001287 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
Paul Turneraff3e492012-10-04 13:18:30 +02001288 if (se->avg.decay_count) {
1289 /*
1290 * In a wake-up migration we have to approximate the
1291 * time sleeping. This is because we can't synchronize
1292 * clock_task between the two cpus, and it is not
1293 * guaranteed to be read-safe. Instead, we can
1294 * approximate this using our carried decays, which are
1295 * explicitly atomically readable.
1296 */
1297 se->avg.last_runnable_update -= (-se->avg.decay_count)
1298 << 20;
1299 update_entity_load_avg(se, 0);
1300 /* Indicate that we're now synchronized and on-rq */
1301 se->avg.decay_count = 0;
1302 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001303 wakeup = 0;
1304 } else {
1305 __synchronize_entity_decay(se);
1306 }
1307
Paul Turneraff3e492012-10-04 13:18:30 +02001308 /* migrated tasks did not contribute to our blocked load */
1309 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001310 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02001311 update_entity_load_avg(se, 0);
1312 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001313
Paul Turner2dac7542012-10-04 13:18:30 +02001314 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02001315 /* we force update consideration on load-balancer moves */
1316 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02001317}
1318
Paul Turner9ee474f2012-10-04 13:18:30 +02001319/*
1320 * Remove se's load from this cfs_rq child load-average, if the entity is
1321 * transitioning to a blocked state we track its projected decay using
1322 * blocked_load_avg.
1323 */
Paul Turner2dac7542012-10-04 13:18:30 +02001324static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001325 struct sched_entity *se,
1326 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001327{
Paul Turner9ee474f2012-10-04 13:18:30 +02001328 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001329 /* we force update consideration on load-balancer moves */
1330 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02001331
Paul Turner2dac7542012-10-04 13:18:30 +02001332 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001333 if (sleep) {
1334 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1335 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1336 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001337}
Paul Turner9d85f212012-10-04 13:18:29 +02001338#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001339static inline void update_entity_load_avg(struct sched_entity *se,
1340 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001341static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001342static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001343 struct sched_entity *se,
1344 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001345static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001346 struct sched_entity *se,
1347 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02001348static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
1349 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001350#endif
1351
Ingo Molnar2396af62007-08-09 11:16:48 +02001352static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001353{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001354#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001355 struct task_struct *tsk = NULL;
1356
1357 if (entity_is_task(se))
1358 tsk = task_of(se);
1359
Lucas De Marchi41acab82010-03-10 23:37:45 -03001360 if (se->statistics.sleep_start) {
1361 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001362
1363 if ((s64)delta < 0)
1364 delta = 0;
1365
Lucas De Marchi41acab82010-03-10 23:37:45 -03001366 if (unlikely(delta > se->statistics.sleep_max))
1367 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001368
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001369 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001370 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001371
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001372 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001373 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001374 trace_sched_stat_sleep(tsk, delta);
1375 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001376 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001377 if (se->statistics.block_start) {
1378 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001379
1380 if ((s64)delta < 0)
1381 delta = 0;
1382
Lucas De Marchi41acab82010-03-10 23:37:45 -03001383 if (unlikely(delta > se->statistics.block_max))
1384 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001385
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001386 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001387 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001388
Peter Zijlstrae4143142009-07-23 20:13:26 +02001389 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001390 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001391 se->statistics.iowait_sum += delta;
1392 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001393 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001394 }
1395
Andrew Vaginb781a602011-11-28 12:03:35 +03001396 trace_sched_stat_blocked(tsk, delta);
1397
Peter Zijlstrae4143142009-07-23 20:13:26 +02001398 /*
1399 * Blocking time is in units of nanosecs, so shift by
1400 * 20 to get a milliseconds-range estimation of the
1401 * amount of time that the task spent sleeping:
1402 */
1403 if (unlikely(prof_on == SLEEP_PROFILING)) {
1404 profile_hits(SLEEP_PROFILING,
1405 (void *)get_wchan(tsk),
1406 delta >> 20);
1407 }
1408 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001409 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001410 }
1411#endif
1412}
1413
Peter Zijlstraddc97292007-10-15 17:00:10 +02001414static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1415{
1416#ifdef CONFIG_SCHED_DEBUG
1417 s64 d = se->vruntime - cfs_rq->min_vruntime;
1418
1419 if (d < 0)
1420 d = -d;
1421
1422 if (d > 3*sysctl_sched_latency)
1423 schedstat_inc(cfs_rq, nr_spread_over);
1424#endif
1425}
1426
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001427static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001428place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1429{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001430 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001431
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001432 /*
1433 * The 'current' period is already promised to the current tasks,
1434 * however the extra weight of the new task will slow them down a
1435 * little, place the new task so that it fits in the slot that
1436 * stays open at the end.
1437 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001438 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001439 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001440
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001441 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001442 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001443 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001444
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001445 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001446 * Halve their sleep time's effect, to allow
1447 * for a gentler effect of sleepers:
1448 */
1449 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1450 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001451
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001452 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001453 }
1454
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001455 /* ensure we never gain time by being placed backwards. */
1456 vruntime = max_vruntime(se->vruntime, vruntime);
1457
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001458 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001459}
1460
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001461static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1462
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001463static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001464enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001465{
1466 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001467 * Update the normalized vruntime before updating min_vruntime
1468 * through callig update_curr().
1469 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001470 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001471 se->vruntime += cfs_rq->min_vruntime;
1472
1473 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001474 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001475 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001476 update_curr(cfs_rq);
Paul Turnerf269ae02012-10-04 13:18:31 +02001477 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08001478 account_entity_enqueue(cfs_rq, se);
1479 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001480
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001481 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001482 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001483 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001484 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001485
Ingo Molnard2417e52007-08-09 11:16:47 +02001486 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001487 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001488 if (se != cfs_rq->curr)
1489 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001490 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001491
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001492 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001493 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001494 check_enqueue_throttle(cfs_rq);
1495 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001496}
1497
Rik van Riel2c13c9192011-02-01 09:48:37 -05001498static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001499{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001500 for_each_sched_entity(se) {
1501 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1502 if (cfs_rq->last == se)
1503 cfs_rq->last = NULL;
1504 else
1505 break;
1506 }
1507}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001508
Rik van Riel2c13c9192011-02-01 09:48:37 -05001509static void __clear_buddies_next(struct sched_entity *se)
1510{
1511 for_each_sched_entity(se) {
1512 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1513 if (cfs_rq->next == se)
1514 cfs_rq->next = NULL;
1515 else
1516 break;
1517 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001518}
1519
Rik van Rielac53db52011-02-01 09:51:03 -05001520static void __clear_buddies_skip(struct sched_entity *se)
1521{
1522 for_each_sched_entity(se) {
1523 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1524 if (cfs_rq->skip == se)
1525 cfs_rq->skip = NULL;
1526 else
1527 break;
1528 }
1529}
1530
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001531static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1532{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001533 if (cfs_rq->last == se)
1534 __clear_buddies_last(se);
1535
1536 if (cfs_rq->next == se)
1537 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001538
1539 if (cfs_rq->skip == se)
1540 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001541}
1542
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001543static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001544
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001545static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001546dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001547{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001548 /*
1549 * Update run-time statistics of the 'current'.
1550 */
1551 update_curr(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08001552 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001553
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001554 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001555 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001556#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001557 if (entity_is_task(se)) {
1558 struct task_struct *tsk = task_of(se);
1559
1560 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001561 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001562 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001563 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001564 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001565#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001566 }
1567
Peter Zijlstra2002c692008-11-11 11:52:33 +01001568 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001569
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001570 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001571 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08001572 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001573 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001574
1575 /*
1576 * Normalize the entity after updating the min_vruntime because the
1577 * update can refer to the ->curr item and we need to reflect this
1578 * movement in our normalized position.
1579 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001580 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001581 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001582
Paul Turnerd8b49862011-07-21 09:43:41 -07001583 /* return excess runtime on last dequeue */
1584 return_cfs_rq_runtime(cfs_rq);
1585
Peter Zijlstra1e876232011-05-17 16:21:10 -07001586 update_min_vruntime(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08001587 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001588}
1589
1590/*
1591 * Preempt the current task with a newly woken task if needed:
1592 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001593static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001594check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001595{
Peter Zijlstra11697832007-09-05 14:32:49 +02001596 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001597 struct sched_entity *se;
1598 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001599
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001600 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001601 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001602 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001603 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001604 /*
1605 * The current task ran long enough, ensure it doesn't get
1606 * re-elected due to buddy favours.
1607 */
1608 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001609 return;
1610 }
1611
1612 /*
1613 * Ensure that a task that missed wakeup preemption by a
1614 * narrow margin doesn't have to wait for a full slice.
1615 * This also mitigates buddy induced latencies under load.
1616 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001617 if (delta_exec < sysctl_sched_min_granularity)
1618 return;
1619
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001620 se = __pick_first_entity(cfs_rq);
1621 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001622
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001623 if (delta < 0)
1624 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001625
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001626 if (delta > ideal_runtime)
1627 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001628}
1629
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001630static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001631set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001632{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001633 /* 'current' is not kept within the tree. */
1634 if (se->on_rq) {
1635 /*
1636 * Any task has to be enqueued before it get to execute on
1637 * a CPU. So account for the time it spent waiting on the
1638 * runqueue.
1639 */
1640 update_stats_wait_end(cfs_rq, se);
1641 __dequeue_entity(cfs_rq, se);
1642 }
1643
Ingo Molnar79303e92007-08-09 11:16:47 +02001644 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001645 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001646#ifdef CONFIG_SCHEDSTATS
1647 /*
1648 * Track our maximum slice length, if the CPU's load is at
1649 * least twice that of our own weight (i.e. dont track it
1650 * when there are only lesser-weight tasks around):
1651 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001652 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001653 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001654 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1655 }
1656#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001657 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001658}
1659
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001660static int
1661wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1662
Rik van Rielac53db52011-02-01 09:51:03 -05001663/*
1664 * Pick the next process, keeping these things in mind, in this order:
1665 * 1) keep things fair between processes/task groups
1666 * 2) pick the "next" process, since someone really wants that to run
1667 * 3) pick the "last" process, for cache locality
1668 * 4) do not run the "skip" process, if something else is available
1669 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001670static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001671{
Rik van Rielac53db52011-02-01 09:51:03 -05001672 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001673 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001674
Rik van Rielac53db52011-02-01 09:51:03 -05001675 /*
1676 * Avoid running the skip buddy, if running something else can
1677 * be done without getting too unfair.
1678 */
1679 if (cfs_rq->skip == se) {
1680 struct sched_entity *second = __pick_next_entity(se);
1681 if (second && wakeup_preempt_entity(second, left) < 1)
1682 se = second;
1683 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001684
Mike Galbraithf685cea2009-10-23 23:09:22 +02001685 /*
1686 * Prefer last buddy, try to return the CPU to a preempted task.
1687 */
1688 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1689 se = cfs_rq->last;
1690
Rik van Rielac53db52011-02-01 09:51:03 -05001691 /*
1692 * Someone really wants this to run. If it's not unfair, run it.
1693 */
1694 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1695 se = cfs_rq->next;
1696
Mike Galbraithf685cea2009-10-23 23:09:22 +02001697 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001698
1699 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001700}
1701
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001702static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1703
Ingo Molnarab6cde22007-08-09 11:16:48 +02001704static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001705{
1706 /*
1707 * If still on the runqueue then deactivate_task()
1708 * was not called and update_curr() has to be done:
1709 */
1710 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001711 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001712
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001713 /* throttle cfs_rqs exceeding runtime */
1714 check_cfs_rq_runtime(cfs_rq);
1715
Peter Zijlstraddc97292007-10-15 17:00:10 +02001716 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001717 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001718 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001719 /* Put 'current' back into the tree. */
1720 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001721 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001722 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001723 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001724 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001725}
1726
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001727static void
1728entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001729{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001730 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001731 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001732 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001733 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001734
Paul Turner43365bd2010-12-15 19:10:17 -08001735 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001736 * Ensure that runnable average is periodically updated.
1737 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001738 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001739 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001740
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001741#ifdef CONFIG_SCHED_HRTICK
1742 /*
1743 * queued ticks are scheduled to match the slice, so don't bother
1744 * validating it and just reschedule.
1745 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001746 if (queued) {
1747 resched_task(rq_of(cfs_rq)->curr);
1748 return;
1749 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001750 /*
1751 * don't let the period tick interfere with the hrtick preemption
1752 */
1753 if (!sched_feat(DOUBLE_TICK) &&
1754 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1755 return;
1756#endif
1757
Yong Zhang2c2efae2011-07-29 16:20:33 +08001758 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001759 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001760}
1761
Paul Turnerab84d312011-07-21 09:43:28 -07001762
1763/**************************************************
1764 * CFS bandwidth control machinery
1765 */
1766
1767#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001768
1769#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001770static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001771
1772static inline bool cfs_bandwidth_used(void)
1773{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001774 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001775}
1776
1777void account_cfs_bandwidth_used(int enabled, int was_enabled)
1778{
1779 /* only need to count groups transitioning between enabled/!enabled */
1780 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001781 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001782 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001783 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001784}
1785#else /* HAVE_JUMP_LABEL */
1786static bool cfs_bandwidth_used(void)
1787{
1788 return true;
1789}
1790
1791void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1792#endif /* HAVE_JUMP_LABEL */
1793
Paul Turnerab84d312011-07-21 09:43:28 -07001794/*
1795 * default period for cfs group bandwidth.
1796 * default: 0.1s, units: nanoseconds
1797 */
1798static inline u64 default_cfs_period(void)
1799{
1800 return 100000000ULL;
1801}
Paul Turnerec12cb72011-07-21 09:43:30 -07001802
1803static inline u64 sched_cfs_bandwidth_slice(void)
1804{
1805 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1806}
1807
Paul Turnera9cf55b2011-07-21 09:43:32 -07001808/*
1809 * Replenish runtime according to assigned quota and update expiration time.
1810 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1811 * additional synchronization around rq->lock.
1812 *
1813 * requires cfs_b->lock
1814 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001815void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001816{
1817 u64 now;
1818
1819 if (cfs_b->quota == RUNTIME_INF)
1820 return;
1821
1822 now = sched_clock_cpu(smp_processor_id());
1823 cfs_b->runtime = cfs_b->quota;
1824 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1825}
1826
Peter Zijlstra029632f2011-10-25 10:00:11 +02001827static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1828{
1829 return &tg->cfs_bandwidth;
1830}
1831
Paul Turnerf1b17282012-10-04 13:18:31 +02001832/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
1833static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
1834{
1835 if (unlikely(cfs_rq->throttle_count))
1836 return cfs_rq->throttled_clock_task;
1837
1838 return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time;
1839}
1840
Paul Turner85dac902011-07-21 09:43:33 -07001841/* returns 0 on failure to allocate runtime */
1842static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001843{
1844 struct task_group *tg = cfs_rq->tg;
1845 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001846 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001847
1848 /* note: this is a positive sum as runtime_remaining <= 0 */
1849 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1850
1851 raw_spin_lock(&cfs_b->lock);
1852 if (cfs_b->quota == RUNTIME_INF)
1853 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001854 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001855 /*
1856 * If the bandwidth pool has become inactive, then at least one
1857 * period must have elapsed since the last consumption.
1858 * Refresh the global state and ensure bandwidth timer becomes
1859 * active.
1860 */
1861 if (!cfs_b->timer_active) {
1862 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001863 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001864 }
Paul Turner58088ad2011-07-21 09:43:31 -07001865
1866 if (cfs_b->runtime > 0) {
1867 amount = min(cfs_b->runtime, min_amount);
1868 cfs_b->runtime -= amount;
1869 cfs_b->idle = 0;
1870 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001871 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001872 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001873 raw_spin_unlock(&cfs_b->lock);
1874
1875 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001876 /*
1877 * we may have advanced our local expiration to account for allowed
1878 * spread between our sched_clock and the one on which runtime was
1879 * issued.
1880 */
1881 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1882 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001883
1884 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001885}
1886
1887/*
1888 * Note: This depends on the synchronization provided by sched_clock and the
1889 * fact that rq->clock snapshots this value.
1890 */
1891static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1892{
1893 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1894 struct rq *rq = rq_of(cfs_rq);
1895
1896 /* if the deadline is ahead of our clock, nothing to do */
1897 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1898 return;
1899
1900 if (cfs_rq->runtime_remaining < 0)
1901 return;
1902
1903 /*
1904 * If the local deadline has passed we have to consider the
1905 * possibility that our sched_clock is 'fast' and the global deadline
1906 * has not truly expired.
1907 *
1908 * Fortunately we can check determine whether this the case by checking
1909 * whether the global deadline has advanced.
1910 */
1911
1912 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1913 /* extend local deadline, drift is bounded above by 2 ticks */
1914 cfs_rq->runtime_expires += TICK_NSEC;
1915 } else {
1916 /* global deadline is ahead, expiration has passed */
1917 cfs_rq->runtime_remaining = 0;
1918 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001919}
1920
1921static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1922 unsigned long delta_exec)
1923{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001924 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001925 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001926 expire_cfs_rq_runtime(cfs_rq);
1927
1928 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001929 return;
1930
Paul Turner85dac902011-07-21 09:43:33 -07001931 /*
1932 * if we're unable to extend our runtime we resched so that the active
1933 * hierarchy can be throttled
1934 */
1935 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1936 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001937}
1938
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001939static __always_inline
1940void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001941{
Paul Turner56f570e2011-11-07 20:26:33 -08001942 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001943 return;
1944
1945 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1946}
1947
Paul Turner85dac902011-07-21 09:43:33 -07001948static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1949{
Paul Turner56f570e2011-11-07 20:26:33 -08001950 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001951}
1952
Paul Turner64660c82011-07-21 09:43:36 -07001953/* check whether cfs_rq, or any parent, is throttled */
1954static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1955{
Paul Turner56f570e2011-11-07 20:26:33 -08001956 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001957}
1958
1959/*
1960 * Ensure that neither of the group entities corresponding to src_cpu or
1961 * dest_cpu are members of a throttled hierarchy when performing group
1962 * load-balance operations.
1963 */
1964static inline int throttled_lb_pair(struct task_group *tg,
1965 int src_cpu, int dest_cpu)
1966{
1967 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1968
1969 src_cfs_rq = tg->cfs_rq[src_cpu];
1970 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1971
1972 return throttled_hierarchy(src_cfs_rq) ||
1973 throttled_hierarchy(dest_cfs_rq);
1974}
1975
1976/* updated child weight may affect parent so we have to do this bottom up */
1977static int tg_unthrottle_up(struct task_group *tg, void *data)
1978{
1979 struct rq *rq = data;
1980 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1981
1982 cfs_rq->throttle_count--;
1983#ifdef CONFIG_SMP
1984 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02001985 /* adjust cfs_rq_clock_task() */
1986 cfs_rq->throttled_clock_task_time += rq->clock_task -
1987 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07001988 }
1989#endif
1990
1991 return 0;
1992}
1993
1994static int tg_throttle_down(struct task_group *tg, void *data)
1995{
1996 struct rq *rq = data;
1997 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1998
Paul Turner82958362012-10-04 13:18:31 +02001999 /* group is entering throttled state, stop time */
2000 if (!cfs_rq->throttle_count)
Paul Turnerf1b17282012-10-04 13:18:31 +02002001 cfs_rq->throttled_clock_task = rq->clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07002002 cfs_rq->throttle_count++;
2003
2004 return 0;
2005}
2006
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002007static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07002008{
2009 struct rq *rq = rq_of(cfs_rq);
2010 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2011 struct sched_entity *se;
2012 long task_delta, dequeue = 1;
2013
2014 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2015
Paul Turnerf1b17282012-10-04 13:18:31 +02002016 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07002017 rcu_read_lock();
2018 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
2019 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07002020
2021 task_delta = cfs_rq->h_nr_running;
2022 for_each_sched_entity(se) {
2023 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
2024 /* throttled entity or throttle-on-deactivate */
2025 if (!se->on_rq)
2026 break;
2027
2028 if (dequeue)
2029 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
2030 qcfs_rq->h_nr_running -= task_delta;
2031
2032 if (qcfs_rq->load.weight)
2033 dequeue = 0;
2034 }
2035
2036 if (!se)
2037 rq->nr_running -= task_delta;
2038
2039 cfs_rq->throttled = 1;
Paul Turnerf1b17282012-10-04 13:18:31 +02002040 cfs_rq->throttled_clock = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07002041 raw_spin_lock(&cfs_b->lock);
2042 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
2043 raw_spin_unlock(&cfs_b->lock);
2044}
2045
Peter Zijlstra029632f2011-10-25 10:00:11 +02002046void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07002047{
2048 struct rq *rq = rq_of(cfs_rq);
2049 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2050 struct sched_entity *se;
2051 int enqueue = 1;
2052 long task_delta;
2053
2054 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2055
2056 cfs_rq->throttled = 0;
2057 raw_spin_lock(&cfs_b->lock);
Paul Turnerf1b17282012-10-04 13:18:31 +02002058 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07002059 list_del_rcu(&cfs_rq->throttled_list);
2060 raw_spin_unlock(&cfs_b->lock);
2061
Paul Turner64660c82011-07-21 09:43:36 -07002062 update_rq_clock(rq);
2063 /* update hierarchical throttle state */
2064 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
2065
Paul Turner671fd9d2011-07-21 09:43:34 -07002066 if (!cfs_rq->load.weight)
2067 return;
2068
2069 task_delta = cfs_rq->h_nr_running;
2070 for_each_sched_entity(se) {
2071 if (se->on_rq)
2072 enqueue = 0;
2073
2074 cfs_rq = cfs_rq_of(se);
2075 if (enqueue)
2076 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2077 cfs_rq->h_nr_running += task_delta;
2078
2079 if (cfs_rq_throttled(cfs_rq))
2080 break;
2081 }
2082
2083 if (!se)
2084 rq->nr_running += task_delta;
2085
2086 /* determine whether we need to wake up potentially idle cpu */
2087 if (rq->curr == rq->idle && rq->cfs.nr_running)
2088 resched_task(rq->curr);
2089}
2090
2091static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2092 u64 remaining, u64 expires)
2093{
2094 struct cfs_rq *cfs_rq;
2095 u64 runtime = remaining;
2096
2097 rcu_read_lock();
2098 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2099 throttled_list) {
2100 struct rq *rq = rq_of(cfs_rq);
2101
2102 raw_spin_lock(&rq->lock);
2103 if (!cfs_rq_throttled(cfs_rq))
2104 goto next;
2105
2106 runtime = -cfs_rq->runtime_remaining + 1;
2107 if (runtime > remaining)
2108 runtime = remaining;
2109 remaining -= runtime;
2110
2111 cfs_rq->runtime_remaining += runtime;
2112 cfs_rq->runtime_expires = expires;
2113
2114 /* we check whether we're throttled above */
2115 if (cfs_rq->runtime_remaining > 0)
2116 unthrottle_cfs_rq(cfs_rq);
2117
2118next:
2119 raw_spin_unlock(&rq->lock);
2120
2121 if (!remaining)
2122 break;
2123 }
2124 rcu_read_unlock();
2125
2126 return remaining;
2127}
2128
Paul Turner58088ad2011-07-21 09:43:31 -07002129/*
2130 * Responsible for refilling a task_group's bandwidth and unthrottling its
2131 * cfs_rqs as appropriate. If there has been no activity within the last
2132 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2133 * used to track this state.
2134 */
2135static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2136{
Paul Turner671fd9d2011-07-21 09:43:34 -07002137 u64 runtime, runtime_expires;
2138 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002139
2140 raw_spin_lock(&cfs_b->lock);
2141 /* no need to continue the timer with no bandwidth constraint */
2142 if (cfs_b->quota == RUNTIME_INF)
2143 goto out_unlock;
2144
Paul Turner671fd9d2011-07-21 09:43:34 -07002145 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2146 /* idle depends on !throttled (for the case of a large deficit) */
2147 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002148 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002149
Paul Turnera9cf55b2011-07-21 09:43:32 -07002150 /* if we're going inactive then everything else can be deferred */
2151 if (idle)
2152 goto out_unlock;
2153
2154 __refill_cfs_bandwidth_runtime(cfs_b);
2155
Paul Turner671fd9d2011-07-21 09:43:34 -07002156 if (!throttled) {
2157 /* mark as potentially idle for the upcoming period */
2158 cfs_b->idle = 1;
2159 goto out_unlock;
2160 }
Paul Turner58088ad2011-07-21 09:43:31 -07002161
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002162 /* account preceding periods in which throttling occurred */
2163 cfs_b->nr_throttled += overrun;
2164
Paul Turner671fd9d2011-07-21 09:43:34 -07002165 /*
2166 * There are throttled entities so we must first use the new bandwidth
2167 * to unthrottle them before making it generally available. This
2168 * ensures that all existing debts will be paid before a new cfs_rq is
2169 * allowed to run.
2170 */
2171 runtime = cfs_b->runtime;
2172 runtime_expires = cfs_b->runtime_expires;
2173 cfs_b->runtime = 0;
2174
2175 /*
2176 * This check is repeated as we are holding onto the new bandwidth
2177 * while we unthrottle. This can potentially race with an unthrottled
2178 * group trying to acquire new bandwidth from the global pool.
2179 */
2180 while (throttled && runtime > 0) {
2181 raw_spin_unlock(&cfs_b->lock);
2182 /* we can't nest cfs_b->lock while distributing bandwidth */
2183 runtime = distribute_cfs_runtime(cfs_b, runtime,
2184 runtime_expires);
2185 raw_spin_lock(&cfs_b->lock);
2186
2187 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2188 }
2189
2190 /* return (any) remaining runtime */
2191 cfs_b->runtime = runtime;
2192 /*
2193 * While we are ensured activity in the period following an
2194 * unthrottle, this also covers the case in which the new bandwidth is
2195 * insufficient to cover the existing bandwidth deficit. (Forcing the
2196 * timer to remain active while there are any throttled entities.)
2197 */
2198 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002199out_unlock:
2200 if (idle)
2201 cfs_b->timer_active = 0;
2202 raw_spin_unlock(&cfs_b->lock);
2203
2204 return idle;
2205}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002206
Paul Turnerd8b49862011-07-21 09:43:41 -07002207/* a cfs_rq won't donate quota below this amount */
2208static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2209/* minimum remaining period time to redistribute slack quota */
2210static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2211/* how long we wait to gather additional slack before distributing */
2212static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2213
2214/* are we near the end of the current quota period? */
2215static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2216{
2217 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2218 u64 remaining;
2219
2220 /* if the call-back is running a quota refresh is already occurring */
2221 if (hrtimer_callback_running(refresh_timer))
2222 return 1;
2223
2224 /* is a quota refresh about to occur? */
2225 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2226 if (remaining < min_expire)
2227 return 1;
2228
2229 return 0;
2230}
2231
2232static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2233{
2234 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2235
2236 /* if there's a quota refresh soon don't bother with slack */
2237 if (runtime_refresh_within(cfs_b, min_left))
2238 return;
2239
2240 start_bandwidth_timer(&cfs_b->slack_timer,
2241 ns_to_ktime(cfs_bandwidth_slack_period));
2242}
2243
2244/* we know any runtime found here is valid as update_curr() precedes return */
2245static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2246{
2247 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2248 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2249
2250 if (slack_runtime <= 0)
2251 return;
2252
2253 raw_spin_lock(&cfs_b->lock);
2254 if (cfs_b->quota != RUNTIME_INF &&
2255 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2256 cfs_b->runtime += slack_runtime;
2257
2258 /* we are under rq->lock, defer unthrottling using a timer */
2259 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2260 !list_empty(&cfs_b->throttled_cfs_rq))
2261 start_cfs_slack_bandwidth(cfs_b);
2262 }
2263 raw_spin_unlock(&cfs_b->lock);
2264
2265 /* even if it's not valid for return we don't want to try again */
2266 cfs_rq->runtime_remaining -= slack_runtime;
2267}
2268
2269static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2270{
Paul Turner56f570e2011-11-07 20:26:33 -08002271 if (!cfs_bandwidth_used())
2272 return;
2273
Paul Turnerfccfdc62011-11-07 20:26:34 -08002274 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002275 return;
2276
2277 __return_cfs_rq_runtime(cfs_rq);
2278}
2279
2280/*
2281 * This is done with a timer (instead of inline with bandwidth return) since
2282 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2283 */
2284static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2285{
2286 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2287 u64 expires;
2288
2289 /* confirm we're still not at a refresh boundary */
2290 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2291 return;
2292
2293 raw_spin_lock(&cfs_b->lock);
2294 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2295 runtime = cfs_b->runtime;
2296 cfs_b->runtime = 0;
2297 }
2298 expires = cfs_b->runtime_expires;
2299 raw_spin_unlock(&cfs_b->lock);
2300
2301 if (!runtime)
2302 return;
2303
2304 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2305
2306 raw_spin_lock(&cfs_b->lock);
2307 if (expires == cfs_b->runtime_expires)
2308 cfs_b->runtime = runtime;
2309 raw_spin_unlock(&cfs_b->lock);
2310}
2311
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002312/*
2313 * When a group wakes up we want to make sure that its quota is not already
2314 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2315 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2316 */
2317static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2318{
Paul Turner56f570e2011-11-07 20:26:33 -08002319 if (!cfs_bandwidth_used())
2320 return;
2321
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002322 /* an active group must be handled by the update_curr()->put() path */
2323 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2324 return;
2325
2326 /* ensure the group is not already throttled */
2327 if (cfs_rq_throttled(cfs_rq))
2328 return;
2329
2330 /* update runtime allocation */
2331 account_cfs_rq_runtime(cfs_rq, 0);
2332 if (cfs_rq->runtime_remaining <= 0)
2333 throttle_cfs_rq(cfs_rq);
2334}
2335
2336/* conditionally throttle active cfs_rq's from put_prev_entity() */
2337static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2338{
Paul Turner56f570e2011-11-07 20:26:33 -08002339 if (!cfs_bandwidth_used())
2340 return;
2341
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002342 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2343 return;
2344
2345 /*
2346 * it's possible for a throttled entity to be forced into a running
2347 * state (e.g. set_curr_task), in this case we're finished.
2348 */
2349 if (cfs_rq_throttled(cfs_rq))
2350 return;
2351
2352 throttle_cfs_rq(cfs_rq);
2353}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002354
2355static inline u64 default_cfs_period(void);
2356static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2357static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2358
2359static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2360{
2361 struct cfs_bandwidth *cfs_b =
2362 container_of(timer, struct cfs_bandwidth, slack_timer);
2363 do_sched_cfs_slack_timer(cfs_b);
2364
2365 return HRTIMER_NORESTART;
2366}
2367
2368static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2369{
2370 struct cfs_bandwidth *cfs_b =
2371 container_of(timer, struct cfs_bandwidth, period_timer);
2372 ktime_t now;
2373 int overrun;
2374 int idle = 0;
2375
2376 for (;;) {
2377 now = hrtimer_cb_get_time(timer);
2378 overrun = hrtimer_forward(timer, now, cfs_b->period);
2379
2380 if (!overrun)
2381 break;
2382
2383 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2384 }
2385
2386 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2387}
2388
2389void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2390{
2391 raw_spin_lock_init(&cfs_b->lock);
2392 cfs_b->runtime = 0;
2393 cfs_b->quota = RUNTIME_INF;
2394 cfs_b->period = ns_to_ktime(default_cfs_period());
2395
2396 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2397 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2398 cfs_b->period_timer.function = sched_cfs_period_timer;
2399 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2400 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2401}
2402
2403static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2404{
2405 cfs_rq->runtime_enabled = 0;
2406 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2407}
2408
2409/* requires cfs_b->lock, may release to reprogram timer */
2410void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2411{
2412 /*
2413 * The timer may be active because we're trying to set a new bandwidth
2414 * period or because we're racing with the tear-down path
2415 * (timer_active==0 becomes visible before the hrtimer call-back
2416 * terminates). In either case we ensure that it's re-programmed
2417 */
2418 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2419 raw_spin_unlock(&cfs_b->lock);
2420 /* ensure cfs_b->lock is available while we wait */
2421 hrtimer_cancel(&cfs_b->period_timer);
2422
2423 raw_spin_lock(&cfs_b->lock);
2424 /* if someone else restarted the timer then we're done */
2425 if (cfs_b->timer_active)
2426 return;
2427 }
2428
2429 cfs_b->timer_active = 1;
2430 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2431}
2432
2433static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2434{
2435 hrtimer_cancel(&cfs_b->period_timer);
2436 hrtimer_cancel(&cfs_b->slack_timer);
2437}
2438
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002439static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002440{
2441 struct cfs_rq *cfs_rq;
2442
2443 for_each_leaf_cfs_rq(rq, cfs_rq) {
2444 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2445
2446 if (!cfs_rq->runtime_enabled)
2447 continue;
2448
2449 /*
2450 * clock_task is not advancing so we just need to make sure
2451 * there's some valid quota amount
2452 */
2453 cfs_rq->runtime_remaining = cfs_b->quota;
2454 if (cfs_rq_throttled(cfs_rq))
2455 unthrottle_cfs_rq(cfs_rq);
2456 }
2457}
2458
2459#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02002460static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
2461{
2462 return rq_of(cfs_rq)->clock_task;
2463}
2464
2465static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
2466 unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002467static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2468static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002469static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002470
2471static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2472{
2473 return 0;
2474}
Paul Turner64660c82011-07-21 09:43:36 -07002475
2476static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2477{
2478 return 0;
2479}
2480
2481static inline int throttled_lb_pair(struct task_group *tg,
2482 int src_cpu, int dest_cpu)
2483{
2484 return 0;
2485}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002486
2487void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2488
2489#ifdef CONFIG_FAIR_GROUP_SCHED
2490static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002491#endif
2492
Peter Zijlstra029632f2011-10-25 10:00:11 +02002493static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2494{
2495 return NULL;
2496}
2497static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002498static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002499
2500#endif /* CONFIG_CFS_BANDWIDTH */
2501
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002502/**************************************************
2503 * CFS operations on tasks:
2504 */
2505
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002506#ifdef CONFIG_SCHED_HRTICK
2507static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2508{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002509 struct sched_entity *se = &p->se;
2510 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2511
2512 WARN_ON(task_rq(p) != rq);
2513
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002514 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002515 u64 slice = sched_slice(cfs_rq, se);
2516 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2517 s64 delta = slice - ran;
2518
2519 if (delta < 0) {
2520 if (rq->curr == p)
2521 resched_task(p);
2522 return;
2523 }
2524
2525 /*
2526 * Don't schedule slices shorter than 10000ns, that just
2527 * doesn't make sense. Rely on vruntime for fairness.
2528 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002529 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002530 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002531
Peter Zijlstra31656512008-07-18 18:01:23 +02002532 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002533 }
2534}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002535
2536/*
2537 * called from enqueue/dequeue and updates the hrtick when the
2538 * current task is from our class and nr_running is low enough
2539 * to matter.
2540 */
2541static void hrtick_update(struct rq *rq)
2542{
2543 struct task_struct *curr = rq->curr;
2544
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002545 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002546 return;
2547
2548 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2549 hrtick_start_fair(rq, curr);
2550}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302551#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002552static inline void
2553hrtick_start_fair(struct rq *rq, struct task_struct *p)
2554{
2555}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002556
2557static inline void hrtick_update(struct rq *rq)
2558{
2559}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002560#endif
2561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002562/*
2563 * The enqueue_task method is called before nr_running is
2564 * increased. Here we update the fair scheduling stats and
2565 * then put the task into the rbtree:
2566 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002567static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002568enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002569{
2570 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002571 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002572
2573 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002574 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002575 break;
2576 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002577 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002578
2579 /*
2580 * end evaluation on encountering a throttled cfs_rq
2581 *
2582 * note: in the case of encountering a throttled cfs_rq we will
2583 * post the final h_nr_running increment below.
2584 */
2585 if (cfs_rq_throttled(cfs_rq))
2586 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002587 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002588
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002589 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002590 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002591
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002592 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002593 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002594 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002595
Paul Turner85dac902011-07-21 09:43:33 -07002596 if (cfs_rq_throttled(cfs_rq))
2597 break;
2598
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002599 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002600 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002601 }
2602
Ben Segall18bf2802012-10-04 12:51:20 +02002603 if (!se) {
2604 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002605 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002606 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002607 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002608}
2609
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002610static void set_next_buddy(struct sched_entity *se);
2611
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002612/*
2613 * The dequeue_task method is called before nr_running is
2614 * decreased. We remove the task from the rbtree and
2615 * update the fair scheduling stats:
2616 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002617static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002618{
2619 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002620 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002621 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002622
2623 for_each_sched_entity(se) {
2624 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002625 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002626
2627 /*
2628 * end evaluation on encountering a throttled cfs_rq
2629 *
2630 * note: in the case of encountering a throttled cfs_rq we will
2631 * post the final h_nr_running decrement below.
2632 */
2633 if (cfs_rq_throttled(cfs_rq))
2634 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002635 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002636
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002637 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002638 if (cfs_rq->load.weight) {
2639 /*
2640 * Bias pick_next to pick a task from this cfs_rq, as
2641 * p is sleeping when it is within its sched_slice.
2642 */
2643 if (task_sleep && parent_entity(se))
2644 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002645
2646 /* avoid re-evaluating load for this entity */
2647 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002648 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002649 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002650 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002651 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002652
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002653 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002654 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002655 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002656
Paul Turner85dac902011-07-21 09:43:33 -07002657 if (cfs_rq_throttled(cfs_rq))
2658 break;
2659
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002660 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002661 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002662 }
2663
Ben Segall18bf2802012-10-04 12:51:20 +02002664 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002665 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002666 update_rq_runnable_avg(rq, 1);
2667 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002668 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002669}
2670
Gregory Haskinse7693a32008-01-25 21:08:09 +01002671#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002672/* Used instead of source_load when we know the type == 0 */
2673static unsigned long weighted_cpuload(const int cpu)
2674{
2675 return cpu_rq(cpu)->load.weight;
2676}
2677
2678/*
2679 * Return a low guess at the load of a migration-source cpu weighted
2680 * according to the scheduling class and "nice" value.
2681 *
2682 * We want to under-estimate the load of migration sources, to
2683 * balance conservatively.
2684 */
2685static unsigned long source_load(int cpu, int type)
2686{
2687 struct rq *rq = cpu_rq(cpu);
2688 unsigned long total = weighted_cpuload(cpu);
2689
2690 if (type == 0 || !sched_feat(LB_BIAS))
2691 return total;
2692
2693 return min(rq->cpu_load[type-1], total);
2694}
2695
2696/*
2697 * Return a high guess at the load of a migration-target cpu weighted
2698 * according to the scheduling class and "nice" value.
2699 */
2700static unsigned long target_load(int cpu, int type)
2701{
2702 struct rq *rq = cpu_rq(cpu);
2703 unsigned long total = weighted_cpuload(cpu);
2704
2705 if (type == 0 || !sched_feat(LB_BIAS))
2706 return total;
2707
2708 return max(rq->cpu_load[type-1], total);
2709}
2710
2711static unsigned long power_of(int cpu)
2712{
2713 return cpu_rq(cpu)->cpu_power;
2714}
2715
2716static unsigned long cpu_avg_load_per_task(int cpu)
2717{
2718 struct rq *rq = cpu_rq(cpu);
2719 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2720
2721 if (nr_running)
2722 return rq->load.weight / nr_running;
2723
2724 return 0;
2725}
2726
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002727
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002728static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002729{
2730 struct sched_entity *se = &p->se;
2731 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002732 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002733
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002734#ifndef CONFIG_64BIT
2735 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002736
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002737 do {
2738 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2739 smp_rmb();
2740 min_vruntime = cfs_rq->min_vruntime;
2741 } while (min_vruntime != min_vruntime_copy);
2742#else
2743 min_vruntime = cfs_rq->min_vruntime;
2744#endif
2745
2746 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002747}
2748
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002749#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002750/*
2751 * effective_load() calculates the load change as seen from the root_task_group
2752 *
2753 * Adding load to a group doesn't make a group heavier, but can cause movement
2754 * of group shares between cpus. Assuming the shares were perfectly aligned one
2755 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002756 *
2757 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2758 * on this @cpu and results in a total addition (subtraction) of @wg to the
2759 * total group weight.
2760 *
2761 * Given a runqueue weight distribution (rw_i) we can compute a shares
2762 * distribution (s_i) using:
2763 *
2764 * s_i = rw_i / \Sum rw_j (1)
2765 *
2766 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2767 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2768 * shares distribution (s_i):
2769 *
2770 * rw_i = { 2, 4, 1, 0 }
2771 * s_i = { 2/7, 4/7, 1/7, 0 }
2772 *
2773 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2774 * task used to run on and the CPU the waker is running on), we need to
2775 * compute the effect of waking a task on either CPU and, in case of a sync
2776 * wakeup, compute the effect of the current task going to sleep.
2777 *
2778 * So for a change of @wl to the local @cpu with an overall group weight change
2779 * of @wl we can compute the new shares distribution (s'_i) using:
2780 *
2781 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2782 *
2783 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2784 * differences in waking a task to CPU 0. The additional task changes the
2785 * weight and shares distributions like:
2786 *
2787 * rw'_i = { 3, 4, 1, 0 }
2788 * s'_i = { 3/8, 4/8, 1/8, 0 }
2789 *
2790 * We can then compute the difference in effective weight by using:
2791 *
2792 * dw_i = S * (s'_i - s_i) (3)
2793 *
2794 * Where 'S' is the group weight as seen by its parent.
2795 *
2796 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2797 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2798 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002799 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002800static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002801{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002802 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002803
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002804 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002805 return wl;
2806
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002807 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002808 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002809
Paul Turner977dda72011-01-14 17:57:50 -08002810 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002811
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002812 /*
2813 * W = @wg + \Sum rw_j
2814 */
2815 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002816
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002817 /*
2818 * w = rw_i + @wl
2819 */
2820 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002821
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002822 /*
2823 * wl = S * s'_i; see (2)
2824 */
2825 if (W > 0 && w < W)
2826 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002827 else
2828 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002829
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002830 /*
2831 * Per the above, wl is the new se->load.weight value; since
2832 * those are clipped to [MIN_SHARES, ...) do so now. See
2833 * calc_cfs_shares().
2834 */
Paul Turner977dda72011-01-14 17:57:50 -08002835 if (wl < MIN_SHARES)
2836 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002837
2838 /*
2839 * wl = dw_i = S * (s'_i - s_i); see (3)
2840 */
Paul Turner977dda72011-01-14 17:57:50 -08002841 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002842
2843 /*
2844 * Recursively apply this logic to all parent groups to compute
2845 * the final effective load change on the root group. Since
2846 * only the @tg group gets extra weight, all parent groups can
2847 * only redistribute existing shares. @wl is the shift in shares
2848 * resulting from this level per the above.
2849 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002850 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002851 }
2852
2853 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002854}
2855#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002856
Peter Zijlstra83378262008-06-27 13:41:37 +02002857static inline unsigned long effective_load(struct task_group *tg, int cpu,
2858 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002859{
Peter Zijlstra83378262008-06-27 13:41:37 +02002860 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002861}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002862
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002863#endif
2864
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002865static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002866{
Paul Turnere37b6a72011-01-21 20:44:59 -08002867 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002868 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002869 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002870 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002871 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002872 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002873
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002874 idx = sd->wake_idx;
2875 this_cpu = smp_processor_id();
2876 prev_cpu = task_cpu(p);
2877 load = source_load(prev_cpu, idx);
2878 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002879
2880 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002881 * If sync wakeup then subtract the (maximum possible)
2882 * effect of the currently running task from the load
2883 * of the current CPU:
2884 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002885 if (sync) {
2886 tg = task_group(current);
2887 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002888
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002889 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002890 load += effective_load(tg, prev_cpu, 0, -weight);
2891 }
2892
2893 tg = task_group(p);
2894 weight = p->se.load.weight;
2895
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002896 /*
2897 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002898 * due to the sync cause above having dropped this_load to 0, we'll
2899 * always have an imbalance, but there's really nothing you can do
2900 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002901 *
2902 * Otherwise check if either cpus are near enough in load to allow this
2903 * task to be woken on this_cpu.
2904 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002905 if (this_load > 0) {
2906 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002907
2908 this_eff_load = 100;
2909 this_eff_load *= power_of(prev_cpu);
2910 this_eff_load *= this_load +
2911 effective_load(tg, this_cpu, weight, weight);
2912
2913 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2914 prev_eff_load *= power_of(this_cpu);
2915 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2916
2917 balanced = this_eff_load <= prev_eff_load;
2918 } else
2919 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002920
2921 /*
2922 * If the currently running task will sleep within
2923 * a reasonable amount of time then attract this newly
2924 * woken task:
2925 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002926 if (sync && balanced)
2927 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002928
Lucas De Marchi41acab82010-03-10 23:37:45 -03002929 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002930 tl_per_task = cpu_avg_load_per_task(this_cpu);
2931
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002932 if (balanced ||
2933 (this_load <= load &&
2934 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002935 /*
2936 * This domain has SD_WAKE_AFFINE and
2937 * p is cache cold in this domain, and
2938 * there is no bad imbalance.
2939 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002940 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002941 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002942
2943 return 1;
2944 }
2945 return 0;
2946}
2947
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002948/*
2949 * find_idlest_group finds and returns the least busy CPU group within the
2950 * domain.
2951 */
2952static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002953find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002954 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002955{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002956 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002957 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002958 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002959
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002960 do {
2961 unsigned long load, avg_load;
2962 int local_group;
2963 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002964
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002965 /* Skip over this group if it has no CPUs allowed */
2966 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002967 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002968 continue;
2969
2970 local_group = cpumask_test_cpu(this_cpu,
2971 sched_group_cpus(group));
2972
2973 /* Tally up the load of all CPUs in the group */
2974 avg_load = 0;
2975
2976 for_each_cpu(i, sched_group_cpus(group)) {
2977 /* Bias balancing toward cpus of our domain */
2978 if (local_group)
2979 load = source_load(i, load_idx);
2980 else
2981 load = target_load(i, load_idx);
2982
2983 avg_load += load;
2984 }
2985
2986 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002987 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002988
2989 if (local_group) {
2990 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002991 } else if (avg_load < min_load) {
2992 min_load = avg_load;
2993 idlest = group;
2994 }
2995 } while (group = group->next, group != sd->groups);
2996
2997 if (!idlest || 100*this_load < imbalance*min_load)
2998 return NULL;
2999 return idlest;
3000}
3001
3002/*
3003 * find_idlest_cpu - find the idlest cpu among the cpus in group.
3004 */
3005static int
3006find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
3007{
3008 unsigned long load, min_load = ULONG_MAX;
3009 int idlest = -1;
3010 int i;
3011
3012 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003013 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003014 load = weighted_cpuload(i);
3015
3016 if (load < min_load || (load == min_load && i == this_cpu)) {
3017 min_load = load;
3018 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003019 }
3020 }
3021
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003022 return idlest;
3023}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003024
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003025/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003026 * Try and locate an idle CPU in the sched_domain.
3027 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003028static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003029{
3030 int cpu = smp_processor_id();
3031 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003032 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07003033 struct sched_group *sg;
3034 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003035
3036 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003037 * If the task is going to be woken-up on this cpu and if it is
3038 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003039 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003040 if (target == cpu && idle_cpu(cpu))
3041 return cpu;
3042
3043 /*
3044 * If the task is going to be woken-up on the cpu where it previously
3045 * ran and if it is currently idle, then it the right target.
3046 */
3047 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003048 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003049
3050 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07003051 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003052 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01003053 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08003054 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07003055 sg = sd->groups;
3056 do {
3057 if (!cpumask_intersects(sched_group_cpus(sg),
3058 tsk_cpus_allowed(p)))
3059 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02003060
Linus Torvalds37407ea2012-09-16 12:29:43 -07003061 for_each_cpu(i, sched_group_cpus(sg)) {
3062 if (!idle_cpu(i))
3063 goto next;
3064 }
3065
3066 target = cpumask_first_and(sched_group_cpus(sg),
3067 tsk_cpus_allowed(p));
3068 goto done;
3069next:
3070 sg = sg->next;
3071 } while (sg != sd->groups);
3072 }
3073done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003074 return target;
3075}
3076
3077/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003078 * sched_balance_self: balance the current task (running on cpu) in domains
3079 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3080 * SD_BALANCE_EXEC.
3081 *
3082 * Balance, ie. select the least loaded group.
3083 *
3084 * Returns the target CPU number, or the same CPU if no balancing is needed.
3085 *
3086 * preempt must be disabled.
3087 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003088static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003089select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003090{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003091 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003092 int cpu = smp_processor_id();
3093 int prev_cpu = task_cpu(p);
3094 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003095 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003096 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003097
Peter Zijlstra29baa742012-04-23 12:11:21 +02003098 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003099 return prev_cpu;
3100
Peter Zijlstra0763a662009-09-14 19:37:39 +02003101 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003102 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003103 want_affine = 1;
3104 new_cpu = prev_cpu;
3105 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003106
Peter Zijlstradce840a2011-04-07 14:09:50 +02003107 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003108 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01003109 if (!(tmp->flags & SD_LOAD_BALANCE))
3110 continue;
3111
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003112 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003113 * If both cpu and prev_cpu are part of this domain,
3114 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003115 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003116 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3117 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3118 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003119 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003120 }
3121
Alex Shif03542a2012-07-26 08:55:34 +08003122 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003123 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003124 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003125
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003126 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003127 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003128 prev_cpu = cpu;
3129
3130 new_cpu = select_idle_sibling(p, prev_cpu);
3131 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003132 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003133
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003134 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003135 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003136 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003137 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003138
Peter Zijlstra0763a662009-09-14 19:37:39 +02003139 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003140 sd = sd->child;
3141 continue;
3142 }
3143
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003144 if (sd_flag & SD_BALANCE_WAKE)
3145 load_idx = sd->wake_idx;
3146
3147 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003148 if (!group) {
3149 sd = sd->child;
3150 continue;
3151 }
3152
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003153 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003154 if (new_cpu == -1 || new_cpu == cpu) {
3155 /* Now try balancing at a lower domain level of cpu */
3156 sd = sd->child;
3157 continue;
3158 }
3159
3160 /* Now try balancing at a lower domain level of new_cpu */
3161 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003162 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003163 sd = NULL;
3164 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003165 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003166 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003167 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003168 sd = tmp;
3169 }
3170 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003171 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003172unlock:
3173 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003174
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003175 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003176}
Paul Turner0a74bef2012-10-04 13:18:30 +02003177
3178/*
Paul Turnerf4e26b12012-10-04 13:18:32 +02003179 * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
3180 * removed when useful for applications beyond shares distribution (e.g.
3181 * load-balance).
3182 */
3183#ifdef CONFIG_FAIR_GROUP_SCHED
3184/*
Paul Turner0a74bef2012-10-04 13:18:30 +02003185 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3186 * cfs_rq_of(p) references at time of call are still valid and identify the
3187 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3188 * other assumptions, including the state of rq->lock, should be made.
3189 */
3190static void
3191migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3192{
Paul Turneraff3e492012-10-04 13:18:30 +02003193 struct sched_entity *se = &p->se;
3194 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3195
3196 /*
3197 * Load tracking: accumulate removed load so that it can be processed
3198 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3199 * to blocked load iff they have a positive decay-count. It can never
3200 * be negative here since on-rq tasks have decay-count == 0.
3201 */
3202 if (se->avg.decay_count) {
3203 se->avg.decay_count = -__synchronize_entity_decay(se);
3204 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3205 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003206}
Paul Turnerf4e26b12012-10-04 13:18:32 +02003207#endif
Gregory Haskinse7693a32008-01-25 21:08:09 +01003208#endif /* CONFIG_SMP */
3209
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003210static unsigned long
3211wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003212{
3213 unsigned long gran = sysctl_sched_wakeup_granularity;
3214
3215 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003216 * Since its curr running now, convert the gran from real-time
3217 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003218 *
3219 * By using 'se' instead of 'curr' we penalize light tasks, so
3220 * they get preempted easier. That is, if 'se' < 'curr' then
3221 * the resulting gran will be larger, therefore penalizing the
3222 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3223 * be smaller, again penalizing the lighter task.
3224 *
3225 * This is especially important for buddies when the leftmost
3226 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003227 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003228 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003229}
3230
3231/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003232 * Should 'se' preempt 'curr'.
3233 *
3234 * |s1
3235 * |s2
3236 * |s3
3237 * g
3238 * |<--->|c
3239 *
3240 * w(c, s1) = -1
3241 * w(c, s2) = 0
3242 * w(c, s3) = 1
3243 *
3244 */
3245static int
3246wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3247{
3248 s64 gran, vdiff = curr->vruntime - se->vruntime;
3249
3250 if (vdiff <= 0)
3251 return -1;
3252
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003253 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003254 if (vdiff > gran)
3255 return 1;
3256
3257 return 0;
3258}
3259
Peter Zijlstra02479092008-11-04 21:25:10 +01003260static void set_last_buddy(struct sched_entity *se)
3261{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003262 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3263 return;
3264
3265 for_each_sched_entity(se)
3266 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003267}
3268
3269static void set_next_buddy(struct sched_entity *se)
3270{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003271 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3272 return;
3273
3274 for_each_sched_entity(se)
3275 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003276}
3277
Rik van Rielac53db52011-02-01 09:51:03 -05003278static void set_skip_buddy(struct sched_entity *se)
3279{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003280 for_each_sched_entity(se)
3281 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003282}
3283
Peter Zijlstra464b7522008-10-24 11:06:15 +02003284/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003285 * Preempt the current task with a newly woken task if needed:
3286 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003287static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003288{
3289 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003290 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003291 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003292 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003293 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003294
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003295 if (unlikely(se == pse))
3296 return;
3297
Paul Turner5238cdd2011-07-21 09:43:37 -07003298 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003299 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003300 * unconditionally check_prempt_curr() after an enqueue (which may have
3301 * lead to a throttle). This both saves work and prevents false
3302 * next-buddy nomination below.
3303 */
3304 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3305 return;
3306
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003307 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003308 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003309 next_buddy_marked = 1;
3310 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003311
Bharata B Raoaec0a512008-08-28 14:42:49 +05303312 /*
3313 * We can come here with TIF_NEED_RESCHED already set from new task
3314 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003315 *
3316 * Note: this also catches the edge-case of curr being in a throttled
3317 * group (e.g. via set_curr_task), since update_curr() (in the
3318 * enqueue of curr) will have resulted in resched being set. This
3319 * prevents us from potentially nominating it as a false LAST_BUDDY
3320 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303321 */
3322 if (test_tsk_need_resched(curr))
3323 return;
3324
Darren Harta2f5c9a2011-02-22 13:04:33 -08003325 /* Idle tasks are by definition preempted by non-idle tasks. */
3326 if (unlikely(curr->policy == SCHED_IDLE) &&
3327 likely(p->policy != SCHED_IDLE))
3328 goto preempt;
3329
Ingo Molnar91c234b2007-10-15 17:00:18 +02003330 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003331 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3332 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003333 */
Ingo Molnar8ed92e512012-10-14 14:28:50 +02003334 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003335 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003336
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003337 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003338 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003339 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003340 if (wakeup_preempt_entity(se, pse) == 1) {
3341 /*
3342 * Bias pick_next to pick the sched entity that is
3343 * triggering this preemption.
3344 */
3345 if (!next_buddy_marked)
3346 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003347 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003348 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003349
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003350 return;
3351
3352preempt:
3353 resched_task(curr);
3354 /*
3355 * Only set the backward buddy when the current task is still
3356 * on the rq. This can happen when a wakeup gets interleaved
3357 * with schedule on the ->pre_schedule() or idle_balance()
3358 * point, either of which can * drop the rq lock.
3359 *
3360 * Also, during early boot the idle thread is in the fair class,
3361 * for obvious reasons its a bad idea to schedule back to it.
3362 */
3363 if (unlikely(!se->on_rq || curr == rq->idle))
3364 return;
3365
3366 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3367 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003368}
3369
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003370static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003371{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003372 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003373 struct cfs_rq *cfs_rq = &rq->cfs;
3374 struct sched_entity *se;
3375
Tim Blechmann36ace272009-11-24 11:55:45 +01003376 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003377 return NULL;
3378
3379 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003380 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003381 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003382 cfs_rq = group_cfs_rq(se);
3383 } while (cfs_rq);
3384
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003385 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003386 if (hrtick_enabled(rq))
3387 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003388
3389 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003390}
3391
3392/*
3393 * Account for a descheduled task:
3394 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003395static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003396{
3397 struct sched_entity *se = &prev->se;
3398 struct cfs_rq *cfs_rq;
3399
3400 for_each_sched_entity(se) {
3401 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003402 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003403 }
3404}
3405
Rik van Rielac53db52011-02-01 09:51:03 -05003406/*
3407 * sched_yield() is very simple
3408 *
3409 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3410 */
3411static void yield_task_fair(struct rq *rq)
3412{
3413 struct task_struct *curr = rq->curr;
3414 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3415 struct sched_entity *se = &curr->se;
3416
3417 /*
3418 * Are we the only task in the tree?
3419 */
3420 if (unlikely(rq->nr_running == 1))
3421 return;
3422
3423 clear_buddies(cfs_rq, se);
3424
3425 if (curr->policy != SCHED_BATCH) {
3426 update_rq_clock(rq);
3427 /*
3428 * Update run-time statistics of the 'current'.
3429 */
3430 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003431 /*
3432 * Tell update_rq_clock() that we've just updated,
3433 * so we don't do microscopic update in schedule()
3434 * and double the fastpath cost.
3435 */
3436 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003437 }
3438
3439 set_skip_buddy(se);
3440}
3441
Mike Galbraithd95f4122011-02-01 09:50:51 -05003442static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3443{
3444 struct sched_entity *se = &p->se;
3445
Paul Turner5238cdd2011-07-21 09:43:37 -07003446 /* throttled hierarchies are not runnable */
3447 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003448 return false;
3449
3450 /* Tell the scheduler that we'd really like pse to run next. */
3451 set_next_buddy(se);
3452
Mike Galbraithd95f4122011-02-01 09:50:51 -05003453 yield_task_fair(rq);
3454
3455 return true;
3456}
3457
Peter Williams681f3e62007-10-24 18:23:51 +02003458#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003459/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02003460 * Fair scheduling class load-balancing methods.
3461 *
3462 * BASICS
3463 *
3464 * The purpose of load-balancing is to achieve the same basic fairness the
3465 * per-cpu scheduler provides, namely provide a proportional amount of compute
3466 * time to each task. This is expressed in the following equation:
3467 *
3468 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
3469 *
3470 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
3471 * W_i,0 is defined as:
3472 *
3473 * W_i,0 = \Sum_j w_i,j (2)
3474 *
3475 * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight
3476 * is derived from the nice value as per prio_to_weight[].
3477 *
3478 * The weight average is an exponential decay average of the instantaneous
3479 * weight:
3480 *
3481 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
3482 *
3483 * P_i is the cpu power (or compute capacity) of cpu i, typically it is the
3484 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
3485 * can also include other factors [XXX].
3486 *
3487 * To achieve this balance we define a measure of imbalance which follows
3488 * directly from (1):
3489 *
3490 * imb_i,j = max{ avg(W/P), W_i/P_i } - min{ avg(W/P), W_j/P_j } (4)
3491 *
3492 * We them move tasks around to minimize the imbalance. In the continuous
3493 * function space it is obvious this converges, in the discrete case we get
3494 * a few fun cases generally called infeasible weight scenarios.
3495 *
3496 * [XXX expand on:
3497 * - infeasible weights;
3498 * - local vs global optima in the discrete case. ]
3499 *
3500 *
3501 * SCHED DOMAINS
3502 *
3503 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
3504 * for all i,j solution, we create a tree of cpus that follows the hardware
3505 * topology where each level pairs two lower groups (or better). This results
3506 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
3507 * tree to only the first of the previous level and we decrease the frequency
3508 * of load-balance at each level inv. proportional to the number of cpus in
3509 * the groups.
3510 *
3511 * This yields:
3512 *
3513 * log_2 n 1 n
3514 * \Sum { --- * --- * 2^i } = O(n) (5)
3515 * i = 0 2^i 2^i
3516 * `- size of each group
3517 * | | `- number of cpus doing load-balance
3518 * | `- freq
3519 * `- sum over all levels
3520 *
3521 * Coupled with a limit on how many tasks we can migrate every balance pass,
3522 * this makes (5) the runtime complexity of the balancer.
3523 *
3524 * An important property here is that each CPU is still (indirectly) connected
3525 * to every other cpu in at most O(log n) steps:
3526 *
3527 * The adjacency matrix of the resulting graph is given by:
3528 *
3529 * log_2 n
3530 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
3531 * k = 0
3532 *
3533 * And you'll find that:
3534 *
3535 * A^(log_2 n)_i,j != 0 for all i,j (7)
3536 *
3537 * Showing there's indeed a path between every cpu in at most O(log n) steps.
3538 * The task movement gives a factor of O(m), giving a convergence complexity
3539 * of:
3540 *
3541 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
3542 *
3543 *
3544 * WORK CONSERVING
3545 *
3546 * In order to avoid CPUs going idle while there's still work to do, new idle
3547 * balancing is more aggressive and has the newly idle cpu iterate up the domain
3548 * tree itself instead of relying on other CPUs to bring it work.
3549 *
3550 * This adds some complexity to both (5) and (8) but it reduces the total idle
3551 * time.
3552 *
3553 * [XXX more?]
3554 *
3555 *
3556 * CGROUPS
3557 *
3558 * Cgroups make a horror show out of (2), instead of a simple sum we get:
3559 *
3560 * s_k,i
3561 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
3562 * S_k
3563 *
3564 * Where
3565 *
3566 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
3567 *
3568 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
3569 *
3570 * The big problem is S_k, its a global sum needed to compute a local (W_i)
3571 * property.
3572 *
3573 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
3574 * rewrite all of this once again.]
3575 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003576
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003577static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3578
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003579#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003580#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303581#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003582
3583struct lb_env {
3584 struct sched_domain *sd;
3585
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003586 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303587 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003588
3589 int dst_cpu;
3590 struct rq *dst_rq;
3591
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303592 struct cpumask *dst_grpmask;
3593 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003594 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003595 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08003596 /* The set of CPUs under consideration for load-balancing */
3597 struct cpumask *cpus;
3598
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003599 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003600
3601 unsigned int loop;
3602 unsigned int loop_break;
3603 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003604};
3605
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003606/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003607 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003608 * Both runqueues must be locked.
3609 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003610static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003611{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003612 deactivate_task(env->src_rq, p, 0);
3613 set_task_cpu(p, env->dst_cpu);
3614 activate_task(env->dst_rq, p, 0);
3615 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003616}
3617
3618/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003619 * Is this task likely cache-hot:
3620 */
3621static int
3622task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3623{
3624 s64 delta;
3625
3626 if (p->sched_class != &fair_sched_class)
3627 return 0;
3628
3629 if (unlikely(p->policy == SCHED_IDLE))
3630 return 0;
3631
3632 /*
3633 * Buddy candidates are cache hot:
3634 */
3635 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3636 (&p->se == cfs_rq_of(&p->se)->next ||
3637 &p->se == cfs_rq_of(&p->se)->last))
3638 return 1;
3639
3640 if (sysctl_sched_migration_cost == -1)
3641 return 1;
3642 if (sysctl_sched_migration_cost == 0)
3643 return 0;
3644
3645 delta = now - p->se.exec_start;
3646
3647 return delta < (s64)sysctl_sched_migration_cost;
3648}
3649
3650/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003651 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3652 */
3653static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003654int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003655{
3656 int tsk_cache_hot = 0;
3657 /*
3658 * We do not migrate tasks that are:
3659 * 1) running (obviously), or
3660 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3661 * 3) are cache-hot on their current CPU.
3662 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003663 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303664 int new_dst_cpu;
3665
Lucas De Marchi41acab82010-03-10 23:37:45 -03003666 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303667
3668 /*
3669 * Remember if this task can be migrated to any other cpu in
3670 * our sched_group. We may want to revisit it if we couldn't
3671 * meet load balance goals by pulling other tasks on src_cpu.
3672 *
3673 * Also avoid computing new_dst_cpu if we have already computed
3674 * one in current iteration.
3675 */
3676 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3677 return 0;
3678
3679 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3680 tsk_cpus_allowed(p));
3681 if (new_dst_cpu < nr_cpu_ids) {
3682 env->flags |= LBF_SOME_PINNED;
3683 env->new_dst_cpu = new_dst_cpu;
3684 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003685 return 0;
3686 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303687
3688 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003689 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003690
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003691 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003692 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003693 return 0;
3694 }
3695
3696 /*
3697 * Aggressive migration if:
3698 * 1) task is cache cold, or
3699 * 2) too many balance attempts have failed.
3700 */
3701
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003702 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003703 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003704 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003705#ifdef CONFIG_SCHEDSTATS
3706 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003707 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003708 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003709 }
3710#endif
3711 return 1;
3712 }
3713
3714 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003715 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003716 return 0;
3717 }
3718 return 1;
3719}
3720
Peter Zijlstra897c3952009-12-17 17:45:42 +01003721/*
3722 * move_one_task tries to move exactly one task from busiest to this_rq, as
3723 * part of active balancing operations within "domain".
3724 * Returns 1 if successful and 0 otherwise.
3725 *
3726 * Called with both runqueues locked.
3727 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003728static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003729{
3730 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003731
Peter Zijlstra367456c2012-02-20 21:49:09 +01003732 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3733 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3734 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003735
Peter Zijlstra367456c2012-02-20 21:49:09 +01003736 if (!can_migrate_task(p, env))
3737 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003738
Peter Zijlstra367456c2012-02-20 21:49:09 +01003739 move_task(p, env);
3740 /*
3741 * Right now, this is only the second place move_task()
3742 * is called, so we can safely collect move_task()
3743 * stats here rather than inside move_task().
3744 */
3745 schedstat_inc(env->sd, lb_gained[env->idle]);
3746 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003747 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003748 return 0;
3749}
3750
Peter Zijlstra367456c2012-02-20 21:49:09 +01003751static unsigned long task_h_load(struct task_struct *p);
3752
Peter Zijlstraeb953082012-04-17 13:38:40 +02003753static const unsigned int sched_nr_migrate_break = 32;
3754
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003755/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003756 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003757 * this_rq, as part of a balancing operation within domain "sd".
3758 * Returns 1 if successful and 0 otherwise.
3759 *
3760 * Called with both runqueues locked.
3761 */
3762static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003763{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003764 struct list_head *tasks = &env->src_rq->cfs_tasks;
3765 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003766 unsigned long load;
3767 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003768
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003769 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003770 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003771
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003772 while (!list_empty(tasks)) {
3773 p = list_first_entry(tasks, struct task_struct, se.group_node);
3774
Peter Zijlstra367456c2012-02-20 21:49:09 +01003775 env->loop++;
3776 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003777 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003778 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003779
3780 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003781 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003782 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003783 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003784 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003785 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003786
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003787 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003788 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003789
Peter Zijlstra367456c2012-02-20 21:49:09 +01003790 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003791
Peter Zijlstraeb953082012-04-17 13:38:40 +02003792 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003793 goto next;
3794
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003795 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003796 goto next;
3797
3798 if (!can_migrate_task(p, env))
3799 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003800
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003801 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003802 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003803 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003804
3805#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003806 /*
3807 * NEWIDLE balancing is a source of latency, so preemptible
3808 * kernels will stop after the first task is pulled to minimize
3809 * the critical section.
3810 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003811 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003812 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003813#endif
3814
Peter Zijlstraee00e662009-12-17 17:25:20 +01003815 /*
3816 * We only want to steal up to the prescribed amount of
3817 * weighted load.
3818 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003819 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003820 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003821
Peter Zijlstra367456c2012-02-20 21:49:09 +01003822 continue;
3823next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003824 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003825 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003826
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003827 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003828 * Right now, this is one of only two places move_task() is called,
3829 * so we can safely collect move_task() stats here rather than
3830 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003831 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003832 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003833
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003834 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003835}
3836
Peter Zijlstra230059de2009-12-17 17:47:12 +01003837#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003838/*
3839 * update tg->load_weight by folding this cpu's load_avg
3840 */
Paul Turner48a16752012-10-04 13:18:31 +02003841static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003842{
Paul Turner48a16752012-10-04 13:18:31 +02003843 struct sched_entity *se = tg->se[cpu];
3844 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu];
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003845
Paul Turner48a16752012-10-04 13:18:31 +02003846 /* throttled entities do not contribute to load */
3847 if (throttled_hierarchy(cfs_rq))
3848 return;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003849
Paul Turneraff3e492012-10-04 13:18:30 +02003850 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003851
Paul Turner82958362012-10-04 13:18:31 +02003852 if (se) {
3853 update_entity_load_avg(se, 1);
3854 /*
3855 * We pivot on our runnable average having decayed to zero for
3856 * list removal. This generally implies that all our children
3857 * have also been removed (modulo rounding error or bandwidth
3858 * control); however, such cases are rare and we can fix these
3859 * at enqueue.
3860 *
3861 * TODO: fix up out-of-order children on enqueue.
3862 */
3863 if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
3864 list_del_leaf_cfs_rq(cfs_rq);
3865 } else {
Paul Turner48a16752012-10-04 13:18:31 +02003866 struct rq *rq = rq_of(cfs_rq);
Paul Turner82958362012-10-04 13:18:31 +02003867 update_rq_runnable_avg(rq, rq->nr_running);
3868 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003869}
3870
Paul Turner48a16752012-10-04 13:18:31 +02003871static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003872{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003873 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02003874 struct cfs_rq *cfs_rq;
3875 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003876
Paul Turner48a16752012-10-04 13:18:31 +02003877 raw_spin_lock_irqsave(&rq->lock, flags);
3878 update_rq_clock(rq);
Peter Zijlstra9763b672011-07-13 13:09:25 +02003879 /*
3880 * Iterates the task_group tree in a bottom up fashion, see
3881 * list_add_leaf_cfs_rq() for details.
3882 */
Paul Turner64660c82011-07-21 09:43:36 -07003883 for_each_leaf_cfs_rq(rq, cfs_rq) {
Paul Turner48a16752012-10-04 13:18:31 +02003884 /*
3885 * Note: We may want to consider periodically releasing
3886 * rq->lock about these updates so that creating many task
3887 * groups does not result in continually extending hold time.
3888 */
3889 __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003890 }
Paul Turner48a16752012-10-04 13:18:31 +02003891
3892 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003893}
3894
Peter Zijlstra9763b672011-07-13 13:09:25 +02003895/*
3896 * Compute the cpu's hierarchical load factor for each task group.
3897 * This needs to be done in a top-down fashion because the load of a child
3898 * group is a fraction of its parents load.
3899 */
3900static int tg_load_down(struct task_group *tg, void *data)
3901{
3902 unsigned long load;
3903 long cpu = (long)data;
3904
3905 if (!tg->parent) {
3906 load = cpu_rq(cpu)->load.weight;
3907 } else {
3908 load = tg->parent->cfs_rq[cpu]->h_load;
3909 load *= tg->se[cpu]->load.weight;
3910 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3911 }
3912
3913 tg->cfs_rq[cpu]->h_load = load;
3914
3915 return 0;
3916}
3917
3918static void update_h_load(long cpu)
3919{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003920 struct rq *rq = cpu_rq(cpu);
3921 unsigned long now = jiffies;
3922
3923 if (rq->h_load_throttle == now)
3924 return;
3925
3926 rq->h_load_throttle = now;
3927
Peter Zijlstra367456c2012-02-20 21:49:09 +01003928 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003929 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003930 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003931}
3932
Peter Zijlstra367456c2012-02-20 21:49:09 +01003933static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003934{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003935 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3936 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003937
Peter Zijlstra367456c2012-02-20 21:49:09 +01003938 load = p->se.load.weight;
3939 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003940
Peter Zijlstra367456c2012-02-20 21:49:09 +01003941 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003942}
3943#else
Paul Turner48a16752012-10-04 13:18:31 +02003944static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003945{
3946}
3947
Peter Zijlstra367456c2012-02-20 21:49:09 +01003948static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003949{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003950}
3951
3952static unsigned long task_h_load(struct task_struct *p)
3953{
3954 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003955}
3956#endif
3957
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003958/********** Helpers for find_busiest_group ************************/
3959/*
3960 * sd_lb_stats - Structure to store the statistics of a sched_domain
3961 * during load balancing.
3962 */
3963struct sd_lb_stats {
3964 struct sched_group *busiest; /* Busiest group in this sd */
3965 struct sched_group *this; /* Local group in this sd */
3966 unsigned long total_load; /* Total load of all groups in sd */
3967 unsigned long total_pwr; /* Total power of all groups in sd */
3968 unsigned long avg_load; /* Average load across all groups in sd */
3969
3970 /** Statistics of this group */
3971 unsigned long this_load;
3972 unsigned long this_load_per_task;
3973 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003974 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003975 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003976
3977 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003978 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003979 unsigned long max_load;
3980 unsigned long busiest_load_per_task;
3981 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003982 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003983 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003984 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003985
3986 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003987};
3988
3989/*
3990 * sg_lb_stats - stats of a sched_group required for load_balancing
3991 */
3992struct sg_lb_stats {
3993 unsigned long avg_load; /*Avg load across the CPUs of the group */
3994 unsigned long group_load; /* Total load over the CPUs of the group */
3995 unsigned long sum_nr_running; /* Nr tasks running in the group */
3996 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3997 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003998 unsigned long idle_cpus;
3999 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004000 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07004001 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004002};
4003
4004/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004005 * get_sd_load_idx - Obtain the load index for a given sched domain.
4006 * @sd: The sched_domain whose load_idx is to be obtained.
4007 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
4008 */
4009static inline int get_sd_load_idx(struct sched_domain *sd,
4010 enum cpu_idle_type idle)
4011{
4012 int load_idx;
4013
4014 switch (idle) {
4015 case CPU_NOT_IDLE:
4016 load_idx = sd->busy_idx;
4017 break;
4018
4019 case CPU_NEWLY_IDLE:
4020 load_idx = sd->newidle_idx;
4021 break;
4022 default:
4023 load_idx = sd->idle_idx;
4024 break;
4025 }
4026
4027 return load_idx;
4028}
4029
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004030unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
4031{
Nikhil Rao1399fa72011-05-18 10:09:39 -07004032 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004033}
4034
4035unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
4036{
4037 return default_scale_freq_power(sd, cpu);
4038}
4039
4040unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
4041{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004042 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004043 unsigned long smt_gain = sd->smt_gain;
4044
4045 smt_gain /= weight;
4046
4047 return smt_gain;
4048}
4049
4050unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
4051{
4052 return default_scale_smt_power(sd, cpu);
4053}
4054
4055unsigned long scale_rt_power(int cpu)
4056{
4057 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02004058 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004059
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02004060 /*
4061 * Since we're reading these variables without serialization make sure
4062 * we read them once before doing sanity checks on them.
4063 */
4064 age_stamp = ACCESS_ONCE(rq->age_stamp);
4065 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07004066
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02004067 total = sched_avg_period() + (rq->clock - age_stamp);
4068
4069 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07004070 /* Ensures that power won't end up being negative */
4071 available = 0;
4072 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02004073 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07004074 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004075
Nikhil Rao1399fa72011-05-18 10:09:39 -07004076 if (unlikely((s64)total < SCHED_POWER_SCALE))
4077 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004078
Nikhil Rao1399fa72011-05-18 10:09:39 -07004079 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004080
4081 return div_u64(available, total);
4082}
4083
4084static void update_cpu_power(struct sched_domain *sd, int cpu)
4085{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004086 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07004087 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004088 struct sched_group *sdg = sd->groups;
4089
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004090 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
4091 if (sched_feat(ARCH_POWER))
4092 power *= arch_scale_smt_power(sd, cpu);
4093 else
4094 power *= default_scale_smt_power(sd, cpu);
4095
Nikhil Rao1399fa72011-05-18 10:09:39 -07004096 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004097 }
4098
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004099 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004100
4101 if (sched_feat(ARCH_POWER))
4102 power *= arch_scale_freq_power(sd, cpu);
4103 else
4104 power *= default_scale_freq_power(sd, cpu);
4105
Nikhil Rao1399fa72011-05-18 10:09:39 -07004106 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004107
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004108 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004109 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004110
4111 if (!power)
4112 power = 1;
4113
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004114 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004115 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004116}
4117
Peter Zijlstra029632f2011-10-25 10:00:11 +02004118void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004119{
4120 struct sched_domain *child = sd->child;
4121 struct sched_group *group, *sdg = sd->groups;
4122 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01004123 unsigned long interval;
4124
4125 interval = msecs_to_jiffies(sd->balance_interval);
4126 interval = clamp(interval, 1UL, max_load_balance_interval);
4127 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004128
4129 if (!child) {
4130 update_cpu_power(sd, cpu);
4131 return;
4132 }
4133
4134 power = 0;
4135
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02004136 if (child->flags & SD_OVERLAP) {
4137 /*
4138 * SD_OVERLAP domains cannot assume that child groups
4139 * span the current group.
4140 */
4141
4142 for_each_cpu(cpu, sched_group_cpus(sdg))
4143 power += power_of(cpu);
4144 } else {
4145 /*
4146 * !SD_OVERLAP domains can assume that child groups
4147 * span the current group.
4148 */
4149
4150 group = child->groups;
4151 do {
4152 power += group->sgp->power;
4153 group = group->next;
4154 } while (group != child->groups);
4155 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004156
Peter Zijlstrac3decf02012-05-31 12:05:32 +02004157 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004158}
4159
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004160/*
4161 * Try and fix up capacity for tiny siblings, this is needed when
4162 * things like SD_ASYM_PACKING need f_b_g to select another sibling
4163 * which on its own isn't powerful enough.
4164 *
4165 * See update_sd_pick_busiest() and check_asym_packing().
4166 */
4167static inline int
4168fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
4169{
4170 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07004171 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004172 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02004173 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004174 return 0;
4175
4176 /*
4177 * If ~90% of the cpu_power is still there, we're good.
4178 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004179 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004180 return 1;
4181
4182 return 0;
4183}
4184
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004185/**
4186 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004187 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004188 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004189 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004190 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004191 * @balance: Should we balance.
4192 * @sgs: variable to hold the statistics for this group.
4193 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004194static inline void update_sg_lb_stats(struct lb_env *env,
4195 struct sched_group *group, int load_idx,
Michael Wangb94031302012-07-12 16:10:13 +08004196 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004197{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004198 unsigned long nr_running, max_nr_running, min_nr_running;
4199 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004200 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004201 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004202 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004203
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004204 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004205 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004206
4207 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004208 max_cpu_load = 0;
4209 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004210 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004211 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004212
Michael Wangb94031302012-07-12 16:10:13 +08004213 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004214 struct rq *rq = cpu_rq(i);
4215
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004216 nr_running = rq->nr_running;
4217
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004218 /* Bias balancing toward cpus of our domain */
4219 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004220 if (idle_cpu(i) && !first_idle_cpu &&
4221 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004222 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004223 balance_cpu = i;
4224 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004225
4226 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004227 } else {
4228 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004229 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004230 max_cpu_load = load;
4231 if (min_cpu_load > load)
4232 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004233
4234 if (nr_running > max_nr_running)
4235 max_nr_running = nr_running;
4236 if (min_nr_running > nr_running)
4237 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004238 }
4239
4240 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004241 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004242 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004243 if (idle_cpu(i))
4244 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004245 }
4246
4247 /*
4248 * First idle cpu or the first cpu(busiest) in this sched group
4249 * is eligible for doing load balancing at this and above
4250 * domains. In the newly idle case, we will allow all the cpu's
4251 * to do the newly idle load balance.
4252 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004253 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004254 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004255 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004256 *balance = 0;
4257 return;
4258 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004259 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004260 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004261 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004262 }
4263
4264 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004265 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004266
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004267 /*
4268 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004269 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004270 *
4271 * APZ: with cgroup the avg task weight can vary wildly and
4272 * might not be a suitable number - should we keep a
4273 * normalized nr_running number somewhere that negates
4274 * the hierarchy?
4275 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004276 if (sgs->sum_nr_running)
4277 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004278
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004279 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4280 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004281 sgs->group_imb = 1;
4282
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004283 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004284 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004285 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004286 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004287 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004288
4289 if (sgs->group_capacity > sgs->sum_nr_running)
4290 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004291}
4292
4293/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004294 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004295 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004296 * @sds: sched_domain statistics
4297 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004298 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004299 *
4300 * Determine if @sg is a busier group than the previously selected
4301 * busiest group.
4302 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004303static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004304 struct sd_lb_stats *sds,
4305 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004306 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004307{
4308 if (sgs->avg_load <= sds->max_load)
4309 return false;
4310
4311 if (sgs->sum_nr_running > sgs->group_capacity)
4312 return true;
4313
4314 if (sgs->group_imb)
4315 return true;
4316
4317 /*
4318 * ASYM_PACKING needs to move all the work to the lowest
4319 * numbered CPUs in the group, therefore mark all groups
4320 * higher than ourself as busy.
4321 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004322 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4323 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004324 if (!sds->busiest)
4325 return true;
4326
4327 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4328 return true;
4329 }
4330
4331 return false;
4332}
4333
4334/**
Hui Kang461819a2011-10-11 23:00:59 -04004335 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004336 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004337 * @balance: Should we balance.
4338 * @sds: variable to hold the statistics for this sched_domain.
4339 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004340static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004341 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004342{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004343 struct sched_domain *child = env->sd->child;
4344 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004345 struct sg_lb_stats sgs;
4346 int load_idx, prefer_sibling = 0;
4347
4348 if (child && child->flags & SD_PREFER_SIBLING)
4349 prefer_sibling = 1;
4350
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004351 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004352
4353 do {
4354 int local_group;
4355
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004356 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004357 memset(&sgs, 0, sizeof(sgs));
Michael Wangb94031302012-07-12 16:10:13 +08004358 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004359
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004360 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004361 return;
4362
4363 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004364 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004365
4366 /*
4367 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004368 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004369 * and move all the excess tasks away. We lower the capacity
4370 * of a group only if the local group has the capacity to fit
4371 * these excess tasks, i.e. nr_running < group_capacity. The
4372 * extra check prevents the case where you always pull from the
4373 * heaviest group when it is already under-utilized (possible
4374 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004375 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004376 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004377 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4378
4379 if (local_group) {
4380 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004381 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004382 sds->this_nr_running = sgs.sum_nr_running;
4383 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004384 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004385 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004386 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004387 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004388 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004389 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004390 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004391 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004392 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004393 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004394 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004395 sds->group_imb = sgs.group_imb;
4396 }
4397
Michael Neuling532cb4c2010-06-08 14:57:02 +10004398 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004399 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004400}
4401
Michael Neuling532cb4c2010-06-08 14:57:02 +10004402/**
4403 * check_asym_packing - Check to see if the group is packed into the
4404 * sched doman.
4405 *
4406 * This is primarily intended to used at the sibling level. Some
4407 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4408 * case of POWER7, it can move to lower SMT modes only when higher
4409 * threads are idle. When in lower SMT modes, the threads will
4410 * perform better since they share less core resources. Hence when we
4411 * have idle threads, we want them to be the higher ones.
4412 *
4413 * This packing function is run on idle threads. It checks to see if
4414 * the busiest CPU in this domain (core in the P7 case) has a higher
4415 * CPU number than the packing function is being run on. Here we are
4416 * assuming lower CPU number will be equivalent to lower a SMT thread
4417 * number.
4418 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004419 * Returns 1 when packing is required and a task should be moved to
4420 * this CPU. The amount of the imbalance is returned in *imbalance.
4421 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004422 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004423 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004424 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004425static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004426{
4427 int busiest_cpu;
4428
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004429 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004430 return 0;
4431
4432 if (!sds->busiest)
4433 return 0;
4434
4435 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004436 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004437 return 0;
4438
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004439 env->imbalance = DIV_ROUND_CLOSEST(
4440 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4441
Michael Neuling532cb4c2010-06-08 14:57:02 +10004442 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004443}
4444
4445/**
4446 * fix_small_imbalance - Calculate the minor imbalance that exists
4447 * amongst the groups of a sched_domain, during
4448 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004449 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004450 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004451 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004452static inline
4453void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004454{
4455 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4456 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004457 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004458
4459 if (sds->this_nr_running) {
4460 sds->this_load_per_task /= sds->this_nr_running;
4461 if (sds->busiest_load_per_task >
4462 sds->this_load_per_task)
4463 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004464 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004465 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004466 cpu_avg_load_per_task(env->dst_cpu);
4467 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004468
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004469 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004470 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004471 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004472
4473 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4474 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004475 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004476 return;
4477 }
4478
4479 /*
4480 * OK, we don't have enough imbalance to justify moving tasks,
4481 * however we may be able to increase total CPU power used by
4482 * moving them.
4483 */
4484
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004485 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004486 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004487 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004488 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004489 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004490
4491 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004492 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004493 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004494 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004495 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004496 min(sds->busiest_load_per_task, sds->max_load - tmp);
4497
4498 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004499 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004500 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004501 tmp = (sds->max_load * sds->busiest->sgp->power) /
4502 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004503 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004504 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004505 sds->this->sgp->power;
4506 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004507 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004508 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004509
4510 /* Move if we gain throughput */
4511 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004512 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004513}
4514
4515/**
4516 * calculate_imbalance - Calculate the amount of imbalance present within the
4517 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004518 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004519 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004520 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004521static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004522{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004523 unsigned long max_pull, load_above_capacity = ~0UL;
4524
4525 sds->busiest_load_per_task /= sds->busiest_nr_running;
4526 if (sds->group_imb) {
4527 sds->busiest_load_per_task =
4528 min(sds->busiest_load_per_task, sds->avg_load);
4529 }
4530
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004531 /*
4532 * In the presence of smp nice balancing, certain scenarios can have
4533 * max load less than avg load(as we skip the groups at or below
4534 * its cpu_power, while calculating max_load..)
4535 */
4536 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004537 env->imbalance = 0;
4538 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004539 }
4540
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004541 if (!sds->group_imb) {
4542 /*
4543 * Don't want to pull so many tasks that a group would go idle.
4544 */
4545 load_above_capacity = (sds->busiest_nr_running -
4546 sds->busiest_group_capacity);
4547
Nikhil Rao1399fa72011-05-18 10:09:39 -07004548 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004549
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004550 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004551 }
4552
4553 /*
4554 * We're trying to get all the cpus to the average_load, so we don't
4555 * want to push ourselves above the average load, nor do we wish to
4556 * reduce the max loaded cpu below the average load. At the same time,
4557 * we also don't want to reduce the group load below the group capacity
4558 * (so that we can implement power-savings policies etc). Thus we look
4559 * for the minimum possible imbalance.
4560 * Be careful of negative numbers as they'll appear as very large values
4561 * with unsigned longs.
4562 */
4563 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004564
4565 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004566 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004567 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004568 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004569
4570 /*
4571 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004572 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004573 * a think about bumping its value to force at least one task to be
4574 * moved
4575 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004576 if (env->imbalance < sds->busiest_load_per_task)
4577 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004578
4579}
Nikhil Raofab47622010-10-15 13:12:29 -07004580
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004581/******* find_busiest_group() helpers end here *********************/
4582
4583/**
4584 * find_busiest_group - Returns the busiest group within the sched_domain
4585 * if there is an imbalance. If there isn't an imbalance, and
4586 * the user has opted for power-savings, it returns a group whose
4587 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4588 * such a group exists.
4589 *
4590 * Also calculates the amount of weighted load which should be moved
4591 * to restore balance.
4592 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004593 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004594 * @balance: Pointer to a variable indicating if this_cpu
4595 * is the appropriate cpu to perform load balancing at this_level.
4596 *
4597 * Returns: - the busiest group if imbalance exists.
4598 * - If no imbalance and user has opted for power-savings balance,
4599 * return the least loaded group whose CPUs can be
4600 * put to idle by rebalancing its tasks onto our group.
4601 */
4602static struct sched_group *
Michael Wangb94031302012-07-12 16:10:13 +08004603find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004604{
4605 struct sd_lb_stats sds;
4606
4607 memset(&sds, 0, sizeof(sds));
4608
4609 /*
4610 * Compute the various statistics relavent for load balancing at
4611 * this level.
4612 */
Michael Wangb94031302012-07-12 16:10:13 +08004613 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004614
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004615 /*
4616 * this_cpu is not the appropriate cpu to perform load balancing at
4617 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004618 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004619 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004620 goto ret;
4621
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004622 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4623 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004624 return sds.busiest;
4625
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004626 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004627 if (!sds.busiest || sds.busiest_nr_running == 0)
4628 goto out_balanced;
4629
Nikhil Rao1399fa72011-05-18 10:09:39 -07004630 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004631
Peter Zijlstra866ab432011-02-21 18:56:47 +01004632 /*
4633 * If the busiest group is imbalanced the below checks don't
4634 * work because they assumes all things are equal, which typically
4635 * isn't true due to cpus_allowed constraints and the like.
4636 */
4637 if (sds.group_imb)
4638 goto force_balance;
4639
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004640 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004641 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004642 !sds.busiest_has_capacity)
4643 goto force_balance;
4644
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004645 /*
4646 * If the local group is more busy than the selected busiest group
4647 * don't try and pull any tasks.
4648 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004649 if (sds.this_load >= sds.max_load)
4650 goto out_balanced;
4651
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004652 /*
4653 * Don't pull any tasks if this group is already above the domain
4654 * average load.
4655 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004656 if (sds.this_load >= sds.avg_load)
4657 goto out_balanced;
4658
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004659 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004660 /*
4661 * This cpu is idle. If the busiest group load doesn't
4662 * have more tasks than the number of available cpu's and
4663 * there is no imbalance between this and busiest group
4664 * wrt to idle cpu's, it is balanced.
4665 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004666 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004667 sds.busiest_nr_running <= sds.busiest_group_weight)
4668 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004669 } else {
4670 /*
4671 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4672 * imbalance_pct to be conservative.
4673 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004674 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004675 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004676 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004677
Nikhil Raofab47622010-10-15 13:12:29 -07004678force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004679 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004680 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004681 return sds.busiest;
4682
4683out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004684ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004685 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004686 return NULL;
4687}
4688
4689/*
4690 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4691 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004692static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004693 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004694{
4695 struct rq *busiest = NULL, *rq;
4696 unsigned long max_load = 0;
4697 int i;
4698
4699 for_each_cpu(i, sched_group_cpus(group)) {
4700 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004701 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4702 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004703 unsigned long wl;
4704
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004705 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004706 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004707
Michael Wangb94031302012-07-12 16:10:13 +08004708 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004709 continue;
4710
4711 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004712 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004713
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004714 /*
4715 * When comparing with imbalance, use weighted_cpuload()
4716 * which is not scaled with the cpu power.
4717 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004718 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004719 continue;
4720
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004721 /*
4722 * For the load comparisons with the other cpu's, consider
4723 * the weighted_cpuload() scaled with the cpu power, so that
4724 * the load can be moved away from the cpu that is potentially
4725 * running at a lower capacity.
4726 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004727 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004728
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004729 if (wl > max_load) {
4730 max_load = wl;
4731 busiest = rq;
4732 }
4733 }
4734
4735 return busiest;
4736}
4737
4738/*
4739 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4740 * so long as it is large enough.
4741 */
4742#define MAX_PINNED_INTERVAL 512
4743
4744/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004745DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004746
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004747static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004748{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004749 struct sched_domain *sd = env->sd;
4750
4751 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004752
4753 /*
4754 * ASYM_PACKING needs to force migrate tasks from busy but
4755 * higher numbered CPUs in order to pack all tasks in the
4756 * lowest numbered CPUs.
4757 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004758 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004759 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004760 }
4761
4762 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4763}
4764
Tejun Heo969c7922010-05-06 18:49:21 +02004765static int active_load_balance_cpu_stop(void *data);
4766
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004767/*
4768 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4769 * tasks if there is an imbalance.
4770 */
4771static int load_balance(int this_cpu, struct rq *this_rq,
4772 struct sched_domain *sd, enum cpu_idle_type idle,
4773 int *balance)
4774{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304775 int ld_moved, cur_ld_moved, active_balance = 0;
4776 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004777 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004778 struct rq *busiest;
4779 unsigned long flags;
4780 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4781
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004782 struct lb_env env = {
4783 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004784 .dst_cpu = this_cpu,
4785 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304786 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004787 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004788 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08004789 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004790 };
4791
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004792 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304793 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004794
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004795 schedstat_inc(sd, lb_count[idle]);
4796
4797redo:
Michael Wangb94031302012-07-12 16:10:13 +08004798 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004799
4800 if (*balance == 0)
4801 goto out_balanced;
4802
4803 if (!group) {
4804 schedstat_inc(sd, lb_nobusyg[idle]);
4805 goto out_balanced;
4806 }
4807
Michael Wangb94031302012-07-12 16:10:13 +08004808 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004809 if (!busiest) {
4810 schedstat_inc(sd, lb_nobusyq[idle]);
4811 goto out_balanced;
4812 }
4813
Michael Wang78feefc2012-08-06 16:41:59 +08004814 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004815
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004816 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004817
4818 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304819 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004820 if (busiest->nr_running > 1) {
4821 /*
4822 * Attempt to move tasks. If find_busiest_group has found
4823 * an imbalance but busiest->nr_running <= 1, the group is
4824 * still unbalanced. ld_moved simply stays zero, so it is
4825 * correctly treated as an imbalance.
4826 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004827 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004828 env.src_cpu = busiest->cpu;
4829 env.src_rq = busiest;
4830 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004831
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004832 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004833more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004834 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004835 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304836
4837 /*
4838 * cur_ld_moved - load moved in current iteration
4839 * ld_moved - cumulative load moved across iterations
4840 */
4841 cur_ld_moved = move_tasks(&env);
4842 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004843 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004844 local_irq_restore(flags);
4845
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004846 if (env.flags & LBF_NEED_BREAK) {
4847 env.flags &= ~LBF_NEED_BREAK;
4848 goto more_balance;
4849 }
4850
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004851 /*
4852 * some other cpu did the load balance for us.
4853 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304854 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4855 resched_cpu(env.dst_cpu);
4856
4857 /*
4858 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4859 * us and move them to an alternate dst_cpu in our sched_group
4860 * where they can run. The upper limit on how many times we
4861 * iterate on same src_cpu is dependent on number of cpus in our
4862 * sched_group.
4863 *
4864 * This changes load balance semantics a bit on who can move
4865 * load to a given_cpu. In addition to the given_cpu itself
4866 * (or a ilb_cpu acting on its behalf where given_cpu is
4867 * nohz-idle), we now have balance_cpu in a position to move
4868 * load to given_cpu. In rare situations, this may cause
4869 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4870 * _independently_ and at _same_ time to move some load to
4871 * given_cpu) causing exceess load to be moved to given_cpu.
4872 * This however should not happen so much in practice and
4873 * moreover subsequent load balance cycles should correct the
4874 * excess load moved.
4875 */
4876 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4877 lb_iterations++ < max_lb_iterations) {
4878
Michael Wang78feefc2012-08-06 16:41:59 +08004879 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304880 env.dst_cpu = env.new_dst_cpu;
4881 env.flags &= ~LBF_SOME_PINNED;
4882 env.loop = 0;
4883 env.loop_break = sched_nr_migrate_break;
4884 /*
4885 * Go back to "more_balance" rather than "redo" since we
4886 * need to continue with same src_cpu.
4887 */
4888 goto more_balance;
4889 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004890
4891 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004892 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004893 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304894 if (!cpumask_empty(cpus)) {
4895 env.loop = 0;
4896 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004897 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304898 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004899 goto out_balanced;
4900 }
4901 }
4902
4903 if (!ld_moved) {
4904 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004905 /*
4906 * Increment the failure counter only on periodic balance.
4907 * We do not want newidle balance, which can be very
4908 * frequent, pollute the failure counter causing
4909 * excessive cache_hot migrations and active balances.
4910 */
4911 if (idle != CPU_NEWLY_IDLE)
4912 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004913
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004914 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004915 raw_spin_lock_irqsave(&busiest->lock, flags);
4916
Tejun Heo969c7922010-05-06 18:49:21 +02004917 /* don't kick the active_load_balance_cpu_stop,
4918 * if the curr task on busiest cpu can't be
4919 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004920 */
4921 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004922 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004923 raw_spin_unlock_irqrestore(&busiest->lock,
4924 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004925 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004926 goto out_one_pinned;
4927 }
4928
Tejun Heo969c7922010-05-06 18:49:21 +02004929 /*
4930 * ->active_balance synchronizes accesses to
4931 * ->active_balance_work. Once set, it's cleared
4932 * only after active load balance is finished.
4933 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004934 if (!busiest->active_balance) {
4935 busiest->active_balance = 1;
4936 busiest->push_cpu = this_cpu;
4937 active_balance = 1;
4938 }
4939 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004940
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004941 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004942 stop_one_cpu_nowait(cpu_of(busiest),
4943 active_load_balance_cpu_stop, busiest,
4944 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004945 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004946
4947 /*
4948 * We've kicked active balancing, reset the failure
4949 * counter.
4950 */
4951 sd->nr_balance_failed = sd->cache_nice_tries+1;
4952 }
4953 } else
4954 sd->nr_balance_failed = 0;
4955
4956 if (likely(!active_balance)) {
4957 /* We were unbalanced, so reset the balancing interval */
4958 sd->balance_interval = sd->min_interval;
4959 } else {
4960 /*
4961 * If we've begun active balancing, start to back off. This
4962 * case may not be covered by the all_pinned logic if there
4963 * is only 1 task on the busy runqueue (because we don't call
4964 * move_tasks).
4965 */
4966 if (sd->balance_interval < sd->max_interval)
4967 sd->balance_interval *= 2;
4968 }
4969
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004970 goto out;
4971
4972out_balanced:
4973 schedstat_inc(sd, lb_balanced[idle]);
4974
4975 sd->nr_balance_failed = 0;
4976
4977out_one_pinned:
4978 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004979 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004980 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004981 (sd->balance_interval < sd->max_interval))
4982 sd->balance_interval *= 2;
4983
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004984 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004985out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004986 return ld_moved;
4987}
4988
4989/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004990 * idle_balance is called by schedule() if this_cpu is about to become
4991 * idle. Attempts to pull tasks from other CPUs.
4992 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004993void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004994{
4995 struct sched_domain *sd;
4996 int pulled_task = 0;
4997 unsigned long next_balance = jiffies + HZ;
4998
4999 this_rq->idle_stamp = this_rq->clock;
5000
5001 if (this_rq->avg_idle < sysctl_sched_migration_cost)
5002 return;
5003
Ben Segall18bf2802012-10-04 12:51:20 +02005004 update_rq_runnable_avg(this_rq, 1);
5005
Peter Zijlstraf492e122009-12-23 15:29:42 +01005006 /*
5007 * Drop the rq->lock, but keep IRQ/preempt disabled.
5008 */
5009 raw_spin_unlock(&this_rq->lock);
5010
Paul Turner48a16752012-10-04 13:18:31 +02005011 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005012 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005013 for_each_domain(this_cpu, sd) {
5014 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01005015 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005016
5017 if (!(sd->flags & SD_LOAD_BALANCE))
5018 continue;
5019
Peter Zijlstraf492e122009-12-23 15:29:42 +01005020 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005021 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01005022 pulled_task = load_balance(this_cpu, this_rq,
5023 sd, CPU_NEWLY_IDLE, &balance);
5024 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005025
5026 interval = msecs_to_jiffies(sd->balance_interval);
5027 if (time_after(next_balance, sd->last_balance + interval))
5028 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08005029 if (pulled_task) {
5030 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005031 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08005032 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005033 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005034 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01005035
5036 raw_spin_lock(&this_rq->lock);
5037
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005038 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
5039 /*
5040 * We are going idle. next_balance may be set based on
5041 * a busy processor. So reset next_balance.
5042 */
5043 this_rq->next_balance = next_balance;
5044 }
5045}
5046
5047/*
Tejun Heo969c7922010-05-06 18:49:21 +02005048 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
5049 * running tasks off the busiest CPU onto idle CPUs. It requires at
5050 * least 1 task to be running on each physical CPU where possible, and
5051 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005052 */
Tejun Heo969c7922010-05-06 18:49:21 +02005053static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005054{
Tejun Heo969c7922010-05-06 18:49:21 +02005055 struct rq *busiest_rq = data;
5056 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005057 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02005058 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005059 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02005060
5061 raw_spin_lock_irq(&busiest_rq->lock);
5062
5063 /* make sure the requested cpu hasn't gone down in the meantime */
5064 if (unlikely(busiest_cpu != smp_processor_id() ||
5065 !busiest_rq->active_balance))
5066 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005067
5068 /* Is there any task to move? */
5069 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02005070 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005071
5072 /*
5073 * This condition is "impossible", if it occurs
5074 * we need to fix it. Originally reported by
5075 * Bjorn Helgaas on a 128-cpu setup.
5076 */
5077 BUG_ON(busiest_rq == target_rq);
5078
5079 /* move a task from busiest_rq to target_rq */
5080 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005081
5082 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02005083 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005084 for_each_domain(target_cpu, sd) {
5085 if ((sd->flags & SD_LOAD_BALANCE) &&
5086 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
5087 break;
5088 }
5089
5090 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005091 struct lb_env env = {
5092 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005093 .dst_cpu = target_cpu,
5094 .dst_rq = target_rq,
5095 .src_cpu = busiest_rq->cpu,
5096 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005097 .idle = CPU_IDLE,
5098 };
5099
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005100 schedstat_inc(sd, alb_count);
5101
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005102 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005103 schedstat_inc(sd, alb_pushed);
5104 else
5105 schedstat_inc(sd, alb_failed);
5106 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005107 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005108 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02005109out_unlock:
5110 busiest_rq->active_balance = 0;
5111 raw_spin_unlock_irq(&busiest_rq->lock);
5112 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005113}
5114
5115#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005116/*
5117 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005118 * - When one of the busy CPUs notice that there may be an idle rebalancing
5119 * needed, they will kick the idle load balancer, which then does idle
5120 * load balancing for all the idle CPUs.
5121 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005122static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005123 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005124 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005125 unsigned long next_balance; /* in jiffy units */
5126} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005127
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01005128static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005129{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005130 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005131
Suresh Siddha786d6dc72011-12-01 17:07:35 -08005132 if (ilb < nr_cpu_ids && idle_cpu(ilb))
5133 return ilb;
5134
5135 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005136}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005137
5138/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005139 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
5140 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
5141 * CPU (if there is one).
5142 */
5143static void nohz_balancer_kick(int cpu)
5144{
5145 int ilb_cpu;
5146
5147 nohz.next_balance++;
5148
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005149 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005150
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005151 if (ilb_cpu >= nr_cpu_ids)
5152 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005153
Suresh Siddhacd490c52011-12-06 11:26:34 -08005154 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08005155 return;
5156 /*
5157 * Use smp_send_reschedule() instead of resched_cpu().
5158 * This way we generate a sched IPI on the target cpu which
5159 * is idle. And the softirq performing nohz idle load balance
5160 * will be run before returning from the IPI.
5161 */
5162 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005163 return;
5164}
5165
Alex Shic1cc0172012-09-10 15:10:58 +08005166static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08005167{
5168 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
5169 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
5170 atomic_dec(&nohz.nr_cpus);
5171 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
5172 }
5173}
5174
Suresh Siddha69e1e812011-12-01 17:07:33 -08005175static inline void set_cpu_sd_state_busy(void)
5176{
5177 struct sched_domain *sd;
5178 int cpu = smp_processor_id();
5179
5180 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5181 return;
5182 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
5183
5184 rcu_read_lock();
5185 for_each_domain(cpu, sd)
5186 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
5187 rcu_read_unlock();
5188}
5189
5190void set_cpu_sd_state_idle(void)
5191{
5192 struct sched_domain *sd;
5193 int cpu = smp_processor_id();
5194
5195 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5196 return;
5197 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5198
5199 rcu_read_lock();
5200 for_each_domain(cpu, sd)
5201 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5202 rcu_read_unlock();
5203}
5204
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005205/*
Alex Shic1cc0172012-09-10 15:10:58 +08005206 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005207 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005208 */
Alex Shic1cc0172012-09-10 15:10:58 +08005209void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005210{
Suresh Siddha71325962012-01-19 18:28:57 -08005211 /*
5212 * If this cpu is going down, then nothing needs to be done.
5213 */
5214 if (!cpu_active(cpu))
5215 return;
5216
Alex Shic1cc0172012-09-10 15:10:58 +08005217 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5218 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005219
Alex Shic1cc0172012-09-10 15:10:58 +08005220 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5221 atomic_inc(&nohz.nr_cpus);
5222 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005223}
Suresh Siddha71325962012-01-19 18:28:57 -08005224
5225static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5226 unsigned long action, void *hcpu)
5227{
5228 switch (action & ~CPU_TASKS_FROZEN) {
5229 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005230 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005231 return NOTIFY_OK;
5232 default:
5233 return NOTIFY_DONE;
5234 }
5235}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005236#endif
5237
5238static DEFINE_SPINLOCK(balancing);
5239
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005240/*
5241 * Scale the max load_balance interval with the number of CPUs in the system.
5242 * This trades load-balance latency on larger machines for less cross talk.
5243 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005244void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005245{
5246 max_load_balance_interval = HZ*num_online_cpus()/10;
5247}
5248
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005249/*
5250 * It checks each scheduling domain to see if it is due to be balanced,
5251 * and initiates a balancing operation if so.
5252 *
5253 * Balancing parameters are set up in arch_init_sched_domains.
5254 */
5255static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5256{
5257 int balance = 1;
5258 struct rq *rq = cpu_rq(cpu);
5259 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005260 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005261 /* Earliest time when we have to do rebalance again */
5262 unsigned long next_balance = jiffies + 60*HZ;
5263 int update_next_balance = 0;
5264 int need_serialize;
5265
Paul Turner48a16752012-10-04 13:18:31 +02005266 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005267
Peter Zijlstradce840a2011-04-07 14:09:50 +02005268 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005269 for_each_domain(cpu, sd) {
5270 if (!(sd->flags & SD_LOAD_BALANCE))
5271 continue;
5272
5273 interval = sd->balance_interval;
5274 if (idle != CPU_IDLE)
5275 interval *= sd->busy_factor;
5276
5277 /* scale ms to jiffies */
5278 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005279 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005280
5281 need_serialize = sd->flags & SD_SERIALIZE;
5282
5283 if (need_serialize) {
5284 if (!spin_trylock(&balancing))
5285 goto out;
5286 }
5287
5288 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5289 if (load_balance(cpu, rq, sd, idle, &balance)) {
5290 /*
5291 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005292 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005293 */
5294 idle = CPU_NOT_IDLE;
5295 }
5296 sd->last_balance = jiffies;
5297 }
5298 if (need_serialize)
5299 spin_unlock(&balancing);
5300out:
5301 if (time_after(next_balance, sd->last_balance + interval)) {
5302 next_balance = sd->last_balance + interval;
5303 update_next_balance = 1;
5304 }
5305
5306 /*
5307 * Stop the load balance at this level. There is another
5308 * CPU in our sched group which is doing load balancing more
5309 * actively.
5310 */
5311 if (!balance)
5312 break;
5313 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005314 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005315
5316 /*
5317 * next_balance will be updated only when there is a need.
5318 * When the cpu is attached to null domain for ex, it will not be
5319 * updated.
5320 */
5321 if (likely(update_next_balance))
5322 rq->next_balance = next_balance;
5323}
5324
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005325#ifdef CONFIG_NO_HZ
5326/*
5327 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5328 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5329 */
5330static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5331{
5332 struct rq *this_rq = cpu_rq(this_cpu);
5333 struct rq *rq;
5334 int balance_cpu;
5335
Suresh Siddha1c792db2011-12-01 17:07:32 -08005336 if (idle != CPU_IDLE ||
5337 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5338 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005339
5340 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005341 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005342 continue;
5343
5344 /*
5345 * If this cpu gets work to do, stop the load balancing
5346 * work being done for other cpus. Next load
5347 * balancing owner will pick it up.
5348 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005349 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005350 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005351
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005352 rq = cpu_rq(balance_cpu);
5353
5354 raw_spin_lock_irq(&rq->lock);
5355 update_rq_clock(rq);
5356 update_idle_cpu_load(rq);
5357 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005358
5359 rebalance_domains(balance_cpu, CPU_IDLE);
5360
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005361 if (time_after(this_rq->next_balance, rq->next_balance))
5362 this_rq->next_balance = rq->next_balance;
5363 }
5364 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005365end:
5366 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005367}
5368
5369/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005370 * Current heuristic for kicking the idle load balancer in the presence
5371 * of an idle cpu is the system.
5372 * - This rq has more than one task.
5373 * - At any scheduler domain level, this cpu's scheduler group has multiple
5374 * busy cpu's exceeding the group's power.
5375 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5376 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005377 */
5378static inline int nohz_kick_needed(struct rq *rq, int cpu)
5379{
5380 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005381 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005382
Suresh Siddha1c792db2011-12-01 17:07:32 -08005383 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005384 return 0;
5385
Suresh Siddha1c792db2011-12-01 17:07:32 -08005386 /*
5387 * We may be recently in ticked or tickless idle mode. At the first
5388 * busy tick after returning from idle, we will update the busy stats.
5389 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005390 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005391 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005392
5393 /*
5394 * None are in tickless mode and hence no need for NOHZ idle load
5395 * balancing.
5396 */
5397 if (likely(!atomic_read(&nohz.nr_cpus)))
5398 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005399
5400 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005401 return 0;
5402
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005403 if (rq->nr_running >= 2)
5404 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005405
Peter Zijlstra067491b2011-12-07 14:32:08 +01005406 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005407 for_each_domain(cpu, sd) {
5408 struct sched_group *sg = sd->groups;
5409 struct sched_group_power *sgp = sg->sgp;
5410 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005411
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005412 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005413 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005414
5415 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5416 && (cpumask_first_and(nohz.idle_cpus_mask,
5417 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005418 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005419
5420 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5421 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005422 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005423 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005424 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005425
5426need_kick_unlock:
5427 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005428need_kick:
5429 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005430}
5431#else
5432static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5433#endif
5434
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005435/*
5436 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005437 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005438 */
5439static void run_rebalance_domains(struct softirq_action *h)
5440{
5441 int this_cpu = smp_processor_id();
5442 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005443 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005444 CPU_IDLE : CPU_NOT_IDLE;
5445
5446 rebalance_domains(this_cpu, idle);
5447
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005448 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005449 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005450 * balancing on behalf of the other idle cpus whose ticks are
5451 * stopped.
5452 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005453 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005454}
5455
5456static inline int on_null_domain(int cpu)
5457{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005458 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005459}
5460
5461/*
5462 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005463 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005464void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005465{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005466 /* Don't need to rebalance while attached to NULL domain */
5467 if (time_after_eq(jiffies, rq->next_balance) &&
5468 likely(!on_null_domain(cpu)))
5469 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005470#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005471 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005472 nohz_balancer_kick(cpu);
5473#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005474}
5475
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005476static void rq_online_fair(struct rq *rq)
5477{
5478 update_sysctl();
5479}
5480
5481static void rq_offline_fair(struct rq *rq)
5482{
5483 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005484
5485 /* Ensure any throttled groups are reachable by pick_next_task */
5486 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005487}
5488
Dhaval Giani55e12e52008-06-24 23:39:43 +05305489#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005490
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005491/*
5492 * scheduler tick hitting a task of our scheduling class:
5493 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005494static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005495{
5496 struct cfs_rq *cfs_rq;
5497 struct sched_entity *se = &curr->se;
5498
5499 for_each_sched_entity(se) {
5500 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005501 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005502 }
Ben Segall18bf2802012-10-04 12:51:20 +02005503
5504 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005505}
5506
5507/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005508 * called on fork with the child task as argument from the parent's context
5509 * - child not yet on the tasklist
5510 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005511 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005512static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005513{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005514 struct cfs_rq *cfs_rq;
5515 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005516 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005517 struct rq *rq = this_rq();
5518 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005519
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005520 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005521
Peter Zijlstra861d0342010-08-19 13:31:43 +02005522 update_rq_clock(rq);
5523
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005524 cfs_rq = task_cfs_rq(current);
5525 curr = cfs_rq->curr;
5526
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005527 if (unlikely(task_cpu(p) != this_cpu)) {
5528 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005529 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005530 rcu_read_unlock();
5531 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005532
Ting Yang7109c442007-08-28 12:53:24 +02005533 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005534
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005535 if (curr)
5536 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005537 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005538
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005539 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005540 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005541 * Upon rescheduling, sched_class::put_prev_task() will place
5542 * 'current' within the tree based on its new key value.
5543 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005544 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305545 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005546 }
5547
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005548 se->vruntime -= cfs_rq->min_vruntime;
5549
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005550 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005551}
5552
Steven Rostedtcb469842008-01-25 21:08:22 +01005553/*
5554 * Priority of the task has changed. Check to see if we preempt
5555 * the current task.
5556 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005557static void
5558prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005559{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005560 if (!p->se.on_rq)
5561 return;
5562
Steven Rostedtcb469842008-01-25 21:08:22 +01005563 /*
5564 * Reschedule if we are currently running on this runqueue and
5565 * our priority decreased, or if we are not currently running on
5566 * this runqueue and our priority is higher than the current's
5567 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005568 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005569 if (p->prio > oldprio)
5570 resched_task(rq->curr);
5571 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005572 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005573}
5574
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005575static void switched_from_fair(struct rq *rq, struct task_struct *p)
5576{
5577 struct sched_entity *se = &p->se;
5578 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5579
5580 /*
5581 * Ensure the task's vruntime is normalized, so that when its
5582 * switched back to the fair class the enqueue_entity(.flags=0) will
5583 * do the right thing.
5584 *
5585 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5586 * have normalized the vruntime, if it was !on_rq, then only when
5587 * the task is sleeping will it still have non-normalized vruntime.
5588 */
5589 if (!se->on_rq && p->state != TASK_RUNNING) {
5590 /*
5591 * Fix up our vruntime so that the current sleep doesn't
5592 * cause 'unlimited' sleep bonus.
5593 */
5594 place_entity(cfs_rq, se, 0);
5595 se->vruntime -= cfs_rq->min_vruntime;
5596 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005597
5598#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5599 /*
5600 * Remove our load from contribution when we leave sched_fair
5601 * and ensure we don't carry in an old decay_count if we
5602 * switch back.
5603 */
5604 if (p->se.avg.decay_count) {
5605 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5606 __synchronize_entity_decay(&p->se);
5607 subtract_blocked_load_contrib(cfs_rq,
5608 p->se.avg.load_avg_contrib);
5609 }
5610#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005611}
5612
Steven Rostedtcb469842008-01-25 21:08:22 +01005613/*
5614 * We switched to the sched_fair class.
5615 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005616static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005617{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005618 if (!p->se.on_rq)
5619 return;
5620
Steven Rostedtcb469842008-01-25 21:08:22 +01005621 /*
5622 * We were most likely switched from sched_rt, so
5623 * kick off the schedule if running, otherwise just see
5624 * if we can still preempt the current task.
5625 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005626 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005627 resched_task(rq->curr);
5628 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005629 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005630}
5631
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005632/* Account for a task changing its policy or group.
5633 *
5634 * This routine is mostly called to set cfs_rq->curr field when a task
5635 * migrates between groups/classes.
5636 */
5637static void set_curr_task_fair(struct rq *rq)
5638{
5639 struct sched_entity *se = &rq->curr->se;
5640
Paul Turnerec12cb72011-07-21 09:43:30 -07005641 for_each_sched_entity(se) {
5642 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5643
5644 set_next_entity(cfs_rq, se);
5645 /* ensure bandwidth has been allocated on our new cfs_rq */
5646 account_cfs_rq_runtime(cfs_rq, 0);
5647 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005648}
5649
Peter Zijlstra029632f2011-10-25 10:00:11 +02005650void init_cfs_rq(struct cfs_rq *cfs_rq)
5651{
5652 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005653 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5654#ifndef CONFIG_64BIT
5655 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5656#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005657#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5658 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005659 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005660#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005661}
5662
Peter Zijlstra810b3812008-02-29 15:21:01 -05005663#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005664static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005665{
Paul Turneraff3e492012-10-04 13:18:30 +02005666 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005667 /*
5668 * If the task was not on the rq at the time of this cgroup movement
5669 * it must have been asleep, sleeping tasks keep their ->vruntime
5670 * absolute on their old rq until wakeup (needed for the fair sleeper
5671 * bonus in place_entity()).
5672 *
5673 * If it was on the rq, we've just 'preempted' it, which does convert
5674 * ->vruntime to a relative base.
5675 *
5676 * Make sure both cases convert their relative position when migrating
5677 * to another cgroup's rq. This does somewhat interfere with the
5678 * fair sleeper stuff for the first placement, but who cares.
5679 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005680 /*
5681 * When !on_rq, vruntime of the task has usually NOT been normalized.
5682 * But there are some cases where it has already been normalized:
5683 *
5684 * - Moving a forked child which is waiting for being woken up by
5685 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005686 * - Moving a task which has been woken up by try_to_wake_up() and
5687 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005688 *
5689 * To prevent boost or penalty in the new cfs_rq caused by delta
5690 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5691 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005692 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005693 on_rq = 1;
5694
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005695 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005696 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5697 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005698 if (!on_rq) {
5699 cfs_rq = cfs_rq_of(&p->se);
5700 p->se.vruntime += cfs_rq->min_vruntime;
5701#ifdef CONFIG_SMP
5702 /*
5703 * migrate_task_rq_fair() will have removed our previous
5704 * contribution, but we must synchronize for ongoing future
5705 * decay.
5706 */
5707 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5708 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5709#endif
5710 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005711}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005712
5713void free_fair_sched_group(struct task_group *tg)
5714{
5715 int i;
5716
5717 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5718
5719 for_each_possible_cpu(i) {
5720 if (tg->cfs_rq)
5721 kfree(tg->cfs_rq[i]);
5722 if (tg->se)
5723 kfree(tg->se[i]);
5724 }
5725
5726 kfree(tg->cfs_rq);
5727 kfree(tg->se);
5728}
5729
5730int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5731{
5732 struct cfs_rq *cfs_rq;
5733 struct sched_entity *se;
5734 int i;
5735
5736 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5737 if (!tg->cfs_rq)
5738 goto err;
5739 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5740 if (!tg->se)
5741 goto err;
5742
5743 tg->shares = NICE_0_LOAD;
5744
5745 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5746
5747 for_each_possible_cpu(i) {
5748 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5749 GFP_KERNEL, cpu_to_node(i));
5750 if (!cfs_rq)
5751 goto err;
5752
5753 se = kzalloc_node(sizeof(struct sched_entity),
5754 GFP_KERNEL, cpu_to_node(i));
5755 if (!se)
5756 goto err_free_rq;
5757
5758 init_cfs_rq(cfs_rq);
5759 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5760 }
5761
5762 return 1;
5763
5764err_free_rq:
5765 kfree(cfs_rq);
5766err:
5767 return 0;
5768}
5769
5770void unregister_fair_sched_group(struct task_group *tg, int cpu)
5771{
5772 struct rq *rq = cpu_rq(cpu);
5773 unsigned long flags;
5774
5775 /*
5776 * Only empty task groups can be destroyed; so we can speculatively
5777 * check on_list without danger of it being re-added.
5778 */
5779 if (!tg->cfs_rq[cpu]->on_list)
5780 return;
5781
5782 raw_spin_lock_irqsave(&rq->lock, flags);
5783 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5784 raw_spin_unlock_irqrestore(&rq->lock, flags);
5785}
5786
5787void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5788 struct sched_entity *se, int cpu,
5789 struct sched_entity *parent)
5790{
5791 struct rq *rq = cpu_rq(cpu);
5792
5793 cfs_rq->tg = tg;
5794 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005795 init_cfs_rq_runtime(cfs_rq);
5796
5797 tg->cfs_rq[cpu] = cfs_rq;
5798 tg->se[cpu] = se;
5799
5800 /* se could be NULL for root_task_group */
5801 if (!se)
5802 return;
5803
5804 if (!parent)
5805 se->cfs_rq = &rq->cfs;
5806 else
5807 se->cfs_rq = parent->my_q;
5808
5809 se->my_q = cfs_rq;
5810 update_load_set(&se->load, 0);
5811 se->parent = parent;
5812}
5813
5814static DEFINE_MUTEX(shares_mutex);
5815
5816int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5817{
5818 int i;
5819 unsigned long flags;
5820
5821 /*
5822 * We can't change the weight of the root cgroup.
5823 */
5824 if (!tg->se[0])
5825 return -EINVAL;
5826
5827 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5828
5829 mutex_lock(&shares_mutex);
5830 if (tg->shares == shares)
5831 goto done;
5832
5833 tg->shares = shares;
5834 for_each_possible_cpu(i) {
5835 struct rq *rq = cpu_rq(i);
5836 struct sched_entity *se;
5837
5838 se = tg->se[i];
5839 /* Propagate contribution to hierarchy */
5840 raw_spin_lock_irqsave(&rq->lock, flags);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08005841 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005842 update_cfs_shares(group_cfs_rq(se));
5843 raw_spin_unlock_irqrestore(&rq->lock, flags);
5844 }
5845
5846done:
5847 mutex_unlock(&shares_mutex);
5848 return 0;
5849}
5850#else /* CONFIG_FAIR_GROUP_SCHED */
5851
5852void free_fair_sched_group(struct task_group *tg) { }
5853
5854int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5855{
5856 return 1;
5857}
5858
5859void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5860
5861#endif /* CONFIG_FAIR_GROUP_SCHED */
5862
Peter Zijlstra810b3812008-02-29 15:21:01 -05005863
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005864static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005865{
5866 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005867 unsigned int rr_interval = 0;
5868
5869 /*
5870 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5871 * idle runqueue:
5872 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005873 if (rq->cfs.load.weight)
5874 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005875
5876 return rr_interval;
5877}
5878
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005879/*
5880 * All the scheduling class methods:
5881 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005882const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005883 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005884 .enqueue_task = enqueue_task_fair,
5885 .dequeue_task = dequeue_task_fair,
5886 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005887 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005888
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005889 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005890
5891 .pick_next_task = pick_next_task_fair,
5892 .put_prev_task = put_prev_task_fair,
5893
Peter Williams681f3e62007-10-24 18:23:51 +02005894#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005895 .select_task_rq = select_task_rq_fair,
Paul Turnerf4e26b12012-10-04 13:18:32 +02005896#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner0a74bef2012-10-04 13:18:30 +02005897 .migrate_task_rq = migrate_task_rq_fair,
Paul Turnerf4e26b12012-10-04 13:18:32 +02005898#endif
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005899 .rq_online = rq_online_fair,
5900 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005901
5902 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005903#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005904
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005905 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005906 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005907 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005908
5909 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005910 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005911 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005912
Peter Williams0d721ce2009-09-21 01:31:53 +00005913 .get_rr_interval = get_rr_interval_fair,
5914
Peter Zijlstra810b3812008-02-29 15:21:01 -05005915#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005916 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005917#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005918};
5919
5920#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005921void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005922{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005923 struct cfs_rq *cfs_rq;
5924
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005925 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005926 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005927 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005928 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005929}
5930#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005931
5932__init void init_sched_fair_class(void)
5933{
5934#ifdef CONFIG_SMP
5935 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5936
5937#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005938 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005939 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005940 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005941#endif
5942#endif /* SMP */
5943
5944}