blob: e20cb2693ef7ae7afb5d6bbf890594820db12b89 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Paul Turnera7a4f8a2010-11-15 15:47:06 -080092/*
93 * The exponential sliding window over which load is averaged for shares
94 * distribution.
95 * (default: 10msec)
96 */
97unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
98
Paul Turnerec12cb72011-07-21 09:43:30 -070099#ifdef CONFIG_CFS_BANDWIDTH
100/*
101 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
102 * each time a cfs_rq requests quota.
103 *
104 * Note: in the case that the slice exceeds the runtime remaining (either due
105 * to consumption or the quota being specified to be smaller than the slice)
106 * we will always only issue the remaining available time.
107 *
108 * default: 5 msec, units: microseconds
109 */
110unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
111#endif
112
Peter Zijlstra029632f2011-10-25 10:00:11 +0200113/*
114 * Increase the granularity value when there are more CPUs,
115 * because with more CPUs the 'effective latency' as visible
116 * to users decreases. But the relationship is not linear,
117 * so pick a second-best guess by going with the log2 of the
118 * number of CPUs.
119 *
120 * This idea comes from the SD scheduler of Con Kolivas:
121 */
122static int get_update_sysctl_factor(void)
123{
124 unsigned int cpus = min_t(int, num_online_cpus(), 8);
125 unsigned int factor;
126
127 switch (sysctl_sched_tunable_scaling) {
128 case SCHED_TUNABLESCALING_NONE:
129 factor = 1;
130 break;
131 case SCHED_TUNABLESCALING_LINEAR:
132 factor = cpus;
133 break;
134 case SCHED_TUNABLESCALING_LOG:
135 default:
136 factor = 1 + ilog2(cpus);
137 break;
138 }
139
140 return factor;
141}
142
143static void update_sysctl(void)
144{
145 unsigned int factor = get_update_sysctl_factor();
146
147#define SET_SYSCTL(name) \
148 (sysctl_##name = (factor) * normalized_sysctl_##name)
149 SET_SYSCTL(sched_min_granularity);
150 SET_SYSCTL(sched_latency);
151 SET_SYSCTL(sched_wakeup_granularity);
152#undef SET_SYSCTL
153}
154
155void sched_init_granularity(void)
156{
157 update_sysctl();
158}
159
160#if BITS_PER_LONG == 32
161# define WMULT_CONST (~0UL)
162#else
163# define WMULT_CONST (1UL << 32)
164#endif
165
166#define WMULT_SHIFT 32
167
168/*
169 * Shift right and round:
170 */
171#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
172
173/*
174 * delta *= weight / lw
175 */
176static unsigned long
177calc_delta_mine(unsigned long delta_exec, unsigned long weight,
178 struct load_weight *lw)
179{
180 u64 tmp;
181
182 /*
183 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
184 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
185 * 2^SCHED_LOAD_RESOLUTION.
186 */
187 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
188 tmp = (u64)delta_exec * scale_load_down(weight);
189 else
190 tmp = (u64)delta_exec;
191
192 if (!lw->inv_weight) {
193 unsigned long w = scale_load_down(lw->weight);
194
195 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
196 lw->inv_weight = 1;
197 else if (unlikely(!w))
198 lw->inv_weight = WMULT_CONST;
199 else
200 lw->inv_weight = WMULT_CONST / w;
201 }
202
203 /*
204 * Check whether we'd overflow the 64-bit multiplication:
205 */
206 if (unlikely(tmp > WMULT_CONST))
207 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
208 WMULT_SHIFT/2);
209 else
210 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
211
212 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
213}
214
215
216const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200217
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200218/**************************************************************
219 * CFS operations on generic schedulable entities:
220 */
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224/* cpu runqueue to which this cfs_rq is attached */
225static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
226{
227 return cfs_rq->rq;
228}
229
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200230/* An entity is a task if it doesn't "own" a runqueue */
231#define entity_is_task(se) (!se->my_q)
232
Peter Zijlstra8f488942009-07-24 12:25:30 +0200233static inline struct task_struct *task_of(struct sched_entity *se)
234{
235#ifdef CONFIG_SCHED_DEBUG
236 WARN_ON_ONCE(!entity_is_task(se));
237#endif
238 return container_of(se, struct task_struct, se);
239}
240
Peter Zijlstrab7581492008-04-19 19:45:00 +0200241/* Walk up scheduling entities hierarchy */
242#define for_each_sched_entity(se) \
243 for (; se; se = se->parent)
244
245static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
246{
247 return p->se.cfs_rq;
248}
249
250/* runqueue on which this entity is (to be) queued */
251static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
252{
253 return se->cfs_rq;
254}
255
256/* runqueue "owned" by this group */
257static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
258{
259 return grp->my_q;
260}
261
Paul Turneraff3e492012-10-04 13:18:30 +0200262static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
263 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200264
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800265static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
266{
267 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800268 /*
269 * Ensure we either appear before our parent (if already
270 * enqueued) or force our parent to appear after us when it is
271 * enqueued. The fact that we always enqueue bottom-up
272 * reduces this to two cases.
273 */
274 if (cfs_rq->tg->parent &&
275 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
276 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800278 } else {
279 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
280 &rq_of(cfs_rq)->leaf_cfs_rq_list);
281 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282
283 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200284 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200285 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800286 }
287}
288
289static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (cfs_rq->on_list) {
292 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
293 cfs_rq->on_list = 0;
294 }
295}
296
Peter Zijlstrab7581492008-04-19 19:45:00 +0200297/* Iterate thr' all leaf cfs_rq's on a runqueue */
298#define for_each_leaf_cfs_rq(rq, cfs_rq) \
299 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
300
301/* Do the two (enqueued) entities belong to the same group ? */
302static inline int
303is_same_group(struct sched_entity *se, struct sched_entity *pse)
304{
305 if (se->cfs_rq == pse->cfs_rq)
306 return 1;
307
308 return 0;
309}
310
311static inline struct sched_entity *parent_entity(struct sched_entity *se)
312{
313 return se->parent;
314}
315
Peter Zijlstra464b7522008-10-24 11:06:15 +0200316/* return depth at which a sched entity is present in the hierarchy */
317static inline int depth_se(struct sched_entity *se)
318{
319 int depth = 0;
320
321 for_each_sched_entity(se)
322 depth++;
323
324 return depth;
325}
326
327static void
328find_matching_se(struct sched_entity **se, struct sched_entity **pse)
329{
330 int se_depth, pse_depth;
331
332 /*
333 * preemption test can be made between sibling entities who are in the
334 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
335 * both tasks until we find their ancestors who are siblings of common
336 * parent.
337 */
338
339 /* First walk up until both entities are at same depth */
340 se_depth = depth_se(*se);
341 pse_depth = depth_se(*pse);
342
343 while (se_depth > pse_depth) {
344 se_depth--;
345 *se = parent_entity(*se);
346 }
347
348 while (pse_depth > se_depth) {
349 pse_depth--;
350 *pse = parent_entity(*pse);
351 }
352
353 while (!is_same_group(*se, *pse)) {
354 *se = parent_entity(*se);
355 *pse = parent_entity(*pse);
356 }
357}
358
Peter Zijlstra8f488942009-07-24 12:25:30 +0200359#else /* !CONFIG_FAIR_GROUP_SCHED */
360
361static inline struct task_struct *task_of(struct sched_entity *se)
362{
363 return container_of(se, struct task_struct, se);
364}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365
366static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
367{
368 return container_of(cfs_rq, struct rq, cfs);
369}
370
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200371#define entity_is_task(se) 1
372
Peter Zijlstrab7581492008-04-19 19:45:00 +0200373#define for_each_sched_entity(se) \
374 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200379}
380
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
382{
383 struct task_struct *p = task_of(se);
384 struct rq *rq = task_rq(p);
385
386 return &rq->cfs;
387}
388
389/* runqueue "owned" by this group */
390static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
391{
392 return NULL;
393}
394
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800395static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
396{
397}
398
399static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
400{
401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403#define for_each_leaf_cfs_rq(rq, cfs_rq) \
404 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
405
406static inline int
407is_same_group(struct sched_entity *se, struct sched_entity *pse)
408{
409 return 1;
410}
411
412static inline struct sched_entity *parent_entity(struct sched_entity *se)
413{
414 return NULL;
415}
416
Peter Zijlstra464b7522008-10-24 11:06:15 +0200417static inline void
418find_matching_se(struct sched_entity **se, struct sched_entity **pse)
419{
420}
421
Peter Zijlstrab7581492008-04-19 19:45:00 +0200422#endif /* CONFIG_FAIR_GROUP_SCHED */
423
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700424static __always_inline
425void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427/**************************************************************
428 * Scheduling class tree data structure manipulation methods:
429 */
430
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200431static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200432{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200433 s64 delta = (s64)(vruntime - min_vruntime);
434 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435 min_vruntime = vruntime;
436
437 return min_vruntime;
438}
439
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200440static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200441{
442 s64 delta = (s64)(vruntime - min_vruntime);
443 if (delta < 0)
444 min_vruntime = vruntime;
445
446 return min_vruntime;
447}
448
Fabio Checconi54fdc582009-07-16 12:32:27 +0200449static inline int entity_before(struct sched_entity *a,
450 struct sched_entity *b)
451{
452 return (s64)(a->vruntime - b->vruntime) < 0;
453}
454
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200455static void update_min_vruntime(struct cfs_rq *cfs_rq)
456{
457 u64 vruntime = cfs_rq->min_vruntime;
458
459 if (cfs_rq->curr)
460 vruntime = cfs_rq->curr->vruntime;
461
462 if (cfs_rq->rb_leftmost) {
463 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
464 struct sched_entity,
465 run_node);
466
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100467 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200468 vruntime = se->vruntime;
469 else
470 vruntime = min_vruntime(vruntime, se->vruntime);
471 }
472
473 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200474#ifndef CONFIG_64BIT
475 smp_wmb();
476 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
477#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478}
479
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200480/*
481 * Enqueue an entity into the rb-tree:
482 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200483static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484{
485 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
486 struct rb_node *parent = NULL;
487 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488 int leftmost = 1;
489
490 /*
491 * Find the right place in the rbtree:
492 */
493 while (*link) {
494 parent = *link;
495 entry = rb_entry(parent, struct sched_entity, run_node);
496 /*
497 * We dont care about collisions. Nodes with
498 * the same key stay together.
499 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200500 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200501 link = &parent->rb_left;
502 } else {
503 link = &parent->rb_right;
504 leftmost = 0;
505 }
506 }
507
508 /*
509 * Maintain a cache of leftmost tree entries (it is frequently
510 * used):
511 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200512 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200513 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200514
515 rb_link_node(&se->run_node, parent, link);
516 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517}
518
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200519static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521 if (cfs_rq->rb_leftmost == &se->run_node) {
522 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100523
524 next_node = rb_next(&se->run_node);
525 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200527
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200529}
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100533 struct rb_node *left = cfs_rq->rb_leftmost;
534
535 if (!left)
536 return NULL;
537
538 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200539}
540
Rik van Rielac53db52011-02-01 09:51:03 -0500541static struct sched_entity *__pick_next_entity(struct sched_entity *se)
542{
543 struct rb_node *next = rb_next(&se->run_node);
544
545 if (!next)
546 return NULL;
547
548 return rb_entry(next, struct sched_entity, run_node);
549}
550
551#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100554 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200555
Balbir Singh70eee742008-02-22 13:25:53 +0530556 if (!last)
557 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100558
559 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200560}
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562/**************************************************************
563 * Scheduling class statistics methods:
564 */
565
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100566int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700567 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100568 loff_t *ppos)
569{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100571 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100572
573 if (ret || !write)
574 return ret;
575
576 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
577 sysctl_sched_min_granularity);
578
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579#define WRT_SYSCTL(name) \
580 (normalized_sysctl_##name = sysctl_##name / (factor))
581 WRT_SYSCTL(sched_min_granularity);
582 WRT_SYSCTL(sched_latency);
583 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100584#undef WRT_SYSCTL
585
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586 return 0;
587}
588#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200589
590/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200591 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200592 */
593static inline unsigned long
594calc_delta_fair(unsigned long delta, struct sched_entity *se)
595{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200596 if (unlikely(se->load.weight != NICE_0_LOAD))
597 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200598
599 return delta;
600}
601
602/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603 * The idea is to set a period in which each task runs once.
604 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200605 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * this period because otherwise the slices get too small.
607 *
608 * p = (nr <= nl) ? l : l*nr/nl
609 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200610static u64 __sched_period(unsigned long nr_running)
611{
612 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100613 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200614
615 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100616 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200618 }
619
620 return period;
621}
622
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200623/*
624 * We calculate the wall-time slice from the period by taking a part
625 * proportional to the weight.
626 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200627 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200629static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200630{
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200632
Mike Galbraith0a582442009-01-02 12:16:42 +0100633 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100634 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200635 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100636
637 cfs_rq = cfs_rq_of(se);
638 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200641 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100642
643 update_load_add(&lw, se->load.weight);
644 load = &lw;
645 }
646 slice = calc_delta_mine(slice, se->load.weight, load);
647 }
648 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200649}
650
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200652 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200658 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659}
660
Paul Turnerd6b55912010-11-15 15:47:09 -0800661static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800662static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800663
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200664/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200665 * Update the current task's runtime statistics. Skip current tasks that
666 * are not in our scheduling class.
667 */
668static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200669__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
670 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200672 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200673
Lucas De Marchi41acab82010-03-10 23:37:45 -0300674 schedstat_set(curr->statistics.exec_max,
675 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200676
677 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200678 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200679 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100680
Ingo Molnare9acbff2007-10-15 17:00:04 +0200681 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200682 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800683
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100684#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800685 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800686#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200687}
688
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200689static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200691 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700692 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693 unsigned long delta_exec;
694
695 if (unlikely(!curr))
696 return;
697
698 /*
699 * Get the amount of time the current task was running
700 * since the last time we changed load (this cannot
701 * overflow on 32 bits):
702 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200703 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100704 if (!delta_exec)
705 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200706
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200707 __update_curr(cfs_rq, curr, delta_exec);
708 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100709
710 if (entity_is_task(curr)) {
711 struct task_struct *curtask = task_of(curr);
712
Ingo Molnarf977bb42009-09-13 18:15:54 +0200713 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100714 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700715 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100716 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700717
718 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200719}
720
721static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200722update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200723{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300724 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200725}
726
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200727/*
728 * Task is being enqueued - update stats:
729 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200730static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200732 /*
733 * Are we enqueueing a waiting task? (for current tasks
734 * a dequeue/enqueue event is a NOP)
735 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200736 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200737 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200738}
739
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200741update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200742{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300743 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
744 rq_of(cfs_rq)->clock - se->statistics.wait_start));
745 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
746 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
747 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200748#ifdef CONFIG_SCHEDSTATS
749 if (entity_is_task(se)) {
750 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300751 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200752 }
753#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300754 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200755}
756
757static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200758update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200759{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200760 /*
761 * Mark the end of the wait period if dequeueing a
762 * waiting task:
763 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200764 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200765 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200766}
767
768/*
769 * We are picking a new current task - update its stats:
770 */
771static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200772update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200773{
774 /*
775 * We are starting a new run period:
776 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700777 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200778}
779
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200780/**************************************************
781 * Scheduling class queueing methods:
782 */
783
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200784static void
785account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
786{
787 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200788 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200789 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100790#ifdef CONFIG_SMP
791 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200792 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100793#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200794 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200795}
796
797static void
798account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
799{
800 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200801 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200802 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100803 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530804 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200805 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200806}
807
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800808#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700809/* we need this in update_cfs_load and load-balance functions below */
810static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800811# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800812static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
813 int global_update)
814{
815 struct task_group *tg = cfs_rq->tg;
816 long load_avg;
817
818 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
819 load_avg -= cfs_rq->load_contribution;
820
821 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
822 atomic_add(load_avg, &tg->load_weight);
823 cfs_rq->load_contribution += load_avg;
824 }
825}
826
827static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800828{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800829 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800830 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800831 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800832
Paul Turner64660c82011-07-21 09:43:36 -0700833 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800834 return;
835
Paul Turner05ca62c2011-01-21 20:45:02 -0800836 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800837 delta = now - cfs_rq->load_stamp;
838
Paul Turnere33078b2010-11-15 15:47:04 -0800839 /* truncate load history at 4 idle periods */
840 if (cfs_rq->load_stamp > cfs_rq->load_last &&
841 now - cfs_rq->load_last > 4 * period) {
842 cfs_rq->load_period = 0;
843 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800844 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800845 }
846
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800847 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800848 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800849 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800850 if (load) {
851 cfs_rq->load_last = now;
852 cfs_rq->load_avg += delta * load;
853 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800854
Paul Turnerd6b55912010-11-15 15:47:09 -0800855 /* consider updating load contribution on each fold or truncate */
856 if (global_update || cfs_rq->load_period > period
857 || !cfs_rq->load_period)
858 update_cfs_rq_load_contribution(cfs_rq, global_update);
859
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800860 while (cfs_rq->load_period > period) {
861 /*
862 * Inline assembly required to prevent the compiler
863 * optimising this loop into a divmod call.
864 * See __iter_div_u64_rem() for another example of this.
865 */
866 asm("" : "+rm" (cfs_rq->load_period));
867 cfs_rq->load_period /= 2;
868 cfs_rq->load_avg /= 2;
869 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800870
Paul Turnere33078b2010-11-15 15:47:04 -0800871 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
872 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800873}
874
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200875static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
876{
877 long tg_weight;
878
879 /*
880 * Use this CPU's actual weight instead of the last load_contribution
881 * to gain a more accurate current total weight. See
882 * update_cfs_rq_load_contribution().
883 */
884 tg_weight = atomic_read(&tg->load_weight);
885 tg_weight -= cfs_rq->load_contribution;
886 tg_weight += cfs_rq->load.weight;
887
888 return tg_weight;
889}
890
Paul Turner6d5ab292011-01-21 20:45:01 -0800891static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800892{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200893 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800894
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200895 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800896 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800898 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200899 if (tg_weight)
900 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800901
902 if (shares < MIN_SHARES)
903 shares = MIN_SHARES;
904 if (shares > tg->shares)
905 shares = tg->shares;
906
907 return shares;
908}
909
910static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
911{
912 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
913 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800914 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800915 }
916}
917# else /* CONFIG_SMP */
918static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
919{
920}
921
Paul Turner6d5ab292011-01-21 20:45:01 -0800922static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800923{
924 return tg->shares;
925}
926
927static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
928{
929}
930# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800931static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
932 unsigned long weight)
933{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800934 if (se->on_rq) {
935 /* commit outstanding execution time */
936 if (cfs_rq->curr == se)
937 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800938 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800939 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800940
941 update_load_set(&se->load, weight);
942
943 if (se->on_rq)
944 account_entity_enqueue(cfs_rq, se);
945}
946
Paul Turner6d5ab292011-01-21 20:45:01 -0800947static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800948{
949 struct task_group *tg;
950 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800951 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800952
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800953 tg = cfs_rq->tg;
954 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700955 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800956 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800957#ifndef CONFIG_SMP
958 if (likely(se->load.weight == tg->shares))
959 return;
960#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800961 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800962
963 reweight_entity(cfs_rq_of(se), se, shares);
964}
965#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800966static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800967{
968}
969
Paul Turner6d5ab292011-01-21 20:45:01 -0800970static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800971{
972}
Paul Turner43365bd2010-12-15 19:10:17 -0800973
974static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
975{
976}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800977#endif /* CONFIG_FAIR_GROUP_SCHED */
978
Paul Turner9d85f212012-10-04 13:18:29 +0200979#ifdef CONFIG_SMP
980/*
981 * Approximate:
982 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
983 */
984static __always_inline u64 decay_load(u64 val, u64 n)
985{
986 for (; n && val; n--) {
987 val *= 4008;
988 val >>= 12;
989 }
990
991 return val;
992}
993
994/*
995 * We can represent the historical contribution to runnable average as the
996 * coefficients of a geometric series. To do this we sub-divide our runnable
997 * history into segments of approximately 1ms (1024us); label the segment that
998 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
999 *
1000 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
1001 * p0 p1 p2
1002 * (now) (~1ms ago) (~2ms ago)
1003 *
1004 * Let u_i denote the fraction of p_i that the entity was runnable.
1005 *
1006 * We then designate the fractions u_i as our co-efficients, yielding the
1007 * following representation of historical load:
1008 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
1009 *
1010 * We choose y based on the with of a reasonably scheduling period, fixing:
1011 * y^32 = 0.5
1012 *
1013 * This means that the contribution to load ~32ms ago (u_32) will be weighted
1014 * approximately half as much as the contribution to load within the last ms
1015 * (u_0).
1016 *
1017 * When a period "rolls over" and we have new u_0`, multiplying the previous
1018 * sum again by y is sufficient to update:
1019 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
1020 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
1021 */
1022static __always_inline int __update_entity_runnable_avg(u64 now,
1023 struct sched_avg *sa,
1024 int runnable)
1025{
1026 u64 delta;
1027 int delta_w, decayed = 0;
1028
1029 delta = now - sa->last_runnable_update;
1030 /*
1031 * This should only happen when time goes backwards, which it
1032 * unfortunately does during sched clock init when we swap over to TSC.
1033 */
1034 if ((s64)delta < 0) {
1035 sa->last_runnable_update = now;
1036 return 0;
1037 }
1038
1039 /*
1040 * Use 1024ns as the unit of measurement since it's a reasonable
1041 * approximation of 1us and fast to compute.
1042 */
1043 delta >>= 10;
1044 if (!delta)
1045 return 0;
1046 sa->last_runnable_update = now;
1047
1048 /* delta_w is the amount already accumulated against our next period */
1049 delta_w = sa->runnable_avg_period % 1024;
1050 if (delta + delta_w >= 1024) {
1051 /* period roll-over */
1052 decayed = 1;
1053
1054 /*
1055 * Now that we know we're crossing a period boundary, figure
1056 * out how much from delta we need to complete the current
1057 * period and accrue it.
1058 */
1059 delta_w = 1024 - delta_w;
1060 BUG_ON(delta_w > delta);
1061 do {
1062 if (runnable)
1063 sa->runnable_avg_sum += delta_w;
1064 sa->runnable_avg_period += delta_w;
1065
1066 /*
1067 * Remainder of delta initiates a new period, roll over
1068 * the previous.
1069 */
1070 sa->runnable_avg_sum =
1071 decay_load(sa->runnable_avg_sum, 1);
1072 sa->runnable_avg_period =
1073 decay_load(sa->runnable_avg_period, 1);
1074
1075 delta -= delta_w;
1076 /* New period is empty */
1077 delta_w = 1024;
1078 } while (delta >= 1024);
1079 }
1080
1081 /* Remainder of delta accrued against u_0` */
1082 if (runnable)
1083 sa->runnable_avg_sum += delta;
1084 sa->runnable_avg_period += delta;
1085
1086 return decayed;
1087}
1088
Paul Turner9ee474f2012-10-04 13:18:30 +02001089/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02001090static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02001091{
1092 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1093 u64 decays = atomic64_read(&cfs_rq->decay_counter);
1094
1095 decays -= se->avg.decay_count;
1096 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02001097 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02001098
1099 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
1100 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02001101
1102 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02001103}
1104
Paul Turnerc566e8e2012-10-04 13:18:30 +02001105#ifdef CONFIG_FAIR_GROUP_SCHED
1106static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1107 int force_update)
1108{
1109 struct task_group *tg = cfs_rq->tg;
1110 s64 tg_contrib;
1111
1112 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
1113 tg_contrib -= cfs_rq->tg_load_contrib;
1114
1115 if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
1116 atomic64_add(tg_contrib, &tg->load_avg);
1117 cfs_rq->tg_load_contrib += tg_contrib;
1118 }
1119}
Paul Turner8165e142012-10-04 13:18:31 +02001120
1121static inline void __update_group_entity_contrib(struct sched_entity *se)
1122{
1123 struct cfs_rq *cfs_rq = group_cfs_rq(se);
1124 struct task_group *tg = cfs_rq->tg;
1125 u64 contrib;
1126
1127 contrib = cfs_rq->tg_load_contrib * tg->shares;
1128 se->avg.load_avg_contrib = div64_u64(contrib,
1129 atomic64_read(&tg->load_avg) + 1);
1130}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001131#else
1132static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
1133 int force_update) {}
Paul Turner8165e142012-10-04 13:18:31 +02001134static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Paul Turnerc566e8e2012-10-04 13:18:30 +02001135#endif
1136
Paul Turner8165e142012-10-04 13:18:31 +02001137static inline void __update_task_entity_contrib(struct sched_entity *se)
1138{
1139 u32 contrib;
1140
1141 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
1142 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
1143 contrib /= (se->avg.runnable_avg_period + 1);
1144 se->avg.load_avg_contrib = scale_load(contrib);
1145}
1146
Paul Turner2dac7542012-10-04 13:18:30 +02001147/* Compute the current contribution to load_avg by se, return any delta */
1148static long __update_entity_load_avg_contrib(struct sched_entity *se)
1149{
1150 long old_contrib = se->avg.load_avg_contrib;
1151
Paul Turner8165e142012-10-04 13:18:31 +02001152 if (entity_is_task(se)) {
1153 __update_task_entity_contrib(se);
1154 } else {
1155 __update_group_entity_contrib(se);
1156 }
Paul Turner2dac7542012-10-04 13:18:30 +02001157
1158 return se->avg.load_avg_contrib - old_contrib;
1159}
1160
Paul Turner9ee474f2012-10-04 13:18:30 +02001161static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
1162 long load_contrib)
1163{
1164 if (likely(load_contrib < cfs_rq->blocked_load_avg))
1165 cfs_rq->blocked_load_avg -= load_contrib;
1166 else
1167 cfs_rq->blocked_load_avg = 0;
1168}
1169
Paul Turner9d85f212012-10-04 13:18:29 +02001170/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02001171static inline void update_entity_load_avg(struct sched_entity *se,
1172 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02001173{
Paul Turner2dac7542012-10-04 13:18:30 +02001174 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1175 long contrib_delta;
1176
1177 if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg,
1178 se->on_rq))
1179 return;
1180
1181 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02001182
1183 if (!update_cfs_rq)
1184 return;
1185
Paul Turner2dac7542012-10-04 13:18:30 +02001186 if (se->on_rq)
1187 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02001188 else
1189 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
1190}
1191
1192/*
1193 * Decay the load contributed by all blocked children and account this so that
1194 * their contribution may appropriately discounted when they wake up.
1195 */
Paul Turneraff3e492012-10-04 13:18:30 +02001196static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001197{
1198 u64 now = rq_of(cfs_rq)->clock_task >> 20;
1199 u64 decays;
1200
1201 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02001202 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02001203 return;
1204
Paul Turneraff3e492012-10-04 13:18:30 +02001205 if (atomic64_read(&cfs_rq->removed_load)) {
1206 u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
1207 subtract_blocked_load_contrib(cfs_rq, removed_load);
1208 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001209
Paul Turneraff3e492012-10-04 13:18:30 +02001210 if (decays) {
1211 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
1212 decays);
1213 atomic64_add(decays, &cfs_rq->decay_counter);
1214 cfs_rq->last_decay = now;
1215 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02001216
1217 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02001218}
Ben Segall18bf2802012-10-04 12:51:20 +02001219
1220static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
1221{
1222 __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
1223}
Paul Turner2dac7542012-10-04 13:18:30 +02001224
1225/* Add the load generated by se into cfs_rq's child load-average */
1226static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001227 struct sched_entity *se,
1228 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02001229{
Paul Turneraff3e492012-10-04 13:18:30 +02001230 /*
1231 * We track migrations using entity decay_count <= 0, on a wake-up
1232 * migration we use a negative decay count to track the remote decays
1233 * accumulated while sleeping.
1234 */
1235 if (unlikely(se->avg.decay_count <= 0)) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001236 se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
Paul Turneraff3e492012-10-04 13:18:30 +02001237 if (se->avg.decay_count) {
1238 /*
1239 * In a wake-up migration we have to approximate the
1240 * time sleeping. This is because we can't synchronize
1241 * clock_task between the two cpus, and it is not
1242 * guaranteed to be read-safe. Instead, we can
1243 * approximate this using our carried decays, which are
1244 * explicitly atomically readable.
1245 */
1246 se->avg.last_runnable_update -= (-se->avg.decay_count)
1247 << 20;
1248 update_entity_load_avg(se, 0);
1249 /* Indicate that we're now synchronized and on-rq */
1250 se->avg.decay_count = 0;
1251 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001252 wakeup = 0;
1253 } else {
1254 __synchronize_entity_decay(se);
1255 }
1256
Paul Turneraff3e492012-10-04 13:18:30 +02001257 /* migrated tasks did not contribute to our blocked load */
1258 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02001259 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02001260 update_entity_load_avg(se, 0);
1261 }
Paul Turner9ee474f2012-10-04 13:18:30 +02001262
Paul Turner2dac7542012-10-04 13:18:30 +02001263 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02001264 /* we force update consideration on load-balancer moves */
1265 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02001266}
1267
Paul Turner9ee474f2012-10-04 13:18:30 +02001268/*
1269 * Remove se's load from this cfs_rq child load-average, if the entity is
1270 * transitioning to a blocked state we track its projected decay using
1271 * blocked_load_avg.
1272 */
Paul Turner2dac7542012-10-04 13:18:30 +02001273static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001274 struct sched_entity *se,
1275 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02001276{
Paul Turner9ee474f2012-10-04 13:18:30 +02001277 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001278 /* we force update consideration on load-balancer moves */
1279 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02001280
Paul Turner2dac7542012-10-04 13:18:30 +02001281 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02001282 if (sleep) {
1283 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
1284 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
1285 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02001286}
Paul Turner9d85f212012-10-04 13:18:29 +02001287#else
Paul Turner9ee474f2012-10-04 13:18:30 +02001288static inline void update_entity_load_avg(struct sched_entity *se,
1289 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02001290static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001291static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001292 struct sched_entity *se,
1293 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02001294static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02001295 struct sched_entity *se,
1296 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02001297static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
1298 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02001299#endif
1300
Ingo Molnar2396af62007-08-09 11:16:48 +02001301static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001302{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001303#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001304 struct task_struct *tsk = NULL;
1305
1306 if (entity_is_task(se))
1307 tsk = task_of(se);
1308
Lucas De Marchi41acab82010-03-10 23:37:45 -03001309 if (se->statistics.sleep_start) {
1310 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001311
1312 if ((s64)delta < 0)
1313 delta = 0;
1314
Lucas De Marchi41acab82010-03-10 23:37:45 -03001315 if (unlikely(delta > se->statistics.sleep_max))
1316 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001317
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001318 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001319 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001320
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001321 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001322 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001323 trace_sched_stat_sleep(tsk, delta);
1324 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001325 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001326 if (se->statistics.block_start) {
1327 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001328
1329 if ((s64)delta < 0)
1330 delta = 0;
1331
Lucas De Marchi41acab82010-03-10 23:37:45 -03001332 if (unlikely(delta > se->statistics.block_max))
1333 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001334
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001335 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001336 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001337
Peter Zijlstrae4143142009-07-23 20:13:26 +02001338 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001339 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001340 se->statistics.iowait_sum += delta;
1341 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001342 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001343 }
1344
Andrew Vaginb781a602011-11-28 12:03:35 +03001345 trace_sched_stat_blocked(tsk, delta);
1346
Peter Zijlstrae4143142009-07-23 20:13:26 +02001347 /*
1348 * Blocking time is in units of nanosecs, so shift by
1349 * 20 to get a milliseconds-range estimation of the
1350 * amount of time that the task spent sleeping:
1351 */
1352 if (unlikely(prof_on == SLEEP_PROFILING)) {
1353 profile_hits(SLEEP_PROFILING,
1354 (void *)get_wchan(tsk),
1355 delta >> 20);
1356 }
1357 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001358 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001359 }
1360#endif
1361}
1362
Peter Zijlstraddc97292007-10-15 17:00:10 +02001363static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1364{
1365#ifdef CONFIG_SCHED_DEBUG
1366 s64 d = se->vruntime - cfs_rq->min_vruntime;
1367
1368 if (d < 0)
1369 d = -d;
1370
1371 if (d > 3*sysctl_sched_latency)
1372 schedstat_inc(cfs_rq, nr_spread_over);
1373#endif
1374}
1375
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001376static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001377place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1378{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001379 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001380
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001381 /*
1382 * The 'current' period is already promised to the current tasks,
1383 * however the extra weight of the new task will slow them down a
1384 * little, place the new task so that it fits in the slot that
1385 * stays open at the end.
1386 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001387 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001388 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001389
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001390 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001391 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001392 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001393
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001394 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001395 * Halve their sleep time's effect, to allow
1396 * for a gentler effect of sleepers:
1397 */
1398 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1399 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001400
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001401 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001402 }
1403
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001404 /* ensure we never gain time by being placed backwards. */
1405 vruntime = max_vruntime(se->vruntime, vruntime);
1406
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001407 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001408}
1409
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001410static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1411
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001412static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001413enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001414{
1415 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001416 * Update the normalized vruntime before updating min_vruntime
1417 * through callig update_curr().
1418 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001419 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001420 se->vruntime += cfs_rq->min_vruntime;
1421
1422 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001423 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001424 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001425 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001426 update_cfs_load(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02001427 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001428 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001429 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001430
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001431 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001432 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001433 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001434 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001435
Ingo Molnard2417e52007-08-09 11:16:47 +02001436 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001437 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001438 if (se != cfs_rq->curr)
1439 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001440 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001441
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001442 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001443 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001444 check_enqueue_throttle(cfs_rq);
1445 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001446}
1447
Rik van Riel2c13c9192011-02-01 09:48:37 -05001448static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001449{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001450 for_each_sched_entity(se) {
1451 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1452 if (cfs_rq->last == se)
1453 cfs_rq->last = NULL;
1454 else
1455 break;
1456 }
1457}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001458
Rik van Riel2c13c9192011-02-01 09:48:37 -05001459static void __clear_buddies_next(struct sched_entity *se)
1460{
1461 for_each_sched_entity(se) {
1462 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1463 if (cfs_rq->next == se)
1464 cfs_rq->next = NULL;
1465 else
1466 break;
1467 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001468}
1469
Rik van Rielac53db52011-02-01 09:51:03 -05001470static void __clear_buddies_skip(struct sched_entity *se)
1471{
1472 for_each_sched_entity(se) {
1473 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1474 if (cfs_rq->skip == se)
1475 cfs_rq->skip = NULL;
1476 else
1477 break;
1478 }
1479}
1480
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001481static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1482{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001483 if (cfs_rq->last == se)
1484 __clear_buddies_last(se);
1485
1486 if (cfs_rq->next == se)
1487 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001488
1489 if (cfs_rq->skip == se)
1490 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001491}
1492
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001493static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001494
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001495static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001496dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001497{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001498 /*
1499 * Update run-time statistics of the 'current'.
1500 */
1501 update_curr(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02001502 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001503
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001504 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001505 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001506#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001507 if (entity_is_task(se)) {
1508 struct task_struct *tsk = task_of(se);
1509
1510 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001511 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001512 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001513 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001514 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001515#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001516 }
1517
Peter Zijlstra2002c692008-11-11 11:52:33 +01001518 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001519
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001520 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001521 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001522 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001523 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001524 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001525
1526 /*
1527 * Normalize the entity after updating the min_vruntime because the
1528 * update can refer to the ->curr item and we need to reflect this
1529 * movement in our normalized position.
1530 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001531 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001532 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001533
Paul Turnerd8b49862011-07-21 09:43:41 -07001534 /* return excess runtime on last dequeue */
1535 return_cfs_rq_runtime(cfs_rq);
1536
Peter Zijlstra1e876232011-05-17 16:21:10 -07001537 update_min_vruntime(cfs_rq);
1538 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001539}
1540
1541/*
1542 * Preempt the current task with a newly woken task if needed:
1543 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001544static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001545check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001546{
Peter Zijlstra11697832007-09-05 14:32:49 +02001547 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001548 struct sched_entity *se;
1549 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001550
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001551 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001552 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001553 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001554 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001555 /*
1556 * The current task ran long enough, ensure it doesn't get
1557 * re-elected due to buddy favours.
1558 */
1559 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001560 return;
1561 }
1562
1563 /*
1564 * Ensure that a task that missed wakeup preemption by a
1565 * narrow margin doesn't have to wait for a full slice.
1566 * This also mitigates buddy induced latencies under load.
1567 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001568 if (delta_exec < sysctl_sched_min_granularity)
1569 return;
1570
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001571 se = __pick_first_entity(cfs_rq);
1572 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001573
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001574 if (delta < 0)
1575 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001576
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001577 if (delta > ideal_runtime)
1578 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001579}
1580
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001581static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001582set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001583{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001584 /* 'current' is not kept within the tree. */
1585 if (se->on_rq) {
1586 /*
1587 * Any task has to be enqueued before it get to execute on
1588 * a CPU. So account for the time it spent waiting on the
1589 * runqueue.
1590 */
1591 update_stats_wait_end(cfs_rq, se);
1592 __dequeue_entity(cfs_rq, se);
1593 }
1594
Ingo Molnar79303e92007-08-09 11:16:47 +02001595 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001596 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001597#ifdef CONFIG_SCHEDSTATS
1598 /*
1599 * Track our maximum slice length, if the CPU's load is at
1600 * least twice that of our own weight (i.e. dont track it
1601 * when there are only lesser-weight tasks around):
1602 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001603 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001604 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001605 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1606 }
1607#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001608 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001609}
1610
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001611static int
1612wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1613
Rik van Rielac53db52011-02-01 09:51:03 -05001614/*
1615 * Pick the next process, keeping these things in mind, in this order:
1616 * 1) keep things fair between processes/task groups
1617 * 2) pick the "next" process, since someone really wants that to run
1618 * 3) pick the "last" process, for cache locality
1619 * 4) do not run the "skip" process, if something else is available
1620 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001621static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001622{
Rik van Rielac53db52011-02-01 09:51:03 -05001623 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001624 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001625
Rik van Rielac53db52011-02-01 09:51:03 -05001626 /*
1627 * Avoid running the skip buddy, if running something else can
1628 * be done without getting too unfair.
1629 */
1630 if (cfs_rq->skip == se) {
1631 struct sched_entity *second = __pick_next_entity(se);
1632 if (second && wakeup_preempt_entity(second, left) < 1)
1633 se = second;
1634 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001635
Mike Galbraithf685cea2009-10-23 23:09:22 +02001636 /*
1637 * Prefer last buddy, try to return the CPU to a preempted task.
1638 */
1639 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1640 se = cfs_rq->last;
1641
Rik van Rielac53db52011-02-01 09:51:03 -05001642 /*
1643 * Someone really wants this to run. If it's not unfair, run it.
1644 */
1645 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1646 se = cfs_rq->next;
1647
Mike Galbraithf685cea2009-10-23 23:09:22 +02001648 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001649
1650 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001651}
1652
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001653static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1654
Ingo Molnarab6cde22007-08-09 11:16:48 +02001655static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001656{
1657 /*
1658 * If still on the runqueue then deactivate_task()
1659 * was not called and update_curr() has to be done:
1660 */
1661 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001662 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001663
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001664 /* throttle cfs_rqs exceeding runtime */
1665 check_cfs_rq_runtime(cfs_rq);
1666
Peter Zijlstraddc97292007-10-15 17:00:10 +02001667 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001668 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001669 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001670 /* Put 'current' back into the tree. */
1671 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02001672 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02001673 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001674 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001675 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001676}
1677
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001678static void
1679entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001680{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001681 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001682 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001683 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001684 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001685
Paul Turner43365bd2010-12-15 19:10:17 -08001686 /*
Paul Turner9d85f212012-10-04 13:18:29 +02001687 * Ensure that runnable average is periodically updated.
1688 */
Paul Turner9ee474f2012-10-04 13:18:30 +02001689 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02001690 update_cfs_rq_blocked_load(cfs_rq, 1);
Paul Turner9d85f212012-10-04 13:18:29 +02001691
1692 /*
Paul Turner43365bd2010-12-15 19:10:17 -08001693 * Update share accounting for long-running entities.
1694 */
1695 update_entity_shares_tick(cfs_rq);
1696
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001697#ifdef CONFIG_SCHED_HRTICK
1698 /*
1699 * queued ticks are scheduled to match the slice, so don't bother
1700 * validating it and just reschedule.
1701 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001702 if (queued) {
1703 resched_task(rq_of(cfs_rq)->curr);
1704 return;
1705 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001706 /*
1707 * don't let the period tick interfere with the hrtick preemption
1708 */
1709 if (!sched_feat(DOUBLE_TICK) &&
1710 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1711 return;
1712#endif
1713
Yong Zhang2c2efae2011-07-29 16:20:33 +08001714 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001715 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001716}
1717
Paul Turnerab84d312011-07-21 09:43:28 -07001718
1719/**************************************************
1720 * CFS bandwidth control machinery
1721 */
1722
1723#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001724
1725#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001726static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001727
1728static inline bool cfs_bandwidth_used(void)
1729{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001730 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001731}
1732
1733void account_cfs_bandwidth_used(int enabled, int was_enabled)
1734{
1735 /* only need to count groups transitioning between enabled/!enabled */
1736 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001737 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001738 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001739 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001740}
1741#else /* HAVE_JUMP_LABEL */
1742static bool cfs_bandwidth_used(void)
1743{
1744 return true;
1745}
1746
1747void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1748#endif /* HAVE_JUMP_LABEL */
1749
Paul Turnerab84d312011-07-21 09:43:28 -07001750/*
1751 * default period for cfs group bandwidth.
1752 * default: 0.1s, units: nanoseconds
1753 */
1754static inline u64 default_cfs_period(void)
1755{
1756 return 100000000ULL;
1757}
Paul Turnerec12cb72011-07-21 09:43:30 -07001758
1759static inline u64 sched_cfs_bandwidth_slice(void)
1760{
1761 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1762}
1763
Paul Turnera9cf55b2011-07-21 09:43:32 -07001764/*
1765 * Replenish runtime according to assigned quota and update expiration time.
1766 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1767 * additional synchronization around rq->lock.
1768 *
1769 * requires cfs_b->lock
1770 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001771void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001772{
1773 u64 now;
1774
1775 if (cfs_b->quota == RUNTIME_INF)
1776 return;
1777
1778 now = sched_clock_cpu(smp_processor_id());
1779 cfs_b->runtime = cfs_b->quota;
1780 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1781}
1782
Peter Zijlstra029632f2011-10-25 10:00:11 +02001783static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1784{
1785 return &tg->cfs_bandwidth;
1786}
1787
Paul Turner85dac902011-07-21 09:43:33 -07001788/* returns 0 on failure to allocate runtime */
1789static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001790{
1791 struct task_group *tg = cfs_rq->tg;
1792 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001793 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001794
1795 /* note: this is a positive sum as runtime_remaining <= 0 */
1796 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1797
1798 raw_spin_lock(&cfs_b->lock);
1799 if (cfs_b->quota == RUNTIME_INF)
1800 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001801 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001802 /*
1803 * If the bandwidth pool has become inactive, then at least one
1804 * period must have elapsed since the last consumption.
1805 * Refresh the global state and ensure bandwidth timer becomes
1806 * active.
1807 */
1808 if (!cfs_b->timer_active) {
1809 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001810 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001811 }
Paul Turner58088ad2011-07-21 09:43:31 -07001812
1813 if (cfs_b->runtime > 0) {
1814 amount = min(cfs_b->runtime, min_amount);
1815 cfs_b->runtime -= amount;
1816 cfs_b->idle = 0;
1817 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001818 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001819 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001820 raw_spin_unlock(&cfs_b->lock);
1821
1822 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001823 /*
1824 * we may have advanced our local expiration to account for allowed
1825 * spread between our sched_clock and the one on which runtime was
1826 * issued.
1827 */
1828 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1829 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001830
1831 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001832}
1833
1834/*
1835 * Note: This depends on the synchronization provided by sched_clock and the
1836 * fact that rq->clock snapshots this value.
1837 */
1838static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1839{
1840 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1841 struct rq *rq = rq_of(cfs_rq);
1842
1843 /* if the deadline is ahead of our clock, nothing to do */
1844 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1845 return;
1846
1847 if (cfs_rq->runtime_remaining < 0)
1848 return;
1849
1850 /*
1851 * If the local deadline has passed we have to consider the
1852 * possibility that our sched_clock is 'fast' and the global deadline
1853 * has not truly expired.
1854 *
1855 * Fortunately we can check determine whether this the case by checking
1856 * whether the global deadline has advanced.
1857 */
1858
1859 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1860 /* extend local deadline, drift is bounded above by 2 ticks */
1861 cfs_rq->runtime_expires += TICK_NSEC;
1862 } else {
1863 /* global deadline is ahead, expiration has passed */
1864 cfs_rq->runtime_remaining = 0;
1865 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001866}
1867
1868static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1869 unsigned long delta_exec)
1870{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001871 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001872 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001873 expire_cfs_rq_runtime(cfs_rq);
1874
1875 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001876 return;
1877
Paul Turner85dac902011-07-21 09:43:33 -07001878 /*
1879 * if we're unable to extend our runtime we resched so that the active
1880 * hierarchy can be throttled
1881 */
1882 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1883 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001884}
1885
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001886static __always_inline
1887void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001888{
Paul Turner56f570e2011-11-07 20:26:33 -08001889 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001890 return;
1891
1892 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1893}
1894
Paul Turner85dac902011-07-21 09:43:33 -07001895static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1896{
Paul Turner56f570e2011-11-07 20:26:33 -08001897 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001898}
1899
Paul Turner64660c82011-07-21 09:43:36 -07001900/* check whether cfs_rq, or any parent, is throttled */
1901static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1902{
Paul Turner56f570e2011-11-07 20:26:33 -08001903 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001904}
1905
1906/*
1907 * Ensure that neither of the group entities corresponding to src_cpu or
1908 * dest_cpu are members of a throttled hierarchy when performing group
1909 * load-balance operations.
1910 */
1911static inline int throttled_lb_pair(struct task_group *tg,
1912 int src_cpu, int dest_cpu)
1913{
1914 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1915
1916 src_cfs_rq = tg->cfs_rq[src_cpu];
1917 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1918
1919 return throttled_hierarchy(src_cfs_rq) ||
1920 throttled_hierarchy(dest_cfs_rq);
1921}
1922
1923/* updated child weight may affect parent so we have to do this bottom up */
1924static int tg_unthrottle_up(struct task_group *tg, void *data)
1925{
1926 struct rq *rq = data;
1927 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1928
1929 cfs_rq->throttle_count--;
1930#ifdef CONFIG_SMP
1931 if (!cfs_rq->throttle_count) {
1932 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1933
1934 /* leaving throttled state, advance shares averaging windows */
1935 cfs_rq->load_stamp += delta;
1936 cfs_rq->load_last += delta;
1937
1938 /* update entity weight now that we are on_rq again */
1939 update_cfs_shares(cfs_rq);
1940 }
1941#endif
1942
1943 return 0;
1944}
1945
1946static int tg_throttle_down(struct task_group *tg, void *data)
1947{
1948 struct rq *rq = data;
1949 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1950
1951 /* group is entering throttled state, record last load */
1952 if (!cfs_rq->throttle_count)
1953 update_cfs_load(cfs_rq, 0);
1954 cfs_rq->throttle_count++;
1955
1956 return 0;
1957}
1958
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001959static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001960{
1961 struct rq *rq = rq_of(cfs_rq);
1962 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1963 struct sched_entity *se;
1964 long task_delta, dequeue = 1;
1965
1966 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1967
1968 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001969 rcu_read_lock();
1970 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1971 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001972
1973 task_delta = cfs_rq->h_nr_running;
1974 for_each_sched_entity(se) {
1975 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1976 /* throttled entity or throttle-on-deactivate */
1977 if (!se->on_rq)
1978 break;
1979
1980 if (dequeue)
1981 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1982 qcfs_rq->h_nr_running -= task_delta;
1983
1984 if (qcfs_rq->load.weight)
1985 dequeue = 0;
1986 }
1987
1988 if (!se)
1989 rq->nr_running -= task_delta;
1990
1991 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001992 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001993 raw_spin_lock(&cfs_b->lock);
1994 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1995 raw_spin_unlock(&cfs_b->lock);
1996}
1997
Peter Zijlstra029632f2011-10-25 10:00:11 +02001998void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001999{
2000 struct rq *rq = rq_of(cfs_rq);
2001 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2002 struct sched_entity *se;
2003 int enqueue = 1;
2004 long task_delta;
2005
2006 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
2007
2008 cfs_rq->throttled = 0;
2009 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002010 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07002011 list_del_rcu(&cfs_rq->throttled_list);
2012 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002013 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07002014
Paul Turner64660c82011-07-21 09:43:36 -07002015 update_rq_clock(rq);
2016 /* update hierarchical throttle state */
2017 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
2018
Paul Turner671fd9d2011-07-21 09:43:34 -07002019 if (!cfs_rq->load.weight)
2020 return;
2021
2022 task_delta = cfs_rq->h_nr_running;
2023 for_each_sched_entity(se) {
2024 if (se->on_rq)
2025 enqueue = 0;
2026
2027 cfs_rq = cfs_rq_of(se);
2028 if (enqueue)
2029 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
2030 cfs_rq->h_nr_running += task_delta;
2031
2032 if (cfs_rq_throttled(cfs_rq))
2033 break;
2034 }
2035
2036 if (!se)
2037 rq->nr_running += task_delta;
2038
2039 /* determine whether we need to wake up potentially idle cpu */
2040 if (rq->curr == rq->idle && rq->cfs.nr_running)
2041 resched_task(rq->curr);
2042}
2043
2044static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
2045 u64 remaining, u64 expires)
2046{
2047 struct cfs_rq *cfs_rq;
2048 u64 runtime = remaining;
2049
2050 rcu_read_lock();
2051 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
2052 throttled_list) {
2053 struct rq *rq = rq_of(cfs_rq);
2054
2055 raw_spin_lock(&rq->lock);
2056 if (!cfs_rq_throttled(cfs_rq))
2057 goto next;
2058
2059 runtime = -cfs_rq->runtime_remaining + 1;
2060 if (runtime > remaining)
2061 runtime = remaining;
2062 remaining -= runtime;
2063
2064 cfs_rq->runtime_remaining += runtime;
2065 cfs_rq->runtime_expires = expires;
2066
2067 /* we check whether we're throttled above */
2068 if (cfs_rq->runtime_remaining > 0)
2069 unthrottle_cfs_rq(cfs_rq);
2070
2071next:
2072 raw_spin_unlock(&rq->lock);
2073
2074 if (!remaining)
2075 break;
2076 }
2077 rcu_read_unlock();
2078
2079 return remaining;
2080}
2081
Paul Turner58088ad2011-07-21 09:43:31 -07002082/*
2083 * Responsible for refilling a task_group's bandwidth and unthrottling its
2084 * cfs_rqs as appropriate. If there has been no activity within the last
2085 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
2086 * used to track this state.
2087 */
2088static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
2089{
Paul Turner671fd9d2011-07-21 09:43:34 -07002090 u64 runtime, runtime_expires;
2091 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07002092
2093 raw_spin_lock(&cfs_b->lock);
2094 /* no need to continue the timer with no bandwidth constraint */
2095 if (cfs_b->quota == RUNTIME_INF)
2096 goto out_unlock;
2097
Paul Turner671fd9d2011-07-21 09:43:34 -07002098 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2099 /* idle depends on !throttled (for the case of a large deficit) */
2100 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002101 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07002102
Paul Turnera9cf55b2011-07-21 09:43:32 -07002103 /* if we're going inactive then everything else can be deferred */
2104 if (idle)
2105 goto out_unlock;
2106
2107 __refill_cfs_bandwidth_runtime(cfs_b);
2108
Paul Turner671fd9d2011-07-21 09:43:34 -07002109 if (!throttled) {
2110 /* mark as potentially idle for the upcoming period */
2111 cfs_b->idle = 1;
2112 goto out_unlock;
2113 }
Paul Turner58088ad2011-07-21 09:43:31 -07002114
Nikhil Raoe8da1b12011-07-21 09:43:40 -07002115 /* account preceding periods in which throttling occurred */
2116 cfs_b->nr_throttled += overrun;
2117
Paul Turner671fd9d2011-07-21 09:43:34 -07002118 /*
2119 * There are throttled entities so we must first use the new bandwidth
2120 * to unthrottle them before making it generally available. This
2121 * ensures that all existing debts will be paid before a new cfs_rq is
2122 * allowed to run.
2123 */
2124 runtime = cfs_b->runtime;
2125 runtime_expires = cfs_b->runtime_expires;
2126 cfs_b->runtime = 0;
2127
2128 /*
2129 * This check is repeated as we are holding onto the new bandwidth
2130 * while we unthrottle. This can potentially race with an unthrottled
2131 * group trying to acquire new bandwidth from the global pool.
2132 */
2133 while (throttled && runtime > 0) {
2134 raw_spin_unlock(&cfs_b->lock);
2135 /* we can't nest cfs_b->lock while distributing bandwidth */
2136 runtime = distribute_cfs_runtime(cfs_b, runtime,
2137 runtime_expires);
2138 raw_spin_lock(&cfs_b->lock);
2139
2140 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2141 }
2142
2143 /* return (any) remaining runtime */
2144 cfs_b->runtime = runtime;
2145 /*
2146 * While we are ensured activity in the period following an
2147 * unthrottle, this also covers the case in which the new bandwidth is
2148 * insufficient to cover the existing bandwidth deficit. (Forcing the
2149 * timer to remain active while there are any throttled entities.)
2150 */
2151 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002152out_unlock:
2153 if (idle)
2154 cfs_b->timer_active = 0;
2155 raw_spin_unlock(&cfs_b->lock);
2156
2157 return idle;
2158}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002159
Paul Turnerd8b49862011-07-21 09:43:41 -07002160/* a cfs_rq won't donate quota below this amount */
2161static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2162/* minimum remaining period time to redistribute slack quota */
2163static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2164/* how long we wait to gather additional slack before distributing */
2165static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2166
2167/* are we near the end of the current quota period? */
2168static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2169{
2170 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2171 u64 remaining;
2172
2173 /* if the call-back is running a quota refresh is already occurring */
2174 if (hrtimer_callback_running(refresh_timer))
2175 return 1;
2176
2177 /* is a quota refresh about to occur? */
2178 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2179 if (remaining < min_expire)
2180 return 1;
2181
2182 return 0;
2183}
2184
2185static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2186{
2187 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2188
2189 /* if there's a quota refresh soon don't bother with slack */
2190 if (runtime_refresh_within(cfs_b, min_left))
2191 return;
2192
2193 start_bandwidth_timer(&cfs_b->slack_timer,
2194 ns_to_ktime(cfs_bandwidth_slack_period));
2195}
2196
2197/* we know any runtime found here is valid as update_curr() precedes return */
2198static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2199{
2200 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2201 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2202
2203 if (slack_runtime <= 0)
2204 return;
2205
2206 raw_spin_lock(&cfs_b->lock);
2207 if (cfs_b->quota != RUNTIME_INF &&
2208 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2209 cfs_b->runtime += slack_runtime;
2210
2211 /* we are under rq->lock, defer unthrottling using a timer */
2212 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2213 !list_empty(&cfs_b->throttled_cfs_rq))
2214 start_cfs_slack_bandwidth(cfs_b);
2215 }
2216 raw_spin_unlock(&cfs_b->lock);
2217
2218 /* even if it's not valid for return we don't want to try again */
2219 cfs_rq->runtime_remaining -= slack_runtime;
2220}
2221
2222static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2223{
Paul Turner56f570e2011-11-07 20:26:33 -08002224 if (!cfs_bandwidth_used())
2225 return;
2226
Paul Turnerfccfdc62011-11-07 20:26:34 -08002227 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002228 return;
2229
2230 __return_cfs_rq_runtime(cfs_rq);
2231}
2232
2233/*
2234 * This is done with a timer (instead of inline with bandwidth return) since
2235 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2236 */
2237static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2238{
2239 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2240 u64 expires;
2241
2242 /* confirm we're still not at a refresh boundary */
2243 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2244 return;
2245
2246 raw_spin_lock(&cfs_b->lock);
2247 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2248 runtime = cfs_b->runtime;
2249 cfs_b->runtime = 0;
2250 }
2251 expires = cfs_b->runtime_expires;
2252 raw_spin_unlock(&cfs_b->lock);
2253
2254 if (!runtime)
2255 return;
2256
2257 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2258
2259 raw_spin_lock(&cfs_b->lock);
2260 if (expires == cfs_b->runtime_expires)
2261 cfs_b->runtime = runtime;
2262 raw_spin_unlock(&cfs_b->lock);
2263}
2264
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002265/*
2266 * When a group wakes up we want to make sure that its quota is not already
2267 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2268 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2269 */
2270static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2271{
Paul Turner56f570e2011-11-07 20:26:33 -08002272 if (!cfs_bandwidth_used())
2273 return;
2274
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002275 /* an active group must be handled by the update_curr()->put() path */
2276 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2277 return;
2278
2279 /* ensure the group is not already throttled */
2280 if (cfs_rq_throttled(cfs_rq))
2281 return;
2282
2283 /* update runtime allocation */
2284 account_cfs_rq_runtime(cfs_rq, 0);
2285 if (cfs_rq->runtime_remaining <= 0)
2286 throttle_cfs_rq(cfs_rq);
2287}
2288
2289/* conditionally throttle active cfs_rq's from put_prev_entity() */
2290static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2291{
Paul Turner56f570e2011-11-07 20:26:33 -08002292 if (!cfs_bandwidth_used())
2293 return;
2294
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002295 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2296 return;
2297
2298 /*
2299 * it's possible for a throttled entity to be forced into a running
2300 * state (e.g. set_curr_task), in this case we're finished.
2301 */
2302 if (cfs_rq_throttled(cfs_rq))
2303 return;
2304
2305 throttle_cfs_rq(cfs_rq);
2306}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002307
2308static inline u64 default_cfs_period(void);
2309static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2310static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2311
2312static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2313{
2314 struct cfs_bandwidth *cfs_b =
2315 container_of(timer, struct cfs_bandwidth, slack_timer);
2316 do_sched_cfs_slack_timer(cfs_b);
2317
2318 return HRTIMER_NORESTART;
2319}
2320
2321static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2322{
2323 struct cfs_bandwidth *cfs_b =
2324 container_of(timer, struct cfs_bandwidth, period_timer);
2325 ktime_t now;
2326 int overrun;
2327 int idle = 0;
2328
2329 for (;;) {
2330 now = hrtimer_cb_get_time(timer);
2331 overrun = hrtimer_forward(timer, now, cfs_b->period);
2332
2333 if (!overrun)
2334 break;
2335
2336 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2337 }
2338
2339 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2340}
2341
2342void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2343{
2344 raw_spin_lock_init(&cfs_b->lock);
2345 cfs_b->runtime = 0;
2346 cfs_b->quota = RUNTIME_INF;
2347 cfs_b->period = ns_to_ktime(default_cfs_period());
2348
2349 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2350 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2351 cfs_b->period_timer.function = sched_cfs_period_timer;
2352 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2353 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2354}
2355
2356static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2357{
2358 cfs_rq->runtime_enabled = 0;
2359 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2360}
2361
2362/* requires cfs_b->lock, may release to reprogram timer */
2363void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2364{
2365 /*
2366 * The timer may be active because we're trying to set a new bandwidth
2367 * period or because we're racing with the tear-down path
2368 * (timer_active==0 becomes visible before the hrtimer call-back
2369 * terminates). In either case we ensure that it's re-programmed
2370 */
2371 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2372 raw_spin_unlock(&cfs_b->lock);
2373 /* ensure cfs_b->lock is available while we wait */
2374 hrtimer_cancel(&cfs_b->period_timer);
2375
2376 raw_spin_lock(&cfs_b->lock);
2377 /* if someone else restarted the timer then we're done */
2378 if (cfs_b->timer_active)
2379 return;
2380 }
2381
2382 cfs_b->timer_active = 1;
2383 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2384}
2385
2386static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2387{
2388 hrtimer_cancel(&cfs_b->period_timer);
2389 hrtimer_cancel(&cfs_b->slack_timer);
2390}
2391
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002392static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002393{
2394 struct cfs_rq *cfs_rq;
2395
2396 for_each_leaf_cfs_rq(rq, cfs_rq) {
2397 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2398
2399 if (!cfs_rq->runtime_enabled)
2400 continue;
2401
2402 /*
2403 * clock_task is not advancing so we just need to make sure
2404 * there's some valid quota amount
2405 */
2406 cfs_rq->runtime_remaining = cfs_b->quota;
2407 if (cfs_rq_throttled(cfs_rq))
2408 unthrottle_cfs_rq(cfs_rq);
2409 }
2410}
2411
2412#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002413static __always_inline
2414void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002415static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2416static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002417static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002418
2419static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2420{
2421 return 0;
2422}
Paul Turner64660c82011-07-21 09:43:36 -07002423
2424static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2425{
2426 return 0;
2427}
2428
2429static inline int throttled_lb_pair(struct task_group *tg,
2430 int src_cpu, int dest_cpu)
2431{
2432 return 0;
2433}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002434
2435void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2436
2437#ifdef CONFIG_FAIR_GROUP_SCHED
2438static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002439#endif
2440
Peter Zijlstra029632f2011-10-25 10:00:11 +02002441static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2442{
2443 return NULL;
2444}
2445static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002446static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002447
2448#endif /* CONFIG_CFS_BANDWIDTH */
2449
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002450/**************************************************
2451 * CFS operations on tasks:
2452 */
2453
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002454#ifdef CONFIG_SCHED_HRTICK
2455static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2456{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002457 struct sched_entity *se = &p->se;
2458 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2459
2460 WARN_ON(task_rq(p) != rq);
2461
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002462 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002463 u64 slice = sched_slice(cfs_rq, se);
2464 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2465 s64 delta = slice - ran;
2466
2467 if (delta < 0) {
2468 if (rq->curr == p)
2469 resched_task(p);
2470 return;
2471 }
2472
2473 /*
2474 * Don't schedule slices shorter than 10000ns, that just
2475 * doesn't make sense. Rely on vruntime for fairness.
2476 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002477 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002478 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002479
Peter Zijlstra31656512008-07-18 18:01:23 +02002480 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002481 }
2482}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002483
2484/*
2485 * called from enqueue/dequeue and updates the hrtick when the
2486 * current task is from our class and nr_running is low enough
2487 * to matter.
2488 */
2489static void hrtick_update(struct rq *rq)
2490{
2491 struct task_struct *curr = rq->curr;
2492
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002493 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002494 return;
2495
2496 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2497 hrtick_start_fair(rq, curr);
2498}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302499#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002500static inline void
2501hrtick_start_fair(struct rq *rq, struct task_struct *p)
2502{
2503}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002504
2505static inline void hrtick_update(struct rq *rq)
2506{
2507}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002508#endif
2509
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002510/*
2511 * The enqueue_task method is called before nr_running is
2512 * increased. Here we update the fair scheduling stats and
2513 * then put the task into the rbtree:
2514 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002515static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002516enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002517{
2518 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002519 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002520
2521 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002522 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002523 break;
2524 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002525 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002526
2527 /*
2528 * end evaluation on encountering a throttled cfs_rq
2529 *
2530 * note: in the case of encountering a throttled cfs_rq we will
2531 * post the final h_nr_running increment below.
2532 */
2533 if (cfs_rq_throttled(cfs_rq))
2534 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002535 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002536
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002537 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002538 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002539
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002540 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002541 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002542 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002543
Paul Turner85dac902011-07-21 09:43:33 -07002544 if (cfs_rq_throttled(cfs_rq))
2545 break;
2546
Paul Turnerd6b55912010-11-15 15:47:09 -08002547 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002548 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002549 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002550 }
2551
Ben Segall18bf2802012-10-04 12:51:20 +02002552 if (!se) {
2553 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07002554 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002555 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002556 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002557}
2558
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002559static void set_next_buddy(struct sched_entity *se);
2560
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002561/*
2562 * The dequeue_task method is called before nr_running is
2563 * decreased. We remove the task from the rbtree and
2564 * update the fair scheduling stats:
2565 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002566static void dequeue_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;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002570 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002571
2572 for_each_sched_entity(se) {
2573 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002574 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002575
2576 /*
2577 * end evaluation on encountering a throttled cfs_rq
2578 *
2579 * note: in the case of encountering a throttled cfs_rq we will
2580 * post the final h_nr_running decrement below.
2581 */
2582 if (cfs_rq_throttled(cfs_rq))
2583 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002584 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002585
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002586 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002587 if (cfs_rq->load.weight) {
2588 /*
2589 * Bias pick_next to pick a task from this cfs_rq, as
2590 * p is sleeping when it is within its sched_slice.
2591 */
2592 if (task_sleep && parent_entity(se))
2593 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002594
2595 /* avoid re-evaluating load for this entity */
2596 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002597 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002598 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002599 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002600 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002601
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002602 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002603 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002604 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002605
Paul Turner85dac902011-07-21 09:43:33 -07002606 if (cfs_rq_throttled(cfs_rq))
2607 break;
2608
Paul Turnerd6b55912010-11-15 15:47:09 -08002609 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002610 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02002611 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002612 }
2613
Ben Segall18bf2802012-10-04 12:51:20 +02002614 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07002615 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02002616 update_rq_runnable_avg(rq, 1);
2617 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002618 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002619}
2620
Gregory Haskinse7693a32008-01-25 21:08:09 +01002621#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002622/* Used instead of source_load when we know the type == 0 */
2623static unsigned long weighted_cpuload(const int cpu)
2624{
2625 return cpu_rq(cpu)->load.weight;
2626}
2627
2628/*
2629 * Return a low guess at the load of a migration-source cpu weighted
2630 * according to the scheduling class and "nice" value.
2631 *
2632 * We want to under-estimate the load of migration sources, to
2633 * balance conservatively.
2634 */
2635static unsigned long source_load(int cpu, int type)
2636{
2637 struct rq *rq = cpu_rq(cpu);
2638 unsigned long total = weighted_cpuload(cpu);
2639
2640 if (type == 0 || !sched_feat(LB_BIAS))
2641 return total;
2642
2643 return min(rq->cpu_load[type-1], total);
2644}
2645
2646/*
2647 * Return a high guess at the load of a migration-target cpu weighted
2648 * according to the scheduling class and "nice" value.
2649 */
2650static unsigned long target_load(int cpu, int type)
2651{
2652 struct rq *rq = cpu_rq(cpu);
2653 unsigned long total = weighted_cpuload(cpu);
2654
2655 if (type == 0 || !sched_feat(LB_BIAS))
2656 return total;
2657
2658 return max(rq->cpu_load[type-1], total);
2659}
2660
2661static unsigned long power_of(int cpu)
2662{
2663 return cpu_rq(cpu)->cpu_power;
2664}
2665
2666static unsigned long cpu_avg_load_per_task(int cpu)
2667{
2668 struct rq *rq = cpu_rq(cpu);
2669 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2670
2671 if (nr_running)
2672 return rq->load.weight / nr_running;
2673
2674 return 0;
2675}
2676
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002677
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002678static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002679{
2680 struct sched_entity *se = &p->se;
2681 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002682 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002683
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002684#ifndef CONFIG_64BIT
2685 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002686
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002687 do {
2688 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2689 smp_rmb();
2690 min_vruntime = cfs_rq->min_vruntime;
2691 } while (min_vruntime != min_vruntime_copy);
2692#else
2693 min_vruntime = cfs_rq->min_vruntime;
2694#endif
2695
2696 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002697}
2698
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002699#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002700/*
2701 * effective_load() calculates the load change as seen from the root_task_group
2702 *
2703 * Adding load to a group doesn't make a group heavier, but can cause movement
2704 * of group shares between cpus. Assuming the shares were perfectly aligned one
2705 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002706 *
2707 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2708 * on this @cpu and results in a total addition (subtraction) of @wg to the
2709 * total group weight.
2710 *
2711 * Given a runqueue weight distribution (rw_i) we can compute a shares
2712 * distribution (s_i) using:
2713 *
2714 * s_i = rw_i / \Sum rw_j (1)
2715 *
2716 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2717 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2718 * shares distribution (s_i):
2719 *
2720 * rw_i = { 2, 4, 1, 0 }
2721 * s_i = { 2/7, 4/7, 1/7, 0 }
2722 *
2723 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2724 * task used to run on and the CPU the waker is running on), we need to
2725 * compute the effect of waking a task on either CPU and, in case of a sync
2726 * wakeup, compute the effect of the current task going to sleep.
2727 *
2728 * So for a change of @wl to the local @cpu with an overall group weight change
2729 * of @wl we can compute the new shares distribution (s'_i) using:
2730 *
2731 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2732 *
2733 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2734 * differences in waking a task to CPU 0. The additional task changes the
2735 * weight and shares distributions like:
2736 *
2737 * rw'_i = { 3, 4, 1, 0 }
2738 * s'_i = { 3/8, 4/8, 1/8, 0 }
2739 *
2740 * We can then compute the difference in effective weight by using:
2741 *
2742 * dw_i = S * (s'_i - s_i) (3)
2743 *
2744 * Where 'S' is the group weight as seen by its parent.
2745 *
2746 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2747 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2748 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002749 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002750static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002751{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002752 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002753
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002754 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002755 return wl;
2756
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002757 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002758 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002759
Paul Turner977dda72011-01-14 17:57:50 -08002760 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002761
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002762 /*
2763 * W = @wg + \Sum rw_j
2764 */
2765 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002766
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002767 /*
2768 * w = rw_i + @wl
2769 */
2770 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002771
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002772 /*
2773 * wl = S * s'_i; see (2)
2774 */
2775 if (W > 0 && w < W)
2776 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002777 else
2778 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002779
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002780 /*
2781 * Per the above, wl is the new se->load.weight value; since
2782 * those are clipped to [MIN_SHARES, ...) do so now. See
2783 * calc_cfs_shares().
2784 */
Paul Turner977dda72011-01-14 17:57:50 -08002785 if (wl < MIN_SHARES)
2786 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002787
2788 /*
2789 * wl = dw_i = S * (s'_i - s_i); see (3)
2790 */
Paul Turner977dda72011-01-14 17:57:50 -08002791 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002792
2793 /*
2794 * Recursively apply this logic to all parent groups to compute
2795 * the final effective load change on the root group. Since
2796 * only the @tg group gets extra weight, all parent groups can
2797 * only redistribute existing shares. @wl is the shift in shares
2798 * resulting from this level per the above.
2799 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002800 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002801 }
2802
2803 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002804}
2805#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002806
Peter Zijlstra83378262008-06-27 13:41:37 +02002807static inline unsigned long effective_load(struct task_group *tg, int cpu,
2808 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002809{
Peter Zijlstra83378262008-06-27 13:41:37 +02002810 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002811}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002812
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002813#endif
2814
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002815static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002816{
Paul Turnere37b6a72011-01-21 20:44:59 -08002817 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002818 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002819 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002820 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002821 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002822 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002823
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002824 idx = sd->wake_idx;
2825 this_cpu = smp_processor_id();
2826 prev_cpu = task_cpu(p);
2827 load = source_load(prev_cpu, idx);
2828 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002829
2830 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002831 * If sync wakeup then subtract the (maximum possible)
2832 * effect of the currently running task from the load
2833 * of the current CPU:
2834 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002835 if (sync) {
2836 tg = task_group(current);
2837 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002838
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002839 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002840 load += effective_load(tg, prev_cpu, 0, -weight);
2841 }
2842
2843 tg = task_group(p);
2844 weight = p->se.load.weight;
2845
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002846 /*
2847 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002848 * due to the sync cause above having dropped this_load to 0, we'll
2849 * always have an imbalance, but there's really nothing you can do
2850 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002851 *
2852 * Otherwise check if either cpus are near enough in load to allow this
2853 * task to be woken on this_cpu.
2854 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002855 if (this_load > 0) {
2856 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002857
2858 this_eff_load = 100;
2859 this_eff_load *= power_of(prev_cpu);
2860 this_eff_load *= this_load +
2861 effective_load(tg, this_cpu, weight, weight);
2862
2863 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2864 prev_eff_load *= power_of(this_cpu);
2865 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2866
2867 balanced = this_eff_load <= prev_eff_load;
2868 } else
2869 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002870
2871 /*
2872 * If the currently running task will sleep within
2873 * a reasonable amount of time then attract this newly
2874 * woken task:
2875 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002876 if (sync && balanced)
2877 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002878
Lucas De Marchi41acab82010-03-10 23:37:45 -03002879 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002880 tl_per_task = cpu_avg_load_per_task(this_cpu);
2881
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002882 if (balanced ||
2883 (this_load <= load &&
2884 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002885 /*
2886 * This domain has SD_WAKE_AFFINE and
2887 * p is cache cold in this domain, and
2888 * there is no bad imbalance.
2889 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002890 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002891 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002892
2893 return 1;
2894 }
2895 return 0;
2896}
2897
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002898/*
2899 * find_idlest_group finds and returns the least busy CPU group within the
2900 * domain.
2901 */
2902static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002903find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002904 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002905{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002906 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002907 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002908 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002909
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002910 do {
2911 unsigned long load, avg_load;
2912 int local_group;
2913 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002914
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002915 /* Skip over this group if it has no CPUs allowed */
2916 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002917 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002918 continue;
2919
2920 local_group = cpumask_test_cpu(this_cpu,
2921 sched_group_cpus(group));
2922
2923 /* Tally up the load of all CPUs in the group */
2924 avg_load = 0;
2925
2926 for_each_cpu(i, sched_group_cpus(group)) {
2927 /* Bias balancing toward cpus of our domain */
2928 if (local_group)
2929 load = source_load(i, load_idx);
2930 else
2931 load = target_load(i, load_idx);
2932
2933 avg_load += load;
2934 }
2935
2936 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002937 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002938
2939 if (local_group) {
2940 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002941 } else if (avg_load < min_load) {
2942 min_load = avg_load;
2943 idlest = group;
2944 }
2945 } while (group = group->next, group != sd->groups);
2946
2947 if (!idlest || 100*this_load < imbalance*min_load)
2948 return NULL;
2949 return idlest;
2950}
2951
2952/*
2953 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2954 */
2955static int
2956find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2957{
2958 unsigned long load, min_load = ULONG_MAX;
2959 int idlest = -1;
2960 int i;
2961
2962 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002963 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002964 load = weighted_cpuload(i);
2965
2966 if (load < min_load || (load == min_load && i == this_cpu)) {
2967 min_load = load;
2968 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002969 }
2970 }
2971
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002972 return idlest;
2973}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002974
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002975/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002976 * Try and locate an idle CPU in the sched_domain.
2977 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002978static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002979{
2980 int cpu = smp_processor_id();
2981 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002982 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07002983 struct sched_group *sg;
2984 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002985
2986 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002987 * If the task is going to be woken-up on this cpu and if it is
2988 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002989 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002990 if (target == cpu && idle_cpu(cpu))
2991 return cpu;
2992
2993 /*
2994 * If the task is going to be woken-up on the cpu where it previously
2995 * ran and if it is currently idle, then it the right target.
2996 */
2997 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002998 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002999
3000 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07003001 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003002 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01003003 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08003004 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07003005 sg = sd->groups;
3006 do {
3007 if (!cpumask_intersects(sched_group_cpus(sg),
3008 tsk_cpus_allowed(p)))
3009 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02003010
Linus Torvalds37407ea2012-09-16 12:29:43 -07003011 for_each_cpu(i, sched_group_cpus(sg)) {
3012 if (!idle_cpu(i))
3013 goto next;
3014 }
3015
3016 target = cpumask_first_and(sched_group_cpus(sg),
3017 tsk_cpus_allowed(p));
3018 goto done;
3019next:
3020 sg = sg->next;
3021 } while (sg != sd->groups);
3022 }
3023done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01003024 return target;
3025}
3026
3027/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003028 * sched_balance_self: balance the current task (running on cpu) in domains
3029 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
3030 * SD_BALANCE_EXEC.
3031 *
3032 * Balance, ie. select the least loaded group.
3033 *
3034 * Returns the target CPU number, or the same CPU if no balancing is needed.
3035 *
3036 * preempt must be disabled.
3037 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01003038static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003039select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003040{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003041 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003042 int cpu = smp_processor_id();
3043 int prev_cpu = task_cpu(p);
3044 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003045 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003046 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003047
Peter Zijlstra29baa742012-04-23 12:11:21 +02003048 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01003049 return prev_cpu;
3050
Peter Zijlstra0763a662009-09-14 19:37:39 +02003051 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02003052 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003053 want_affine = 1;
3054 new_cpu = prev_cpu;
3055 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01003056
Peter Zijlstradce840a2011-04-07 14:09:50 +02003057 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003058 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01003059 if (!(tmp->flags & SD_LOAD_BALANCE))
3060 continue;
3061
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003062 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003063 * If both cpu and prev_cpu are part of this domain,
3064 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01003065 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07003066 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
3067 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
3068 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08003069 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003070 }
3071
Alex Shif03542a2012-07-26 08:55:34 +08003072 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02003073 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003074 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003075
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003076 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08003077 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02003078 prev_cpu = cpu;
3079
3080 new_cpu = select_idle_sibling(p, prev_cpu);
3081 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01003082 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02003083
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003084 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003085 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003086 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003087 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003088
Peter Zijlstra0763a662009-09-14 19:37:39 +02003089 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003090 sd = sd->child;
3091 continue;
3092 }
3093
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02003094 if (sd_flag & SD_BALANCE_WAKE)
3095 load_idx = sd->wake_idx;
3096
3097 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003098 if (!group) {
3099 sd = sd->child;
3100 continue;
3101 }
3102
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02003103 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003104 if (new_cpu == -1 || new_cpu == cpu) {
3105 /* Now try balancing at a lower domain level of cpu */
3106 sd = sd->child;
3107 continue;
3108 }
3109
3110 /* Now try balancing at a lower domain level of new_cpu */
3111 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003112 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003113 sd = NULL;
3114 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003115 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003116 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02003117 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02003118 sd = tmp;
3119 }
3120 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01003121 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003122unlock:
3123 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01003124
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003125 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01003126}
Paul Turner0a74bef2012-10-04 13:18:30 +02003127
3128/*
3129 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
3130 * cfs_rq_of(p) references at time of call are still valid and identify the
3131 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
3132 * other assumptions, including the state of rq->lock, should be made.
3133 */
3134static void
3135migrate_task_rq_fair(struct task_struct *p, int next_cpu)
3136{
Paul Turneraff3e492012-10-04 13:18:30 +02003137 struct sched_entity *se = &p->se;
3138 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3139
3140 /*
3141 * Load tracking: accumulate removed load so that it can be processed
3142 * when we next update owning cfs_rq under rq->lock. Tasks contribute
3143 * to blocked load iff they have a positive decay-count. It can never
3144 * be negative here since on-rq tasks have decay-count == 0.
3145 */
3146 if (se->avg.decay_count) {
3147 se->avg.decay_count = -__synchronize_entity_decay(se);
3148 atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
3149 }
Paul Turner0a74bef2012-10-04 13:18:30 +02003150}
Gregory Haskinse7693a32008-01-25 21:08:09 +01003151#endif /* CONFIG_SMP */
3152
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003153static unsigned long
3154wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003155{
3156 unsigned long gran = sysctl_sched_wakeup_granularity;
3157
3158 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003159 * Since its curr running now, convert the gran from real-time
3160 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003161 *
3162 * By using 'se' instead of 'curr' we penalize light tasks, so
3163 * they get preempted easier. That is, if 'se' < 'curr' then
3164 * the resulting gran will be larger, therefore penalizing the
3165 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3166 * be smaller, again penalizing the lighter task.
3167 *
3168 * This is especially important for buddies when the leftmost
3169 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003170 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003171 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003172}
3173
3174/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003175 * Should 'se' preempt 'curr'.
3176 *
3177 * |s1
3178 * |s2
3179 * |s3
3180 * g
3181 * |<--->|c
3182 *
3183 * w(c, s1) = -1
3184 * w(c, s2) = 0
3185 * w(c, s3) = 1
3186 *
3187 */
3188static int
3189wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3190{
3191 s64 gran, vdiff = curr->vruntime - se->vruntime;
3192
3193 if (vdiff <= 0)
3194 return -1;
3195
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003196 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003197 if (vdiff > gran)
3198 return 1;
3199
3200 return 0;
3201}
3202
Peter Zijlstra02479092008-11-04 21:25:10 +01003203static void set_last_buddy(struct sched_entity *se)
3204{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003205 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3206 return;
3207
3208 for_each_sched_entity(se)
3209 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003210}
3211
3212static void set_next_buddy(struct sched_entity *se)
3213{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003214 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3215 return;
3216
3217 for_each_sched_entity(se)
3218 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003219}
3220
Rik van Rielac53db52011-02-01 09:51:03 -05003221static void set_skip_buddy(struct sched_entity *se)
3222{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003223 for_each_sched_entity(se)
3224 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003225}
3226
Peter Zijlstra464b7522008-10-24 11:06:15 +02003227/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003228 * Preempt the current task with a newly woken task if needed:
3229 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003230static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003231{
3232 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003233 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003234 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003235 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003236 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003237
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003238 if (unlikely(se == pse))
3239 return;
3240
Paul Turner5238cdd2011-07-21 09:43:37 -07003241 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003242 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003243 * unconditionally check_prempt_curr() after an enqueue (which may have
3244 * lead to a throttle). This both saves work and prevents false
3245 * next-buddy nomination below.
3246 */
3247 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3248 return;
3249
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003250 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003251 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003252 next_buddy_marked = 1;
3253 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003254
Bharata B Raoaec0a512008-08-28 14:42:49 +05303255 /*
3256 * We can come here with TIF_NEED_RESCHED already set from new task
3257 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003258 *
3259 * Note: this also catches the edge-case of curr being in a throttled
3260 * group (e.g. via set_curr_task), since update_curr() (in the
3261 * enqueue of curr) will have resulted in resched being set. This
3262 * prevents us from potentially nominating it as a false LAST_BUDDY
3263 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303264 */
3265 if (test_tsk_need_resched(curr))
3266 return;
3267
Darren Harta2f5c9a2011-02-22 13:04:33 -08003268 /* Idle tasks are by definition preempted by non-idle tasks. */
3269 if (unlikely(curr->policy == SCHED_IDLE) &&
3270 likely(p->policy != SCHED_IDLE))
3271 goto preempt;
3272
Ingo Molnar91c234b2007-10-15 17:00:18 +02003273 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003274 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3275 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003276 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003277 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003278 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003279
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003280 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003281 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003282 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003283 if (wakeup_preempt_entity(se, pse) == 1) {
3284 /*
3285 * Bias pick_next to pick the sched entity that is
3286 * triggering this preemption.
3287 */
3288 if (!next_buddy_marked)
3289 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003290 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003291 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003292
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003293 return;
3294
3295preempt:
3296 resched_task(curr);
3297 /*
3298 * Only set the backward buddy when the current task is still
3299 * on the rq. This can happen when a wakeup gets interleaved
3300 * with schedule on the ->pre_schedule() or idle_balance()
3301 * point, either of which can * drop the rq lock.
3302 *
3303 * Also, during early boot the idle thread is in the fair class,
3304 * for obvious reasons its a bad idea to schedule back to it.
3305 */
3306 if (unlikely(!se->on_rq || curr == rq->idle))
3307 return;
3308
3309 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3310 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003311}
3312
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003313static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003314{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003315 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003316 struct cfs_rq *cfs_rq = &rq->cfs;
3317 struct sched_entity *se;
3318
Tim Blechmann36ace272009-11-24 11:55:45 +01003319 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003320 return NULL;
3321
3322 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003323 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003324 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003325 cfs_rq = group_cfs_rq(se);
3326 } while (cfs_rq);
3327
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003328 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003329 if (hrtick_enabled(rq))
3330 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003331
3332 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003333}
3334
3335/*
3336 * Account for a descheduled task:
3337 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003338static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003339{
3340 struct sched_entity *se = &prev->se;
3341 struct cfs_rq *cfs_rq;
3342
3343 for_each_sched_entity(se) {
3344 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003345 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003346 }
3347}
3348
Rik van Rielac53db52011-02-01 09:51:03 -05003349/*
3350 * sched_yield() is very simple
3351 *
3352 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3353 */
3354static void yield_task_fair(struct rq *rq)
3355{
3356 struct task_struct *curr = rq->curr;
3357 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3358 struct sched_entity *se = &curr->se;
3359
3360 /*
3361 * Are we the only task in the tree?
3362 */
3363 if (unlikely(rq->nr_running == 1))
3364 return;
3365
3366 clear_buddies(cfs_rq, se);
3367
3368 if (curr->policy != SCHED_BATCH) {
3369 update_rq_clock(rq);
3370 /*
3371 * Update run-time statistics of the 'current'.
3372 */
3373 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003374 /*
3375 * Tell update_rq_clock() that we've just updated,
3376 * so we don't do microscopic update in schedule()
3377 * and double the fastpath cost.
3378 */
3379 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003380 }
3381
3382 set_skip_buddy(se);
3383}
3384
Mike Galbraithd95f4122011-02-01 09:50:51 -05003385static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3386{
3387 struct sched_entity *se = &p->se;
3388
Paul Turner5238cdd2011-07-21 09:43:37 -07003389 /* throttled hierarchies are not runnable */
3390 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003391 return false;
3392
3393 /* Tell the scheduler that we'd really like pse to run next. */
3394 set_next_buddy(se);
3395
Mike Galbraithd95f4122011-02-01 09:50:51 -05003396 yield_task_fair(rq);
3397
3398 return true;
3399}
3400
Peter Williams681f3e62007-10-24 18:23:51 +02003401#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003402/**************************************************
3403 * Fair scheduling class load-balancing methods:
3404 */
3405
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003406static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3407
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003408#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003409#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303410#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003411
3412struct lb_env {
3413 struct sched_domain *sd;
3414
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003415 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303416 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003417
3418 int dst_cpu;
3419 struct rq *dst_rq;
3420
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303421 struct cpumask *dst_grpmask;
3422 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003423 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003424 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08003425 /* The set of CPUs under consideration for load-balancing */
3426 struct cpumask *cpus;
3427
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003428 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003429
3430 unsigned int loop;
3431 unsigned int loop_break;
3432 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003433};
3434
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003435/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003436 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003437 * Both runqueues must be locked.
3438 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003439static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003440{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003441 deactivate_task(env->src_rq, p, 0);
3442 set_task_cpu(p, env->dst_cpu);
3443 activate_task(env->dst_rq, p, 0);
3444 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003445}
3446
3447/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003448 * Is this task likely cache-hot:
3449 */
3450static int
3451task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3452{
3453 s64 delta;
3454
3455 if (p->sched_class != &fair_sched_class)
3456 return 0;
3457
3458 if (unlikely(p->policy == SCHED_IDLE))
3459 return 0;
3460
3461 /*
3462 * Buddy candidates are cache hot:
3463 */
3464 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3465 (&p->se == cfs_rq_of(&p->se)->next ||
3466 &p->se == cfs_rq_of(&p->se)->last))
3467 return 1;
3468
3469 if (sysctl_sched_migration_cost == -1)
3470 return 1;
3471 if (sysctl_sched_migration_cost == 0)
3472 return 0;
3473
3474 delta = now - p->se.exec_start;
3475
3476 return delta < (s64)sysctl_sched_migration_cost;
3477}
3478
3479/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003480 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3481 */
3482static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003483int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003484{
3485 int tsk_cache_hot = 0;
3486 /*
3487 * We do not migrate tasks that are:
3488 * 1) running (obviously), or
3489 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3490 * 3) are cache-hot on their current CPU.
3491 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003492 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303493 int new_dst_cpu;
3494
Lucas De Marchi41acab82010-03-10 23:37:45 -03003495 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303496
3497 /*
3498 * Remember if this task can be migrated to any other cpu in
3499 * our sched_group. We may want to revisit it if we couldn't
3500 * meet load balance goals by pulling other tasks on src_cpu.
3501 *
3502 * Also avoid computing new_dst_cpu if we have already computed
3503 * one in current iteration.
3504 */
3505 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3506 return 0;
3507
3508 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3509 tsk_cpus_allowed(p));
3510 if (new_dst_cpu < nr_cpu_ids) {
3511 env->flags |= LBF_SOME_PINNED;
3512 env->new_dst_cpu = new_dst_cpu;
3513 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003514 return 0;
3515 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303516
3517 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003518 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003519
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003520 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003521 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003522 return 0;
3523 }
3524
3525 /*
3526 * Aggressive migration if:
3527 * 1) task is cache cold, or
3528 * 2) too many balance attempts have failed.
3529 */
3530
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003531 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003532 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003533 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003534#ifdef CONFIG_SCHEDSTATS
3535 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003536 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003537 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003538 }
3539#endif
3540 return 1;
3541 }
3542
3543 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003544 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003545 return 0;
3546 }
3547 return 1;
3548}
3549
Peter Zijlstra897c3952009-12-17 17:45:42 +01003550/*
3551 * move_one_task tries to move exactly one task from busiest to this_rq, as
3552 * part of active balancing operations within "domain".
3553 * Returns 1 if successful and 0 otherwise.
3554 *
3555 * Called with both runqueues locked.
3556 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003557static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003558{
3559 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003560
Peter Zijlstra367456c2012-02-20 21:49:09 +01003561 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3562 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3563 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003564
Peter Zijlstra367456c2012-02-20 21:49:09 +01003565 if (!can_migrate_task(p, env))
3566 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003567
Peter Zijlstra367456c2012-02-20 21:49:09 +01003568 move_task(p, env);
3569 /*
3570 * Right now, this is only the second place move_task()
3571 * is called, so we can safely collect move_task()
3572 * stats here rather than inside move_task().
3573 */
3574 schedstat_inc(env->sd, lb_gained[env->idle]);
3575 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003576 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003577 return 0;
3578}
3579
Peter Zijlstra367456c2012-02-20 21:49:09 +01003580static unsigned long task_h_load(struct task_struct *p);
3581
Peter Zijlstraeb953082012-04-17 13:38:40 +02003582static const unsigned int sched_nr_migrate_break = 32;
3583
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003584/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003585 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003586 * this_rq, as part of a balancing operation within domain "sd".
3587 * Returns 1 if successful and 0 otherwise.
3588 *
3589 * Called with both runqueues locked.
3590 */
3591static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003592{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003593 struct list_head *tasks = &env->src_rq->cfs_tasks;
3594 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003595 unsigned long load;
3596 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003597
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003598 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003599 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003600
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003601 while (!list_empty(tasks)) {
3602 p = list_first_entry(tasks, struct task_struct, se.group_node);
3603
Peter Zijlstra367456c2012-02-20 21:49:09 +01003604 env->loop++;
3605 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003606 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003607 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003608
3609 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003610 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003611 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003612 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003613 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003614 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003615
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003616 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003617 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003618
Peter Zijlstra367456c2012-02-20 21:49:09 +01003619 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003620
Peter Zijlstraeb953082012-04-17 13:38:40 +02003621 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003622 goto next;
3623
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003624 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003625 goto next;
3626
3627 if (!can_migrate_task(p, env))
3628 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003629
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003630 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003631 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003632 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003633
3634#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003635 /*
3636 * NEWIDLE balancing is a source of latency, so preemptible
3637 * kernels will stop after the first task is pulled to minimize
3638 * the critical section.
3639 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003640 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003641 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003642#endif
3643
Peter Zijlstraee00e662009-12-17 17:25:20 +01003644 /*
3645 * We only want to steal up to the prescribed amount of
3646 * weighted load.
3647 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003648 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003649 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003650
Peter Zijlstra367456c2012-02-20 21:49:09 +01003651 continue;
3652next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003653 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003654 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003655
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003656 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003657 * Right now, this is one of only two places move_task() is called,
3658 * so we can safely collect move_task() stats here rather than
3659 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003660 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003661 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003662
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003663 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003664}
3665
Peter Zijlstra230059de2009-12-17 17:47:12 +01003666#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003667/*
3668 * update tg->load_weight by folding this cpu's load_avg
3669 */
Paul Turner67e86252010-11-15 15:47:05 -08003670static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003671{
3672 struct cfs_rq *cfs_rq;
3673 unsigned long flags;
3674 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003675
3676 if (!tg->se[cpu])
3677 return 0;
3678
3679 rq = cpu_rq(cpu);
3680 cfs_rq = tg->cfs_rq[cpu];
3681
3682 raw_spin_lock_irqsave(&rq->lock, flags);
3683
3684 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003685 update_cfs_load(cfs_rq, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02003686 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003687
3688 /*
3689 * We need to update shares after updating tg->load_weight in
3690 * order to adjust the weight of groups with long running tasks.
3691 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003692 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003693
3694 raw_spin_unlock_irqrestore(&rq->lock, flags);
3695
3696 return 0;
3697}
3698
3699static void update_shares(int cpu)
3700{
3701 struct cfs_rq *cfs_rq;
3702 struct rq *rq = cpu_rq(cpu);
3703
3704 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003705 /*
3706 * Iterates the task_group tree in a bottom up fashion, see
3707 * list_add_leaf_cfs_rq() for details.
3708 */
Paul Turner64660c82011-07-21 09:43:36 -07003709 for_each_leaf_cfs_rq(rq, cfs_rq) {
3710 /* throttled entities do not contribute to load */
3711 if (throttled_hierarchy(cfs_rq))
3712 continue;
3713
Paul Turner67e86252010-11-15 15:47:05 -08003714 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003715 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003716 rcu_read_unlock();
3717}
3718
Peter Zijlstra9763b672011-07-13 13:09:25 +02003719/*
3720 * Compute the cpu's hierarchical load factor for each task group.
3721 * This needs to be done in a top-down fashion because the load of a child
3722 * group is a fraction of its parents load.
3723 */
3724static int tg_load_down(struct task_group *tg, void *data)
3725{
3726 unsigned long load;
3727 long cpu = (long)data;
3728
3729 if (!tg->parent) {
3730 load = cpu_rq(cpu)->load.weight;
3731 } else {
3732 load = tg->parent->cfs_rq[cpu]->h_load;
3733 load *= tg->se[cpu]->load.weight;
3734 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3735 }
3736
3737 tg->cfs_rq[cpu]->h_load = load;
3738
3739 return 0;
3740}
3741
3742static void update_h_load(long cpu)
3743{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003744 struct rq *rq = cpu_rq(cpu);
3745 unsigned long now = jiffies;
3746
3747 if (rq->h_load_throttle == now)
3748 return;
3749
3750 rq->h_load_throttle = now;
3751
Peter Zijlstra367456c2012-02-20 21:49:09 +01003752 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003753 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003754 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003755}
3756
Peter Zijlstra367456c2012-02-20 21:49:09 +01003757static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003758{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003759 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3760 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003761
Peter Zijlstra367456c2012-02-20 21:49:09 +01003762 load = p->se.load.weight;
3763 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003764
Peter Zijlstra367456c2012-02-20 21:49:09 +01003765 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003766}
3767#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003768static inline void update_shares(int cpu)
3769{
3770}
3771
Peter Zijlstra367456c2012-02-20 21:49:09 +01003772static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003773{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003774}
3775
3776static unsigned long task_h_load(struct task_struct *p)
3777{
3778 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003779}
3780#endif
3781
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003782/********** Helpers for find_busiest_group ************************/
3783/*
3784 * sd_lb_stats - Structure to store the statistics of a sched_domain
3785 * during load balancing.
3786 */
3787struct sd_lb_stats {
3788 struct sched_group *busiest; /* Busiest group in this sd */
3789 struct sched_group *this; /* Local group in this sd */
3790 unsigned long total_load; /* Total load of all groups in sd */
3791 unsigned long total_pwr; /* Total power of all groups in sd */
3792 unsigned long avg_load; /* Average load across all groups in sd */
3793
3794 /** Statistics of this group */
3795 unsigned long this_load;
3796 unsigned long this_load_per_task;
3797 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003798 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003799 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003800
3801 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003802 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003803 unsigned long max_load;
3804 unsigned long busiest_load_per_task;
3805 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003806 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003807 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003808 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003809
3810 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003811};
3812
3813/*
3814 * sg_lb_stats - stats of a sched_group required for load_balancing
3815 */
3816struct sg_lb_stats {
3817 unsigned long avg_load; /*Avg load across the CPUs of the group */
3818 unsigned long group_load; /* Total load over the CPUs of the group */
3819 unsigned long sum_nr_running; /* Nr tasks running in the group */
3820 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3821 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003822 unsigned long idle_cpus;
3823 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003824 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003825 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003826};
3827
3828/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003829 * get_sd_load_idx - Obtain the load index for a given sched domain.
3830 * @sd: The sched_domain whose load_idx is to be obtained.
3831 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3832 */
3833static inline int get_sd_load_idx(struct sched_domain *sd,
3834 enum cpu_idle_type idle)
3835{
3836 int load_idx;
3837
3838 switch (idle) {
3839 case CPU_NOT_IDLE:
3840 load_idx = sd->busy_idx;
3841 break;
3842
3843 case CPU_NEWLY_IDLE:
3844 load_idx = sd->newidle_idx;
3845 break;
3846 default:
3847 load_idx = sd->idle_idx;
3848 break;
3849 }
3850
3851 return load_idx;
3852}
3853
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003854unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3855{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003856 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003857}
3858
3859unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3860{
3861 return default_scale_freq_power(sd, cpu);
3862}
3863
3864unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3865{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003866 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003867 unsigned long smt_gain = sd->smt_gain;
3868
3869 smt_gain /= weight;
3870
3871 return smt_gain;
3872}
3873
3874unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3875{
3876 return default_scale_smt_power(sd, cpu);
3877}
3878
3879unsigned long scale_rt_power(int cpu)
3880{
3881 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003882 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003883
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003884 /*
3885 * Since we're reading these variables without serialization make sure
3886 * we read them once before doing sanity checks on them.
3887 */
3888 age_stamp = ACCESS_ONCE(rq->age_stamp);
3889 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003890
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003891 total = sched_avg_period() + (rq->clock - age_stamp);
3892
3893 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003894 /* Ensures that power won't end up being negative */
3895 available = 0;
3896 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003897 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003898 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003899
Nikhil Rao1399fa72011-05-18 10:09:39 -07003900 if (unlikely((s64)total < SCHED_POWER_SCALE))
3901 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003902
Nikhil Rao1399fa72011-05-18 10:09:39 -07003903 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003904
3905 return div_u64(available, total);
3906}
3907
3908static void update_cpu_power(struct sched_domain *sd, int cpu)
3909{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003910 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003911 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003912 struct sched_group *sdg = sd->groups;
3913
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003914 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3915 if (sched_feat(ARCH_POWER))
3916 power *= arch_scale_smt_power(sd, cpu);
3917 else
3918 power *= default_scale_smt_power(sd, cpu);
3919
Nikhil Rao1399fa72011-05-18 10:09:39 -07003920 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003921 }
3922
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003923 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003924
3925 if (sched_feat(ARCH_POWER))
3926 power *= arch_scale_freq_power(sd, cpu);
3927 else
3928 power *= default_scale_freq_power(sd, cpu);
3929
Nikhil Rao1399fa72011-05-18 10:09:39 -07003930 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003931
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003932 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003933 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003934
3935 if (!power)
3936 power = 1;
3937
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003938 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003939 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940}
3941
Peter Zijlstra029632f2011-10-25 10:00:11 +02003942void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943{
3944 struct sched_domain *child = sd->child;
3945 struct sched_group *group, *sdg = sd->groups;
3946 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003947 unsigned long interval;
3948
3949 interval = msecs_to_jiffies(sd->balance_interval);
3950 interval = clamp(interval, 1UL, max_load_balance_interval);
3951 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003952
3953 if (!child) {
3954 update_cpu_power(sd, cpu);
3955 return;
3956 }
3957
3958 power = 0;
3959
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003960 if (child->flags & SD_OVERLAP) {
3961 /*
3962 * SD_OVERLAP domains cannot assume that child groups
3963 * span the current group.
3964 */
3965
3966 for_each_cpu(cpu, sched_group_cpus(sdg))
3967 power += power_of(cpu);
3968 } else {
3969 /*
3970 * !SD_OVERLAP domains can assume that child groups
3971 * span the current group.
3972 */
3973
3974 group = child->groups;
3975 do {
3976 power += group->sgp->power;
3977 group = group->next;
3978 } while (group != child->groups);
3979 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003980
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003981 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003982}
3983
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003984/*
3985 * Try and fix up capacity for tiny siblings, this is needed when
3986 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3987 * which on its own isn't powerful enough.
3988 *
3989 * See update_sd_pick_busiest() and check_asym_packing().
3990 */
3991static inline int
3992fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3993{
3994 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003995 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003996 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003997 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003998 return 0;
3999
4000 /*
4001 * If ~90% of the cpu_power is still there, we're good.
4002 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004003 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004004 return 1;
4005
4006 return 0;
4007}
4008
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004009/**
4010 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004011 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004012 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004013 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004014 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004015 * @balance: Should we balance.
4016 * @sgs: variable to hold the statistics for this group.
4017 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004018static inline void update_sg_lb_stats(struct lb_env *env,
4019 struct sched_group *group, int load_idx,
Michael Wangb94031302012-07-12 16:10:13 +08004020 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004021{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004022 unsigned long nr_running, max_nr_running, min_nr_running;
4023 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004024 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004025 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004026 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004027
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06004028 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02004029 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004030
4031 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004032 max_cpu_load = 0;
4033 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07004034 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004035 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004036
Michael Wangb94031302012-07-12 16:10:13 +08004037 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004038 struct rq *rq = cpu_rq(i);
4039
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004040 nr_running = rq->nr_running;
4041
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004042 /* Bias balancing toward cpus of our domain */
4043 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02004044 if (idle_cpu(i) && !first_idle_cpu &&
4045 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004046 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004047 balance_cpu = i;
4048 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004049
4050 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004051 } else {
4052 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004053 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004054 max_cpu_load = load;
4055 if (min_cpu_load > load)
4056 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004057
4058 if (nr_running > max_nr_running)
4059 max_nr_running = nr_running;
4060 if (min_nr_running > nr_running)
4061 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004062 }
4063
4064 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004065 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004066 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004067 if (idle_cpu(i))
4068 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004069 }
4070
4071 /*
4072 * First idle cpu or the first cpu(busiest) in this sched group
4073 * is eligible for doing load balancing at this and above
4074 * domains. In the newly idle case, we will allow all the cpu's
4075 * to do the newly idle load balance.
4076 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01004077 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004078 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004079 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01004080 *balance = 0;
4081 return;
4082 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004083 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01004084 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004085 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004086 }
4087
4088 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004089 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004090
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004091 /*
4092 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01004093 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094 *
4095 * APZ: with cgroup the avg task weight can vary wildly and
4096 * might not be a suitable number - should we keep a
4097 * normalized nr_running number somewhere that negates
4098 * the hierarchy?
4099 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004100 if (sgs->sum_nr_running)
4101 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004102
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02004103 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
4104 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004105 sgs->group_imb = 1;
4106
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004107 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004108 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004109 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004110 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004111 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07004112
4113 if (sgs->group_capacity > sgs->sum_nr_running)
4114 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004115}
4116
4117/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10004118 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07004119 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004120 * @sds: sched_domain statistics
4121 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10004122 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10004123 *
4124 * Determine if @sg is a busier group than the previously selected
4125 * busiest group.
4126 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004127static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004128 struct sd_lb_stats *sds,
4129 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004130 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004131{
4132 if (sgs->avg_load <= sds->max_load)
4133 return false;
4134
4135 if (sgs->sum_nr_running > sgs->group_capacity)
4136 return true;
4137
4138 if (sgs->group_imb)
4139 return true;
4140
4141 /*
4142 * ASYM_PACKING needs to move all the work to the lowest
4143 * numbered CPUs in the group, therefore mark all groups
4144 * higher than ourself as busy.
4145 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004146 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
4147 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004148 if (!sds->busiest)
4149 return true;
4150
4151 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
4152 return true;
4153 }
4154
4155 return false;
4156}
4157
4158/**
Hui Kang461819a2011-10-11 23:00:59 -04004159 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004160 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004161 * @balance: Should we balance.
4162 * @sds: variable to hold the statistics for this sched_domain.
4163 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004164static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004165 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004166{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004167 struct sched_domain *child = env->sd->child;
4168 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004169 struct sg_lb_stats sgs;
4170 int load_idx, prefer_sibling = 0;
4171
4172 if (child && child->flags & SD_PREFER_SIBLING)
4173 prefer_sibling = 1;
4174
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004175 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004176
4177 do {
4178 int local_group;
4179
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004180 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004181 memset(&sgs, 0, sizeof(sgs));
Michael Wangb94031302012-07-12 16:10:13 +08004182 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004183
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004184 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004185 return;
4186
4187 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004188 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004189
4190 /*
4191 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004192 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004193 * and move all the excess tasks away. We lower the capacity
4194 * of a group only if the local group has the capacity to fit
4195 * these excess tasks, i.e. nr_running < group_capacity. The
4196 * extra check prevents the case where you always pull from the
4197 * heaviest group when it is already under-utilized (possible
4198 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004199 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004200 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004201 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4202
4203 if (local_group) {
4204 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004205 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004206 sds->this_nr_running = sgs.sum_nr_running;
4207 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004208 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004209 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004210 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004211 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004212 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004213 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004214 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004215 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004216 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004217 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004218 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004219 sds->group_imb = sgs.group_imb;
4220 }
4221
Michael Neuling532cb4c2010-06-08 14:57:02 +10004222 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004223 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004224}
4225
Michael Neuling532cb4c2010-06-08 14:57:02 +10004226/**
4227 * check_asym_packing - Check to see if the group is packed into the
4228 * sched doman.
4229 *
4230 * This is primarily intended to used at the sibling level. Some
4231 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4232 * case of POWER7, it can move to lower SMT modes only when higher
4233 * threads are idle. When in lower SMT modes, the threads will
4234 * perform better since they share less core resources. Hence when we
4235 * have idle threads, we want them to be the higher ones.
4236 *
4237 * This packing function is run on idle threads. It checks to see if
4238 * the busiest CPU in this domain (core in the P7 case) has a higher
4239 * CPU number than the packing function is being run on. Here we are
4240 * assuming lower CPU number will be equivalent to lower a SMT thread
4241 * number.
4242 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004243 * Returns 1 when packing is required and a task should be moved to
4244 * this CPU. The amount of the imbalance is returned in *imbalance.
4245 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004246 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004247 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004248 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004249static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004250{
4251 int busiest_cpu;
4252
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004253 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004254 return 0;
4255
4256 if (!sds->busiest)
4257 return 0;
4258
4259 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004260 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004261 return 0;
4262
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004263 env->imbalance = DIV_ROUND_CLOSEST(
4264 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4265
Michael Neuling532cb4c2010-06-08 14:57:02 +10004266 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004267}
4268
4269/**
4270 * fix_small_imbalance - Calculate the minor imbalance that exists
4271 * amongst the groups of a sched_domain, during
4272 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004273 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004274 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004275 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004276static inline
4277void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004278{
4279 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4280 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004281 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004282
4283 if (sds->this_nr_running) {
4284 sds->this_load_per_task /= sds->this_nr_running;
4285 if (sds->busiest_load_per_task >
4286 sds->this_load_per_task)
4287 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004288 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004289 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004290 cpu_avg_load_per_task(env->dst_cpu);
4291 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004292
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004293 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004294 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004295 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004296
4297 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4298 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004299 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004300 return;
4301 }
4302
4303 /*
4304 * OK, we don't have enough imbalance to justify moving tasks,
4305 * however we may be able to increase total CPU power used by
4306 * moving them.
4307 */
4308
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004309 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004310 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004311 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004312 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004313 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004314
4315 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004316 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004317 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004318 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004319 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004320 min(sds->busiest_load_per_task, sds->max_load - tmp);
4321
4322 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004323 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004324 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004325 tmp = (sds->max_load * sds->busiest->sgp->power) /
4326 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004327 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004328 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004329 sds->this->sgp->power;
4330 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004331 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004332 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004333
4334 /* Move if we gain throughput */
4335 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004336 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004337}
4338
4339/**
4340 * calculate_imbalance - Calculate the amount of imbalance present within the
4341 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004342 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004343 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004344 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004345static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004346{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004347 unsigned long max_pull, load_above_capacity = ~0UL;
4348
4349 sds->busiest_load_per_task /= sds->busiest_nr_running;
4350 if (sds->group_imb) {
4351 sds->busiest_load_per_task =
4352 min(sds->busiest_load_per_task, sds->avg_load);
4353 }
4354
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004355 /*
4356 * In the presence of smp nice balancing, certain scenarios can have
4357 * max load less than avg load(as we skip the groups at or below
4358 * its cpu_power, while calculating max_load..)
4359 */
4360 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004361 env->imbalance = 0;
4362 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004363 }
4364
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004365 if (!sds->group_imb) {
4366 /*
4367 * Don't want to pull so many tasks that a group would go idle.
4368 */
4369 load_above_capacity = (sds->busiest_nr_running -
4370 sds->busiest_group_capacity);
4371
Nikhil Rao1399fa72011-05-18 10:09:39 -07004372 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004373
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004374 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004375 }
4376
4377 /*
4378 * We're trying to get all the cpus to the average_load, so we don't
4379 * want to push ourselves above the average load, nor do we wish to
4380 * reduce the max loaded cpu below the average load. At the same time,
4381 * we also don't want to reduce the group load below the group capacity
4382 * (so that we can implement power-savings policies etc). Thus we look
4383 * for the minimum possible imbalance.
4384 * Be careful of negative numbers as they'll appear as very large values
4385 * with unsigned longs.
4386 */
4387 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004388
4389 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004390 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004391 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004392 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004393
4394 /*
4395 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004396 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004397 * a think about bumping its value to force at least one task to be
4398 * moved
4399 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004400 if (env->imbalance < sds->busiest_load_per_task)
4401 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004402
4403}
Nikhil Raofab47622010-10-15 13:12:29 -07004404
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004405/******* find_busiest_group() helpers end here *********************/
4406
4407/**
4408 * find_busiest_group - Returns the busiest group within the sched_domain
4409 * if there is an imbalance. If there isn't an imbalance, and
4410 * the user has opted for power-savings, it returns a group whose
4411 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4412 * such a group exists.
4413 *
4414 * Also calculates the amount of weighted load which should be moved
4415 * to restore balance.
4416 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004417 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004418 * @balance: Pointer to a variable indicating if this_cpu
4419 * is the appropriate cpu to perform load balancing at this_level.
4420 *
4421 * Returns: - the busiest group if imbalance exists.
4422 * - If no imbalance and user has opted for power-savings balance,
4423 * return the least loaded group whose CPUs can be
4424 * put to idle by rebalancing its tasks onto our group.
4425 */
4426static struct sched_group *
Michael Wangb94031302012-07-12 16:10:13 +08004427find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004428{
4429 struct sd_lb_stats sds;
4430
4431 memset(&sds, 0, sizeof(sds));
4432
4433 /*
4434 * Compute the various statistics relavent for load balancing at
4435 * this level.
4436 */
Michael Wangb94031302012-07-12 16:10:13 +08004437 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004438
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004439 /*
4440 * this_cpu is not the appropriate cpu to perform load balancing at
4441 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004442 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004443 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004444 goto ret;
4445
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004446 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4447 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004448 return sds.busiest;
4449
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004450 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004451 if (!sds.busiest || sds.busiest_nr_running == 0)
4452 goto out_balanced;
4453
Nikhil Rao1399fa72011-05-18 10:09:39 -07004454 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004455
Peter Zijlstra866ab432011-02-21 18:56:47 +01004456 /*
4457 * If the busiest group is imbalanced the below checks don't
4458 * work because they assumes all things are equal, which typically
4459 * isn't true due to cpus_allowed constraints and the like.
4460 */
4461 if (sds.group_imb)
4462 goto force_balance;
4463
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004464 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004465 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004466 !sds.busiest_has_capacity)
4467 goto force_balance;
4468
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004469 /*
4470 * If the local group is more busy than the selected busiest group
4471 * don't try and pull any tasks.
4472 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004473 if (sds.this_load >= sds.max_load)
4474 goto out_balanced;
4475
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004476 /*
4477 * Don't pull any tasks if this group is already above the domain
4478 * average load.
4479 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004480 if (sds.this_load >= sds.avg_load)
4481 goto out_balanced;
4482
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004483 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004484 /*
4485 * This cpu is idle. If the busiest group load doesn't
4486 * have more tasks than the number of available cpu's and
4487 * there is no imbalance between this and busiest group
4488 * wrt to idle cpu's, it is balanced.
4489 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004490 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004491 sds.busiest_nr_running <= sds.busiest_group_weight)
4492 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004493 } else {
4494 /*
4495 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4496 * imbalance_pct to be conservative.
4497 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004498 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004499 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004500 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004501
Nikhil Raofab47622010-10-15 13:12:29 -07004502force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004503 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004504 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004505 return sds.busiest;
4506
4507out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004508ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004509 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004510 return NULL;
4511}
4512
4513/*
4514 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4515 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004516static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004517 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004518{
4519 struct rq *busiest = NULL, *rq;
4520 unsigned long max_load = 0;
4521 int i;
4522
4523 for_each_cpu(i, sched_group_cpus(group)) {
4524 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004525 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4526 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004527 unsigned long wl;
4528
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004529 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004530 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004531
Michael Wangb94031302012-07-12 16:10:13 +08004532 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004533 continue;
4534
4535 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004536 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004537
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004538 /*
4539 * When comparing with imbalance, use weighted_cpuload()
4540 * which is not scaled with the cpu power.
4541 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004542 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004543 continue;
4544
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004545 /*
4546 * For the load comparisons with the other cpu's, consider
4547 * the weighted_cpuload() scaled with the cpu power, so that
4548 * the load can be moved away from the cpu that is potentially
4549 * running at a lower capacity.
4550 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004551 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004552
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004553 if (wl > max_load) {
4554 max_load = wl;
4555 busiest = rq;
4556 }
4557 }
4558
4559 return busiest;
4560}
4561
4562/*
4563 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4564 * so long as it is large enough.
4565 */
4566#define MAX_PINNED_INTERVAL 512
4567
4568/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004569DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004570
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004571static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004572{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004573 struct sched_domain *sd = env->sd;
4574
4575 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004576
4577 /*
4578 * ASYM_PACKING needs to force migrate tasks from busy but
4579 * higher numbered CPUs in order to pack all tasks in the
4580 * lowest numbered CPUs.
4581 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004582 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004583 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004584 }
4585
4586 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4587}
4588
Tejun Heo969c7922010-05-06 18:49:21 +02004589static int active_load_balance_cpu_stop(void *data);
4590
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004591/*
4592 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4593 * tasks if there is an imbalance.
4594 */
4595static int load_balance(int this_cpu, struct rq *this_rq,
4596 struct sched_domain *sd, enum cpu_idle_type idle,
4597 int *balance)
4598{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304599 int ld_moved, cur_ld_moved, active_balance = 0;
4600 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004601 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004602 struct rq *busiest;
4603 unsigned long flags;
4604 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4605
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004606 struct lb_env env = {
4607 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004608 .dst_cpu = this_cpu,
4609 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304610 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004611 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004612 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08004613 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004614 };
4615
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004616 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304617 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004618
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004619 schedstat_inc(sd, lb_count[idle]);
4620
4621redo:
Michael Wangb94031302012-07-12 16:10:13 +08004622 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004623
4624 if (*balance == 0)
4625 goto out_balanced;
4626
4627 if (!group) {
4628 schedstat_inc(sd, lb_nobusyg[idle]);
4629 goto out_balanced;
4630 }
4631
Michael Wangb94031302012-07-12 16:10:13 +08004632 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004633 if (!busiest) {
4634 schedstat_inc(sd, lb_nobusyq[idle]);
4635 goto out_balanced;
4636 }
4637
Michael Wang78feefc2012-08-06 16:41:59 +08004638 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004639
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004640 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004641
4642 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304643 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004644 if (busiest->nr_running > 1) {
4645 /*
4646 * Attempt to move tasks. If find_busiest_group has found
4647 * an imbalance but busiest->nr_running <= 1, the group is
4648 * still unbalanced. ld_moved simply stays zero, so it is
4649 * correctly treated as an imbalance.
4650 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004651 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004652 env.src_cpu = busiest->cpu;
4653 env.src_rq = busiest;
4654 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004655
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004656 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004657more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004658 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004659 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304660
4661 /*
4662 * cur_ld_moved - load moved in current iteration
4663 * ld_moved - cumulative load moved across iterations
4664 */
4665 cur_ld_moved = move_tasks(&env);
4666 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004667 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004668 local_irq_restore(flags);
4669
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004670 if (env.flags & LBF_NEED_BREAK) {
4671 env.flags &= ~LBF_NEED_BREAK;
4672 goto more_balance;
4673 }
4674
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004675 /*
4676 * some other cpu did the load balance for us.
4677 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304678 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4679 resched_cpu(env.dst_cpu);
4680
4681 /*
4682 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4683 * us and move them to an alternate dst_cpu in our sched_group
4684 * where they can run. The upper limit on how many times we
4685 * iterate on same src_cpu is dependent on number of cpus in our
4686 * sched_group.
4687 *
4688 * This changes load balance semantics a bit on who can move
4689 * load to a given_cpu. In addition to the given_cpu itself
4690 * (or a ilb_cpu acting on its behalf where given_cpu is
4691 * nohz-idle), we now have balance_cpu in a position to move
4692 * load to given_cpu. In rare situations, this may cause
4693 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4694 * _independently_ and at _same_ time to move some load to
4695 * given_cpu) causing exceess load to be moved to given_cpu.
4696 * This however should not happen so much in practice and
4697 * moreover subsequent load balance cycles should correct the
4698 * excess load moved.
4699 */
4700 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4701 lb_iterations++ < max_lb_iterations) {
4702
Michael Wang78feefc2012-08-06 16:41:59 +08004703 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304704 env.dst_cpu = env.new_dst_cpu;
4705 env.flags &= ~LBF_SOME_PINNED;
4706 env.loop = 0;
4707 env.loop_break = sched_nr_migrate_break;
4708 /*
4709 * Go back to "more_balance" rather than "redo" since we
4710 * need to continue with same src_cpu.
4711 */
4712 goto more_balance;
4713 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004714
4715 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004716 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004717 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304718 if (!cpumask_empty(cpus)) {
4719 env.loop = 0;
4720 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004721 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304722 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004723 goto out_balanced;
4724 }
4725 }
4726
4727 if (!ld_moved) {
4728 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004729 /*
4730 * Increment the failure counter only on periodic balance.
4731 * We do not want newidle balance, which can be very
4732 * frequent, pollute the failure counter causing
4733 * excessive cache_hot migrations and active balances.
4734 */
4735 if (idle != CPU_NEWLY_IDLE)
4736 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004737
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004738 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004739 raw_spin_lock_irqsave(&busiest->lock, flags);
4740
Tejun Heo969c7922010-05-06 18:49:21 +02004741 /* don't kick the active_load_balance_cpu_stop,
4742 * if the curr task on busiest cpu can't be
4743 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004744 */
4745 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004746 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004747 raw_spin_unlock_irqrestore(&busiest->lock,
4748 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004749 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004750 goto out_one_pinned;
4751 }
4752
Tejun Heo969c7922010-05-06 18:49:21 +02004753 /*
4754 * ->active_balance synchronizes accesses to
4755 * ->active_balance_work. Once set, it's cleared
4756 * only after active load balance is finished.
4757 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004758 if (!busiest->active_balance) {
4759 busiest->active_balance = 1;
4760 busiest->push_cpu = this_cpu;
4761 active_balance = 1;
4762 }
4763 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004764
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004765 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004766 stop_one_cpu_nowait(cpu_of(busiest),
4767 active_load_balance_cpu_stop, busiest,
4768 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004769 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004770
4771 /*
4772 * We've kicked active balancing, reset the failure
4773 * counter.
4774 */
4775 sd->nr_balance_failed = sd->cache_nice_tries+1;
4776 }
4777 } else
4778 sd->nr_balance_failed = 0;
4779
4780 if (likely(!active_balance)) {
4781 /* We were unbalanced, so reset the balancing interval */
4782 sd->balance_interval = sd->min_interval;
4783 } else {
4784 /*
4785 * If we've begun active balancing, start to back off. This
4786 * case may not be covered by the all_pinned logic if there
4787 * is only 1 task on the busy runqueue (because we don't call
4788 * move_tasks).
4789 */
4790 if (sd->balance_interval < sd->max_interval)
4791 sd->balance_interval *= 2;
4792 }
4793
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004794 goto out;
4795
4796out_balanced:
4797 schedstat_inc(sd, lb_balanced[idle]);
4798
4799 sd->nr_balance_failed = 0;
4800
4801out_one_pinned:
4802 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004803 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004804 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004805 (sd->balance_interval < sd->max_interval))
4806 sd->balance_interval *= 2;
4807
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004808 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004809out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004810 return ld_moved;
4811}
4812
4813/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004814 * idle_balance is called by schedule() if this_cpu is about to become
4815 * idle. Attempts to pull tasks from other CPUs.
4816 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004817void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004818{
4819 struct sched_domain *sd;
4820 int pulled_task = 0;
4821 unsigned long next_balance = jiffies + HZ;
4822
4823 this_rq->idle_stamp = this_rq->clock;
4824
4825 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4826 return;
4827
Ben Segall18bf2802012-10-04 12:51:20 +02004828 update_rq_runnable_avg(this_rq, 1);
4829
Peter Zijlstraf492e122009-12-23 15:29:42 +01004830 /*
4831 * Drop the rq->lock, but keep IRQ/preempt disabled.
4832 */
4833 raw_spin_unlock(&this_rq->lock);
4834
Paul Turnerc66eaf62010-11-15 15:47:07 -08004835 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004836 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004837 for_each_domain(this_cpu, sd) {
4838 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004839 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004840
4841 if (!(sd->flags & SD_LOAD_BALANCE))
4842 continue;
4843
Peter Zijlstraf492e122009-12-23 15:29:42 +01004844 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004845 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004846 pulled_task = load_balance(this_cpu, this_rq,
4847 sd, CPU_NEWLY_IDLE, &balance);
4848 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004849
4850 interval = msecs_to_jiffies(sd->balance_interval);
4851 if (time_after(next_balance, sd->last_balance + interval))
4852 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004853 if (pulled_task) {
4854 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004855 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004856 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004857 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004858 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004859
4860 raw_spin_lock(&this_rq->lock);
4861
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004862 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4863 /*
4864 * We are going idle. next_balance may be set based on
4865 * a busy processor. So reset next_balance.
4866 */
4867 this_rq->next_balance = next_balance;
4868 }
4869}
4870
4871/*
Tejun Heo969c7922010-05-06 18:49:21 +02004872 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4873 * running tasks off the busiest CPU onto idle CPUs. It requires at
4874 * least 1 task to be running on each physical CPU where possible, and
4875 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004876 */
Tejun Heo969c7922010-05-06 18:49:21 +02004877static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004878{
Tejun Heo969c7922010-05-06 18:49:21 +02004879 struct rq *busiest_rq = data;
4880 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004881 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004882 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004883 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004884
4885 raw_spin_lock_irq(&busiest_rq->lock);
4886
4887 /* make sure the requested cpu hasn't gone down in the meantime */
4888 if (unlikely(busiest_cpu != smp_processor_id() ||
4889 !busiest_rq->active_balance))
4890 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004891
4892 /* Is there any task to move? */
4893 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004894 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004895
4896 /*
4897 * This condition is "impossible", if it occurs
4898 * we need to fix it. Originally reported by
4899 * Bjorn Helgaas on a 128-cpu setup.
4900 */
4901 BUG_ON(busiest_rq == target_rq);
4902
4903 /* move a task from busiest_rq to target_rq */
4904 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004905
4906 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004907 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004908 for_each_domain(target_cpu, sd) {
4909 if ((sd->flags & SD_LOAD_BALANCE) &&
4910 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4911 break;
4912 }
4913
4914 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004915 struct lb_env env = {
4916 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004917 .dst_cpu = target_cpu,
4918 .dst_rq = target_rq,
4919 .src_cpu = busiest_rq->cpu,
4920 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004921 .idle = CPU_IDLE,
4922 };
4923
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004924 schedstat_inc(sd, alb_count);
4925
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004926 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004927 schedstat_inc(sd, alb_pushed);
4928 else
4929 schedstat_inc(sd, alb_failed);
4930 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004931 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004932 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004933out_unlock:
4934 busiest_rq->active_balance = 0;
4935 raw_spin_unlock_irq(&busiest_rq->lock);
4936 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004937}
4938
4939#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004940/*
4941 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004942 * - When one of the busy CPUs notice that there may be an idle rebalancing
4943 * needed, they will kick the idle load balancer, which then does idle
4944 * load balancing for all the idle CPUs.
4945 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004946static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004947 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004948 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004949 unsigned long next_balance; /* in jiffy units */
4950} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004951
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004952static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004953{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004954 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004955
Suresh Siddha786d6dc72011-12-01 17:07:35 -08004956 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4957 return ilb;
4958
4959 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004960}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004961
4962/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004963 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4964 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4965 * CPU (if there is one).
4966 */
4967static void nohz_balancer_kick(int cpu)
4968{
4969 int ilb_cpu;
4970
4971 nohz.next_balance++;
4972
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004973 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004974
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004975 if (ilb_cpu >= nr_cpu_ids)
4976 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004977
Suresh Siddhacd490c52011-12-06 11:26:34 -08004978 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004979 return;
4980 /*
4981 * Use smp_send_reschedule() instead of resched_cpu().
4982 * This way we generate a sched IPI on the target cpu which
4983 * is idle. And the softirq performing nohz idle load balance
4984 * will be run before returning from the IPI.
4985 */
4986 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004987 return;
4988}
4989
Alex Shic1cc0172012-09-10 15:10:58 +08004990static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004991{
4992 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4993 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4994 atomic_dec(&nohz.nr_cpus);
4995 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4996 }
4997}
4998
Suresh Siddha69e1e812011-12-01 17:07:33 -08004999static inline void set_cpu_sd_state_busy(void)
5000{
5001 struct sched_domain *sd;
5002 int cpu = smp_processor_id();
5003
5004 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5005 return;
5006 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
5007
5008 rcu_read_lock();
5009 for_each_domain(cpu, sd)
5010 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
5011 rcu_read_unlock();
5012}
5013
5014void set_cpu_sd_state_idle(void)
5015{
5016 struct sched_domain *sd;
5017 int cpu = smp_processor_id();
5018
5019 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
5020 return;
5021 set_bit(NOHZ_IDLE, nohz_flags(cpu));
5022
5023 rcu_read_lock();
5024 for_each_domain(cpu, sd)
5025 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
5026 rcu_read_unlock();
5027}
5028
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005029/*
Alex Shic1cc0172012-09-10 15:10:58 +08005030 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005031 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005032 */
Alex Shic1cc0172012-09-10 15:10:58 +08005033void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005034{
Suresh Siddha71325962012-01-19 18:28:57 -08005035 /*
5036 * If this cpu is going down, then nothing needs to be done.
5037 */
5038 if (!cpu_active(cpu))
5039 return;
5040
Alex Shic1cc0172012-09-10 15:10:58 +08005041 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
5042 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005043
Alex Shic1cc0172012-09-10 15:10:58 +08005044 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
5045 atomic_inc(&nohz.nr_cpus);
5046 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005047}
Suresh Siddha71325962012-01-19 18:28:57 -08005048
5049static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
5050 unsigned long action, void *hcpu)
5051{
5052 switch (action & ~CPU_TASKS_FROZEN) {
5053 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08005054 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08005055 return NOTIFY_OK;
5056 default:
5057 return NOTIFY_DONE;
5058 }
5059}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005060#endif
5061
5062static DEFINE_SPINLOCK(balancing);
5063
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005064/*
5065 * Scale the max load_balance interval with the number of CPUs in the system.
5066 * This trades load-balance latency on larger machines for less cross talk.
5067 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005068void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005069{
5070 max_load_balance_interval = HZ*num_online_cpus()/10;
5071}
5072
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005073/*
5074 * It checks each scheduling domain to see if it is due to be balanced,
5075 * and initiates a balancing operation if so.
5076 *
5077 * Balancing parameters are set up in arch_init_sched_domains.
5078 */
5079static void rebalance_domains(int cpu, enum cpu_idle_type idle)
5080{
5081 int balance = 1;
5082 struct rq *rq = cpu_rq(cpu);
5083 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005084 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005085 /* Earliest time when we have to do rebalance again */
5086 unsigned long next_balance = jiffies + 60*HZ;
5087 int update_next_balance = 0;
5088 int need_serialize;
5089
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005090 update_shares(cpu);
5091
Peter Zijlstradce840a2011-04-07 14:09:50 +02005092 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005093 for_each_domain(cpu, sd) {
5094 if (!(sd->flags & SD_LOAD_BALANCE))
5095 continue;
5096
5097 interval = sd->balance_interval;
5098 if (idle != CPU_IDLE)
5099 interval *= sd->busy_factor;
5100
5101 /* scale ms to jiffies */
5102 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005103 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005104
5105 need_serialize = sd->flags & SD_SERIALIZE;
5106
5107 if (need_serialize) {
5108 if (!spin_trylock(&balancing))
5109 goto out;
5110 }
5111
5112 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5113 if (load_balance(cpu, rq, sd, idle, &balance)) {
5114 /*
5115 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005116 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005117 */
5118 idle = CPU_NOT_IDLE;
5119 }
5120 sd->last_balance = jiffies;
5121 }
5122 if (need_serialize)
5123 spin_unlock(&balancing);
5124out:
5125 if (time_after(next_balance, sd->last_balance + interval)) {
5126 next_balance = sd->last_balance + interval;
5127 update_next_balance = 1;
5128 }
5129
5130 /*
5131 * Stop the load balance at this level. There is another
5132 * CPU in our sched group which is doing load balancing more
5133 * actively.
5134 */
5135 if (!balance)
5136 break;
5137 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005138 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005139
5140 /*
5141 * next_balance will be updated only when there is a need.
5142 * When the cpu is attached to null domain for ex, it will not be
5143 * updated.
5144 */
5145 if (likely(update_next_balance))
5146 rq->next_balance = next_balance;
5147}
5148
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005149#ifdef CONFIG_NO_HZ
5150/*
5151 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5152 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5153 */
5154static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5155{
5156 struct rq *this_rq = cpu_rq(this_cpu);
5157 struct rq *rq;
5158 int balance_cpu;
5159
Suresh Siddha1c792db2011-12-01 17:07:32 -08005160 if (idle != CPU_IDLE ||
5161 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5162 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005163
5164 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005165 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005166 continue;
5167
5168 /*
5169 * If this cpu gets work to do, stop the load balancing
5170 * work being done for other cpus. Next load
5171 * balancing owner will pick it up.
5172 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005173 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005174 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005175
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005176 rq = cpu_rq(balance_cpu);
5177
5178 raw_spin_lock_irq(&rq->lock);
5179 update_rq_clock(rq);
5180 update_idle_cpu_load(rq);
5181 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005182
5183 rebalance_domains(balance_cpu, CPU_IDLE);
5184
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005185 if (time_after(this_rq->next_balance, rq->next_balance))
5186 this_rq->next_balance = rq->next_balance;
5187 }
5188 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005189end:
5190 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005191}
5192
5193/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005194 * Current heuristic for kicking the idle load balancer in the presence
5195 * of an idle cpu is the system.
5196 * - This rq has more than one task.
5197 * - At any scheduler domain level, this cpu's scheduler group has multiple
5198 * busy cpu's exceeding the group's power.
5199 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5200 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005201 */
5202static inline int nohz_kick_needed(struct rq *rq, int cpu)
5203{
5204 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005205 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005206
Suresh Siddha1c792db2011-12-01 17:07:32 -08005207 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005208 return 0;
5209
Suresh Siddha1c792db2011-12-01 17:07:32 -08005210 /*
5211 * We may be recently in ticked or tickless idle mode. At the first
5212 * busy tick after returning from idle, we will update the busy stats.
5213 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005214 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005215 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005216
5217 /*
5218 * None are in tickless mode and hence no need for NOHZ idle load
5219 * balancing.
5220 */
5221 if (likely(!atomic_read(&nohz.nr_cpus)))
5222 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005223
5224 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005225 return 0;
5226
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005227 if (rq->nr_running >= 2)
5228 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005229
Peter Zijlstra067491b2011-12-07 14:32:08 +01005230 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005231 for_each_domain(cpu, sd) {
5232 struct sched_group *sg = sd->groups;
5233 struct sched_group_power *sgp = sg->sgp;
5234 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005235
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005236 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005237 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005238
5239 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5240 && (cpumask_first_and(nohz.idle_cpus_mask,
5241 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005242 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005243
5244 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5245 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005246 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005247 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005248 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005249
5250need_kick_unlock:
5251 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005252need_kick:
5253 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005254}
5255#else
5256static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5257#endif
5258
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005259/*
5260 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005261 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005262 */
5263static void run_rebalance_domains(struct softirq_action *h)
5264{
5265 int this_cpu = smp_processor_id();
5266 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005267 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005268 CPU_IDLE : CPU_NOT_IDLE;
5269
5270 rebalance_domains(this_cpu, idle);
5271
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005272 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005273 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005274 * balancing on behalf of the other idle cpus whose ticks are
5275 * stopped.
5276 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005277 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005278}
5279
5280static inline int on_null_domain(int cpu)
5281{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005282 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005283}
5284
5285/*
5286 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005287 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005288void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005289{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005290 /* Don't need to rebalance while attached to NULL domain */
5291 if (time_after_eq(jiffies, rq->next_balance) &&
5292 likely(!on_null_domain(cpu)))
5293 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005294#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005295 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005296 nohz_balancer_kick(cpu);
5297#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005298}
5299
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005300static void rq_online_fair(struct rq *rq)
5301{
5302 update_sysctl();
5303}
5304
5305static void rq_offline_fair(struct rq *rq)
5306{
5307 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005308
5309 /* Ensure any throttled groups are reachable by pick_next_task */
5310 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005311}
5312
Dhaval Giani55e12e52008-06-24 23:39:43 +05305313#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005314
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005315/*
5316 * scheduler tick hitting a task of our scheduling class:
5317 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005318static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005319{
5320 struct cfs_rq *cfs_rq;
5321 struct sched_entity *se = &curr->se;
5322
5323 for_each_sched_entity(se) {
5324 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005325 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005326 }
Ben Segall18bf2802012-10-04 12:51:20 +02005327
5328 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005329}
5330
5331/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005332 * called on fork with the child task as argument from the parent's context
5333 * - child not yet on the tasklist
5334 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005335 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005336static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005337{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005338 struct cfs_rq *cfs_rq;
5339 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005340 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005341 struct rq *rq = this_rq();
5342 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005343
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005344 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005345
Peter Zijlstra861d0342010-08-19 13:31:43 +02005346 update_rq_clock(rq);
5347
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005348 cfs_rq = task_cfs_rq(current);
5349 curr = cfs_rq->curr;
5350
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005351 if (unlikely(task_cpu(p) != this_cpu)) {
5352 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005353 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005354 rcu_read_unlock();
5355 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005356
Ting Yang7109c442007-08-28 12:53:24 +02005357 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005358
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005359 if (curr)
5360 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005361 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005362
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005363 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005364 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005365 * Upon rescheduling, sched_class::put_prev_task() will place
5366 * 'current' within the tree based on its new key value.
5367 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005368 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305369 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005370 }
5371
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005372 se->vruntime -= cfs_rq->min_vruntime;
5373
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005374 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005375}
5376
Steven Rostedtcb469842008-01-25 21:08:22 +01005377/*
5378 * Priority of the task has changed. Check to see if we preempt
5379 * the current task.
5380 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005381static void
5382prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005383{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005384 if (!p->se.on_rq)
5385 return;
5386
Steven Rostedtcb469842008-01-25 21:08:22 +01005387 /*
5388 * Reschedule if we are currently running on this runqueue and
5389 * our priority decreased, or if we are not currently running on
5390 * this runqueue and our priority is higher than the current's
5391 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005392 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005393 if (p->prio > oldprio)
5394 resched_task(rq->curr);
5395 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005396 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005397}
5398
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005399static void switched_from_fair(struct rq *rq, struct task_struct *p)
5400{
5401 struct sched_entity *se = &p->se;
5402 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5403
5404 /*
5405 * Ensure the task's vruntime is normalized, so that when its
5406 * switched back to the fair class the enqueue_entity(.flags=0) will
5407 * do the right thing.
5408 *
5409 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5410 * have normalized the vruntime, if it was !on_rq, then only when
5411 * the task is sleeping will it still have non-normalized vruntime.
5412 */
5413 if (!se->on_rq && p->state != TASK_RUNNING) {
5414 /*
5415 * Fix up our vruntime so that the current sleep doesn't
5416 * cause 'unlimited' sleep bonus.
5417 */
5418 place_entity(cfs_rq, se, 0);
5419 se->vruntime -= cfs_rq->min_vruntime;
5420 }
Paul Turner9ee474f2012-10-04 13:18:30 +02005421
5422#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5423 /*
5424 * Remove our load from contribution when we leave sched_fair
5425 * and ensure we don't carry in an old decay_count if we
5426 * switch back.
5427 */
5428 if (p->se.avg.decay_count) {
5429 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
5430 __synchronize_entity_decay(&p->se);
5431 subtract_blocked_load_contrib(cfs_rq,
5432 p->se.avg.load_avg_contrib);
5433 }
5434#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005435}
5436
Steven Rostedtcb469842008-01-25 21:08:22 +01005437/*
5438 * We switched to the sched_fair class.
5439 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005440static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005441{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005442 if (!p->se.on_rq)
5443 return;
5444
Steven Rostedtcb469842008-01-25 21:08:22 +01005445 /*
5446 * We were most likely switched from sched_rt, so
5447 * kick off the schedule if running, otherwise just see
5448 * if we can still preempt the current task.
5449 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005450 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005451 resched_task(rq->curr);
5452 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005453 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005454}
5455
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005456/* Account for a task changing its policy or group.
5457 *
5458 * This routine is mostly called to set cfs_rq->curr field when a task
5459 * migrates between groups/classes.
5460 */
5461static void set_curr_task_fair(struct rq *rq)
5462{
5463 struct sched_entity *se = &rq->curr->se;
5464
Paul Turnerec12cb72011-07-21 09:43:30 -07005465 for_each_sched_entity(se) {
5466 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5467
5468 set_next_entity(cfs_rq, se);
5469 /* ensure bandwidth has been allocated on our new cfs_rq */
5470 account_cfs_rq_runtime(cfs_rq, 0);
5471 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005472}
5473
Peter Zijlstra029632f2011-10-25 10:00:11 +02005474void init_cfs_rq(struct cfs_rq *cfs_rq)
5475{
5476 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005477 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5478#ifndef CONFIG_64BIT
5479 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5480#endif
Paul Turner9ee474f2012-10-04 13:18:30 +02005481#if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
5482 atomic64_set(&cfs_rq->decay_counter, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02005483 atomic64_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02005484#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005485}
5486
Peter Zijlstra810b3812008-02-29 15:21:01 -05005487#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005488static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005489{
Paul Turneraff3e492012-10-04 13:18:30 +02005490 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005491 /*
5492 * If the task was not on the rq at the time of this cgroup movement
5493 * it must have been asleep, sleeping tasks keep their ->vruntime
5494 * absolute on their old rq until wakeup (needed for the fair sleeper
5495 * bonus in place_entity()).
5496 *
5497 * If it was on the rq, we've just 'preempted' it, which does convert
5498 * ->vruntime to a relative base.
5499 *
5500 * Make sure both cases convert their relative position when migrating
5501 * to another cgroup's rq. This does somewhat interfere with the
5502 * fair sleeper stuff for the first placement, but who cares.
5503 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005504 /*
5505 * When !on_rq, vruntime of the task has usually NOT been normalized.
5506 * But there are some cases where it has already been normalized:
5507 *
5508 * - Moving a forked child which is waiting for being woken up by
5509 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005510 * - Moving a task which has been woken up by try_to_wake_up() and
5511 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005512 *
5513 * To prevent boost or penalty in the new cfs_rq caused by delta
5514 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5515 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005516 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005517 on_rq = 1;
5518
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005519 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005520 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5521 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02005522 if (!on_rq) {
5523 cfs_rq = cfs_rq_of(&p->se);
5524 p->se.vruntime += cfs_rq->min_vruntime;
5525#ifdef CONFIG_SMP
5526 /*
5527 * migrate_task_rq_fair() will have removed our previous
5528 * contribution, but we must synchronize for ongoing future
5529 * decay.
5530 */
5531 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
5532 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
5533#endif
5534 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05005535}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005536
5537void free_fair_sched_group(struct task_group *tg)
5538{
5539 int i;
5540
5541 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5542
5543 for_each_possible_cpu(i) {
5544 if (tg->cfs_rq)
5545 kfree(tg->cfs_rq[i]);
5546 if (tg->se)
5547 kfree(tg->se[i]);
5548 }
5549
5550 kfree(tg->cfs_rq);
5551 kfree(tg->se);
5552}
5553
5554int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5555{
5556 struct cfs_rq *cfs_rq;
5557 struct sched_entity *se;
5558 int i;
5559
5560 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5561 if (!tg->cfs_rq)
5562 goto err;
5563 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5564 if (!tg->se)
5565 goto err;
5566
5567 tg->shares = NICE_0_LOAD;
5568
5569 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5570
5571 for_each_possible_cpu(i) {
5572 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5573 GFP_KERNEL, cpu_to_node(i));
5574 if (!cfs_rq)
5575 goto err;
5576
5577 se = kzalloc_node(sizeof(struct sched_entity),
5578 GFP_KERNEL, cpu_to_node(i));
5579 if (!se)
5580 goto err_free_rq;
5581
5582 init_cfs_rq(cfs_rq);
5583 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5584 }
5585
5586 return 1;
5587
5588err_free_rq:
5589 kfree(cfs_rq);
5590err:
5591 return 0;
5592}
5593
5594void unregister_fair_sched_group(struct task_group *tg, int cpu)
5595{
5596 struct rq *rq = cpu_rq(cpu);
5597 unsigned long flags;
5598
5599 /*
5600 * Only empty task groups can be destroyed; so we can speculatively
5601 * check on_list without danger of it being re-added.
5602 */
5603 if (!tg->cfs_rq[cpu]->on_list)
5604 return;
5605
5606 raw_spin_lock_irqsave(&rq->lock, flags);
5607 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5608 raw_spin_unlock_irqrestore(&rq->lock, flags);
5609}
5610
5611void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5612 struct sched_entity *se, int cpu,
5613 struct sched_entity *parent)
5614{
5615 struct rq *rq = cpu_rq(cpu);
5616
5617 cfs_rq->tg = tg;
5618 cfs_rq->rq = rq;
5619#ifdef CONFIG_SMP
5620 /* allow initial update_cfs_load() to truncate */
5621 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005622#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005623 init_cfs_rq_runtime(cfs_rq);
5624
5625 tg->cfs_rq[cpu] = cfs_rq;
5626 tg->se[cpu] = se;
5627
5628 /* se could be NULL for root_task_group */
5629 if (!se)
5630 return;
5631
5632 if (!parent)
5633 se->cfs_rq = &rq->cfs;
5634 else
5635 se->cfs_rq = parent->my_q;
5636
5637 se->my_q = cfs_rq;
5638 update_load_set(&se->load, 0);
5639 se->parent = parent;
5640}
5641
5642static DEFINE_MUTEX(shares_mutex);
5643
5644int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5645{
5646 int i;
5647 unsigned long flags;
5648
5649 /*
5650 * We can't change the weight of the root cgroup.
5651 */
5652 if (!tg->se[0])
5653 return -EINVAL;
5654
5655 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5656
5657 mutex_lock(&shares_mutex);
5658 if (tg->shares == shares)
5659 goto done;
5660
5661 tg->shares = shares;
5662 for_each_possible_cpu(i) {
5663 struct rq *rq = cpu_rq(i);
5664 struct sched_entity *se;
5665
5666 se = tg->se[i];
5667 /* Propagate contribution to hierarchy */
5668 raw_spin_lock_irqsave(&rq->lock, flags);
5669 for_each_sched_entity(se)
5670 update_cfs_shares(group_cfs_rq(se));
5671 raw_spin_unlock_irqrestore(&rq->lock, flags);
5672 }
5673
5674done:
5675 mutex_unlock(&shares_mutex);
5676 return 0;
5677}
5678#else /* CONFIG_FAIR_GROUP_SCHED */
5679
5680void free_fair_sched_group(struct task_group *tg) { }
5681
5682int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5683{
5684 return 1;
5685}
5686
5687void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5688
5689#endif /* CONFIG_FAIR_GROUP_SCHED */
5690
Peter Zijlstra810b3812008-02-29 15:21:01 -05005691
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005692static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005693{
5694 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005695 unsigned int rr_interval = 0;
5696
5697 /*
5698 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5699 * idle runqueue:
5700 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005701 if (rq->cfs.load.weight)
5702 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005703
5704 return rr_interval;
5705}
5706
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005707/*
5708 * All the scheduling class methods:
5709 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005710const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005711 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005712 .enqueue_task = enqueue_task_fair,
5713 .dequeue_task = dequeue_task_fair,
5714 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005715 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005716
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005717 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005718
5719 .pick_next_task = pick_next_task_fair,
5720 .put_prev_task = put_prev_task_fair,
5721
Peter Williams681f3e62007-10-24 18:23:51 +02005722#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005723 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02005724 .migrate_task_rq = migrate_task_rq_fair,
Li Zefan4ce72a22008-10-22 15:25:26 +08005725
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005726 .rq_online = rq_online_fair,
5727 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005728
5729 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005730#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005731
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005732 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005733 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005734 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005735
5736 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005737 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005738 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005739
Peter Williams0d721ce2009-09-21 01:31:53 +00005740 .get_rr_interval = get_rr_interval_fair,
5741
Peter Zijlstra810b3812008-02-29 15:21:01 -05005742#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005743 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005744#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005745};
5746
5747#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005748void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005749{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005750 struct cfs_rq *cfs_rq;
5751
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005752 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005753 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005754 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005755 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005756}
5757#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005758
5759__init void init_sched_fair_class(void)
5760{
5761#ifdef CONFIG_SMP
5762 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5763
5764#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005765 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005766 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005767 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005768#endif
5769#endif /* SMP */
5770
5771}