blob: 6ecf455fd95bd214708831fbda34a92f5ece2c57 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Paul Turneraff3e492012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
263 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200264
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800265static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
266{
267 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800268 /*
269 * Ensure we either appear before our parent (if already
270 * enqueued) or force our parent to appear after us when it is
271 * enqueued. The fact that we always enqueue bottom-up
272 * reduces this to two cases.
273 */
274 if (cfs_rq->tg->parent &&
275 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
276 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800278 } else {
279 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
280 &rq_of(cfs_rq)->leaf_cfs_rq_list);
281 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282
283 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200284 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200285 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800286 }
287}
288
289static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (cfs_rq->on_list) {
292 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
293 cfs_rq->on_list = 0;
294 }
295}
296
Peter Zijlstrab7581492008-04-19 19:45:00 +0200297/* Iterate thr' all leaf cfs_rq's on a runqueue */
298#define for_each_leaf_cfs_rq(rq, cfs_rq) \
299 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
300
301/* Do the two (enqueued) entities belong to the same group ? */
302static inline int
303is_same_group(struct sched_entity *se, struct sched_entity *pse)
304{
305 if (se->cfs_rq == pse->cfs_rq)
306 return 1;
307
308 return 0;
309}
310
311static inline struct sched_entity *parent_entity(struct sched_entity *se)
312{
313 return se->parent;
314}
315
Peter Zijlstra464b7522008-10-24 11:06:15 +0200316/* return depth at which a sched entity is present in the hierarchy */
317static inline int depth_se(struct sched_entity *se)
318{
319 int depth = 0;
320
321 for_each_sched_entity(se)
322 depth++;
323
324 return depth;
325}
326
327static void
328find_matching_se(struct sched_entity **se, struct sched_entity **pse)
329{
330 int se_depth, pse_depth;
331
332 /*
333 * preemption test can be made between sibling entities who are in the
334 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
335 * both tasks until we find their ancestors who are siblings of common
336 * parent.
337 */
338
339 /* First walk up until both entities are at same depth */
340 se_depth = depth_se(*se);
341 pse_depth = depth_se(*pse);
342
343 while (se_depth > pse_depth) {
344 se_depth--;
345 *se = parent_entity(*se);
346 }
347
348 while (pse_depth > se_depth) {
349 pse_depth--;
350 *pse = parent_entity(*pse);
351 }
352
353 while (!is_same_group(*se, *pse)) {
354 *se = parent_entity(*se);
355 *pse = parent_entity(*pse);
356 }
357}
358
Peter Zijlstra8f488942009-07-24 12:25:30 +0200359#else /* !CONFIG_FAIR_GROUP_SCHED */
360
361static inline struct task_struct *task_of(struct sched_entity *se)
362{
363 return container_of(se, struct task_struct, se);
364}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365
366static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
367{
368 return container_of(cfs_rq, struct rq, cfs);
369}
370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200371#define entity_is_task(se) 1
372
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373#define for_each_sched_entity(se) \
374 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200379}
380
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
382{
383 struct task_struct *p = task_of(se);
384 struct rq *rq = task_rq(p);
385
386 return &rq->cfs;
387}
388
389/* runqueue "owned" by this group */
390static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
391{
392 return NULL;
393}
394
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800395static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
396{
397}
398
399static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
400{
401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403#define for_each_leaf_cfs_rq(rq, cfs_rq) \
404 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
405
406static inline int
407is_same_group(struct sched_entity *se, struct sched_entity *pse)
408{
409 return 1;
410}
411
412static inline struct sched_entity *parent_entity(struct sched_entity *se)
413{
414 return NULL;
415}
416
Peter Zijlstra464b7522008-10-24 11:06:15 +0200417static inline void
418find_matching_se(struct sched_entity **se, struct sched_entity **pse)
419{
420}
421
Peter Zijlstrab7581492008-04-19 19:45:00 +0200422#endif /* CONFIG_FAIR_GROUP_SCHED */
423
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700424static __always_inline
425void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427/**************************************************************
428 * Scheduling class tree data structure manipulation methods:
429 */
430
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200431static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200432{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200433 s64 delta = (s64)(vruntime - min_vruntime);
434 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435 min_vruntime = vruntime;
436
437 return min_vruntime;
438}
439
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200440static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200441{
442 s64 delta = (s64)(vruntime - min_vruntime);
443 if (delta < 0)
444 min_vruntime = vruntime;
445
446 return min_vruntime;
447}
448
Fabio Checconi54fdc582009-07-16 12:32:27 +0200449static inline int entity_before(struct sched_entity *a,
450 struct sched_entity *b)
451{
452 return (s64)(a->vruntime - b->vruntime) < 0;
453}
454
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200455static void update_min_vruntime(struct cfs_rq *cfs_rq)
456{
457 u64 vruntime = cfs_rq->min_vruntime;
458
459 if (cfs_rq->curr)
460 vruntime = cfs_rq->curr->vruntime;
461
462 if (cfs_rq->rb_leftmost) {
463 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
464 struct sched_entity,
465 run_node);
466
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100467 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200468 vruntime = se->vruntime;
469 else
470 vruntime = min_vruntime(vruntime, se->vruntime);
471 }
472
473 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200474#ifndef CONFIG_64BIT
475 smp_wmb();
476 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
477#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478}
479
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200480/*
481 * Enqueue an entity into the rb-tree:
482 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200483static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484{
485 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
486 struct rb_node *parent = NULL;
487 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488 int leftmost = 1;
489
490 /*
491 * Find the right place in the rbtree:
492 */
493 while (*link) {
494 parent = *link;
495 entry = rb_entry(parent, struct sched_entity, run_node);
496 /*
497 * We dont care about collisions. Nodes with
498 * the same key stay together.
499 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200500 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200501 link = &parent->rb_left;
502 } else {
503 link = &parent->rb_right;
504 leftmost = 0;
505 }
506 }
507
508 /*
509 * Maintain a cache of leftmost tree entries (it is frequently
510 * used):
511 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200512 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200513 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200514
515 rb_link_node(&se->run_node, parent, link);
516 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517}
518
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200519static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 if (cfs_rq->rb_leftmost == &se->run_node) {
522 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100523
524 next_node = rb_next(&se->run_node);
525 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200527
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200529}
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100533 struct rb_node *left = cfs_rq->rb_leftmost;
534
535 if (!left)
536 return NULL;
537
538 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200539}
540
Rik van Rielac53db52011-02-01 09:51:03 -0500541static struct sched_entity *__pick_next_entity(struct sched_entity *se)
542{
543 struct rb_node *next = rb_next(&se->run_node);
544
545 if (!next)
546 return NULL;
547
548 return rb_entry(next, struct sched_entity, run_node);
549}
550
551#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100554 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555
Balbir Singh70eee742008-02-22 13:25:53 +0530556 if (!last)
557 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100558
559 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200560}
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562/**************************************************************
563 * Scheduling class statistics methods:
564 */
565
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700567 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100568 loff_t *ppos)
569{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100571 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100572
573 if (ret || !write)
574 return ret;
575
576 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
577 sysctl_sched_min_granularity);
578
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#define WRT_SYSCTL(name) \
580 (normalized_sysctl_##name = sysctl_##name / (factor))
581 WRT_SYSCTL(sched_min_granularity);
582 WRT_SYSCTL(sched_latency);
583 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100584#undef WRT_SYSCTL
585
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586 return 0;
587}
588#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200589
590/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200592 */
593static inline unsigned long
594calc_delta_fair(unsigned long delta, struct sched_entity *se)
595{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200596 if (unlikely(se->load.weight != NICE_0_LOAD))
597 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200598
599 return delta;
600}
601
602/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603 * The idea is to set a period in which each task runs once.
604 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200605 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * this period because otherwise the slices get too small.
607 *
608 * p = (nr <= nl) ? l : l*nr/nl
609 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200610static u64 __sched_period(unsigned long nr_running)
611{
612 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100613 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200614
615 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100616 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200618 }
619
620 return period;
621}
622
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623/*
624 * We calculate the wall-time slice from the period by taking a part
625 * proportional to the weight.
626 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200629static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200630{
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200632
Mike Galbraith0a582442009-01-02 12:16:42 +0100633 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100634 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200635 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100636
637 cfs_rq = cfs_rq_of(se);
638 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200641 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100642
643 update_load_add(&lw, se->load.weight);
644 load = &lw;
645 }
646 slice = calc_delta_mine(slice, se->load.weight, load);
647 }
648 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200649}
650
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200652 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200658 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659}
660
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 Turner9d85f212012-10-04 13:18:29 +0200885#ifdef CONFIG_SMP
886/*
Paul Turner5b51f2f2012-10-04 13:18:32 +0200887 * We choose a half-life close to 1 scheduling period.
888 * Note: The tables below are dependent on this value.
889 */
890#define LOAD_AVG_PERIOD 32
891#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
892#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */
893
894/* Precomputed fixed inverse multiplies for multiplication by y^n */
895static const u32 runnable_avg_yN_inv[] = {
896 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
897 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
898 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
899 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
900 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
901 0x85aac367, 0x82cd8698,
902};
903
904/*
905 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
906 * over-estimates when re-combining.
907 */
908static const u32 runnable_avg_yN_sum[] = {
909 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
910 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
911 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
912};
913
914/*
Paul Turner9d85f212012-10-04 13:18:29 +0200915 * Approximate:
916 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
917 */
918static __always_inline u64 decay_load(u64 val, u64 n)
919{
Paul Turner5b51f2f2012-10-04 13:18:32 +0200920 unsigned int local_n;
921
922 if (!n)
923 return val;
924 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
925 return 0;
926
927 /* after bounds checking we can collapse to 32-bit */
928 local_n = n;
929
930 /*
931 * As y^PERIOD = 1/2, we can combine
932 * y^n = 1/2^(n/PERIOD) * k^(n%PERIOD)
933 * With a look-up table which covers k^n (n<PERIOD)
934 *
935 * To achieve constant time decay_load.
936 */
937 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
938 val >>= local_n / LOAD_AVG_PERIOD;
939 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +0200940 }
941
Paul Turner5b51f2f2012-10-04 13:18:32 +0200942 val *= runnable_avg_yN_inv[local_n];
943 /* We don't use SRR here since we always want to round down. */
944 return val >> 32;
945}
946
947/*
948 * For updates fully spanning n periods, the contribution to runnable
949 * average will be: \Sum 1024*y^n
950 *
951 * We can compute this reasonably efficiently by combining:
952 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
953 */
954static u32 __compute_runnable_contrib(u64 n)
955{
956 u32 contrib = 0;
957
958 if (likely(n <= LOAD_AVG_PERIOD))
959 return runnable_avg_yN_sum[n];
960 else if (unlikely(n >= LOAD_AVG_MAX_N))
961 return LOAD_AVG_MAX;
962
963 /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */
964 do {
965 contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */
966 contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD];
967
968 n -= LOAD_AVG_PERIOD;
969 } while (n > LOAD_AVG_PERIOD);
970
971 contrib = decay_load(contrib, n);
972 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +0200973}
974
975/*
976 * We can represent the historical contribution to runnable average as the
977 * coefficients of a geometric series. To do this we sub-divide our runnable
978 * history into segments of approximately 1ms (1024us); label the segment that
979 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
980 *
981 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
982 * p0 p1 p2
983 * (now) (~1ms ago) (~2ms ago)
984 *
985 * Let u_i denote the fraction of p_i that the entity was runnable.
986 *
987 * We then designate the fractions u_i as our co-efficients, yielding the
988 * following representation of historical load:
989 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
990 *
991 * We choose y based on the with of a reasonably scheduling period, fixing:
992 * y^32 = 0.5
993 *
994 * This means that the contribution to load ~32ms ago (u_32) will be weighted
995 * approximately half as much as the contribution to load within the last ms
996 * (u_0).
997 *
998 * When a period "rolls over" and we have new u_0`, multiplying the previous
999 * sum again by y is sufficient to update:
1000 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1001 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1002 */
1003static __always_inline int __update_entity_runnable_avg(u64 now,
1004 struct sched_avg *sa,
1005 int runnable)
1006{
Paul Turner5b51f2f2012-10-04 13:18:32 +02001007 u64 delta, periods;
1008 u32 runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02001009 int delta_w, decayed = 0;
1010
1011 delta = now - sa->last_runnable_update;
1012 /*
1013 * This should only happen when time goes backwards, which it
1014 * unfortunately does during sched clock init when we swap over to TSC.
1015 */
1016 if ((s64)delta < 0) {
1017 sa->last_runnable_update = now;
1018 return 0;
1019 }
1020
1021 /*
1022 * Use 1024ns as the unit of measurement since it's a reasonable
1023 * approximation of 1us and fast to compute.
1024 */
1025 delta >>= 10;
1026 if (!delta)
1027 return 0;
1028 sa->last_runnable_update = now;
1029
1030 /* delta_w is the amount already accumulated against our next period */
1031 delta_w = sa->runnable_avg_period % 1024;
1032 if (delta + delta_w >= 1024) {
1033 /* period roll-over */
1034 decayed = 1;
1035
1036 /*
1037 * Now that we know we're crossing a period boundary, figure
1038 * out how much from delta we need to complete the current
1039 * period and accrue it.
1040 */
1041 delta_w = 1024 - delta_w;
Paul Turner5b51f2f2012-10-04 13:18:32 +02001042 if (runnable)
1043 sa->runnable_avg_sum += delta_w;
1044 sa->runnable_avg_period += delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02001045
Paul Turner5b51f2f2012-10-04 13:18:32 +02001046 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02001047
Paul Turner5b51f2f2012-10-04 13:18:32 +02001048 /* Figure out how many additional periods this update spans */
1049 periods = delta / 1024;
1050 delta %= 1024;
1051
1052 sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum,
1053 periods + 1);
1054 sa->runnable_avg_period = decay_load(sa->runnable_avg_period,
1055 periods + 1);
1056
1057 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
1058 runnable_contrib = __compute_runnable_contrib(periods);
1059 if (runnable)
1060 sa->runnable_avg_sum += runnable_contrib;
1061 sa->runnable_avg_period += runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02001062 }
1063
1064 /* Remainder of delta accrued against u_0` */
1065 if (runnable)
1066 sa->runnable_avg_sum += delta;
1067 sa->runnable_avg_period += delta;
1068
1069 return decayed;
1070}
1071
Paul Turner9ee474f2012-10-04 13:18:30 +02001072/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02001073static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02001074{
1075 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1076 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1077
1078 decays -= se->avg.decay_count;
1079 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02001080 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02001081
1082 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1083 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02001084
1085 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02001086}
1087
Paul Turnerc566e8e2012-10-04 13:18:30 +02001088#ifdef CONFIG_FAIR_GROUP_SCHED
1089static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1090 int force_update)
1091{
1092 struct task_group *tg = cfs_rq->tg;
1093 s64 tg_contrib;
1094
1095 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
1096 tg_contrib -= cfs_rq->tg_load_contrib;
1097
1098 if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
1099 atomic64_add(tg_contrib, &tg->load_avg);
1100 cfs_rq->tg_load_contrib += tg_contrib;
1101 }
1102}
Paul Turner8165e142012-10-04 13:18:31 +02001103
Paul Turnerbb17f652012-10-04 13:18:31 +02001104/*
1105 * Aggregate cfs_rq runnable averages into an equivalent task_group
1106 * representation for computing load contributions.
1107 */
1108static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1109 struct cfs_rq *cfs_rq)
1110{
1111 struct task_group *tg = cfs_rq->tg;
1112 long contrib;
1113
1114 /* The fraction of a cpu used by this cfs_rq */
1115 contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
1116 sa->runnable_avg_period + 1);
1117 contrib -= cfs_rq->tg_runnable_contrib;
1118
1119 if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
1120 atomic_add(contrib, &tg->runnable_avg);
1121 cfs_rq->tg_runnable_contrib += contrib;
1122 }
1123}
1124
Paul Turner8165e142012-10-04 13:18:31 +02001125static inline void __update_group_entity_contrib(struct sched_entity *se)
1126{
1127 struct cfs_rq *cfs_rq = group_cfs_rq(se);
1128 struct task_group *tg = cfs_rq->tg;
Paul Turnerbb17f652012-10-04 13:18:31 +02001129 int runnable_avg;
1130
Paul Turner8165e142012-10-04 13:18:31 +02001131 u64 contrib;
1132
1133 contrib = cfs_rq->tg_load_contrib * tg->shares;
1134 se->avg.load_avg_contrib = div64_u64(contrib,
1135 atomic64_read(&tg->load_avg) + 1);
Paul Turnerbb17f652012-10-04 13:18:31 +02001136
1137 /*
1138 * For group entities we need to compute a correction term in the case
1139 * that they are consuming <1 cpu so that we would contribute the same
1140 * load as a task of equal weight.
1141 *
1142 * Explicitly co-ordinating this measurement would be expensive, but
1143 * fortunately the sum of each cpus contribution forms a usable
1144 * lower-bound on the true value.
1145 *
1146 * Consider the aggregate of 2 contributions. Either they are disjoint
1147 * (and the sum represents true value) or they are disjoint and we are
1148 * understating by the aggregate of their overlap.
1149 *
1150 * Extending this to N cpus, for a given overlap, the maximum amount we
1151 * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
1152 * cpus that overlap for this interval and w_i is the interval width.
1153 *
1154 * On a small machine; the first term is well-bounded which bounds the
1155 * total error since w_i is a subset of the period. Whereas on a
1156 * larger machine, while this first term can be larger, if w_i is the
1157 * of consequential size guaranteed to see n_i*w_i quickly converge to
1158 * our upper bound of 1-cpu.
1159 */
1160 runnable_avg = atomic_read(&tg->runnable_avg);
1161 if (runnable_avg < NICE_0_LOAD) {
1162 se->avg.load_avg_contrib *= runnable_avg;
1163 se->avg.load_avg_contrib >>= NICE_0_SHIFT;
1164 }
Paul Turner8165e142012-10-04 13:18:31 +02001165}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001166#else
1167static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1168 int force_update) {}
Paul Turnerbb17f652012-10-04 13:18:31 +02001169static inline void __update_tg_runnable_avg(struct sched_avg *sa,
1170 struct cfs_rq *cfs_rq) {}
Paul Turner8165e142012-10-04 13:18:31 +02001171static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001172#endif
1173
Paul Turner8165e142012-10-04 13:18:31 +02001174static inline void __update_task_entity_contrib(struct sched_entity *se)
1175{
1176 u32 contrib;
1177
1178 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
1179 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
1180 contrib /= (se->avg.runnable_avg_period + 1);
1181 se->avg.load_avg_contrib = scale_load(contrib);
1182}
1183
Paul Turner2dac7542012-10-04 13:18:30 +02001184/* Compute the current contribution to load_avg by se, return any delta */
1185static long __update_entity_load_avg_contrib(struct sched_entity *se)
1186{
1187 long old_contrib = se->avg.load_avg_contrib;
1188
Paul Turner8165e142012-10-04 13:18:31 +02001189 if (entity_is_task(se)) {
1190 __update_task_entity_contrib(se);
1191 } else {
Paul Turnerbb17f652012-10-04 13:18:31 +02001192 __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
Paul Turner8165e142012-10-04 13:18:31 +02001193 __update_group_entity_contrib(se);
1194 }
Paul Turner2dac7542012-10-04 13:18:30 +02001195
1196 return se->avg.load_avg_contrib - old_contrib;
1197}
1198
Paul Turner9ee474f2012-10-04 13:18:30 +02001199static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1200 long load_contrib)
1201{
1202 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1203 cfs_rq->blocked_load_avg -= load_contrib;
1204 else
1205 cfs_rq->blocked_load_avg = 0;
1206}
1207
Paul Turnerf1b17282012-10-04 13:18:31 +02001208static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
1209
Paul Turner9d85f212012-10-04 13:18:29 +02001210/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001211static inline void update_entity_load_avg(struct sched_entity *se,
1212 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001213{
Paul Turner2dac7542012-10-04 13:18:30 +02001214 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1215 long contrib_delta;
Paul Turnerf1b17282012-10-04 13:18:31 +02001216 u64 now;
Paul Turner2dac7542012-10-04 13:18:30 +02001217
Paul Turnerf1b17282012-10-04 13:18:31 +02001218 /*
1219 * For a group entity we need to use their owned cfs_rq_clock_task() in
1220 * case they are the parent of a throttled hierarchy.
1221 */
1222 if (entity_is_task(se))
1223 now = cfs_rq_clock_task(cfs_rq);
1224 else
1225 now = cfs_rq_clock_task(group_cfs_rq(se));
1226
1227 if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
Paul Turner2dac7542012-10-04 13:18:30 +02001228 return;
1229
1230 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001231
1232 if (!update_cfs_rq)
1233 return;
1234
Paul Turner2dac7542012-10-04 13:18:30 +02001235 if (se->on_rq)
1236 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001237 else
1238 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1239}
1240
1241/*
1242 * Decay the load contributed by all blocked children and account this so that
1243 * their contribution may appropriately discounted when they wake up.
1244 */
Paul Turneraff3e492012-10-04 13:18:30 +02001245static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001246{
Paul Turnerf1b17282012-10-04 13:18:31 +02001247 u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
Paul Turner9ee474f2012-10-04 13:18:30 +02001248 u64 decays;
1249
1250 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001251 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001252 return;
1253
Paul Turneraff3e492012-10-04 13:18:30 +02001254 if (atomic64_read(&cfs_rq->removed_load)) {
1255 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1256 subtract_blocked_load_contrib(cfs_rq, removed_load);
1257 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001258
Paul Turneraff3e492012-10-04 13:18:30 +02001259 if (decays) {
1260 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1261 decays);
1262 atomic64_add(decays, &cfs_rq->decay_counter);
1263 cfs_rq->last_decay = now;
1264 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001265
1266 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turnerf269ae02012-10-04 13:18:31 +02001267 update_cfs_shares(cfs_rq);
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);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001477 account_entity_enqueue(cfs_rq, se);
Paul Turnerf269ae02012-10-04 13:18:31 +02001478 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001479
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001480 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001481 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001482 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001483 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001484
Ingo Molnard2417e52007-08-09 11:16:47 +02001485 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001486 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001487 if (se != cfs_rq->curr)
1488 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001489 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001490
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001491 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001492 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001493 check_enqueue_throttle(cfs_rq);
1494 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001495}
1496
Rik van Riel2c13c9192011-02-01 09:48:37 -05001497static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001498{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001499 for_each_sched_entity(se) {
1500 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1501 if (cfs_rq->last == se)
1502 cfs_rq->last = NULL;
1503 else
1504 break;
1505 }
1506}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001507
Rik van Riel2c13c9192011-02-01 09:48:37 -05001508static void __clear_buddies_next(struct sched_entity *se)
1509{
1510 for_each_sched_entity(se) {
1511 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1512 if (cfs_rq->next == se)
1513 cfs_rq->next = NULL;
1514 else
1515 break;
1516 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001517}
1518
Rik van Rielac53db52011-02-01 09:51:03 -05001519static void __clear_buddies_skip(struct sched_entity *se)
1520{
1521 for_each_sched_entity(se) {
1522 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1523 if (cfs_rq->skip == se)
1524 cfs_rq->skip = NULL;
1525 else
1526 break;
1527 }
1528}
1529
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001530static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1531{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001532 if (cfs_rq->last == se)
1533 __clear_buddies_last(se);
1534
1535 if (cfs_rq->next == se)
1536 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001537
1538 if (cfs_rq->skip == se)
1539 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001540}
1541
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001542static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001543
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001544static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001545dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001546{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001547 /*
1548 * Update run-time statistics of the 'current'.
1549 */
1550 update_curr(cfs_rq);
1551
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001552 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001553 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001554#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001555 if (entity_is_task(se)) {
1556 struct task_struct *tsk = task_of(se);
1557
1558 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001559 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001560 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001561 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001562 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001563#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001564 }
1565
Peter Zijlstra2002c692008-11-11 11:52:33 +01001566 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001567
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001568 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001569 __dequeue_entity(cfs_rq, se);
1570 account_entity_dequeue(cfs_rq, se);
Paul Turnerf269ae02012-10-04 13:18:31 +02001571 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001572
1573 /*
1574 * Normalize the entity after updating the min_vruntime because the
1575 * update can refer to the ->curr item and we need to reflect this
1576 * movement in our normalized position.
1577 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001578 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001579 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001580
Paul Turnerd8b49862011-07-21 09:43:41 -07001581 /* return excess runtime on last dequeue */
1582 return_cfs_rq_runtime(cfs_rq);
1583
Peter Zijlstra1e876232011-05-17 16:21:10 -07001584 update_min_vruntime(cfs_rq);
Paul Turnerf269ae02012-10-04 13:18:31 +02001585 se->on_rq = 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001586}
1587
1588/*
1589 * Preempt the current task with a newly woken task if needed:
1590 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001591static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001592check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001593{
Peter Zijlstra11697832007-09-05 14:32:49 +02001594 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001595 struct sched_entity *se;
1596 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001597
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001598 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001599 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001600 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001601 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001602 /*
1603 * The current task ran long enough, ensure it doesn't get
1604 * re-elected due to buddy favours.
1605 */
1606 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001607 return;
1608 }
1609
1610 /*
1611 * Ensure that a task that missed wakeup preemption by a
1612 * narrow margin doesn't have to wait for a full slice.
1613 * This also mitigates buddy induced latencies under load.
1614 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001615 if (delta_exec < sysctl_sched_min_granularity)
1616 return;
1617
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001618 se = __pick_first_entity(cfs_rq);
1619 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001620
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001621 if (delta < 0)
1622 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001623
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001624 if (delta > ideal_runtime)
1625 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001626}
1627
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001628static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001629set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001630{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001631 /* 'current' is not kept within the tree. */
1632 if (se->on_rq) {
1633 /*
1634 * Any task has to be enqueued before it get to execute on
1635 * a CPU. So account for the time it spent waiting on the
1636 * runqueue.
1637 */
1638 update_stats_wait_end(cfs_rq, se);
1639 __dequeue_entity(cfs_rq, se);
1640 }
1641
Ingo Molnar79303e92007-08-09 11:16:47 +02001642 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001643 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001644#ifdef CONFIG_SCHEDSTATS
1645 /*
1646 * Track our maximum slice length, if the CPU's load is at
1647 * least twice that of our own weight (i.e. dont track it
1648 * when there are only lesser-weight tasks around):
1649 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001650 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001651 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001652 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1653 }
1654#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001655 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001656}
1657
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001658static int
1659wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1660
Rik van Rielac53db52011-02-01 09:51:03 -05001661/*
1662 * Pick the next process, keeping these things in mind, in this order:
1663 * 1) keep things fair between processes/task groups
1664 * 2) pick the "next" process, since someone really wants that to run
1665 * 3) pick the "last" process, for cache locality
1666 * 4) do not run the "skip" process, if something else is available
1667 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001668static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001669{
Rik van Rielac53db52011-02-01 09:51:03 -05001670 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001671 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001672
Rik van Rielac53db52011-02-01 09:51:03 -05001673 /*
1674 * Avoid running the skip buddy, if running something else can
1675 * be done without getting too unfair.
1676 */
1677 if (cfs_rq->skip == se) {
1678 struct sched_entity *second = __pick_next_entity(se);
1679 if (second && wakeup_preempt_entity(second, left) < 1)
1680 se = second;
1681 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001682
Mike Galbraithf685cea2009-10-23 23:09:22 +02001683 /*
1684 * Prefer last buddy, try to return the CPU to a preempted task.
1685 */
1686 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1687 se = cfs_rq->last;
1688
Rik van Rielac53db52011-02-01 09:51:03 -05001689 /*
1690 * Someone really wants this to run. If it's not unfair, run it.
1691 */
1692 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1693 se = cfs_rq->next;
1694
Mike Galbraithf685cea2009-10-23 23:09:22 +02001695 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001696
1697 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001698}
1699
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001700static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1701
Ingo Molnarab6cde22007-08-09 11:16:48 +02001702static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001703{
1704 /*
1705 * If still on the runqueue then deactivate_task()
1706 * was not called and update_curr() has to be done:
1707 */
1708 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001709 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001710
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001711 /* throttle cfs_rqs exceeding runtime */
1712 check_cfs_rq_runtime(cfs_rq);
1713
Peter Zijlstraddc97292007-10-15 17:00:10 +02001714 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001715 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001716 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001717 /* Put 'current' back into the tree. */
1718 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001719 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001720 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001721 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001722 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001723}
1724
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001725static void
1726entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001727{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001728 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001729 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001730 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001731 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001732
Paul Turner43365bd2010-12-15 19:10:17 -08001733 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001734 * Ensure that runnable average is periodically updated.
1735 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001736 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001737 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001738
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001739#ifdef CONFIG_SCHED_HRTICK
1740 /*
1741 * queued ticks are scheduled to match the slice, so don't bother
1742 * validating it and just reschedule.
1743 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001744 if (queued) {
1745 resched_task(rq_of(cfs_rq)->curr);
1746 return;
1747 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001748 /*
1749 * don't let the period tick interfere with the hrtick preemption
1750 */
1751 if (!sched_feat(DOUBLE_TICK) &&
1752 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1753 return;
1754#endif
1755
Yong Zhang2c2efae2011-07-29 16:20:33 +08001756 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001757 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001758}
1759
Paul Turnerab84d312011-07-21 09:43:28 -07001760
1761/**************************************************
1762 * CFS bandwidth control machinery
1763 */
1764
1765#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001766
1767#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001768static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001769
1770static inline bool cfs_bandwidth_used(void)
1771{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001772 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001773}
1774
1775void account_cfs_bandwidth_used(int enabled, int was_enabled)
1776{
1777 /* only need to count groups transitioning between enabled/!enabled */
1778 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001779 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001780 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001781 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001782}
1783#else /* HAVE_JUMP_LABEL */
1784static bool cfs_bandwidth_used(void)
1785{
1786 return true;
1787}
1788
1789void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1790#endif /* HAVE_JUMP_LABEL */
1791
Paul Turnerab84d312011-07-21 09:43:28 -07001792/*
1793 * default period for cfs group bandwidth.
1794 * default: 0.1s, units: nanoseconds
1795 */
1796static inline u64 default_cfs_period(void)
1797{
1798 return 100000000ULL;
1799}
Paul Turnerec12cb72011-07-21 09:43:30 -07001800
1801static inline u64 sched_cfs_bandwidth_slice(void)
1802{
1803 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1804}
1805
Paul Turnera9cf55b2011-07-21 09:43:32 -07001806/*
1807 * Replenish runtime according to assigned quota and update expiration time.
1808 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1809 * additional synchronization around rq->lock.
1810 *
1811 * requires cfs_b->lock
1812 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001813void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001814{
1815 u64 now;
1816
1817 if (cfs_b->quota == RUNTIME_INF)
1818 return;
1819
1820 now = sched_clock_cpu(smp_processor_id());
1821 cfs_b->runtime = cfs_b->quota;
1822 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1823}
1824
Peter Zijlstra029632f2011-10-25 10:00:11 +02001825static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1826{
1827 return &tg->cfs_bandwidth;
1828}
1829
Paul Turnerf1b17282012-10-04 13:18:31 +02001830/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
1831static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
1832{
1833 if (unlikely(cfs_rq->throttle_count))
1834 return cfs_rq->throttled_clock_task;
1835
1836 return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time;
1837}
1838
Paul Turner85dac902011-07-21 09:43:33 -07001839/* returns 0 on failure to allocate runtime */
1840static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001841{
1842 struct task_group *tg = cfs_rq->tg;
1843 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001844 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001845
1846 /* note: this is a positive sum as runtime_remaining <= 0 */
1847 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1848
1849 raw_spin_lock(&cfs_b->lock);
1850 if (cfs_b->quota == RUNTIME_INF)
1851 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001852 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001853 /*
1854 * If the bandwidth pool has become inactive, then at least one
1855 * period must have elapsed since the last consumption.
1856 * Refresh the global state and ensure bandwidth timer becomes
1857 * active.
1858 */
1859 if (!cfs_b->timer_active) {
1860 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001861 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001862 }
Paul Turner58088ad2011-07-21 09:43:31 -07001863
1864 if (cfs_b->runtime > 0) {
1865 amount = min(cfs_b->runtime, min_amount);
1866 cfs_b->runtime -= amount;
1867 cfs_b->idle = 0;
1868 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001869 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001870 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001871 raw_spin_unlock(&cfs_b->lock);
1872
1873 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001874 /*
1875 * we may have advanced our local expiration to account for allowed
1876 * spread between our sched_clock and the one on which runtime was
1877 * issued.
1878 */
1879 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1880 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001881
1882 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001883}
1884
1885/*
1886 * Note: This depends on the synchronization provided by sched_clock and the
1887 * fact that rq->clock snapshots this value.
1888 */
1889static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1890{
1891 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1892 struct rq *rq = rq_of(cfs_rq);
1893
1894 /* if the deadline is ahead of our clock, nothing to do */
1895 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1896 return;
1897
1898 if (cfs_rq->runtime_remaining < 0)
1899 return;
1900
1901 /*
1902 * If the local deadline has passed we have to consider the
1903 * possibility that our sched_clock is 'fast' and the global deadline
1904 * has not truly expired.
1905 *
1906 * Fortunately we can check determine whether this the case by checking
1907 * whether the global deadline has advanced.
1908 */
1909
1910 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1911 /* extend local deadline, drift is bounded above by 2 ticks */
1912 cfs_rq->runtime_expires += TICK_NSEC;
1913 } else {
1914 /* global deadline is ahead, expiration has passed */
1915 cfs_rq->runtime_remaining = 0;
1916 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001917}
1918
1919static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1920 unsigned long delta_exec)
1921{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001922 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001923 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001924 expire_cfs_rq_runtime(cfs_rq);
1925
1926 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001927 return;
1928
Paul Turner85dac902011-07-21 09:43:33 -07001929 /*
1930 * if we're unable to extend our runtime we resched so that the active
1931 * hierarchy can be throttled
1932 */
1933 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1934 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001935}
1936
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001937static __always_inline
1938void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001939{
Paul Turner56f570e2011-11-07 20:26:33 -08001940 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001941 return;
1942
1943 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1944}
1945
Paul Turner85dac902011-07-21 09:43:33 -07001946static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1947{
Paul Turner56f570e2011-11-07 20:26:33 -08001948 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001949}
1950
Paul Turner64660c82011-07-21 09:43:36 -07001951/* check whether cfs_rq, or any parent, is throttled */
1952static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1953{
Paul Turner56f570e2011-11-07 20:26:33 -08001954 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001955}
1956
1957/*
1958 * Ensure that neither of the group entities corresponding to src_cpu or
1959 * dest_cpu are members of a throttled hierarchy when performing group
1960 * load-balance operations.
1961 */
1962static inline int throttled_lb_pair(struct task_group *tg,
1963 int src_cpu, int dest_cpu)
1964{
1965 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1966
1967 src_cfs_rq = tg->cfs_rq[src_cpu];
1968 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1969
1970 return throttled_hierarchy(src_cfs_rq) ||
1971 throttled_hierarchy(dest_cfs_rq);
1972}
1973
1974/* updated child weight may affect parent so we have to do this bottom up */
1975static int tg_unthrottle_up(struct task_group *tg, void *data)
1976{
1977 struct rq *rq = data;
1978 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1979
1980 cfs_rq->throttle_count--;
1981#ifdef CONFIG_SMP
1982 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02001983 /* adjust cfs_rq_clock_task() */
1984 cfs_rq->throttled_clock_task_time += rq->clock_task -
1985 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07001986 }
1987#endif
1988
1989 return 0;
1990}
1991
1992static int tg_throttle_down(struct task_group *tg, void *data)
1993{
1994 struct rq *rq = data;
1995 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1996
Paul Turner82958362012-10-04 13:18:31 +02001997 /* group is entering throttled state, stop time */
1998 if (!cfs_rq->throttle_count)
Paul Turnerf1b17282012-10-04 13:18:31 +02001999 cfs_rq->throttled_clock_task = rq->clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07002000 cfs_rq->throttle_count++;
2001
2002 return 0;
2003}
2004
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002005static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07002006{
2007 struct rq *rq = rq_of(cfs_rq);
2008 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2009 struct sched_entity *se;
2010 long task_delta, dequeue = 1;
2011
2012 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2013
Paul Turnerf1b17282012-10-04 13:18:31 +02002014 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07002015 rcu_read_lock();
2016 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
2017 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07002018
2019 task_delta = cfs_rq->h_nr_running;
2020 for_each_sched_entity(se) {
2021 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
2022 /* throttled entity or throttle-on-deactivate */
2023 if (!se->on_rq)
2024 break;
2025
2026 if (dequeue)
2027 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
2028 qcfs_rq->h_nr_running -= task_delta;
2029
2030 if (qcfs_rq->load.weight)
2031 dequeue = 0;
2032 }
2033
2034 if (!se)
2035 rq->nr_running -= task_delta;
2036
2037 cfs_rq->throttled = 1;
Paul Turnerf1b17282012-10-04 13:18:31 +02002038 cfs_rq->throttled_clock = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07002039 raw_spin_lock(&cfs_b->lock);
2040 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
2041 raw_spin_unlock(&cfs_b->lock);
2042}
2043
Peter Zijlstra029632f2011-10-25 10:00:11 +02002044void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07002045{
2046 struct rq *rq = rq_of(cfs_rq);
2047 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2048 struct sched_entity *se;
2049 int enqueue = 1;
2050 long task_delta;
2051
2052 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2053
2054 cfs_rq->throttled = 0;
2055 raw_spin_lock(&cfs_b->lock);
Paul Turnerf1b17282012-10-04 13:18:31 +02002056 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07002057 list_del_rcu(&cfs_rq->throttled_list);
2058 raw_spin_unlock(&cfs_b->lock);
2059
Paul Turner64660c82011-07-21 09:43:36 -07002060 update_rq_clock(rq);
2061 /* update hierarchical throttle state */
2062 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
2063
Paul Turner671fd9d2011-07-21 09:43:34 -07002064 if (!cfs_rq->load.weight)
2065 return;
2066
2067 task_delta = cfs_rq->h_nr_running;
2068 for_each_sched_entity(se) {
2069 if (se->on_rq)
2070 enqueue = 0;
2071
2072 cfs_rq = cfs_rq_of(se);
2073 if (enqueue)
2074 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2075 cfs_rq->h_nr_running += task_delta;
2076
2077 if (cfs_rq_throttled(cfs_rq))
2078 break;
2079 }
2080
2081 if (!se)
2082 rq->nr_running += task_delta;
2083
2084 /* determine whether we need to wake up potentially idle cpu */
2085 if (rq->curr == rq->idle && rq->cfs.nr_running)
2086 resched_task(rq->curr);
2087}
2088
2089static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2090 u64 remaining, u64 expires)
2091{
2092 struct cfs_rq *cfs_rq;
2093 u64 runtime = remaining;
2094
2095 rcu_read_lock();
2096 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2097 throttled_list) {
2098 struct rq *rq = rq_of(cfs_rq);
2099
2100 raw_spin_lock(&rq->lock);
2101 if (!cfs_rq_throttled(cfs_rq))
2102 goto next;
2103
2104 runtime = -cfs_rq->runtime_remaining + 1;
2105 if (runtime > remaining)
2106 runtime = remaining;
2107 remaining -= runtime;
2108
2109 cfs_rq->runtime_remaining += runtime;
2110 cfs_rq->runtime_expires = expires;
2111
2112 /* we check whether we're throttled above */
2113 if (cfs_rq->runtime_remaining > 0)
2114 unthrottle_cfs_rq(cfs_rq);
2115
2116next:
2117 raw_spin_unlock(&rq->lock);
2118
2119 if (!remaining)
2120 break;
2121 }
2122 rcu_read_unlock();
2123
2124 return remaining;
2125}
2126
Paul Turner58088ad2011-07-21 09:43:31 -07002127/*
2128 * Responsible for refilling a task_group's bandwidth and unthrottling its
2129 * cfs_rqs as appropriate. If there has been no activity within the last
2130 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2131 * used to track this state.
2132 */
2133static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2134{
Paul Turner671fd9d2011-07-21 09:43:34 -07002135 u64 runtime, runtime_expires;
2136 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002137
2138 raw_spin_lock(&cfs_b->lock);
2139 /* no need to continue the timer with no bandwidth constraint */
2140 if (cfs_b->quota == RUNTIME_INF)
2141 goto out_unlock;
2142
Paul Turner671fd9d2011-07-21 09:43:34 -07002143 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2144 /* idle depends on !throttled (for the case of a large deficit) */
2145 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002146 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002147
Paul Turnera9cf55b2011-07-21 09:43:32 -07002148 /* if we're going inactive then everything else can be deferred */
2149 if (idle)
2150 goto out_unlock;
2151
2152 __refill_cfs_bandwidth_runtime(cfs_b);
2153
Paul Turner671fd9d2011-07-21 09:43:34 -07002154 if (!throttled) {
2155 /* mark as potentially idle for the upcoming period */
2156 cfs_b->idle = 1;
2157 goto out_unlock;
2158 }
Paul Turner58088ad2011-07-21 09:43:31 -07002159
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002160 /* account preceding periods in which throttling occurred */
2161 cfs_b->nr_throttled += overrun;
2162
Paul Turner671fd9d2011-07-21 09:43:34 -07002163 /*
2164 * There are throttled entities so we must first use the new bandwidth
2165 * to unthrottle them before making it generally available. This
2166 * ensures that all existing debts will be paid before a new cfs_rq is
2167 * allowed to run.
2168 */
2169 runtime = cfs_b->runtime;
2170 runtime_expires = cfs_b->runtime_expires;
2171 cfs_b->runtime = 0;
2172
2173 /*
2174 * This check is repeated as we are holding onto the new bandwidth
2175 * while we unthrottle. This can potentially race with an unthrottled
2176 * group trying to acquire new bandwidth from the global pool.
2177 */
2178 while (throttled && runtime > 0) {
2179 raw_spin_unlock(&cfs_b->lock);
2180 /* we can't nest cfs_b->lock while distributing bandwidth */
2181 runtime = distribute_cfs_runtime(cfs_b, runtime,
2182 runtime_expires);
2183 raw_spin_lock(&cfs_b->lock);
2184
2185 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2186 }
2187
2188 /* return (any) remaining runtime */
2189 cfs_b->runtime = runtime;
2190 /*
2191 * While we are ensured activity in the period following an
2192 * unthrottle, this also covers the case in which the new bandwidth is
2193 * insufficient to cover the existing bandwidth deficit. (Forcing the
2194 * timer to remain active while there are any throttled entities.)
2195 */
2196 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002197out_unlock:
2198 if (idle)
2199 cfs_b->timer_active = 0;
2200 raw_spin_unlock(&cfs_b->lock);
2201
2202 return idle;
2203}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002204
Paul Turnerd8b49862011-07-21 09:43:41 -07002205/* a cfs_rq won't donate quota below this amount */
2206static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2207/* minimum remaining period time to redistribute slack quota */
2208static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2209/* how long we wait to gather additional slack before distributing */
2210static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2211
2212/* are we near the end of the current quota period? */
2213static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2214{
2215 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2216 u64 remaining;
2217
2218 /* if the call-back is running a quota refresh is already occurring */
2219 if (hrtimer_callback_running(refresh_timer))
2220 return 1;
2221
2222 /* is a quota refresh about to occur? */
2223 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2224 if (remaining < min_expire)
2225 return 1;
2226
2227 return 0;
2228}
2229
2230static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2231{
2232 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2233
2234 /* if there's a quota refresh soon don't bother with slack */
2235 if (runtime_refresh_within(cfs_b, min_left))
2236 return;
2237
2238 start_bandwidth_timer(&cfs_b->slack_timer,
2239 ns_to_ktime(cfs_bandwidth_slack_period));
2240}
2241
2242/* we know any runtime found here is valid as update_curr() precedes return */
2243static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2244{
2245 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2246 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2247
2248 if (slack_runtime <= 0)
2249 return;
2250
2251 raw_spin_lock(&cfs_b->lock);
2252 if (cfs_b->quota != RUNTIME_INF &&
2253 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2254 cfs_b->runtime += slack_runtime;
2255
2256 /* we are under rq->lock, defer unthrottling using a timer */
2257 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2258 !list_empty(&cfs_b->throttled_cfs_rq))
2259 start_cfs_slack_bandwidth(cfs_b);
2260 }
2261 raw_spin_unlock(&cfs_b->lock);
2262
2263 /* even if it's not valid for return we don't want to try again */
2264 cfs_rq->runtime_remaining -= slack_runtime;
2265}
2266
2267static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2268{
Paul Turner56f570e2011-11-07 20:26:33 -08002269 if (!cfs_bandwidth_used())
2270 return;
2271
Paul Turnerfccfdc62011-11-07 20:26:34 -08002272 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002273 return;
2274
2275 __return_cfs_rq_runtime(cfs_rq);
2276}
2277
2278/*
2279 * This is done with a timer (instead of inline with bandwidth return) since
2280 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2281 */
2282static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2283{
2284 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2285 u64 expires;
2286
2287 /* confirm we're still not at a refresh boundary */
2288 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2289 return;
2290
2291 raw_spin_lock(&cfs_b->lock);
2292 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2293 runtime = cfs_b->runtime;
2294 cfs_b->runtime = 0;
2295 }
2296 expires = cfs_b->runtime_expires;
2297 raw_spin_unlock(&cfs_b->lock);
2298
2299 if (!runtime)
2300 return;
2301
2302 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2303
2304 raw_spin_lock(&cfs_b->lock);
2305 if (expires == cfs_b->runtime_expires)
2306 cfs_b->runtime = runtime;
2307 raw_spin_unlock(&cfs_b->lock);
2308}
2309
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002310/*
2311 * When a group wakes up we want to make sure that its quota is not already
2312 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2313 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2314 */
2315static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2316{
Paul Turner56f570e2011-11-07 20:26:33 -08002317 if (!cfs_bandwidth_used())
2318 return;
2319
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002320 /* an active group must be handled by the update_curr()->put() path */
2321 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2322 return;
2323
2324 /* ensure the group is not already throttled */
2325 if (cfs_rq_throttled(cfs_rq))
2326 return;
2327
2328 /* update runtime allocation */
2329 account_cfs_rq_runtime(cfs_rq, 0);
2330 if (cfs_rq->runtime_remaining <= 0)
2331 throttle_cfs_rq(cfs_rq);
2332}
2333
2334/* conditionally throttle active cfs_rq's from put_prev_entity() */
2335static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2336{
Paul Turner56f570e2011-11-07 20:26:33 -08002337 if (!cfs_bandwidth_used())
2338 return;
2339
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002340 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2341 return;
2342
2343 /*
2344 * it's possible for a throttled entity to be forced into a running
2345 * state (e.g. set_curr_task), in this case we're finished.
2346 */
2347 if (cfs_rq_throttled(cfs_rq))
2348 return;
2349
2350 throttle_cfs_rq(cfs_rq);
2351}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002352
2353static inline u64 default_cfs_period(void);
2354static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2355static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2356
2357static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2358{
2359 struct cfs_bandwidth *cfs_b =
2360 container_of(timer, struct cfs_bandwidth, slack_timer);
2361 do_sched_cfs_slack_timer(cfs_b);
2362
2363 return HRTIMER_NORESTART;
2364}
2365
2366static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2367{
2368 struct cfs_bandwidth *cfs_b =
2369 container_of(timer, struct cfs_bandwidth, period_timer);
2370 ktime_t now;
2371 int overrun;
2372 int idle = 0;
2373
2374 for (;;) {
2375 now = hrtimer_cb_get_time(timer);
2376 overrun = hrtimer_forward(timer, now, cfs_b->period);
2377
2378 if (!overrun)
2379 break;
2380
2381 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2382 }
2383
2384 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2385}
2386
2387void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2388{
2389 raw_spin_lock_init(&cfs_b->lock);
2390 cfs_b->runtime = 0;
2391 cfs_b->quota = RUNTIME_INF;
2392 cfs_b->period = ns_to_ktime(default_cfs_period());
2393
2394 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2395 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2396 cfs_b->period_timer.function = sched_cfs_period_timer;
2397 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2398 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2399}
2400
2401static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2402{
2403 cfs_rq->runtime_enabled = 0;
2404 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2405}
2406
2407/* requires cfs_b->lock, may release to reprogram timer */
2408void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2409{
2410 /*
2411 * The timer may be active because we're trying to set a new bandwidth
2412 * period or because we're racing with the tear-down path
2413 * (timer_active==0 becomes visible before the hrtimer call-back
2414 * terminates). In either case we ensure that it's re-programmed
2415 */
2416 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2417 raw_spin_unlock(&cfs_b->lock);
2418 /* ensure cfs_b->lock is available while we wait */
2419 hrtimer_cancel(&cfs_b->period_timer);
2420
2421 raw_spin_lock(&cfs_b->lock);
2422 /* if someone else restarted the timer then we're done */
2423 if (cfs_b->timer_active)
2424 return;
2425 }
2426
2427 cfs_b->timer_active = 1;
2428 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2429}
2430
2431static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2432{
2433 hrtimer_cancel(&cfs_b->period_timer);
2434 hrtimer_cancel(&cfs_b->slack_timer);
2435}
2436
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002437static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002438{
2439 struct cfs_rq *cfs_rq;
2440
2441 for_each_leaf_cfs_rq(rq, cfs_rq) {
2442 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2443
2444 if (!cfs_rq->runtime_enabled)
2445 continue;
2446
2447 /*
2448 * clock_task is not advancing so we just need to make sure
2449 * there's some valid quota amount
2450 */
2451 cfs_rq->runtime_remaining = cfs_b->quota;
2452 if (cfs_rq_throttled(cfs_rq))
2453 unthrottle_cfs_rq(cfs_rq);
2454 }
2455}
2456
2457#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02002458static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
2459{
2460 return rq_of(cfs_rq)->clock_task;
2461}
2462
2463static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
2464 unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002465static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2466static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002467static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002468
2469static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2470{
2471 return 0;
2472}
Paul Turner64660c82011-07-21 09:43:36 -07002473
2474static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2475{
2476 return 0;
2477}
2478
2479static inline int throttled_lb_pair(struct task_group *tg,
2480 int src_cpu, int dest_cpu)
2481{
2482 return 0;
2483}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002484
2485void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2486
2487#ifdef CONFIG_FAIR_GROUP_SCHED
2488static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002489#endif
2490
Peter Zijlstra029632f2011-10-25 10:00:11 +02002491static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2492{
2493 return NULL;
2494}
2495static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002496static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002497
2498#endif /* CONFIG_CFS_BANDWIDTH */
2499
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002500/**************************************************
2501 * CFS operations on tasks:
2502 */
2503
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002504#ifdef CONFIG_SCHED_HRTICK
2505static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2506{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002507 struct sched_entity *se = &p->se;
2508 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2509
2510 WARN_ON(task_rq(p) != rq);
2511
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002512 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002513 u64 slice = sched_slice(cfs_rq, se);
2514 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2515 s64 delta = slice - ran;
2516
2517 if (delta < 0) {
2518 if (rq->curr == p)
2519 resched_task(p);
2520 return;
2521 }
2522
2523 /*
2524 * Don't schedule slices shorter than 10000ns, that just
2525 * doesn't make sense. Rely on vruntime for fairness.
2526 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002527 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002528 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002529
Peter Zijlstra31656512008-07-18 18:01:23 +02002530 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002531 }
2532}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002533
2534/*
2535 * called from enqueue/dequeue and updates the hrtick when the
2536 * current task is from our class and nr_running is low enough
2537 * to matter.
2538 */
2539static void hrtick_update(struct rq *rq)
2540{
2541 struct task_struct *curr = rq->curr;
2542
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002543 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002544 return;
2545
2546 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2547 hrtick_start_fair(rq, curr);
2548}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302549#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002550static inline void
2551hrtick_start_fair(struct rq *rq, struct task_struct *p)
2552{
2553}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002554
2555static inline void hrtick_update(struct rq *rq)
2556{
2557}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002558#endif
2559
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002560/*
2561 * The enqueue_task method is called before nr_running is
2562 * increased. Here we update the fair scheduling stats and
2563 * then put the task into the rbtree:
2564 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002565static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002566enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002567{
2568 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002569 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002570
2571 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002572 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002573 break;
2574 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002575 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002576
2577 /*
2578 * end evaluation on encountering a throttled cfs_rq
2579 *
2580 * note: in the case of encountering a throttled cfs_rq we will
2581 * post the final h_nr_running increment below.
2582 */
2583 if (cfs_rq_throttled(cfs_rq))
2584 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002585 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002586
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002587 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002588 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002589
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002590 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002591 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002592 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002593
Paul Turner85dac902011-07-21 09:43:33 -07002594 if (cfs_rq_throttled(cfs_rq))
2595 break;
2596
Paul Turner9ee474f2012-10-04 13:18:30 +02002597 update_entity_load_avg(se, 1);
Paul Turnerf269ae02012-10-04 13:18:31 +02002598 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002599 }
2600
Ben Segall18bf2802012-10-04 12:51:20 +02002601 if (!se) {
2602 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002603 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002604 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002605 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002606}
2607
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002608static void set_next_buddy(struct sched_entity *se);
2609
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002610/*
2611 * The dequeue_task method is called before nr_running is
2612 * decreased. We remove the task from the rbtree and
2613 * update the fair scheduling stats:
2614 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002615static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002616{
2617 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002618 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002619 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002620
2621 for_each_sched_entity(se) {
2622 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002623 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002624
2625 /*
2626 * end evaluation on encountering a throttled cfs_rq
2627 *
2628 * note: in the case of encountering a throttled cfs_rq we will
2629 * post the final h_nr_running decrement below.
2630 */
2631 if (cfs_rq_throttled(cfs_rq))
2632 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002633 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002634
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002635 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002636 if (cfs_rq->load.weight) {
2637 /*
2638 * Bias pick_next to pick a task from this cfs_rq, as
2639 * p is sleeping when it is within its sched_slice.
2640 */
2641 if (task_sleep && parent_entity(se))
2642 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002643
2644 /* avoid re-evaluating load for this entity */
2645 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002646 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002647 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002648 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002649 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002650
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002651 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002652 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002653 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002654
Paul Turner85dac902011-07-21 09:43:33 -07002655 if (cfs_rq_throttled(cfs_rq))
2656 break;
2657
Paul Turner9ee474f2012-10-04 13:18:30 +02002658 update_entity_load_avg(se, 1);
Paul Turnerf269ae02012-10-04 13:18:31 +02002659 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002660 }
2661
Ben Segall18bf2802012-10-04 12:51:20 +02002662 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002663 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002664 update_rq_runnable_avg(rq, 1);
2665 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002666 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002667}
2668
Gregory Haskinse7693a32008-01-25 21:08:09 +01002669#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002670/* Used instead of source_load when we know the type == 0 */
2671static unsigned long weighted_cpuload(const int cpu)
2672{
2673 return cpu_rq(cpu)->load.weight;
2674}
2675
2676/*
2677 * Return a low guess at the load of a migration-source cpu weighted
2678 * according to the scheduling class and "nice" value.
2679 *
2680 * We want to under-estimate the load of migration sources, to
2681 * balance conservatively.
2682 */
2683static unsigned long source_load(int cpu, int type)
2684{
2685 struct rq *rq = cpu_rq(cpu);
2686 unsigned long total = weighted_cpuload(cpu);
2687
2688 if (type == 0 || !sched_feat(LB_BIAS))
2689 return total;
2690
2691 return min(rq->cpu_load[type-1], total);
2692}
2693
2694/*
2695 * Return a high guess at the load of a migration-target cpu weighted
2696 * according to the scheduling class and "nice" value.
2697 */
2698static unsigned long target_load(int cpu, int type)
2699{
2700 struct rq *rq = cpu_rq(cpu);
2701 unsigned long total = weighted_cpuload(cpu);
2702
2703 if (type == 0 || !sched_feat(LB_BIAS))
2704 return total;
2705
2706 return max(rq->cpu_load[type-1], total);
2707}
2708
2709static unsigned long power_of(int cpu)
2710{
2711 return cpu_rq(cpu)->cpu_power;
2712}
2713
2714static unsigned long cpu_avg_load_per_task(int cpu)
2715{
2716 struct rq *rq = cpu_rq(cpu);
2717 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2718
2719 if (nr_running)
2720 return rq->load.weight / nr_running;
2721
2722 return 0;
2723}
2724
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002725
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002726static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002727{
2728 struct sched_entity *se = &p->se;
2729 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002730 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002731
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002732#ifndef CONFIG_64BIT
2733 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002734
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002735 do {
2736 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2737 smp_rmb();
2738 min_vruntime = cfs_rq->min_vruntime;
2739 } while (min_vruntime != min_vruntime_copy);
2740#else
2741 min_vruntime = cfs_rq->min_vruntime;
2742#endif
2743
2744 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002745}
2746
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002747#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002748/*
2749 * effective_load() calculates the load change as seen from the root_task_group
2750 *
2751 * Adding load to a group doesn't make a group heavier, but can cause movement
2752 * of group shares between cpus. Assuming the shares were perfectly aligned one
2753 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002754 *
2755 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2756 * on this @cpu and results in a total addition (subtraction) of @wg to the
2757 * total group weight.
2758 *
2759 * Given a runqueue weight distribution (rw_i) we can compute a shares
2760 * distribution (s_i) using:
2761 *
2762 * s_i = rw_i / \Sum rw_j (1)
2763 *
2764 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2765 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2766 * shares distribution (s_i):
2767 *
2768 * rw_i = { 2, 4, 1, 0 }
2769 * s_i = { 2/7, 4/7, 1/7, 0 }
2770 *
2771 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2772 * task used to run on and the CPU the waker is running on), we need to
2773 * compute the effect of waking a task on either CPU and, in case of a sync
2774 * wakeup, compute the effect of the current task going to sleep.
2775 *
2776 * So for a change of @wl to the local @cpu with an overall group weight change
2777 * of @wl we can compute the new shares distribution (s'_i) using:
2778 *
2779 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2780 *
2781 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2782 * differences in waking a task to CPU 0. The additional task changes the
2783 * weight and shares distributions like:
2784 *
2785 * rw'_i = { 3, 4, 1, 0 }
2786 * s'_i = { 3/8, 4/8, 1/8, 0 }
2787 *
2788 * We can then compute the difference in effective weight by using:
2789 *
2790 * dw_i = S * (s'_i - s_i) (3)
2791 *
2792 * Where 'S' is the group weight as seen by its parent.
2793 *
2794 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2795 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2796 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002797 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002798static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002799{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002800 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002801
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002802 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002803 return wl;
2804
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002805 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002806 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002807
Paul Turner977dda72011-01-14 17:57:50 -08002808 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002809
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002810 /*
2811 * W = @wg + \Sum rw_j
2812 */
2813 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002814
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002815 /*
2816 * w = rw_i + @wl
2817 */
2818 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002819
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002820 /*
2821 * wl = S * s'_i; see (2)
2822 */
2823 if (W > 0 && w < W)
2824 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002825 else
2826 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002827
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002828 /*
2829 * Per the above, wl is the new se->load.weight value; since
2830 * those are clipped to [MIN_SHARES, ...) do so now. See
2831 * calc_cfs_shares().
2832 */
Paul Turner977dda72011-01-14 17:57:50 -08002833 if (wl < MIN_SHARES)
2834 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002835
2836 /*
2837 * wl = dw_i = S * (s'_i - s_i); see (3)
2838 */
Paul Turner977dda72011-01-14 17:57:50 -08002839 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002840
2841 /*
2842 * Recursively apply this logic to all parent groups to compute
2843 * the final effective load change on the root group. Since
2844 * only the @tg group gets extra weight, all parent groups can
2845 * only redistribute existing shares. @wl is the shift in shares
2846 * resulting from this level per the above.
2847 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002848 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002849 }
2850
2851 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002852}
2853#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002854
Peter Zijlstra83378262008-06-27 13:41:37 +02002855static inline unsigned long effective_load(struct task_group *tg, int cpu,
2856 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002857{
Peter Zijlstra83378262008-06-27 13:41:37 +02002858 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002859}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002860
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002861#endif
2862
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002863static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002864{
Paul Turnere37b6a72011-01-21 20:44:59 -08002865 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002866 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002867 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002868 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002869 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002870 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002871
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002872 idx = sd->wake_idx;
2873 this_cpu = smp_processor_id();
2874 prev_cpu = task_cpu(p);
2875 load = source_load(prev_cpu, idx);
2876 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002877
2878 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002879 * If sync wakeup then subtract the (maximum possible)
2880 * effect of the currently running task from the load
2881 * of the current CPU:
2882 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002883 if (sync) {
2884 tg = task_group(current);
2885 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002886
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002887 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002888 load += effective_load(tg, prev_cpu, 0, -weight);
2889 }
2890
2891 tg = task_group(p);
2892 weight = p->se.load.weight;
2893
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002894 /*
2895 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002896 * due to the sync cause above having dropped this_load to 0, we'll
2897 * always have an imbalance, but there's really nothing you can do
2898 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002899 *
2900 * Otherwise check if either cpus are near enough in load to allow this
2901 * task to be woken on this_cpu.
2902 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002903 if (this_load > 0) {
2904 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002905
2906 this_eff_load = 100;
2907 this_eff_load *= power_of(prev_cpu);
2908 this_eff_load *= this_load +
2909 effective_load(tg, this_cpu, weight, weight);
2910
2911 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2912 prev_eff_load *= power_of(this_cpu);
2913 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2914
2915 balanced = this_eff_load <= prev_eff_load;
2916 } else
2917 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002918
2919 /*
2920 * If the currently running task will sleep within
2921 * a reasonable amount of time then attract this newly
2922 * woken task:
2923 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002924 if (sync && balanced)
2925 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002926
Lucas De Marchi41acab82010-03-10 23:37:45 -03002927 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002928 tl_per_task = cpu_avg_load_per_task(this_cpu);
2929
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002930 if (balanced ||
2931 (this_load <= load &&
2932 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002933 /*
2934 * This domain has SD_WAKE_AFFINE and
2935 * p is cache cold in this domain, and
2936 * there is no bad imbalance.
2937 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002938 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002939 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002940
2941 return 1;
2942 }
2943 return 0;
2944}
2945
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002946/*
2947 * find_idlest_group finds and returns the least busy CPU group within the
2948 * domain.
2949 */
2950static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002951find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002952 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002953{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002954 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002955 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002956 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002957
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002958 do {
2959 unsigned long load, avg_load;
2960 int local_group;
2961 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002962
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002963 /* Skip over this group if it has no CPUs allowed */
2964 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002965 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002966 continue;
2967
2968 local_group = cpumask_test_cpu(this_cpu,
2969 sched_group_cpus(group));
2970
2971 /* Tally up the load of all CPUs in the group */
2972 avg_load = 0;
2973
2974 for_each_cpu(i, sched_group_cpus(group)) {
2975 /* Bias balancing toward cpus of our domain */
2976 if (local_group)
2977 load = source_load(i, load_idx);
2978 else
2979 load = target_load(i, load_idx);
2980
2981 avg_load += load;
2982 }
2983
2984 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002985 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002986
2987 if (local_group) {
2988 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002989 } else if (avg_load < min_load) {
2990 min_load = avg_load;
2991 idlest = group;
2992 }
2993 } while (group = group->next, group != sd->groups);
2994
2995 if (!idlest || 100*this_load < imbalance*min_load)
2996 return NULL;
2997 return idlest;
2998}
2999
3000/*
3001 * find_idlest_cpu - find the idlest cpu among the cpus in group.
3002 */
3003static int
3004find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
3005{
3006 unsigned long load, min_load = ULONG_MAX;
3007 int idlest = -1;
3008 int i;
3009
3010 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003011 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003012 load = weighted_cpuload(i);
3013
3014 if (load < min_load || (load == min_load && i == this_cpu)) {
3015 min_load = load;
3016 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003017 }
3018 }
3019
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003020 return idlest;
3021}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003022
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003023/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003024 * Try and locate an idle CPU in the sched_domain.
3025 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003026static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003027{
3028 int cpu = smp_processor_id();
3029 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003030 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07003031 struct sched_group *sg;
3032 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003033
3034 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003035 * If the task is going to be woken-up on this cpu and if it is
3036 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003037 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003038 if (target == cpu && idle_cpu(cpu))
3039 return cpu;
3040
3041 /*
3042 * If the task is going to be woken-up on the cpu where it previously
3043 * ran and if it is currently idle, then it the right target.
3044 */
3045 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003046 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003047
3048 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07003049 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003050 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01003051 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08003052 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07003053 sg = sd->groups;
3054 do {
3055 if (!cpumask_intersects(sched_group_cpus(sg),
3056 tsk_cpus_allowed(p)))
3057 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02003058
Linus Torvalds37407ea2012-09-16 12:29:43 -07003059 for_each_cpu(i, sched_group_cpus(sg)) {
3060 if (!idle_cpu(i))
3061 goto next;
3062 }
3063
3064 target = cpumask_first_and(sched_group_cpus(sg),
3065 tsk_cpus_allowed(p));
3066 goto done;
3067next:
3068 sg = sg->next;
3069 } while (sg != sd->groups);
3070 }
3071done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003072 return target;
3073}
3074
3075/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003076 * sched_balance_self: balance the current task (running on cpu) in domains
3077 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3078 * SD_BALANCE_EXEC.
3079 *
3080 * Balance, ie. select the least loaded group.
3081 *
3082 * Returns the target CPU number, or the same CPU if no balancing is needed.
3083 *
3084 * preempt must be disabled.
3085 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003086static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003087select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003088{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003089 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003090 int cpu = smp_processor_id();
3091 int prev_cpu = task_cpu(p);
3092 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003093 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003094 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003095
Peter Zijlstra29baa742012-04-23 12:11:21 +02003096 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003097 return prev_cpu;
3098
Peter Zijlstra0763a662009-09-14 19:37:39 +02003099 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003100 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003101 want_affine = 1;
3102 new_cpu = prev_cpu;
3103 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003104
Peter Zijlstradce840a2011-04-07 14:09:50 +02003105 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003106 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01003107 if (!(tmp->flags & SD_LOAD_BALANCE))
3108 continue;
3109
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003110 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003111 * If both cpu and prev_cpu are part of this domain,
3112 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003113 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003114 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3115 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3116 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003117 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003118 }
3119
Alex Shif03542a2012-07-26 08:55:34 +08003120 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003121 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003122 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003123
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003124 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003125 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003126 prev_cpu = cpu;
3127
3128 new_cpu = select_idle_sibling(p, prev_cpu);
3129 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003130 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003131
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003132 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003133 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003134 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003135 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003136
Peter Zijlstra0763a662009-09-14 19:37:39 +02003137 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003138 sd = sd->child;
3139 continue;
3140 }
3141
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003142 if (sd_flag & SD_BALANCE_WAKE)
3143 load_idx = sd->wake_idx;
3144
3145 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003146 if (!group) {
3147 sd = sd->child;
3148 continue;
3149 }
3150
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003151 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003152 if (new_cpu == -1 || new_cpu == cpu) {
3153 /* Now try balancing at a lower domain level of cpu */
3154 sd = sd->child;
3155 continue;
3156 }
3157
3158 /* Now try balancing at a lower domain level of new_cpu */
3159 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003160 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003161 sd = NULL;
3162 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003163 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003164 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003165 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003166 sd = tmp;
3167 }
3168 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003169 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003170unlock:
3171 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003172
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003173 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003174}
Paul Turner0a74bef2012-10-04 13:18:30 +02003175
3176/*
3177 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3178 * cfs_rq_of(p) references at time of call are still valid and identify the
3179 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3180 * other assumptions, including the state of rq->lock, should be made.
3181 */
3182static void
3183migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3184{
Paul Turneraff3e492012-10-04 13:18:30 +02003185 struct sched_entity *se = &p->se;
3186 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3187
3188 /*
3189 * Load tracking: accumulate removed load so that it can be processed
3190 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3191 * to blocked load iff they have a positive decay-count. It can never
3192 * be negative here since on-rq tasks have decay-count == 0.
3193 */
3194 if (se->avg.decay_count) {
3195 se->avg.decay_count = -__synchronize_entity_decay(se);
3196 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3197 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003198}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003199#endif /* CONFIG_SMP */
3200
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003201static unsigned long
3202wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003203{
3204 unsigned long gran = sysctl_sched_wakeup_granularity;
3205
3206 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003207 * Since its curr running now, convert the gran from real-time
3208 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003209 *
3210 * By using 'se' instead of 'curr' we penalize light tasks, so
3211 * they get preempted easier. That is, if 'se' < 'curr' then
3212 * the resulting gran will be larger, therefore penalizing the
3213 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3214 * be smaller, again penalizing the lighter task.
3215 *
3216 * This is especially important for buddies when the leftmost
3217 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003218 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003219 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003220}
3221
3222/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003223 * Should 'se' preempt 'curr'.
3224 *
3225 * |s1
3226 * |s2
3227 * |s3
3228 * g
3229 * |<--->|c
3230 *
3231 * w(c, s1) = -1
3232 * w(c, s2) = 0
3233 * w(c, s3) = 1
3234 *
3235 */
3236static int
3237wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3238{
3239 s64 gran, vdiff = curr->vruntime - se->vruntime;
3240
3241 if (vdiff <= 0)
3242 return -1;
3243
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003244 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003245 if (vdiff > gran)
3246 return 1;
3247
3248 return 0;
3249}
3250
Peter Zijlstra02479092008-11-04 21:25:10 +01003251static void set_last_buddy(struct sched_entity *se)
3252{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003253 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3254 return;
3255
3256 for_each_sched_entity(se)
3257 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003258}
3259
3260static void set_next_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)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003267}
3268
Rik van Rielac53db52011-02-01 09:51:03 -05003269static void set_skip_buddy(struct sched_entity *se)
3270{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003271 for_each_sched_entity(se)
3272 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003273}
3274
Peter Zijlstra464b7522008-10-24 11:06:15 +02003275/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003276 * Preempt the current task with a newly woken task if needed:
3277 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003278static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003279{
3280 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003281 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003282 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003283 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003284 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003285
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003286 if (unlikely(se == pse))
3287 return;
3288
Paul Turner5238cdd2011-07-21 09:43:37 -07003289 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003290 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003291 * unconditionally check_prempt_curr() after an enqueue (which may have
3292 * lead to a throttle). This both saves work and prevents false
3293 * next-buddy nomination below.
3294 */
3295 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3296 return;
3297
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003298 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003299 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003300 next_buddy_marked = 1;
3301 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003302
Bharata B Raoaec0a512008-08-28 14:42:49 +05303303 /*
3304 * We can come here with TIF_NEED_RESCHED already set from new task
3305 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003306 *
3307 * Note: this also catches the edge-case of curr being in a throttled
3308 * group (e.g. via set_curr_task), since update_curr() (in the
3309 * enqueue of curr) will have resulted in resched being set. This
3310 * prevents us from potentially nominating it as a false LAST_BUDDY
3311 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303312 */
3313 if (test_tsk_need_resched(curr))
3314 return;
3315
Darren Harta2f5c9a2011-02-22 13:04:33 -08003316 /* Idle tasks are by definition preempted by non-idle tasks. */
3317 if (unlikely(curr->policy == SCHED_IDLE) &&
3318 likely(p->policy != SCHED_IDLE))
3319 goto preempt;
3320
Ingo Molnar91c234b2007-10-15 17:00:18 +02003321 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003322 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3323 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003324 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003325 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003326 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003327
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003328 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003329 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003330 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003331 if (wakeup_preempt_entity(se, pse) == 1) {
3332 /*
3333 * Bias pick_next to pick the sched entity that is
3334 * triggering this preemption.
3335 */
3336 if (!next_buddy_marked)
3337 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003338 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003339 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003340
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003341 return;
3342
3343preempt:
3344 resched_task(curr);
3345 /*
3346 * Only set the backward buddy when the current task is still
3347 * on the rq. This can happen when a wakeup gets interleaved
3348 * with schedule on the ->pre_schedule() or idle_balance()
3349 * point, either of which can * drop the rq lock.
3350 *
3351 * Also, during early boot the idle thread is in the fair class,
3352 * for obvious reasons its a bad idea to schedule back to it.
3353 */
3354 if (unlikely(!se->on_rq || curr == rq->idle))
3355 return;
3356
3357 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3358 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003359}
3360
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003361static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003362{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003363 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003364 struct cfs_rq *cfs_rq = &rq->cfs;
3365 struct sched_entity *se;
3366
Tim Blechmann36ace272009-11-24 11:55:45 +01003367 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003368 return NULL;
3369
3370 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003371 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003372 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003373 cfs_rq = group_cfs_rq(se);
3374 } while (cfs_rq);
3375
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003376 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003377 if (hrtick_enabled(rq))
3378 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003379
3380 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003381}
3382
3383/*
3384 * Account for a descheduled task:
3385 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003386static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003387{
3388 struct sched_entity *se = &prev->se;
3389 struct cfs_rq *cfs_rq;
3390
3391 for_each_sched_entity(se) {
3392 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003393 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003394 }
3395}
3396
Rik van Rielac53db52011-02-01 09:51:03 -05003397/*
3398 * sched_yield() is very simple
3399 *
3400 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3401 */
3402static void yield_task_fair(struct rq *rq)
3403{
3404 struct task_struct *curr = rq->curr;
3405 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3406 struct sched_entity *se = &curr->se;
3407
3408 /*
3409 * Are we the only task in the tree?
3410 */
3411 if (unlikely(rq->nr_running == 1))
3412 return;
3413
3414 clear_buddies(cfs_rq, se);
3415
3416 if (curr->policy != SCHED_BATCH) {
3417 update_rq_clock(rq);
3418 /*
3419 * Update run-time statistics of the 'current'.
3420 */
3421 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003422 /*
3423 * Tell update_rq_clock() that we've just updated,
3424 * so we don't do microscopic update in schedule()
3425 * and double the fastpath cost.
3426 */
3427 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003428 }
3429
3430 set_skip_buddy(se);
3431}
3432
Mike Galbraithd95f4122011-02-01 09:50:51 -05003433static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3434{
3435 struct sched_entity *se = &p->se;
3436
Paul Turner5238cdd2011-07-21 09:43:37 -07003437 /* throttled hierarchies are not runnable */
3438 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003439 return false;
3440
3441 /* Tell the scheduler that we'd really like pse to run next. */
3442 set_next_buddy(se);
3443
Mike Galbraithd95f4122011-02-01 09:50:51 -05003444 yield_task_fair(rq);
3445
3446 return true;
3447}
3448
Peter Williams681f3e62007-10-24 18:23:51 +02003449#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003450/**************************************************
3451 * Fair scheduling class load-balancing methods:
3452 */
3453
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003454static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3455
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003456#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003457#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303458#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003459
3460struct lb_env {
3461 struct sched_domain *sd;
3462
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003463 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303464 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003465
3466 int dst_cpu;
3467 struct rq *dst_rq;
3468
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303469 struct cpumask *dst_grpmask;
3470 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003471 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003472 long imbalance;
Michael Wangb9403132012-07-12 16:10:13 +08003473 /* The set of CPUs under consideration for load-balancing */
3474 struct cpumask *cpus;
3475
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003476 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003477
3478 unsigned int loop;
3479 unsigned int loop_break;
3480 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003481};
3482
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003483/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003484 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003485 * Both runqueues must be locked.
3486 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003487static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003488{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003489 deactivate_task(env->src_rq, p, 0);
3490 set_task_cpu(p, env->dst_cpu);
3491 activate_task(env->dst_rq, p, 0);
3492 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003493}
3494
3495/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003496 * Is this task likely cache-hot:
3497 */
3498static int
3499task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3500{
3501 s64 delta;
3502
3503 if (p->sched_class != &fair_sched_class)
3504 return 0;
3505
3506 if (unlikely(p->policy == SCHED_IDLE))
3507 return 0;
3508
3509 /*
3510 * Buddy candidates are cache hot:
3511 */
3512 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3513 (&p->se == cfs_rq_of(&p->se)->next ||
3514 &p->se == cfs_rq_of(&p->se)->last))
3515 return 1;
3516
3517 if (sysctl_sched_migration_cost == -1)
3518 return 1;
3519 if (sysctl_sched_migration_cost == 0)
3520 return 0;
3521
3522 delta = now - p->se.exec_start;
3523
3524 return delta < (s64)sysctl_sched_migration_cost;
3525}
3526
3527/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003528 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3529 */
3530static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003531int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003532{
3533 int tsk_cache_hot = 0;
3534 /*
3535 * We do not migrate tasks that are:
3536 * 1) running (obviously), or
3537 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3538 * 3) are cache-hot on their current CPU.
3539 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003540 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303541 int new_dst_cpu;
3542
Lucas De Marchi41acab82010-03-10 23:37:45 -03003543 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303544
3545 /*
3546 * Remember if this task can be migrated to any other cpu in
3547 * our sched_group. We may want to revisit it if we couldn't
3548 * meet load balance goals by pulling other tasks on src_cpu.
3549 *
3550 * Also avoid computing new_dst_cpu if we have already computed
3551 * one in current iteration.
3552 */
3553 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3554 return 0;
3555
3556 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3557 tsk_cpus_allowed(p));
3558 if (new_dst_cpu < nr_cpu_ids) {
3559 env->flags |= LBF_SOME_PINNED;
3560 env->new_dst_cpu = new_dst_cpu;
3561 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003562 return 0;
3563 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303564
3565 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003566 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003567
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003568 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003569 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003570 return 0;
3571 }
3572
3573 /*
3574 * Aggressive migration if:
3575 * 1) task is cache cold, or
3576 * 2) too many balance attempts have failed.
3577 */
3578
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003579 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003580 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003581 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003582#ifdef CONFIG_SCHEDSTATS
3583 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003584 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003585 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003586 }
3587#endif
3588 return 1;
3589 }
3590
3591 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003592 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003593 return 0;
3594 }
3595 return 1;
3596}
3597
Peter Zijlstra897c3952009-12-17 17:45:42 +01003598/*
3599 * move_one_task tries to move exactly one task from busiest to this_rq, as
3600 * part of active balancing operations within "domain".
3601 * Returns 1 if successful and 0 otherwise.
3602 *
3603 * Called with both runqueues locked.
3604 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003605static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003606{
3607 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003608
Peter Zijlstra367456c2012-02-20 21:49:09 +01003609 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3610 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3611 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003612
Peter Zijlstra367456c2012-02-20 21:49:09 +01003613 if (!can_migrate_task(p, env))
3614 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003615
Peter Zijlstra367456c2012-02-20 21:49:09 +01003616 move_task(p, env);
3617 /*
3618 * Right now, this is only the second place move_task()
3619 * is called, so we can safely collect move_task()
3620 * stats here rather than inside move_task().
3621 */
3622 schedstat_inc(env->sd, lb_gained[env->idle]);
3623 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003624 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003625 return 0;
3626}
3627
Peter Zijlstra367456c2012-02-20 21:49:09 +01003628static unsigned long task_h_load(struct task_struct *p);
3629
Peter Zijlstraeb953082012-04-17 13:38:40 +02003630static const unsigned int sched_nr_migrate_break = 32;
3631
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003632/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003633 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003634 * this_rq, as part of a balancing operation within domain "sd".
3635 * Returns 1 if successful and 0 otherwise.
3636 *
3637 * Called with both runqueues locked.
3638 */
3639static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003640{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003641 struct list_head *tasks = &env->src_rq->cfs_tasks;
3642 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003643 unsigned long load;
3644 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003645
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003646 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003647 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003648
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003649 while (!list_empty(tasks)) {
3650 p = list_first_entry(tasks, struct task_struct, se.group_node);
3651
Peter Zijlstra367456c2012-02-20 21:49:09 +01003652 env->loop++;
3653 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003654 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003655 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003656
3657 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003658 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003659 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003660 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003661 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003662 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003663
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003664 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003665 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003666
Peter Zijlstra367456c2012-02-20 21:49:09 +01003667 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003668
Peter Zijlstraeb953082012-04-17 13:38:40 +02003669 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003670 goto next;
3671
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003672 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003673 goto next;
3674
3675 if (!can_migrate_task(p, env))
3676 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003677
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003678 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003679 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003680 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003681
3682#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003683 /*
3684 * NEWIDLE balancing is a source of latency, so preemptible
3685 * kernels will stop after the first task is pulled to minimize
3686 * the critical section.
3687 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003688 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003689 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003690#endif
3691
Peter Zijlstraee00e662009-12-17 17:25:20 +01003692 /*
3693 * We only want to steal up to the prescribed amount of
3694 * weighted load.
3695 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003696 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003697 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003698
Peter Zijlstra367456c2012-02-20 21:49:09 +01003699 continue;
3700next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003701 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003702 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003703
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003704 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003705 * Right now, this is one of only two places move_task() is called,
3706 * so we can safely collect move_task() stats here rather than
3707 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003708 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003709 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003710
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003711 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003712}
3713
Peter Zijlstra230059de2009-12-17 17:47:12 +01003714#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003715/*
3716 * update tg->load_weight by folding this cpu's load_avg
3717 */
Paul Turner48a16752012-10-04 13:18:31 +02003718static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003719{
Paul Turner48a16752012-10-04 13:18:31 +02003720 struct sched_entity *se = tg->se[cpu];
3721 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu];
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003722
Paul Turner48a16752012-10-04 13:18:31 +02003723 /* throttled entities do not contribute to load */
3724 if (throttled_hierarchy(cfs_rq))
3725 return;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003726
Paul Turneraff3e492012-10-04 13:18:30 +02003727 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003728
Paul Turner82958362012-10-04 13:18:31 +02003729 if (se) {
3730 update_entity_load_avg(se, 1);
3731 /*
3732 * We pivot on our runnable average having decayed to zero for
3733 * list removal. This generally implies that all our children
3734 * have also been removed (modulo rounding error or bandwidth
3735 * control); however, such cases are rare and we can fix these
3736 * at enqueue.
3737 *
3738 * TODO: fix up out-of-order children on enqueue.
3739 */
3740 if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
3741 list_del_leaf_cfs_rq(cfs_rq);
3742 } else {
Paul Turner48a16752012-10-04 13:18:31 +02003743 struct rq *rq = rq_of(cfs_rq);
Paul Turner82958362012-10-04 13:18:31 +02003744 update_rq_runnable_avg(rq, rq->nr_running);
3745 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003746}
3747
Paul Turner48a16752012-10-04 13:18:31 +02003748static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003749{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003750 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02003751 struct cfs_rq *cfs_rq;
3752 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003753
Paul Turner48a16752012-10-04 13:18:31 +02003754 raw_spin_lock_irqsave(&rq->lock, flags);
3755 update_rq_clock(rq);
Peter Zijlstra9763b672011-07-13 13:09:25 +02003756 /*
3757 * Iterates the task_group tree in a bottom up fashion, see
3758 * list_add_leaf_cfs_rq() for details.
3759 */
Paul Turner64660c82011-07-21 09:43:36 -07003760 for_each_leaf_cfs_rq(rq, cfs_rq) {
Paul Turner48a16752012-10-04 13:18:31 +02003761 /*
3762 * Note: We may want to consider periodically releasing
3763 * rq->lock about these updates so that creating many task
3764 * groups does not result in continually extending hold time.
3765 */
3766 __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003767 }
Paul Turner48a16752012-10-04 13:18:31 +02003768
3769 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003770}
3771
Peter Zijlstra9763b672011-07-13 13:09:25 +02003772/*
3773 * Compute the cpu's hierarchical load factor for each task group.
3774 * This needs to be done in a top-down fashion because the load of a child
3775 * group is a fraction of its parents load.
3776 */
3777static int tg_load_down(struct task_group *tg, void *data)
3778{
3779 unsigned long load;
3780 long cpu = (long)data;
3781
3782 if (!tg->parent) {
3783 load = cpu_rq(cpu)->load.weight;
3784 } else {
3785 load = tg->parent->cfs_rq[cpu]->h_load;
3786 load *= tg->se[cpu]->load.weight;
3787 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3788 }
3789
3790 tg->cfs_rq[cpu]->h_load = load;
3791
3792 return 0;
3793}
3794
3795static void update_h_load(long cpu)
3796{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003797 struct rq *rq = cpu_rq(cpu);
3798 unsigned long now = jiffies;
3799
3800 if (rq->h_load_throttle == now)
3801 return;
3802
3803 rq->h_load_throttle = now;
3804
Peter Zijlstra367456c2012-02-20 21:49:09 +01003805 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003806 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003807 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003808}
3809
Peter Zijlstra367456c2012-02-20 21:49:09 +01003810static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003811{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003812 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3813 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003814
Peter Zijlstra367456c2012-02-20 21:49:09 +01003815 load = p->se.load.weight;
3816 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003817
Peter Zijlstra367456c2012-02-20 21:49:09 +01003818 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003819}
3820#else
Paul Turner48a16752012-10-04 13:18:31 +02003821static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003822{
3823}
3824
Peter Zijlstra367456c2012-02-20 21:49:09 +01003825static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003826{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003827}
3828
3829static unsigned long task_h_load(struct task_struct *p)
3830{
3831 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003832}
3833#endif
3834
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003835/********** Helpers for find_busiest_group ************************/
3836/*
3837 * sd_lb_stats - Structure to store the statistics of a sched_domain
3838 * during load balancing.
3839 */
3840struct sd_lb_stats {
3841 struct sched_group *busiest; /* Busiest group in this sd */
3842 struct sched_group *this; /* Local group in this sd */
3843 unsigned long total_load; /* Total load of all groups in sd */
3844 unsigned long total_pwr; /* Total power of all groups in sd */
3845 unsigned long avg_load; /* Average load across all groups in sd */
3846
3847 /** Statistics of this group */
3848 unsigned long this_load;
3849 unsigned long this_load_per_task;
3850 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003851 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003852 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003853
3854 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003855 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003856 unsigned long max_load;
3857 unsigned long busiest_load_per_task;
3858 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003859 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003860 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003861 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003862
3863 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003864};
3865
3866/*
3867 * sg_lb_stats - stats of a sched_group required for load_balancing
3868 */
3869struct sg_lb_stats {
3870 unsigned long avg_load; /*Avg load across the CPUs of the group */
3871 unsigned long group_load; /* Total load over the CPUs of the group */
3872 unsigned long sum_nr_running; /* Nr tasks running in the group */
3873 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3874 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003875 unsigned long idle_cpus;
3876 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003877 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003878 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003879};
3880
3881/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003882 * get_sd_load_idx - Obtain the load index for a given sched domain.
3883 * @sd: The sched_domain whose load_idx is to be obtained.
3884 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3885 */
3886static inline int get_sd_load_idx(struct sched_domain *sd,
3887 enum cpu_idle_type idle)
3888{
3889 int load_idx;
3890
3891 switch (idle) {
3892 case CPU_NOT_IDLE:
3893 load_idx = sd->busy_idx;
3894 break;
3895
3896 case CPU_NEWLY_IDLE:
3897 load_idx = sd->newidle_idx;
3898 break;
3899 default:
3900 load_idx = sd->idle_idx;
3901 break;
3902 }
3903
3904 return load_idx;
3905}
3906
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003907unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3908{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003909 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003910}
3911
3912unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3913{
3914 return default_scale_freq_power(sd, cpu);
3915}
3916
3917unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3918{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003919 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003920 unsigned long smt_gain = sd->smt_gain;
3921
3922 smt_gain /= weight;
3923
3924 return smt_gain;
3925}
3926
3927unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3928{
3929 return default_scale_smt_power(sd, cpu);
3930}
3931
3932unsigned long scale_rt_power(int cpu)
3933{
3934 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003935 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003936
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003937 /*
3938 * Since we're reading these variables without serialization make sure
3939 * we read them once before doing sanity checks on them.
3940 */
3941 age_stamp = ACCESS_ONCE(rq->age_stamp);
3942 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003943
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003944 total = sched_avg_period() + (rq->clock - age_stamp);
3945
3946 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003947 /* Ensures that power won't end up being negative */
3948 available = 0;
3949 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003950 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003951 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003952
Nikhil Rao1399fa72011-05-18 10:09:39 -07003953 if (unlikely((s64)total < SCHED_POWER_SCALE))
3954 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003955
Nikhil Rao1399fa72011-05-18 10:09:39 -07003956 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003957
3958 return div_u64(available, total);
3959}
3960
3961static void update_cpu_power(struct sched_domain *sd, int cpu)
3962{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003963 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003964 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003965 struct sched_group *sdg = sd->groups;
3966
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003967 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3968 if (sched_feat(ARCH_POWER))
3969 power *= arch_scale_smt_power(sd, cpu);
3970 else
3971 power *= default_scale_smt_power(sd, cpu);
3972
Nikhil Rao1399fa72011-05-18 10:09:39 -07003973 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003974 }
3975
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003976 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003977
3978 if (sched_feat(ARCH_POWER))
3979 power *= arch_scale_freq_power(sd, cpu);
3980 else
3981 power *= default_scale_freq_power(sd, cpu);
3982
Nikhil Rao1399fa72011-05-18 10:09:39 -07003983 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003984
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003985 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003986 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003987
3988 if (!power)
3989 power = 1;
3990
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003991 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003992 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003993}
3994
Peter Zijlstra029632f2011-10-25 10:00:11 +02003995void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003996{
3997 struct sched_domain *child = sd->child;
3998 struct sched_group *group, *sdg = sd->groups;
3999 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01004000 unsigned long interval;
4001
4002 interval = msecs_to_jiffies(sd->balance_interval);
4003 interval = clamp(interval, 1UL, max_load_balance_interval);
4004 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004005
4006 if (!child) {
4007 update_cpu_power(sd, cpu);
4008 return;
4009 }
4010
4011 power = 0;
4012
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02004013 if (child->flags & SD_OVERLAP) {
4014 /*
4015 * SD_OVERLAP domains cannot assume that child groups
4016 * span the current group.
4017 */
4018
4019 for_each_cpu(cpu, sched_group_cpus(sdg))
4020 power += power_of(cpu);
4021 } else {
4022 /*
4023 * !SD_OVERLAP domains can assume that child groups
4024 * span the current group.
4025 */
4026
4027 group = child->groups;
4028 do {
4029 power += group->sgp->power;
4030 group = group->next;
4031 } while (group != child->groups);
4032 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004033
Peter Zijlstrac3decf02012-05-31 12:05:32 +02004034 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004035}
4036
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004037/*
4038 * Try and fix up capacity for tiny siblings, this is needed when
4039 * things like SD_ASYM_PACKING need f_b_g to select another sibling
4040 * which on its own isn't powerful enough.
4041 *
4042 * See update_sd_pick_busiest() and check_asym_packing().
4043 */
4044static inline int
4045fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
4046{
4047 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07004048 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004049 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02004050 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004051 return 0;
4052
4053 /*
4054 * If ~90% of the cpu_power is still there, we're good.
4055 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004056 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004057 return 1;
4058
4059 return 0;
4060}
4061
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004062/**
4063 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004064 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004065 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004066 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004067 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004068 * @balance: Should we balance.
4069 * @sgs: variable to hold the statistics for this group.
4070 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004071static inline void update_sg_lb_stats(struct lb_env *env,
4072 struct sched_group *group, int load_idx,
Michael Wangb9403132012-07-12 16:10:13 +08004073 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004074{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004075 unsigned long nr_running, max_nr_running, min_nr_running;
4076 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004077 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004078 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004079 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004080
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004081 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004082 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004083
4084 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004085 max_cpu_load = 0;
4086 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004087 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004088 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004089
Michael Wangb9403132012-07-12 16:10:13 +08004090 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004091 struct rq *rq = cpu_rq(i);
4092
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004093 nr_running = rq->nr_running;
4094
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004095 /* Bias balancing toward cpus of our domain */
4096 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004097 if (idle_cpu(i) && !first_idle_cpu &&
4098 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004099 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004100 balance_cpu = i;
4101 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004102
4103 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004104 } else {
4105 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004106 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107 max_cpu_load = load;
4108 if (min_cpu_load > load)
4109 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004110
4111 if (nr_running > max_nr_running)
4112 max_nr_running = nr_running;
4113 if (min_nr_running > nr_running)
4114 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004115 }
4116
4117 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004118 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004119 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004120 if (idle_cpu(i))
4121 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004122 }
4123
4124 /*
4125 * First idle cpu or the first cpu(busiest) in this sched group
4126 * is eligible for doing load balancing at this and above
4127 * domains. In the newly idle case, we will allow all the cpu's
4128 * to do the newly idle load balance.
4129 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004130 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004131 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004132 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004133 *balance = 0;
4134 return;
4135 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004136 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004137 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004138 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004139 }
4140
4141 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004142 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004143
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004144 /*
4145 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004146 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004147 *
4148 * APZ: with cgroup the avg task weight can vary wildly and
4149 * might not be a suitable number - should we keep a
4150 * normalized nr_running number somewhere that negates
4151 * the hierarchy?
4152 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004153 if (sgs->sum_nr_running)
4154 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004155
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004156 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4157 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004158 sgs->group_imb = 1;
4159
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004160 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004161 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004162 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004163 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004164 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004165
4166 if (sgs->group_capacity > sgs->sum_nr_running)
4167 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004168}
4169
4170/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004171 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004172 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004173 * @sds: sched_domain statistics
4174 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004175 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004176 *
4177 * Determine if @sg is a busier group than the previously selected
4178 * busiest group.
4179 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004180static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004181 struct sd_lb_stats *sds,
4182 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004183 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004184{
4185 if (sgs->avg_load <= sds->max_load)
4186 return false;
4187
4188 if (sgs->sum_nr_running > sgs->group_capacity)
4189 return true;
4190
4191 if (sgs->group_imb)
4192 return true;
4193
4194 /*
4195 * ASYM_PACKING needs to move all the work to the lowest
4196 * numbered CPUs in the group, therefore mark all groups
4197 * higher than ourself as busy.
4198 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004199 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4200 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004201 if (!sds->busiest)
4202 return true;
4203
4204 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4205 return true;
4206 }
4207
4208 return false;
4209}
4210
4211/**
Hui Kang461819a2011-10-11 23:00:59 -04004212 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004213 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004214 * @balance: Should we balance.
4215 * @sds: variable to hold the statistics for this sched_domain.
4216 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004217static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004218 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004219{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004220 struct sched_domain *child = env->sd->child;
4221 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004222 struct sg_lb_stats sgs;
4223 int load_idx, prefer_sibling = 0;
4224
4225 if (child && child->flags & SD_PREFER_SIBLING)
4226 prefer_sibling = 1;
4227
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004228 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004229
4230 do {
4231 int local_group;
4232
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004233 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004234 memset(&sgs, 0, sizeof(sgs));
Michael Wangb9403132012-07-12 16:10:13 +08004235 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004236
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004237 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004238 return;
4239
4240 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004241 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004242
4243 /*
4244 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004245 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004246 * and move all the excess tasks away. We lower the capacity
4247 * of a group only if the local group has the capacity to fit
4248 * these excess tasks, i.e. nr_running < group_capacity. The
4249 * extra check prevents the case where you always pull from the
4250 * heaviest group when it is already under-utilized (possible
4251 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004252 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004253 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004254 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4255
4256 if (local_group) {
4257 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004258 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004259 sds->this_nr_running = sgs.sum_nr_running;
4260 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004261 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004262 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004263 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004264 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004265 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004266 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004267 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004268 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004269 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004270 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004271 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004272 sds->group_imb = sgs.group_imb;
4273 }
4274
Michael Neuling532cb4c2010-06-08 14:57:02 +10004275 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004276 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004277}
4278
Michael Neuling532cb4c2010-06-08 14:57:02 +10004279/**
4280 * check_asym_packing - Check to see if the group is packed into the
4281 * sched doman.
4282 *
4283 * This is primarily intended to used at the sibling level. Some
4284 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4285 * case of POWER7, it can move to lower SMT modes only when higher
4286 * threads are idle. When in lower SMT modes, the threads will
4287 * perform better since they share less core resources. Hence when we
4288 * have idle threads, we want them to be the higher ones.
4289 *
4290 * This packing function is run on idle threads. It checks to see if
4291 * the busiest CPU in this domain (core in the P7 case) has a higher
4292 * CPU number than the packing function is being run on. Here we are
4293 * assuming lower CPU number will be equivalent to lower a SMT thread
4294 * number.
4295 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004296 * Returns 1 when packing is required and a task should be moved to
4297 * this CPU. The amount of the imbalance is returned in *imbalance.
4298 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004299 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004300 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004301 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004302static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004303{
4304 int busiest_cpu;
4305
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004306 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004307 return 0;
4308
4309 if (!sds->busiest)
4310 return 0;
4311
4312 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004313 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004314 return 0;
4315
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004316 env->imbalance = DIV_ROUND_CLOSEST(
4317 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4318
Michael Neuling532cb4c2010-06-08 14:57:02 +10004319 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004320}
4321
4322/**
4323 * fix_small_imbalance - Calculate the minor imbalance that exists
4324 * amongst the groups of a sched_domain, during
4325 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004326 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004327 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004328 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004329static inline
4330void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004331{
4332 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4333 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004334 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004335
4336 if (sds->this_nr_running) {
4337 sds->this_load_per_task /= sds->this_nr_running;
4338 if (sds->busiest_load_per_task >
4339 sds->this_load_per_task)
4340 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004341 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004342 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004343 cpu_avg_load_per_task(env->dst_cpu);
4344 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004345
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004346 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004347 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004348 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004349
4350 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4351 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004352 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004353 return;
4354 }
4355
4356 /*
4357 * OK, we don't have enough imbalance to justify moving tasks,
4358 * however we may be able to increase total CPU power used by
4359 * moving them.
4360 */
4361
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004362 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004363 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004364 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004365 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004366 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004367
4368 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004369 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004370 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004371 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004372 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004373 min(sds->busiest_load_per_task, sds->max_load - tmp);
4374
4375 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004376 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004377 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004378 tmp = (sds->max_load * sds->busiest->sgp->power) /
4379 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004380 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004381 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004382 sds->this->sgp->power;
4383 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004384 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004385 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004386
4387 /* Move if we gain throughput */
4388 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004389 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004390}
4391
4392/**
4393 * calculate_imbalance - Calculate the amount of imbalance present within the
4394 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004395 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004396 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004397 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004398static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004399{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004400 unsigned long max_pull, load_above_capacity = ~0UL;
4401
4402 sds->busiest_load_per_task /= sds->busiest_nr_running;
4403 if (sds->group_imb) {
4404 sds->busiest_load_per_task =
4405 min(sds->busiest_load_per_task, sds->avg_load);
4406 }
4407
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004408 /*
4409 * In the presence of smp nice balancing, certain scenarios can have
4410 * max load less than avg load(as we skip the groups at or below
4411 * its cpu_power, while calculating max_load..)
4412 */
4413 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004414 env->imbalance = 0;
4415 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004416 }
4417
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004418 if (!sds->group_imb) {
4419 /*
4420 * Don't want to pull so many tasks that a group would go idle.
4421 */
4422 load_above_capacity = (sds->busiest_nr_running -
4423 sds->busiest_group_capacity);
4424
Nikhil Rao1399fa72011-05-18 10:09:39 -07004425 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004426
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004427 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004428 }
4429
4430 /*
4431 * We're trying to get all the cpus to the average_load, so we don't
4432 * want to push ourselves above the average load, nor do we wish to
4433 * reduce the max loaded cpu below the average load. At the same time,
4434 * we also don't want to reduce the group load below the group capacity
4435 * (so that we can implement power-savings policies etc). Thus we look
4436 * for the minimum possible imbalance.
4437 * Be careful of negative numbers as they'll appear as very large values
4438 * with unsigned longs.
4439 */
4440 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004441
4442 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004443 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004444 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004445 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004446
4447 /*
4448 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004449 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004450 * a think about bumping its value to force at least one task to be
4451 * moved
4452 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004453 if (env->imbalance < sds->busiest_load_per_task)
4454 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004455
4456}
Nikhil Raofab47622010-10-15 13:12:29 -07004457
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004458/******* find_busiest_group() helpers end here *********************/
4459
4460/**
4461 * find_busiest_group - Returns the busiest group within the sched_domain
4462 * if there is an imbalance. If there isn't an imbalance, and
4463 * the user has opted for power-savings, it returns a group whose
4464 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4465 * such a group exists.
4466 *
4467 * Also calculates the amount of weighted load which should be moved
4468 * to restore balance.
4469 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004470 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004471 * @balance: Pointer to a variable indicating if this_cpu
4472 * is the appropriate cpu to perform load balancing at this_level.
4473 *
4474 * Returns: - the busiest group if imbalance exists.
4475 * - If no imbalance and user has opted for power-savings balance,
4476 * return the least loaded group whose CPUs can be
4477 * put to idle by rebalancing its tasks onto our group.
4478 */
4479static struct sched_group *
Michael Wangb9403132012-07-12 16:10:13 +08004480find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004481{
4482 struct sd_lb_stats sds;
4483
4484 memset(&sds, 0, sizeof(sds));
4485
4486 /*
4487 * Compute the various statistics relavent for load balancing at
4488 * this level.
4489 */
Michael Wangb9403132012-07-12 16:10:13 +08004490 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004491
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004492 /*
4493 * this_cpu is not the appropriate cpu to perform load balancing at
4494 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004495 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004496 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004497 goto ret;
4498
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004499 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4500 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004501 return sds.busiest;
4502
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004503 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004504 if (!sds.busiest || sds.busiest_nr_running == 0)
4505 goto out_balanced;
4506
Nikhil Rao1399fa72011-05-18 10:09:39 -07004507 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004508
Peter Zijlstra866ab432011-02-21 18:56:47 +01004509 /*
4510 * If the busiest group is imbalanced the below checks don't
4511 * work because they assumes all things are equal, which typically
4512 * isn't true due to cpus_allowed constraints and the like.
4513 */
4514 if (sds.group_imb)
4515 goto force_balance;
4516
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004517 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004518 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004519 !sds.busiest_has_capacity)
4520 goto force_balance;
4521
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004522 /*
4523 * If the local group is more busy than the selected busiest group
4524 * don't try and pull any tasks.
4525 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004526 if (sds.this_load >= sds.max_load)
4527 goto out_balanced;
4528
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004529 /*
4530 * Don't pull any tasks if this group is already above the domain
4531 * average load.
4532 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004533 if (sds.this_load >= sds.avg_load)
4534 goto out_balanced;
4535
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004536 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004537 /*
4538 * This cpu is idle. If the busiest group load doesn't
4539 * have more tasks than the number of available cpu's and
4540 * there is no imbalance between this and busiest group
4541 * wrt to idle cpu's, it is balanced.
4542 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004543 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004544 sds.busiest_nr_running <= sds.busiest_group_weight)
4545 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004546 } else {
4547 /*
4548 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4549 * imbalance_pct to be conservative.
4550 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004551 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004552 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004553 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004554
Nikhil Raofab47622010-10-15 13:12:29 -07004555force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004556 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004557 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004558 return sds.busiest;
4559
4560out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004561ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004562 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004563 return NULL;
4564}
4565
4566/*
4567 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4568 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004569static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004570 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004571{
4572 struct rq *busiest = NULL, *rq;
4573 unsigned long max_load = 0;
4574 int i;
4575
4576 for_each_cpu(i, sched_group_cpus(group)) {
4577 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004578 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4579 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004580 unsigned long wl;
4581
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004582 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004583 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004584
Michael Wangb9403132012-07-12 16:10:13 +08004585 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004586 continue;
4587
4588 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004589 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004590
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004591 /*
4592 * When comparing with imbalance, use weighted_cpuload()
4593 * which is not scaled with the cpu power.
4594 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004595 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004596 continue;
4597
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004598 /*
4599 * For the load comparisons with the other cpu's, consider
4600 * the weighted_cpuload() scaled with the cpu power, so that
4601 * the load can be moved away from the cpu that is potentially
4602 * running at a lower capacity.
4603 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004604 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004605
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004606 if (wl > max_load) {
4607 max_load = wl;
4608 busiest = rq;
4609 }
4610 }
4611
4612 return busiest;
4613}
4614
4615/*
4616 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4617 * so long as it is large enough.
4618 */
4619#define MAX_PINNED_INTERVAL 512
4620
4621/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004622DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004623
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004624static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004625{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004626 struct sched_domain *sd = env->sd;
4627
4628 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004629
4630 /*
4631 * ASYM_PACKING needs to force migrate tasks from busy but
4632 * higher numbered CPUs in order to pack all tasks in the
4633 * lowest numbered CPUs.
4634 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004635 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004636 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004637 }
4638
4639 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4640}
4641
Tejun Heo969c7922010-05-06 18:49:21 +02004642static int active_load_balance_cpu_stop(void *data);
4643
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004644/*
4645 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4646 * tasks if there is an imbalance.
4647 */
4648static int load_balance(int this_cpu, struct rq *this_rq,
4649 struct sched_domain *sd, enum cpu_idle_type idle,
4650 int *balance)
4651{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304652 int ld_moved, cur_ld_moved, active_balance = 0;
4653 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004654 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004655 struct rq *busiest;
4656 unsigned long flags;
4657 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4658
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004659 struct lb_env env = {
4660 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004661 .dst_cpu = this_cpu,
4662 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304663 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004664 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004665 .loop_break = sched_nr_migrate_break,
Michael Wangb9403132012-07-12 16:10:13 +08004666 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004667 };
4668
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004669 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304670 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004671
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004672 schedstat_inc(sd, lb_count[idle]);
4673
4674redo:
Michael Wangb9403132012-07-12 16:10:13 +08004675 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004676
4677 if (*balance == 0)
4678 goto out_balanced;
4679
4680 if (!group) {
4681 schedstat_inc(sd, lb_nobusyg[idle]);
4682 goto out_balanced;
4683 }
4684
Michael Wangb9403132012-07-12 16:10:13 +08004685 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004686 if (!busiest) {
4687 schedstat_inc(sd, lb_nobusyq[idle]);
4688 goto out_balanced;
4689 }
4690
Michael Wang78feefc2012-08-06 16:41:59 +08004691 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004692
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004693 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004694
4695 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304696 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004697 if (busiest->nr_running > 1) {
4698 /*
4699 * Attempt to move tasks. If find_busiest_group has found
4700 * an imbalance but busiest->nr_running <= 1, the group is
4701 * still unbalanced. ld_moved simply stays zero, so it is
4702 * correctly treated as an imbalance.
4703 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004704 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004705 env.src_cpu = busiest->cpu;
4706 env.src_rq = busiest;
4707 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004708
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004709 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004710more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004711 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004712 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304713
4714 /*
4715 * cur_ld_moved - load moved in current iteration
4716 * ld_moved - cumulative load moved across iterations
4717 */
4718 cur_ld_moved = move_tasks(&env);
4719 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004720 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004721 local_irq_restore(flags);
4722
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004723 if (env.flags & LBF_NEED_BREAK) {
4724 env.flags &= ~LBF_NEED_BREAK;
4725 goto more_balance;
4726 }
4727
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004728 /*
4729 * some other cpu did the load balance for us.
4730 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304731 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4732 resched_cpu(env.dst_cpu);
4733
4734 /*
4735 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4736 * us and move them to an alternate dst_cpu in our sched_group
4737 * where they can run. The upper limit on how many times we
4738 * iterate on same src_cpu is dependent on number of cpus in our
4739 * sched_group.
4740 *
4741 * This changes load balance semantics a bit on who can move
4742 * load to a given_cpu. In addition to the given_cpu itself
4743 * (or a ilb_cpu acting on its behalf where given_cpu is
4744 * nohz-idle), we now have balance_cpu in a position to move
4745 * load to given_cpu. In rare situations, this may cause
4746 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4747 * _independently_ and at _same_ time to move some load to
4748 * given_cpu) causing exceess load to be moved to given_cpu.
4749 * This however should not happen so much in practice and
4750 * moreover subsequent load balance cycles should correct the
4751 * excess load moved.
4752 */
4753 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4754 lb_iterations++ < max_lb_iterations) {
4755
Michael Wang78feefc2012-08-06 16:41:59 +08004756 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304757 env.dst_cpu = env.new_dst_cpu;
4758 env.flags &= ~LBF_SOME_PINNED;
4759 env.loop = 0;
4760 env.loop_break = sched_nr_migrate_break;
4761 /*
4762 * Go back to "more_balance" rather than "redo" since we
4763 * need to continue with same src_cpu.
4764 */
4765 goto more_balance;
4766 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004767
4768 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004769 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004770 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304771 if (!cpumask_empty(cpus)) {
4772 env.loop = 0;
4773 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004774 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304775 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004776 goto out_balanced;
4777 }
4778 }
4779
4780 if (!ld_moved) {
4781 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004782 /*
4783 * Increment the failure counter only on periodic balance.
4784 * We do not want newidle balance, which can be very
4785 * frequent, pollute the failure counter causing
4786 * excessive cache_hot migrations and active balances.
4787 */
4788 if (idle != CPU_NEWLY_IDLE)
4789 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004790
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004791 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004792 raw_spin_lock_irqsave(&busiest->lock, flags);
4793
Tejun Heo969c7922010-05-06 18:49:21 +02004794 /* don't kick the active_load_balance_cpu_stop,
4795 * if the curr task on busiest cpu can't be
4796 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004797 */
4798 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004799 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004800 raw_spin_unlock_irqrestore(&busiest->lock,
4801 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004802 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004803 goto out_one_pinned;
4804 }
4805
Tejun Heo969c7922010-05-06 18:49:21 +02004806 /*
4807 * ->active_balance synchronizes accesses to
4808 * ->active_balance_work. Once set, it's cleared
4809 * only after active load balance is finished.
4810 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004811 if (!busiest->active_balance) {
4812 busiest->active_balance = 1;
4813 busiest->push_cpu = this_cpu;
4814 active_balance = 1;
4815 }
4816 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004817
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004818 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004819 stop_one_cpu_nowait(cpu_of(busiest),
4820 active_load_balance_cpu_stop, busiest,
4821 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004822 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004823
4824 /*
4825 * We've kicked active balancing, reset the failure
4826 * counter.
4827 */
4828 sd->nr_balance_failed = sd->cache_nice_tries+1;
4829 }
4830 } else
4831 sd->nr_balance_failed = 0;
4832
4833 if (likely(!active_balance)) {
4834 /* We were unbalanced, so reset the balancing interval */
4835 sd->balance_interval = sd->min_interval;
4836 } else {
4837 /*
4838 * If we've begun active balancing, start to back off. This
4839 * case may not be covered by the all_pinned logic if there
4840 * is only 1 task on the busy runqueue (because we don't call
4841 * move_tasks).
4842 */
4843 if (sd->balance_interval < sd->max_interval)
4844 sd->balance_interval *= 2;
4845 }
4846
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004847 goto out;
4848
4849out_balanced:
4850 schedstat_inc(sd, lb_balanced[idle]);
4851
4852 sd->nr_balance_failed = 0;
4853
4854out_one_pinned:
4855 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004856 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004857 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004858 (sd->balance_interval < sd->max_interval))
4859 sd->balance_interval *= 2;
4860
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004861 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004862out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004863 return ld_moved;
4864}
4865
4866/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004867 * idle_balance is called by schedule() if this_cpu is about to become
4868 * idle. Attempts to pull tasks from other CPUs.
4869 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004870void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004871{
4872 struct sched_domain *sd;
4873 int pulled_task = 0;
4874 unsigned long next_balance = jiffies + HZ;
4875
4876 this_rq->idle_stamp = this_rq->clock;
4877
4878 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4879 return;
4880
Ben Segall18bf2802012-10-04 12:51:20 +02004881 update_rq_runnable_avg(this_rq, 1);
4882
Peter Zijlstraf492e122009-12-23 15:29:42 +01004883 /*
4884 * Drop the rq->lock, but keep IRQ/preempt disabled.
4885 */
4886 raw_spin_unlock(&this_rq->lock);
4887
Paul Turner48a16752012-10-04 13:18:31 +02004888 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004889 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004890 for_each_domain(this_cpu, sd) {
4891 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004892 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004893
4894 if (!(sd->flags & SD_LOAD_BALANCE))
4895 continue;
4896
Peter Zijlstraf492e122009-12-23 15:29:42 +01004897 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004898 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004899 pulled_task = load_balance(this_cpu, this_rq,
4900 sd, CPU_NEWLY_IDLE, &balance);
4901 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004902
4903 interval = msecs_to_jiffies(sd->balance_interval);
4904 if (time_after(next_balance, sd->last_balance + interval))
4905 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004906 if (pulled_task) {
4907 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004908 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004909 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004910 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004911 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004912
4913 raw_spin_lock(&this_rq->lock);
4914
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004915 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4916 /*
4917 * We are going idle. next_balance may be set based on
4918 * a busy processor. So reset next_balance.
4919 */
4920 this_rq->next_balance = next_balance;
4921 }
4922}
4923
4924/*
Tejun Heo969c7922010-05-06 18:49:21 +02004925 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4926 * running tasks off the busiest CPU onto idle CPUs. It requires at
4927 * least 1 task to be running on each physical CPU where possible, and
4928 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004929 */
Tejun Heo969c7922010-05-06 18:49:21 +02004930static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004931{
Tejun Heo969c7922010-05-06 18:49:21 +02004932 struct rq *busiest_rq = data;
4933 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004934 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004935 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004936 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004937
4938 raw_spin_lock_irq(&busiest_rq->lock);
4939
4940 /* make sure the requested cpu hasn't gone down in the meantime */
4941 if (unlikely(busiest_cpu != smp_processor_id() ||
4942 !busiest_rq->active_balance))
4943 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004944
4945 /* Is there any task to move? */
4946 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004947 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004948
4949 /*
4950 * This condition is "impossible", if it occurs
4951 * we need to fix it. Originally reported by
4952 * Bjorn Helgaas on a 128-cpu setup.
4953 */
4954 BUG_ON(busiest_rq == target_rq);
4955
4956 /* move a task from busiest_rq to target_rq */
4957 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004958
4959 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004960 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004961 for_each_domain(target_cpu, sd) {
4962 if ((sd->flags & SD_LOAD_BALANCE) &&
4963 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4964 break;
4965 }
4966
4967 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004968 struct lb_env env = {
4969 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004970 .dst_cpu = target_cpu,
4971 .dst_rq = target_rq,
4972 .src_cpu = busiest_rq->cpu,
4973 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004974 .idle = CPU_IDLE,
4975 };
4976
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004977 schedstat_inc(sd, alb_count);
4978
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004979 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004980 schedstat_inc(sd, alb_pushed);
4981 else
4982 schedstat_inc(sd, alb_failed);
4983 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004984 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004985 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004986out_unlock:
4987 busiest_rq->active_balance = 0;
4988 raw_spin_unlock_irq(&busiest_rq->lock);
4989 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004990}
4991
4992#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004993/*
4994 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004995 * - When one of the busy CPUs notice that there may be an idle rebalancing
4996 * needed, they will kick the idle load balancer, which then does idle
4997 * load balancing for all the idle CPUs.
4998 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004999static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005000 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005001 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005002 unsigned long next_balance; /* in jiffy units */
5003} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005004
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01005005static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005006{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005007 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005008
Suresh Siddha786d6dc2011-12-01 17:07:35 -08005009 if (ilb < nr_cpu_ids && idle_cpu(ilb))
5010 return ilb;
5011
5012 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005013}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005014
5015/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005016 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
5017 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
5018 * CPU (if there is one).
5019 */
5020static void nohz_balancer_kick(int cpu)
5021{
5022 int ilb_cpu;
5023
5024 nohz.next_balance++;
5025
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005026 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005027
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005028 if (ilb_cpu >= nr_cpu_ids)
5029 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005030
Suresh Siddhacd490c52011-12-06 11:26:34 -08005031 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08005032 return;
5033 /*
5034 * Use smp_send_reschedule() instead of resched_cpu().
5035 * This way we generate a sched IPI on the target cpu which
5036 * is idle. And the softirq performing nohz idle load balance
5037 * will be run before returning from the IPI.
5038 */
5039 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005040 return;
5041}
5042
Alex Shic1cc0172012-09-10 15:10:58 +08005043static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08005044{
5045 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
5046 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
5047 atomic_dec(&nohz.nr_cpus);
5048 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
5049 }
5050}
5051
Suresh Siddha69e1e812011-12-01 17:07:33 -08005052static inline void set_cpu_sd_state_busy(void)
5053{
5054 struct sched_domain *sd;
5055 int cpu = smp_processor_id();
5056
5057 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5058 return;
5059 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
5060
5061 rcu_read_lock();
5062 for_each_domain(cpu, sd)
5063 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
5064 rcu_read_unlock();
5065}
5066
5067void set_cpu_sd_state_idle(void)
5068{
5069 struct sched_domain *sd;
5070 int cpu = smp_processor_id();
5071
5072 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5073 return;
5074 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5075
5076 rcu_read_lock();
5077 for_each_domain(cpu, sd)
5078 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5079 rcu_read_unlock();
5080}
5081
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005082/*
Alex Shic1cc0172012-09-10 15:10:58 +08005083 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005084 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005085 */
Alex Shic1cc0172012-09-10 15:10:58 +08005086void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005087{
Suresh Siddha71325962012-01-19 18:28:57 -08005088 /*
5089 * If this cpu is going down, then nothing needs to be done.
5090 */
5091 if (!cpu_active(cpu))
5092 return;
5093
Alex Shic1cc0172012-09-10 15:10:58 +08005094 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5095 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005096
Alex Shic1cc0172012-09-10 15:10:58 +08005097 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5098 atomic_inc(&nohz.nr_cpus);
5099 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005100}
Suresh Siddha71325962012-01-19 18:28:57 -08005101
5102static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5103 unsigned long action, void *hcpu)
5104{
5105 switch (action & ~CPU_TASKS_FROZEN) {
5106 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005107 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005108 return NOTIFY_OK;
5109 default:
5110 return NOTIFY_DONE;
5111 }
5112}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005113#endif
5114
5115static DEFINE_SPINLOCK(balancing);
5116
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005117/*
5118 * Scale the max load_balance interval with the number of CPUs in the system.
5119 * This trades load-balance latency on larger machines for less cross talk.
5120 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005121void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005122{
5123 max_load_balance_interval = HZ*num_online_cpus()/10;
5124}
5125
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005126/*
5127 * It checks each scheduling domain to see if it is due to be balanced,
5128 * and initiates a balancing operation if so.
5129 *
5130 * Balancing parameters are set up in arch_init_sched_domains.
5131 */
5132static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5133{
5134 int balance = 1;
5135 struct rq *rq = cpu_rq(cpu);
5136 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005137 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005138 /* Earliest time when we have to do rebalance again */
5139 unsigned long next_balance = jiffies + 60*HZ;
5140 int update_next_balance = 0;
5141 int need_serialize;
5142
Paul Turner48a16752012-10-04 13:18:31 +02005143 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005144
Peter Zijlstradce840a2011-04-07 14:09:50 +02005145 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005146 for_each_domain(cpu, sd) {
5147 if (!(sd->flags & SD_LOAD_BALANCE))
5148 continue;
5149
5150 interval = sd->balance_interval;
5151 if (idle != CPU_IDLE)
5152 interval *= sd->busy_factor;
5153
5154 /* scale ms to jiffies */
5155 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005156 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005157
5158 need_serialize = sd->flags & SD_SERIALIZE;
5159
5160 if (need_serialize) {
5161 if (!spin_trylock(&balancing))
5162 goto out;
5163 }
5164
5165 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5166 if (load_balance(cpu, rq, sd, idle, &balance)) {
5167 /*
5168 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005169 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005170 */
5171 idle = CPU_NOT_IDLE;
5172 }
5173 sd->last_balance = jiffies;
5174 }
5175 if (need_serialize)
5176 spin_unlock(&balancing);
5177out:
5178 if (time_after(next_balance, sd->last_balance + interval)) {
5179 next_balance = sd->last_balance + interval;
5180 update_next_balance = 1;
5181 }
5182
5183 /*
5184 * Stop the load balance at this level. There is another
5185 * CPU in our sched group which is doing load balancing more
5186 * actively.
5187 */
5188 if (!balance)
5189 break;
5190 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005191 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005192
5193 /*
5194 * next_balance will be updated only when there is a need.
5195 * When the cpu is attached to null domain for ex, it will not be
5196 * updated.
5197 */
5198 if (likely(update_next_balance))
5199 rq->next_balance = next_balance;
5200}
5201
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005202#ifdef CONFIG_NO_HZ
5203/*
5204 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5205 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5206 */
5207static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5208{
5209 struct rq *this_rq = cpu_rq(this_cpu);
5210 struct rq *rq;
5211 int balance_cpu;
5212
Suresh Siddha1c792db2011-12-01 17:07:32 -08005213 if (idle != CPU_IDLE ||
5214 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5215 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005216
5217 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005218 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005219 continue;
5220
5221 /*
5222 * If this cpu gets work to do, stop the load balancing
5223 * work being done for other cpus. Next load
5224 * balancing owner will pick it up.
5225 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005226 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005227 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005228
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005229 rq = cpu_rq(balance_cpu);
5230
5231 raw_spin_lock_irq(&rq->lock);
5232 update_rq_clock(rq);
5233 update_idle_cpu_load(rq);
5234 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005235
5236 rebalance_domains(balance_cpu, CPU_IDLE);
5237
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005238 if (time_after(this_rq->next_balance, rq->next_balance))
5239 this_rq->next_balance = rq->next_balance;
5240 }
5241 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005242end:
5243 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005244}
5245
5246/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005247 * Current heuristic for kicking the idle load balancer in the presence
5248 * of an idle cpu is the system.
5249 * - This rq has more than one task.
5250 * - At any scheduler domain level, this cpu's scheduler group has multiple
5251 * busy cpu's exceeding the group's power.
5252 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5253 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005254 */
5255static inline int nohz_kick_needed(struct rq *rq, int cpu)
5256{
5257 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005258 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005259
Suresh Siddha1c792db2011-12-01 17:07:32 -08005260 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005261 return 0;
5262
Suresh Siddha1c792db2011-12-01 17:07:32 -08005263 /*
5264 * We may be recently in ticked or tickless idle mode. At the first
5265 * busy tick after returning from idle, we will update the busy stats.
5266 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005267 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005268 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005269
5270 /*
5271 * None are in tickless mode and hence no need for NOHZ idle load
5272 * balancing.
5273 */
5274 if (likely(!atomic_read(&nohz.nr_cpus)))
5275 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005276
5277 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005278 return 0;
5279
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005280 if (rq->nr_running >= 2)
5281 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005282
Peter Zijlstra067491b2011-12-07 14:32:08 +01005283 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005284 for_each_domain(cpu, sd) {
5285 struct sched_group *sg = sd->groups;
5286 struct sched_group_power *sgp = sg->sgp;
5287 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005288
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005289 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005290 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005291
5292 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5293 && (cpumask_first_and(nohz.idle_cpus_mask,
5294 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005295 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005296
5297 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5298 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005299 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005300 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005301 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005302
5303need_kick_unlock:
5304 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005305need_kick:
5306 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005307}
5308#else
5309static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5310#endif
5311
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005312/*
5313 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005314 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005315 */
5316static void run_rebalance_domains(struct softirq_action *h)
5317{
5318 int this_cpu = smp_processor_id();
5319 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005320 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005321 CPU_IDLE : CPU_NOT_IDLE;
5322
5323 rebalance_domains(this_cpu, idle);
5324
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005325 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005326 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005327 * balancing on behalf of the other idle cpus whose ticks are
5328 * stopped.
5329 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005330 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005331}
5332
5333static inline int on_null_domain(int cpu)
5334{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005335 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005336}
5337
5338/*
5339 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005340 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005341void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005342{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005343 /* Don't need to rebalance while attached to NULL domain */
5344 if (time_after_eq(jiffies, rq->next_balance) &&
5345 likely(!on_null_domain(cpu)))
5346 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005347#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005348 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005349 nohz_balancer_kick(cpu);
5350#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005351}
5352
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005353static void rq_online_fair(struct rq *rq)
5354{
5355 update_sysctl();
5356}
5357
5358static void rq_offline_fair(struct rq *rq)
5359{
5360 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005361
5362 /* Ensure any throttled groups are reachable by pick_next_task */
5363 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005364}
5365
Dhaval Giani55e12e52008-06-24 23:39:43 +05305366#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005367
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005368/*
5369 * scheduler tick hitting a task of our scheduling class:
5370 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005371static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005372{
5373 struct cfs_rq *cfs_rq;
5374 struct sched_entity *se = &curr->se;
5375
5376 for_each_sched_entity(se) {
5377 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005378 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005379 }
Ben Segall18bf2802012-10-04 12:51:20 +02005380
5381 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005382}
5383
5384/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005385 * called on fork with the child task as argument from the parent's context
5386 * - child not yet on the tasklist
5387 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005388 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005389static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005390{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005391 struct cfs_rq *cfs_rq;
5392 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005393 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005394 struct rq *rq = this_rq();
5395 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005396
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005397 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005398
Peter Zijlstra861d0342010-08-19 13:31:43 +02005399 update_rq_clock(rq);
5400
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005401 cfs_rq = task_cfs_rq(current);
5402 curr = cfs_rq->curr;
5403
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005404 if (unlikely(task_cpu(p) != this_cpu)) {
5405 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005406 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005407 rcu_read_unlock();
5408 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005409
Ting Yang7109c442007-08-28 12:53:24 +02005410 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005411
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005412 if (curr)
5413 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005414 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005415
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005416 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005417 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005418 * Upon rescheduling, sched_class::put_prev_task() will place
5419 * 'current' within the tree based on its new key value.
5420 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005421 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305422 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005423 }
5424
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005425 se->vruntime -= cfs_rq->min_vruntime;
5426
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005427 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005428}
5429
Steven Rostedtcb469842008-01-25 21:08:22 +01005430/*
5431 * Priority of the task has changed. Check to see if we preempt
5432 * the current task.
5433 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005434static void
5435prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005436{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005437 if (!p->se.on_rq)
5438 return;
5439
Steven Rostedtcb469842008-01-25 21:08:22 +01005440 /*
5441 * Reschedule if we are currently running on this runqueue and
5442 * our priority decreased, or if we are not currently running on
5443 * this runqueue and our priority is higher than the current's
5444 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005445 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005446 if (p->prio > oldprio)
5447 resched_task(rq->curr);
5448 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005449 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005450}
5451
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005452static void switched_from_fair(struct rq *rq, struct task_struct *p)
5453{
5454 struct sched_entity *se = &p->se;
5455 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5456
5457 /*
5458 * Ensure the task's vruntime is normalized, so that when its
5459 * switched back to the fair class the enqueue_entity(.flags=0) will
5460 * do the right thing.
5461 *
5462 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5463 * have normalized the vruntime, if it was !on_rq, then only when
5464 * the task is sleeping will it still have non-normalized vruntime.
5465 */
5466 if (!se->on_rq && p->state != TASK_RUNNING) {
5467 /*
5468 * Fix up our vruntime so that the current sleep doesn't
5469 * cause 'unlimited' sleep bonus.
5470 */
5471 place_entity(cfs_rq, se, 0);
5472 se->vruntime -= cfs_rq->min_vruntime;
5473 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005474
5475#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5476 /*
5477 * Remove our load from contribution when we leave sched_fair
5478 * and ensure we don't carry in an old decay_count if we
5479 * switch back.
5480 */
5481 if (p->se.avg.decay_count) {
5482 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5483 __synchronize_entity_decay(&p->se);
5484 subtract_blocked_load_contrib(cfs_rq,
5485 p->se.avg.load_avg_contrib);
5486 }
5487#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005488}
5489
Steven Rostedtcb469842008-01-25 21:08:22 +01005490/*
5491 * We switched to the sched_fair class.
5492 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005493static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005494{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005495 if (!p->se.on_rq)
5496 return;
5497
Steven Rostedtcb469842008-01-25 21:08:22 +01005498 /*
5499 * We were most likely switched from sched_rt, so
5500 * kick off the schedule if running, otherwise just see
5501 * if we can still preempt the current task.
5502 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005503 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005504 resched_task(rq->curr);
5505 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005506 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005507}
5508
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005509/* Account for a task changing its policy or group.
5510 *
5511 * This routine is mostly called to set cfs_rq->curr field when a task
5512 * migrates between groups/classes.
5513 */
5514static void set_curr_task_fair(struct rq *rq)
5515{
5516 struct sched_entity *se = &rq->curr->se;
5517
Paul Turnerec12cb72011-07-21 09:43:30 -07005518 for_each_sched_entity(se) {
5519 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5520
5521 set_next_entity(cfs_rq, se);
5522 /* ensure bandwidth has been allocated on our new cfs_rq */
5523 account_cfs_rq_runtime(cfs_rq, 0);
5524 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005525}
5526
Peter Zijlstra029632f2011-10-25 10:00:11 +02005527void init_cfs_rq(struct cfs_rq *cfs_rq)
5528{
5529 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005530 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5531#ifndef CONFIG_64BIT
5532 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5533#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005534#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5535 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005536 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005537#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005538}
5539
Peter Zijlstra810b3812008-02-29 15:21:01 -05005540#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005541static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005542{
Paul Turneraff3e492012-10-04 13:18:30 +02005543 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005544 /*
5545 * If the task was not on the rq at the time of this cgroup movement
5546 * it must have been asleep, sleeping tasks keep their ->vruntime
5547 * absolute on their old rq until wakeup (needed for the fair sleeper
5548 * bonus in place_entity()).
5549 *
5550 * If it was on the rq, we've just 'preempted' it, which does convert
5551 * ->vruntime to a relative base.
5552 *
5553 * Make sure both cases convert their relative position when migrating
5554 * to another cgroup's rq. This does somewhat interfere with the
5555 * fair sleeper stuff for the first placement, but who cares.
5556 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005557 /*
5558 * When !on_rq, vruntime of the task has usually NOT been normalized.
5559 * But there are some cases where it has already been normalized:
5560 *
5561 * - Moving a forked child which is waiting for being woken up by
5562 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005563 * - Moving a task which has been woken up by try_to_wake_up() and
5564 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005565 *
5566 * To prevent boost or penalty in the new cfs_rq caused by delta
5567 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5568 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005569 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005570 on_rq = 1;
5571
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005572 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005573 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5574 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005575 if (!on_rq) {
5576 cfs_rq = cfs_rq_of(&p->se);
5577 p->se.vruntime += cfs_rq->min_vruntime;
5578#ifdef CONFIG_SMP
5579 /*
5580 * migrate_task_rq_fair() will have removed our previous
5581 * contribution, but we must synchronize for ongoing future
5582 * decay.
5583 */
5584 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5585 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5586#endif
5587 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005588}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005589
5590void free_fair_sched_group(struct task_group *tg)
5591{
5592 int i;
5593
5594 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5595
5596 for_each_possible_cpu(i) {
5597 if (tg->cfs_rq)
5598 kfree(tg->cfs_rq[i]);
5599 if (tg->se)
5600 kfree(tg->se[i]);
5601 }
5602
5603 kfree(tg->cfs_rq);
5604 kfree(tg->se);
5605}
5606
5607int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5608{
5609 struct cfs_rq *cfs_rq;
5610 struct sched_entity *se;
5611 int i;
5612
5613 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5614 if (!tg->cfs_rq)
5615 goto err;
5616 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5617 if (!tg->se)
5618 goto err;
5619
5620 tg->shares = NICE_0_LOAD;
5621
5622 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5623
5624 for_each_possible_cpu(i) {
5625 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5626 GFP_KERNEL, cpu_to_node(i));
5627 if (!cfs_rq)
5628 goto err;
5629
5630 se = kzalloc_node(sizeof(struct sched_entity),
5631 GFP_KERNEL, cpu_to_node(i));
5632 if (!se)
5633 goto err_free_rq;
5634
5635 init_cfs_rq(cfs_rq);
5636 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5637 }
5638
5639 return 1;
5640
5641err_free_rq:
5642 kfree(cfs_rq);
5643err:
5644 return 0;
5645}
5646
5647void unregister_fair_sched_group(struct task_group *tg, int cpu)
5648{
5649 struct rq *rq = cpu_rq(cpu);
5650 unsigned long flags;
5651
5652 /*
5653 * Only empty task groups can be destroyed; so we can speculatively
5654 * check on_list without danger of it being re-added.
5655 */
5656 if (!tg->cfs_rq[cpu]->on_list)
5657 return;
5658
5659 raw_spin_lock_irqsave(&rq->lock, flags);
5660 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5661 raw_spin_unlock_irqrestore(&rq->lock, flags);
5662}
5663
5664void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5665 struct sched_entity *se, int cpu,
5666 struct sched_entity *parent)
5667{
5668 struct rq *rq = cpu_rq(cpu);
5669
5670 cfs_rq->tg = tg;
5671 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005672 init_cfs_rq_runtime(cfs_rq);
5673
5674 tg->cfs_rq[cpu] = cfs_rq;
5675 tg->se[cpu] = se;
5676
5677 /* se could be NULL for root_task_group */
5678 if (!se)
5679 return;
5680
5681 if (!parent)
5682 se->cfs_rq = &rq->cfs;
5683 else
5684 se->cfs_rq = parent->my_q;
5685
5686 se->my_q = cfs_rq;
5687 update_load_set(&se->load, 0);
5688 se->parent = parent;
5689}
5690
5691static DEFINE_MUTEX(shares_mutex);
5692
5693int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5694{
5695 int i;
5696 unsigned long flags;
5697
5698 /*
5699 * We can't change the weight of the root cgroup.
5700 */
5701 if (!tg->se[0])
5702 return -EINVAL;
5703
5704 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5705
5706 mutex_lock(&shares_mutex);
5707 if (tg->shares == shares)
5708 goto done;
5709
5710 tg->shares = shares;
5711 for_each_possible_cpu(i) {
5712 struct rq *rq = cpu_rq(i);
5713 struct sched_entity *se;
5714
5715 se = tg->se[i];
5716 /* Propagate contribution to hierarchy */
5717 raw_spin_lock_irqsave(&rq->lock, flags);
Paul Turnerf269ae02012-10-04 13:18:31 +02005718 for_each_sched_entity(se) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02005719 update_cfs_shares(group_cfs_rq(se));
Paul Turnerf269ae02012-10-04 13:18:31 +02005720 /* update contribution to parent */
5721 update_entity_load_avg(se, 1);
5722 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02005723 raw_spin_unlock_irqrestore(&rq->lock, flags);
5724 }
5725
5726done:
5727 mutex_unlock(&shares_mutex);
5728 return 0;
5729}
5730#else /* CONFIG_FAIR_GROUP_SCHED */
5731
5732void free_fair_sched_group(struct task_group *tg) { }
5733
5734int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5735{
5736 return 1;
5737}
5738
5739void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5740
5741#endif /* CONFIG_FAIR_GROUP_SCHED */
5742
Peter Zijlstra810b3812008-02-29 15:21:01 -05005743
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005744static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005745{
5746 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005747 unsigned int rr_interval = 0;
5748
5749 /*
5750 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5751 * idle runqueue:
5752 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005753 if (rq->cfs.load.weight)
5754 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005755
5756 return rr_interval;
5757}
5758
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005759/*
5760 * All the scheduling class methods:
5761 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005762const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005763 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005764 .enqueue_task = enqueue_task_fair,
5765 .dequeue_task = dequeue_task_fair,
5766 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005767 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005768
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005769 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005770
5771 .pick_next_task = pick_next_task_fair,
5772 .put_prev_task = put_prev_task_fair,
5773
Peter Williams681f3e62007-10-24 18:23:51 +02005774#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005775 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005776 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005777
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005778 .rq_online = rq_online_fair,
5779 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005780
5781 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005782#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005783
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005784 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005785 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005786 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005787
5788 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005789 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005790 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005791
Peter Williams0d721ce2009-09-21 01:31:53 +00005792 .get_rr_interval = get_rr_interval_fair,
5793
Peter Zijlstra810b3812008-02-29 15:21:01 -05005794#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005795 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005796#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005797};
5798
5799#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005800void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005801{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005802 struct cfs_rq *cfs_rq;
5803
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005804 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005805 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005806 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005807 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005808}
5809#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005810
5811__init void init_sched_fair_class(void)
5812{
5813#ifdef CONFIG_SMP
5814 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5815
5816#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005817 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005818 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005819 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005820#endif
5821#endif /* SMP */
5822
5823}