blob: 4b577863933f04c133db5406d049ad2b3b99f471 [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>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020029#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000030#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020031#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020032
33#include <trace/events/sched.h>
34
35#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010036
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037/*
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090039 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020040 *
Peter Zijlstra21805082007-08-25 18:41:53 +020041 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020042 * 'timeslice length' - timeslices in CFS are of variable length
43 * and have no persistent notion like in traditional, time-slice
44 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020046 * (to see the precise effective timeslice length of your workload,
47 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 */
Mike Galbraith21406922010-03-11 17:17:15 +010049unsigned int sysctl_sched_latency = 6000000ULL;
50unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020051
52/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010053 * The initial- and re-scaling of tunables is configurable
54 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
55 *
56 * Options are:
57 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
58 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
59 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
60 */
61enum sched_tunable_scaling sysctl_sched_tunable_scaling
62 = SCHED_TUNABLESCALING_LOG;
63
64/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010065 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090066 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020068unsigned int sysctl_sched_min_granularity = 750000ULL;
69unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010070
71/*
72 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
73 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020074static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010075
76/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020078 * parent will (try to) run first.
79 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020080unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020081
82/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020083 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020084 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020085 *
86 * This option delays the preemption effects of decoupled workloads
87 * and reduces their over-scheduling. Synchronous workloads will still
88 * have immediate wakeup/sleep latencies.
89 */
Mike Galbraith172e0822009-09-09 15:41:37 +020090unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010091unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020092
Ingo Molnarda84d962007-10-15 17:00:18 +020093const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
94
Paul Turnera7a4f8a2010-11-15 15:47:06 -080095/*
96 * The exponential sliding window over which load is averaged for shares
97 * distribution.
98 * (default: 10msec)
99 */
100unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
101
Paul Turnerec12cb72011-07-21 09:43:30 -0700102#ifdef CONFIG_CFS_BANDWIDTH
103/*
104 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
105 * each time a cfs_rq requests quota.
106 *
107 * Note: in the case that the slice exceeds the runtime remaining (either due
108 * to consumption or the quota being specified to be smaller than the slice)
109 * we will always only issue the remaining available time.
110 *
111 * default: 5 msec, units: microseconds
112 */
113unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
114#endif
115
Peter Zijlstra029632f2011-10-25 10:00:11 +0200116/*
117 * Increase the granularity value when there are more CPUs,
118 * because with more CPUs the 'effective latency' as visible
119 * to users decreases. But the relationship is not linear,
120 * so pick a second-best guess by going with the log2 of the
121 * number of CPUs.
122 *
123 * This idea comes from the SD scheduler of Con Kolivas:
124 */
125static int get_update_sysctl_factor(void)
126{
127 unsigned int cpus = min_t(int, num_online_cpus(), 8);
128 unsigned int factor;
129
130 switch (sysctl_sched_tunable_scaling) {
131 case SCHED_TUNABLESCALING_NONE:
132 factor = 1;
133 break;
134 case SCHED_TUNABLESCALING_LINEAR:
135 factor = cpus;
136 break;
137 case SCHED_TUNABLESCALING_LOG:
138 default:
139 factor = 1 + ilog2(cpus);
140 break;
141 }
142
143 return factor;
144}
145
146static void update_sysctl(void)
147{
148 unsigned int factor = get_update_sysctl_factor();
149
150#define SET_SYSCTL(name) \
151 (sysctl_##name = (factor) * normalized_sysctl_##name)
152 SET_SYSCTL(sched_min_granularity);
153 SET_SYSCTL(sched_latency);
154 SET_SYSCTL(sched_wakeup_granularity);
155#undef SET_SYSCTL
156}
157
158void sched_init_granularity(void)
159{
160 update_sysctl();
161}
162
163#if BITS_PER_LONG == 32
164# define WMULT_CONST (~0UL)
165#else
166# define WMULT_CONST (1UL << 32)
167#endif
168
169#define WMULT_SHIFT 32
170
171/*
172 * Shift right and round:
173 */
174#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
175
176/*
177 * delta *= weight / lw
178 */
179static unsigned long
180calc_delta_mine(unsigned long delta_exec, unsigned long weight,
181 struct load_weight *lw)
182{
183 u64 tmp;
184
185 /*
186 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
187 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
188 * 2^SCHED_LOAD_RESOLUTION.
189 */
190 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
191 tmp = (u64)delta_exec * scale_load_down(weight);
192 else
193 tmp = (u64)delta_exec;
194
195 if (!lw->inv_weight) {
196 unsigned long w = scale_load_down(lw->weight);
197
198 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
199 lw->inv_weight = 1;
200 else if (unlikely(!w))
201 lw->inv_weight = WMULT_CONST;
202 else
203 lw->inv_weight = WMULT_CONST / w;
204 }
205
206 /*
207 * Check whether we'd overflow the 64-bit multiplication:
208 */
209 if (unlikely(tmp > WMULT_CONST))
210 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
211 WMULT_SHIFT/2);
212 else
213 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
214
215 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
216}
217
218
219const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200220
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200221/**************************************************************
222 * CFS operations on generic schedulable entities:
223 */
224
225#ifdef CONFIG_FAIR_GROUP_SCHED
226
227/* cpu runqueue to which this cfs_rq is attached */
228static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
229{
230 return cfs_rq->rq;
231}
232
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200233/* An entity is a task if it doesn't "own" a runqueue */
234#define entity_is_task(se) (!se->my_q)
235
Peter Zijlstra8f488942009-07-24 12:25:30 +0200236static inline struct task_struct *task_of(struct sched_entity *se)
237{
238#ifdef CONFIG_SCHED_DEBUG
239 WARN_ON_ONCE(!entity_is_task(se));
240#endif
241 return container_of(se, struct task_struct, se);
242}
243
Peter Zijlstrab7581492008-04-19 19:45:00 +0200244/* Walk up scheduling entities hierarchy */
245#define for_each_sched_entity(se) \
246 for (; se; se = se->parent)
247
248static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
249{
250 return p->se.cfs_rq;
251}
252
253/* runqueue on which this entity is (to be) queued */
254static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
255{
256 return se->cfs_rq;
257}
258
259/* runqueue "owned" by this group */
260static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
261{
262 return grp->my_q;
263}
264
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;
284 }
285}
286
287static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
288{
289 if (cfs_rq->on_list) {
290 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
291 cfs_rq->on_list = 0;
292 }
293}
294
Peter Zijlstrab7581492008-04-19 19:45:00 +0200295/* Iterate thr' all leaf cfs_rq's on a runqueue */
296#define for_each_leaf_cfs_rq(rq, cfs_rq) \
297 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
298
299/* Do the two (enqueued) entities belong to the same group ? */
300static inline int
301is_same_group(struct sched_entity *se, struct sched_entity *pse)
302{
303 if (se->cfs_rq == pse->cfs_rq)
304 return 1;
305
306 return 0;
307}
308
309static inline struct sched_entity *parent_entity(struct sched_entity *se)
310{
311 return se->parent;
312}
313
Peter Zijlstra464b7522008-10-24 11:06:15 +0200314/* return depth at which a sched entity is present in the hierarchy */
315static inline int depth_se(struct sched_entity *se)
316{
317 int depth = 0;
318
319 for_each_sched_entity(se)
320 depth++;
321
322 return depth;
323}
324
325static void
326find_matching_se(struct sched_entity **se, struct sched_entity **pse)
327{
328 int se_depth, pse_depth;
329
330 /*
331 * preemption test can be made between sibling entities who are in the
332 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
333 * both tasks until we find their ancestors who are siblings of common
334 * parent.
335 */
336
337 /* First walk up until both entities are at same depth */
338 se_depth = depth_se(*se);
339 pse_depth = depth_se(*pse);
340
341 while (se_depth > pse_depth) {
342 se_depth--;
343 *se = parent_entity(*se);
344 }
345
346 while (pse_depth > se_depth) {
347 pse_depth--;
348 *pse = parent_entity(*pse);
349 }
350
351 while (!is_same_group(*se, *pse)) {
352 *se = parent_entity(*se);
353 *pse = parent_entity(*pse);
354 }
355}
356
Peter Zijlstra8f488942009-07-24 12:25:30 +0200357#else /* !CONFIG_FAIR_GROUP_SCHED */
358
359static inline struct task_struct *task_of(struct sched_entity *se)
360{
361 return container_of(se, struct task_struct, se);
362}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200363
364static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
365{
366 return container_of(cfs_rq, struct rq, cfs);
367}
368
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200369#define entity_is_task(se) 1
370
Peter Zijlstrab7581492008-04-19 19:45:00 +0200371#define for_each_sched_entity(se) \
372 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200373
Peter Zijlstrab7581492008-04-19 19:45:00 +0200374static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377}
378
Peter Zijlstrab7581492008-04-19 19:45:00 +0200379static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
380{
381 struct task_struct *p = task_of(se);
382 struct rq *rq = task_rq(p);
383
384 return &rq->cfs;
385}
386
387/* runqueue "owned" by this group */
388static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
389{
390 return NULL;
391}
392
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800393static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
394{
395}
396
397static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
398{
399}
400
Peter Zijlstrab7581492008-04-19 19:45:00 +0200401#define for_each_leaf_cfs_rq(rq, cfs_rq) \
402 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
403
404static inline int
405is_same_group(struct sched_entity *se, struct sched_entity *pse)
406{
407 return 1;
408}
409
410static inline struct sched_entity *parent_entity(struct sched_entity *se)
411{
412 return NULL;
413}
414
Peter Zijlstra464b7522008-10-24 11:06:15 +0200415static inline void
416find_matching_se(struct sched_entity **se, struct sched_entity **pse)
417{
418}
419
Peter Zijlstrab7581492008-04-19 19:45:00 +0200420#endif /* CONFIG_FAIR_GROUP_SCHED */
421
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700422static __always_inline
423void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200424
425/**************************************************************
426 * Scheduling class tree data structure manipulation methods:
427 */
428
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200429static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200430{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200431 s64 delta = (s64)(vruntime - min_vruntime);
432 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200433 min_vruntime = vruntime;
434
435 return min_vruntime;
436}
437
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200438static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200439{
440 s64 delta = (s64)(vruntime - min_vruntime);
441 if (delta < 0)
442 min_vruntime = vruntime;
443
444 return min_vruntime;
445}
446
Fabio Checconi54fdc582009-07-16 12:32:27 +0200447static inline int entity_before(struct sched_entity *a,
448 struct sched_entity *b)
449{
450 return (s64)(a->vruntime - b->vruntime) < 0;
451}
452
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200453static void update_min_vruntime(struct cfs_rq *cfs_rq)
454{
455 u64 vruntime = cfs_rq->min_vruntime;
456
457 if (cfs_rq->curr)
458 vruntime = cfs_rq->curr->vruntime;
459
460 if (cfs_rq->rb_leftmost) {
461 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
462 struct sched_entity,
463 run_node);
464
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100465 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200466 vruntime = se->vruntime;
467 else
468 vruntime = min_vruntime(vruntime, se->vruntime);
469 }
470
471 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200472#ifndef CONFIG_64BIT
473 smp_wmb();
474 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
475#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200476}
477
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200478/*
479 * Enqueue an entity into the rb-tree:
480 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200481static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200482{
483 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
484 struct rb_node *parent = NULL;
485 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200486 int leftmost = 1;
487
488 /*
489 * Find the right place in the rbtree:
490 */
491 while (*link) {
492 parent = *link;
493 entry = rb_entry(parent, struct sched_entity, run_node);
494 /*
495 * We dont care about collisions. Nodes with
496 * the same key stay together.
497 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200498 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200499 link = &parent->rb_left;
500 } else {
501 link = &parent->rb_right;
502 leftmost = 0;
503 }
504 }
505
506 /*
507 * Maintain a cache of leftmost tree entries (it is frequently
508 * used):
509 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200510 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200511 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200512
513 rb_link_node(&se->run_node, parent, link);
514 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200515}
516
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200517static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200518{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100519 if (cfs_rq->rb_leftmost == &se->run_node) {
520 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100521
522 next_node = rb_next(&se->run_node);
523 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100524 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200525
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200526 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200527}
528
Peter Zijlstra029632f2011-10-25 10:00:11 +0200529struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200530{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100531 struct rb_node *left = cfs_rq->rb_leftmost;
532
533 if (!left)
534 return NULL;
535
536 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200537}
538
Rik van Rielac53db52011-02-01 09:51:03 -0500539static struct sched_entity *__pick_next_entity(struct sched_entity *se)
540{
541 struct rb_node *next = rb_next(&se->run_node);
542
543 if (!next)
544 return NULL;
545
546 return rb_entry(next, struct sched_entity, run_node);
547}
548
549#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200550struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200551{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100552 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200553
Balbir Singh70eee742008-02-22 13:25:53 +0530554 if (!last)
555 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100556
557 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200558}
559
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200560/**************************************************************
561 * Scheduling class statistics methods:
562 */
563
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100564int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700565 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100566 loff_t *ppos)
567{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700568 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100569 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100570
571 if (ret || !write)
572 return ret;
573
574 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
575 sysctl_sched_min_granularity);
576
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100577#define WRT_SYSCTL(name) \
578 (normalized_sysctl_##name = sysctl_##name / (factor))
579 WRT_SYSCTL(sched_min_granularity);
580 WRT_SYSCTL(sched_latency);
581 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100582#undef WRT_SYSCTL
583
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100584 return 0;
585}
586#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200587
588/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200589 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200590 */
591static inline unsigned long
592calc_delta_fair(unsigned long delta, struct sched_entity *se)
593{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200594 if (unlikely(se->load.weight != NICE_0_LOAD))
595 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200596
597 return delta;
598}
599
600/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200601 * The idea is to set a period in which each task runs once.
602 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200603 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200604 * this period because otherwise the slices get too small.
605 *
606 * p = (nr <= nl) ? l : l*nr/nl
607 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200608static u64 __sched_period(unsigned long nr_running)
609{
610 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100611 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200612
613 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100614 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200615 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200616 }
617
618 return period;
619}
620
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200621/*
622 * We calculate the wall-time slice from the period by taking a part
623 * proportional to the weight.
624 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200625 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200626 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200627static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200628{
Mike Galbraith0a582442009-01-02 12:16:42 +0100629 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200630
Mike Galbraith0a582442009-01-02 12:16:42 +0100631 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100632 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200633 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100634
635 cfs_rq = cfs_rq_of(se);
636 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200637
Mike Galbraith0a582442009-01-02 12:16:42 +0100638 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200639 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100640
641 update_load_add(&lw, se->load.weight);
642 load = &lw;
643 }
644 slice = calc_delta_mine(slice, se->load.weight, load);
645 }
646 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200647}
648
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200649/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200650 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200652 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200653 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200654static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200657}
658
Paul Turnerd6b55912010-11-15 15:47:09 -0800659static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800660static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800661
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200662/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200663 * Update the current task's runtime statistics. Skip current tasks that
664 * are not in our scheduling class.
665 */
666static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200667__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
668 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200669{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200670 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671
Lucas De Marchi41acab82010-03-10 23:37:45 -0300672 schedstat_set(curr->statistics.exec_max,
673 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200674
675 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200676 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200677 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100678
Ingo Molnare9acbff2007-10-15 17:00:04 +0200679 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200680 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800681
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100682#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800683 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800684#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200685}
686
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200687static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200688{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200689 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700690 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200691 unsigned long delta_exec;
692
693 if (unlikely(!curr))
694 return;
695
696 /*
697 * Get the amount of time the current task was running
698 * since the last time we changed load (this cannot
699 * overflow on 32 bits):
700 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200701 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100702 if (!delta_exec)
703 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200704
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200705 __update_curr(cfs_rq, curr, delta_exec);
706 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100707
708 if (entity_is_task(curr)) {
709 struct task_struct *curtask = task_of(curr);
710
Ingo Molnarf977bb42009-09-13 18:15:54 +0200711 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100712 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700713 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100714 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700715
716 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200717}
718
719static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200720update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200721{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300722 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200723}
724
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200725/*
726 * Task is being enqueued - update stats:
727 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200728static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200729{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200730 /*
731 * Are we enqueueing a waiting task? (for current tasks
732 * a dequeue/enqueue event is a NOP)
733 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200734 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200735 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200736}
737
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200738static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200739update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300741 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
742 rq_of(cfs_rq)->clock - se->statistics.wait_start));
743 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
744 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
745 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200746#ifdef CONFIG_SCHEDSTATS
747 if (entity_is_task(se)) {
748 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300749 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200750 }
751#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300752 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200753}
754
755static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200756update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200757{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200758 /*
759 * Mark the end of the wait period if dequeueing a
760 * waiting task:
761 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200762 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200763 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200764}
765
766/*
767 * We are picking a new current task - update its stats:
768 */
769static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200770update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200771{
772 /*
773 * We are starting a new run period:
774 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700775 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200776}
777
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200778/**************************************************
779 * Scheduling class queueing methods:
780 */
781
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200782#ifdef CONFIG_NUMA_BALANCING
783/*
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200784 * numa task sample period in ms
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200785 */
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200786unsigned int sysctl_numa_balancing_scan_period_min = 100;
Mel Gormanb8593bf2012-11-21 01:18:23 +0000787unsigned int sysctl_numa_balancing_scan_period_max = 100*50;
788unsigned int sysctl_numa_balancing_scan_period_reset = 100*600;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200789
790/* Portion of address space to scan in MB */
791unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200792
Peter Zijlstra4b96a292012-10-25 14:16:47 +0200793/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
794unsigned int sysctl_numa_balancing_scan_delay = 1000;
795
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200796static void task_numa_placement(struct task_struct *p)
797{
798 int seq = ACCESS_ONCE(p->mm->numa_scan_seq);
799
800 if (p->numa_scan_seq == seq)
801 return;
802 p->numa_scan_seq = seq;
803
804 /* FIXME: Scheduling placement policy hints go here */
805}
806
807/*
808 * Got a PROT_NONE fault for a page on @node.
809 */
Mel Gormanb8593bf2012-11-21 01:18:23 +0000810void task_numa_fault(int node, int pages, bool migrated)
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200811{
812 struct task_struct *p = current;
813
814 /* FIXME: Allocate task-specific structure for placement policy here */
815
Mel Gormanfb003b82012-11-15 09:01:14 +0000816 /*
Mel Gormanb8593bf2012-11-21 01:18:23 +0000817 * If pages are properly placed (did not migrate) then scan slower.
818 * This is reset periodically in case of phase changes
Mel Gormanfb003b82012-11-15 09:01:14 +0000819 */
Mel Gormanb8593bf2012-11-21 01:18:23 +0000820 if (!migrated)
821 p->numa_scan_period = min(sysctl_numa_balancing_scan_period_max,
822 p->numa_scan_period + jiffies_to_msecs(10));
Mel Gormanfb003b82012-11-15 09:01:14 +0000823
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200824 task_numa_placement(p);
825}
826
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200827static void reset_ptenuma_scan(struct task_struct *p)
828{
829 ACCESS_ONCE(p->mm->numa_scan_seq)++;
830 p->mm->numa_scan_offset = 0;
831}
832
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200833/*
834 * The expensive part of numa migration is done from task_work context.
835 * Triggered from task_tick_numa().
836 */
837void task_numa_work(struct callback_head *work)
838{
839 unsigned long migrate, next_scan, now = jiffies;
840 struct task_struct *p = current;
841 struct mm_struct *mm = p->mm;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200842 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +0000843 unsigned long start, end;
844 long pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200845
846 WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
847
848 work->next = work; /* protect against double add */
849 /*
850 * Who cares about NUMA placement when they're dying.
851 *
852 * NOTE: make sure not to dereference p->mm before this check,
853 * exit_task_work() happens _after_ exit_mm() so we could be called
854 * without p->mm even though we still had it when we enqueued this
855 * work.
856 */
857 if (p->flags & PF_EXITING)
858 return;
859
860 /*
Mel Gormanb8593bf2012-11-21 01:18:23 +0000861 * Reset the scan period if enough time has gone by. Objective is that
862 * scanning will be reduced if pages are properly placed. As tasks
863 * can enter different phases this needs to be re-examined. Lacking
864 * proper tracking of reference behaviour, this blunt hammer is used.
865 */
866 migrate = mm->numa_next_reset;
867 if (time_after(now, migrate)) {
868 p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
869 next_scan = now + msecs_to_jiffies(sysctl_numa_balancing_scan_period_reset);
870 xchg(&mm->numa_next_reset, next_scan);
871 }
872
873 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200874 * Enforce maximal scan/migration frequency..
875 */
876 migrate = mm->numa_next_scan;
877 if (time_before(now, migrate))
878 return;
879
880 if (p->numa_scan_period == 0)
881 p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
882
Mel Gormanfb003b82012-11-15 09:01:14 +0000883 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200884 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
885 return;
886
Mel Gormane14808b2012-11-19 10:59:15 +0000887 /*
888 * Do not set pte_numa if the current running node is rate-limited.
889 * This loses statistics on the fault but if we are unwilling to
890 * migrate to this node, it is less likely we can do useful work
891 */
892 if (migrate_ratelimited(numa_node_id()))
893 return;
894
Mel Gorman9f406042012-11-14 18:34:32 +0000895 start = mm->numa_scan_offset;
896 pages = sysctl_numa_balancing_scan_size;
897 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
898 if (!pages)
899 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200900
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200901 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +0000902 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200903 if (!vma) {
904 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +0000905 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200906 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200907 }
Mel Gorman9f406042012-11-14 18:34:32 +0000908 for (; vma; vma = vma->vm_next) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200909 if (!vma_migratable(vma))
910 continue;
911
912 /* Skip small VMAs. They are not likely to be of relevance */
913 if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < HPAGE_PMD_NR)
914 continue;
915
Mel Gorman9f406042012-11-14 18:34:32 +0000916 do {
917 start = max(start, vma->vm_start);
918 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
919 end = min(end, vma->vm_end);
920 pages -= change_prot_numa(vma, start, end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200921
Mel Gorman9f406042012-11-14 18:34:32 +0000922 start = end;
923 if (pages <= 0)
924 goto out;
925 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200926 }
927
Mel Gorman9f406042012-11-14 18:34:32 +0000928out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200929 /*
930 * It is possible to reach the end of the VMA list but the last few VMAs are
931 * not guaranteed to the vma_migratable. If they are not, we would find the
932 * !migratable VMA on the next scan but not reset the scanner to the start
933 * so check it now.
934 */
935 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +0000936 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200937 else
938 reset_ptenuma_scan(p);
939 up_read(&mm->mmap_sem);
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200940}
941
942/*
943 * Drive the periodic memory faults..
944 */
945void task_tick_numa(struct rq *rq, struct task_struct *curr)
946{
947 struct callback_head *work = &curr->numa_work;
948 u64 period, now;
949
950 /*
951 * We don't care about NUMA placement if we don't have memory.
952 */
953 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
954 return;
955
956 /*
957 * Using runtime rather than walltime has the dual advantage that
958 * we (mostly) drive the selection from busy threads and that the
959 * task needs to have done some actual work before we bother with
960 * NUMA placement.
961 */
962 now = curr->se.sum_exec_runtime;
963 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
964
965 if (now - curr->node_stamp > period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +0200966 if (!curr->node_stamp)
967 curr->numa_scan_period = sysctl_numa_balancing_scan_period_min;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200968 curr->node_stamp = now;
969
970 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
971 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
972 task_work_add(curr, work, true);
973 }
974 }
975}
976#else
977static void task_tick_numa(struct rq *rq, struct task_struct *curr)
978{
979}
980#endif /* CONFIG_NUMA_BALANCING */
981
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200982static void
983account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
984{
985 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200986 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200987 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100988#ifdef CONFIG_SMP
989 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200990 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100991#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200992 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200993}
994
995static void
996account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
997{
998 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200999 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02001000 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01001001 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +05301002 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001003 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001004}
1005
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001006#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -07001007/* we need this in update_cfs_load and load-balance functions below */
1008static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001009# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -08001010static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
1011 int global_update)
1012{
1013 struct task_group *tg = cfs_rq->tg;
1014 long load_avg;
1015
1016 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
1017 load_avg -= cfs_rq->load_contribution;
1018
1019 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
1020 atomic_add(load_avg, &tg->load_weight);
1021 cfs_rq->load_contribution += load_avg;
1022 }
1023}
1024
1025static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001026{
Paul Turnera7a4f8a2010-11-15 15:47:06 -08001027 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001028 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -08001029 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001030
Paul Turner64660c82011-07-21 09:43:36 -07001031 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001032 return;
1033
Paul Turner05ca62c2011-01-21 20:45:02 -08001034 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001035 delta = now - cfs_rq->load_stamp;
1036
Paul Turnere33078b2010-11-15 15:47:04 -08001037 /* truncate load history at 4 idle periods */
1038 if (cfs_rq->load_stamp > cfs_rq->load_last &&
1039 now - cfs_rq->load_last > 4 * period) {
1040 cfs_rq->load_period = 0;
1041 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -08001042 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -08001043 }
1044
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001045 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -08001046 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001047 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -08001048 if (load) {
1049 cfs_rq->load_last = now;
1050 cfs_rq->load_avg += delta * load;
1051 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001052
Paul Turnerd6b55912010-11-15 15:47:09 -08001053 /* consider updating load contribution on each fold or truncate */
1054 if (global_update || cfs_rq->load_period > period
1055 || !cfs_rq->load_period)
1056 update_cfs_rq_load_contribution(cfs_rq, global_update);
1057
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001058 while (cfs_rq->load_period > period) {
1059 /*
1060 * Inline assembly required to prevent the compiler
1061 * optimising this loop into a divmod call.
1062 * See __iter_div_u64_rem() for another example of this.
1063 */
1064 asm("" : "+rm" (cfs_rq->load_period));
1065 cfs_rq->load_period /= 2;
1066 cfs_rq->load_avg /= 2;
1067 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001068
Paul Turnere33078b2010-11-15 15:47:04 -08001069 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
1070 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001071}
1072
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001073static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
1074{
1075 long tg_weight;
1076
1077 /*
1078 * Use this CPU's actual weight instead of the last load_contribution
1079 * to gain a more accurate current total weight. See
1080 * update_cfs_rq_load_contribution().
1081 */
1082 tg_weight = atomic_read(&tg->load_weight);
1083 tg_weight -= cfs_rq->load_contribution;
1084 tg_weight += cfs_rq->load.weight;
1085
1086 return tg_weight;
1087}
1088
Paul Turner6d5ab292011-01-21 20:45:01 -08001089static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001090{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001091 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001092
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001093 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -08001094 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001095
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001096 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001097 if (tg_weight)
1098 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001099
1100 if (shares < MIN_SHARES)
1101 shares = MIN_SHARES;
1102 if (shares > tg->shares)
1103 shares = tg->shares;
1104
1105 return shares;
1106}
1107
1108static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1109{
1110 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
1111 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08001112 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001113 }
1114}
1115# else /* CONFIG_SMP */
1116static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
1117{
1118}
1119
Paul Turner6d5ab292011-01-21 20:45:01 -08001120static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001121{
1122 return tg->shares;
1123}
1124
1125static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1126{
1127}
1128# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001129static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
1130 unsigned long weight)
1131{
Paul Turner19e5eeb2010-12-15 19:10:18 -08001132 if (se->on_rq) {
1133 /* commit outstanding execution time */
1134 if (cfs_rq->curr == se)
1135 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001136 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08001137 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001138
1139 update_load_set(&se->load, weight);
1140
1141 if (se->on_rq)
1142 account_entity_enqueue(cfs_rq, se);
1143}
1144
Paul Turner6d5ab292011-01-21 20:45:01 -08001145static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001146{
1147 struct task_group *tg;
1148 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001149 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001150
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001151 tg = cfs_rq->tg;
1152 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07001153 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001154 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001155#ifndef CONFIG_SMP
1156 if (likely(se->load.weight == tg->shares))
1157 return;
1158#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08001159 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001160
1161 reweight_entity(cfs_rq_of(se), se, shares);
1162}
1163#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -08001164static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001165{
1166}
1167
Paul Turner6d5ab292011-01-21 20:45:01 -08001168static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001169{
1170}
Paul Turner43365bd2010-12-15 19:10:17 -08001171
1172static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1173{
1174}
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001175#endif /* CONFIG_FAIR_GROUP_SCHED */
1176
Ingo Molnar2396af62007-08-09 11:16:48 +02001177static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001178{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001179#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001180 struct task_struct *tsk = NULL;
1181
1182 if (entity_is_task(se))
1183 tsk = task_of(se);
1184
Lucas De Marchi41acab82010-03-10 23:37:45 -03001185 if (se->statistics.sleep_start) {
1186 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001187
1188 if ((s64)delta < 0)
1189 delta = 0;
1190
Lucas De Marchi41acab82010-03-10 23:37:45 -03001191 if (unlikely(delta > se->statistics.sleep_max))
1192 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001193
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001194 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001195 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001196
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001197 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001198 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001199 trace_sched_stat_sleep(tsk, delta);
1200 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001201 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001202 if (se->statistics.block_start) {
1203 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001204
1205 if ((s64)delta < 0)
1206 delta = 0;
1207
Lucas De Marchi41acab82010-03-10 23:37:45 -03001208 if (unlikely(delta > se->statistics.block_max))
1209 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001210
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001211 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001212 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001213
Peter Zijlstrae4143142009-07-23 20:13:26 +02001214 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001215 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001216 se->statistics.iowait_sum += delta;
1217 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001218 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001219 }
1220
Andrew Vaginb781a602011-11-28 12:03:35 +03001221 trace_sched_stat_blocked(tsk, delta);
1222
Peter Zijlstrae4143142009-07-23 20:13:26 +02001223 /*
1224 * Blocking time is in units of nanosecs, so shift by
1225 * 20 to get a milliseconds-range estimation of the
1226 * amount of time that the task spent sleeping:
1227 */
1228 if (unlikely(prof_on == SLEEP_PROFILING)) {
1229 profile_hits(SLEEP_PROFILING,
1230 (void *)get_wchan(tsk),
1231 delta >> 20);
1232 }
1233 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001234 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001235 }
1236#endif
1237}
1238
Peter Zijlstraddc97292007-10-15 17:00:10 +02001239static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1240{
1241#ifdef CONFIG_SCHED_DEBUG
1242 s64 d = se->vruntime - cfs_rq->min_vruntime;
1243
1244 if (d < 0)
1245 d = -d;
1246
1247 if (d > 3*sysctl_sched_latency)
1248 schedstat_inc(cfs_rq, nr_spread_over);
1249#endif
1250}
1251
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001252static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001253place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1254{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001255 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001256
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001257 /*
1258 * The 'current' period is already promised to the current tasks,
1259 * however the extra weight of the new task will slow them down a
1260 * little, place the new task so that it fits in the slot that
1261 * stays open at the end.
1262 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001263 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001264 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001265
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001266 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001267 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001268 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001269
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001270 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001271 * Halve their sleep time's effect, to allow
1272 * for a gentler effect of sleepers:
1273 */
1274 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1275 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001276
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001277 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001278 }
1279
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001280 /* ensure we never gain time by being placed backwards. */
1281 vruntime = max_vruntime(se->vruntime, vruntime);
1282
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001283 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001284}
1285
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001286static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1287
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001288static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001289enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001290{
1291 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001292 * Update the normalized vruntime before updating min_vruntime
1293 * through callig update_curr().
1294 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001295 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001296 se->vruntime += cfs_rq->min_vruntime;
1297
1298 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001299 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001300 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001301 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001302 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001303 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001304 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001305
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001306 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001307 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001308 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001309 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001310
Ingo Molnard2417e52007-08-09 11:16:47 +02001311 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001312 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001313 if (se != cfs_rq->curr)
1314 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001315 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001316
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001317 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001318 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001319 check_enqueue_throttle(cfs_rq);
1320 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001321}
1322
Rik van Riel2c13c9192011-02-01 09:48:37 -05001323static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001324{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001325 for_each_sched_entity(se) {
1326 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1327 if (cfs_rq->last == se)
1328 cfs_rq->last = NULL;
1329 else
1330 break;
1331 }
1332}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001333
Rik van Riel2c13c9192011-02-01 09:48:37 -05001334static void __clear_buddies_next(struct sched_entity *se)
1335{
1336 for_each_sched_entity(se) {
1337 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1338 if (cfs_rq->next == se)
1339 cfs_rq->next = NULL;
1340 else
1341 break;
1342 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001343}
1344
Rik van Rielac53db52011-02-01 09:51:03 -05001345static void __clear_buddies_skip(struct sched_entity *se)
1346{
1347 for_each_sched_entity(se) {
1348 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1349 if (cfs_rq->skip == se)
1350 cfs_rq->skip = NULL;
1351 else
1352 break;
1353 }
1354}
1355
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001356static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1357{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001358 if (cfs_rq->last == se)
1359 __clear_buddies_last(se);
1360
1361 if (cfs_rq->next == se)
1362 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001363
1364 if (cfs_rq->skip == se)
1365 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001366}
1367
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001368static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001369
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001370static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001371dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001372{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001373 /*
1374 * Update run-time statistics of the 'current'.
1375 */
1376 update_curr(cfs_rq);
1377
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001378 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001379 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001380#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001381 if (entity_is_task(se)) {
1382 struct task_struct *tsk = task_of(se);
1383
1384 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001385 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001386 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001387 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001388 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001389#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001390 }
1391
Peter Zijlstra2002c692008-11-11 11:52:33 +01001392 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001393
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001394 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001395 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001396 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001397 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001398 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001399
1400 /*
1401 * Normalize the entity after updating the min_vruntime because the
1402 * update can refer to the ->curr item and we need to reflect this
1403 * movement in our normalized position.
1404 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001405 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001406 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001407
Paul Turnerd8b49862011-07-21 09:43:41 -07001408 /* return excess runtime on last dequeue */
1409 return_cfs_rq_runtime(cfs_rq);
1410
Peter Zijlstra1e876232011-05-17 16:21:10 -07001411 update_min_vruntime(cfs_rq);
1412 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001413}
1414
1415/*
1416 * Preempt the current task with a newly woken task if needed:
1417 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001418static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001419check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001420{
Peter Zijlstra11697832007-09-05 14:32:49 +02001421 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001422 struct sched_entity *se;
1423 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001424
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001425 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001426 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001427 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001428 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001429 /*
1430 * The current task ran long enough, ensure it doesn't get
1431 * re-elected due to buddy favours.
1432 */
1433 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001434 return;
1435 }
1436
1437 /*
1438 * Ensure that a task that missed wakeup preemption by a
1439 * narrow margin doesn't have to wait for a full slice.
1440 * This also mitigates buddy induced latencies under load.
1441 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001442 if (delta_exec < sysctl_sched_min_granularity)
1443 return;
1444
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001445 se = __pick_first_entity(cfs_rq);
1446 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001447
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001448 if (delta < 0)
1449 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001450
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001451 if (delta > ideal_runtime)
1452 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001453}
1454
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001455static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001456set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001457{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001458 /* 'current' is not kept within the tree. */
1459 if (se->on_rq) {
1460 /*
1461 * Any task has to be enqueued before it get to execute on
1462 * a CPU. So account for the time it spent waiting on the
1463 * runqueue.
1464 */
1465 update_stats_wait_end(cfs_rq, se);
1466 __dequeue_entity(cfs_rq, se);
1467 }
1468
Ingo Molnar79303e92007-08-09 11:16:47 +02001469 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001470 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001471#ifdef CONFIG_SCHEDSTATS
1472 /*
1473 * Track our maximum slice length, if the CPU's load is at
1474 * least twice that of our own weight (i.e. dont track it
1475 * when there are only lesser-weight tasks around):
1476 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001477 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001478 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001479 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1480 }
1481#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001482 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001483}
1484
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001485static int
1486wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1487
Rik van Rielac53db52011-02-01 09:51:03 -05001488/*
1489 * Pick the next process, keeping these things in mind, in this order:
1490 * 1) keep things fair between processes/task groups
1491 * 2) pick the "next" process, since someone really wants that to run
1492 * 3) pick the "last" process, for cache locality
1493 * 4) do not run the "skip" process, if something else is available
1494 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001495static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001496{
Rik van Rielac53db52011-02-01 09:51:03 -05001497 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001498 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001499
Rik van Rielac53db52011-02-01 09:51:03 -05001500 /*
1501 * Avoid running the skip buddy, if running something else can
1502 * be done without getting too unfair.
1503 */
1504 if (cfs_rq->skip == se) {
1505 struct sched_entity *second = __pick_next_entity(se);
1506 if (second && wakeup_preempt_entity(second, left) < 1)
1507 se = second;
1508 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001509
Mike Galbraithf685cea2009-10-23 23:09:22 +02001510 /*
1511 * Prefer last buddy, try to return the CPU to a preempted task.
1512 */
1513 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1514 se = cfs_rq->last;
1515
Rik van Rielac53db52011-02-01 09:51:03 -05001516 /*
1517 * Someone really wants this to run. If it's not unfair, run it.
1518 */
1519 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1520 se = cfs_rq->next;
1521
Mike Galbraithf685cea2009-10-23 23:09:22 +02001522 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001523
1524 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001525}
1526
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001527static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1528
Ingo Molnarab6cde22007-08-09 11:16:48 +02001529static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001530{
1531 /*
1532 * If still on the runqueue then deactivate_task()
1533 * was not called and update_curr() has to be done:
1534 */
1535 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001536 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001537
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001538 /* throttle cfs_rqs exceeding runtime */
1539 check_cfs_rq_runtime(cfs_rq);
1540
Peter Zijlstraddc97292007-10-15 17:00:10 +02001541 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001542 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001543 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001544 /* Put 'current' back into the tree. */
1545 __enqueue_entity(cfs_rq, prev);
1546 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001547 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001548}
1549
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001550static void
1551entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001552{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001553 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001554 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001555 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001556 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001557
Paul Turner43365bd2010-12-15 19:10:17 -08001558 /*
1559 * Update share accounting for long-running entities.
1560 */
1561 update_entity_shares_tick(cfs_rq);
1562
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001563#ifdef CONFIG_SCHED_HRTICK
1564 /*
1565 * queued ticks are scheduled to match the slice, so don't bother
1566 * validating it and just reschedule.
1567 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001568 if (queued) {
1569 resched_task(rq_of(cfs_rq)->curr);
1570 return;
1571 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001572 /*
1573 * don't let the period tick interfere with the hrtick preemption
1574 */
1575 if (!sched_feat(DOUBLE_TICK) &&
1576 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1577 return;
1578#endif
1579
Yong Zhang2c2efae2011-07-29 16:20:33 +08001580 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001581 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001582}
1583
Paul Turnerab84d312011-07-21 09:43:28 -07001584
1585/**************************************************
1586 * CFS bandwidth control machinery
1587 */
1588
1589#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001590
1591#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001592static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001593
1594static inline bool cfs_bandwidth_used(void)
1595{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001596 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001597}
1598
1599void account_cfs_bandwidth_used(int enabled, int was_enabled)
1600{
1601 /* only need to count groups transitioning between enabled/!enabled */
1602 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001603 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001604 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001605 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001606}
1607#else /* HAVE_JUMP_LABEL */
1608static bool cfs_bandwidth_used(void)
1609{
1610 return true;
1611}
1612
1613void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1614#endif /* HAVE_JUMP_LABEL */
1615
Paul Turnerab84d312011-07-21 09:43:28 -07001616/*
1617 * default period for cfs group bandwidth.
1618 * default: 0.1s, units: nanoseconds
1619 */
1620static inline u64 default_cfs_period(void)
1621{
1622 return 100000000ULL;
1623}
Paul Turnerec12cb72011-07-21 09:43:30 -07001624
1625static inline u64 sched_cfs_bandwidth_slice(void)
1626{
1627 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1628}
1629
Paul Turnera9cf55b2011-07-21 09:43:32 -07001630/*
1631 * Replenish runtime according to assigned quota and update expiration time.
1632 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1633 * additional synchronization around rq->lock.
1634 *
1635 * requires cfs_b->lock
1636 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001637void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001638{
1639 u64 now;
1640
1641 if (cfs_b->quota == RUNTIME_INF)
1642 return;
1643
1644 now = sched_clock_cpu(smp_processor_id());
1645 cfs_b->runtime = cfs_b->quota;
1646 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1647}
1648
Peter Zijlstra029632f2011-10-25 10:00:11 +02001649static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1650{
1651 return &tg->cfs_bandwidth;
1652}
1653
Paul Turner85dac902011-07-21 09:43:33 -07001654/* returns 0 on failure to allocate runtime */
1655static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001656{
1657 struct task_group *tg = cfs_rq->tg;
1658 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001659 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001660
1661 /* note: this is a positive sum as runtime_remaining <= 0 */
1662 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1663
1664 raw_spin_lock(&cfs_b->lock);
1665 if (cfs_b->quota == RUNTIME_INF)
1666 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001667 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001668 /*
1669 * If the bandwidth pool has become inactive, then at least one
1670 * period must have elapsed since the last consumption.
1671 * Refresh the global state and ensure bandwidth timer becomes
1672 * active.
1673 */
1674 if (!cfs_b->timer_active) {
1675 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001676 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001677 }
Paul Turner58088ad2011-07-21 09:43:31 -07001678
1679 if (cfs_b->runtime > 0) {
1680 amount = min(cfs_b->runtime, min_amount);
1681 cfs_b->runtime -= amount;
1682 cfs_b->idle = 0;
1683 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001684 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001685 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001686 raw_spin_unlock(&cfs_b->lock);
1687
1688 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001689 /*
1690 * we may have advanced our local expiration to account for allowed
1691 * spread between our sched_clock and the one on which runtime was
1692 * issued.
1693 */
1694 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1695 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001696
1697 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001698}
1699
1700/*
1701 * Note: This depends on the synchronization provided by sched_clock and the
1702 * fact that rq->clock snapshots this value.
1703 */
1704static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1705{
1706 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1707 struct rq *rq = rq_of(cfs_rq);
1708
1709 /* if the deadline is ahead of our clock, nothing to do */
1710 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1711 return;
1712
1713 if (cfs_rq->runtime_remaining < 0)
1714 return;
1715
1716 /*
1717 * If the local deadline has passed we have to consider the
1718 * possibility that our sched_clock is 'fast' and the global deadline
1719 * has not truly expired.
1720 *
1721 * Fortunately we can check determine whether this the case by checking
1722 * whether the global deadline has advanced.
1723 */
1724
1725 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1726 /* extend local deadline, drift is bounded above by 2 ticks */
1727 cfs_rq->runtime_expires += TICK_NSEC;
1728 } else {
1729 /* global deadline is ahead, expiration has passed */
1730 cfs_rq->runtime_remaining = 0;
1731 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001732}
1733
1734static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1735 unsigned long delta_exec)
1736{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001737 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001738 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001739 expire_cfs_rq_runtime(cfs_rq);
1740
1741 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001742 return;
1743
Paul Turner85dac902011-07-21 09:43:33 -07001744 /*
1745 * if we're unable to extend our runtime we resched so that the active
1746 * hierarchy can be throttled
1747 */
1748 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1749 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001750}
1751
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001752static __always_inline
1753void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001754{
Paul Turner56f570e2011-11-07 20:26:33 -08001755 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001756 return;
1757
1758 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1759}
1760
Paul Turner85dac902011-07-21 09:43:33 -07001761static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1762{
Paul Turner56f570e2011-11-07 20:26:33 -08001763 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001764}
1765
Paul Turner64660c82011-07-21 09:43:36 -07001766/* check whether cfs_rq, or any parent, is throttled */
1767static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1768{
Paul Turner56f570e2011-11-07 20:26:33 -08001769 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001770}
1771
1772/*
1773 * Ensure that neither of the group entities corresponding to src_cpu or
1774 * dest_cpu are members of a throttled hierarchy when performing group
1775 * load-balance operations.
1776 */
1777static inline int throttled_lb_pair(struct task_group *tg,
1778 int src_cpu, int dest_cpu)
1779{
1780 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1781
1782 src_cfs_rq = tg->cfs_rq[src_cpu];
1783 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1784
1785 return throttled_hierarchy(src_cfs_rq) ||
1786 throttled_hierarchy(dest_cfs_rq);
1787}
1788
1789/* updated child weight may affect parent so we have to do this bottom up */
1790static int tg_unthrottle_up(struct task_group *tg, void *data)
1791{
1792 struct rq *rq = data;
1793 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1794
1795 cfs_rq->throttle_count--;
1796#ifdef CONFIG_SMP
1797 if (!cfs_rq->throttle_count) {
1798 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1799
1800 /* leaving throttled state, advance shares averaging windows */
1801 cfs_rq->load_stamp += delta;
1802 cfs_rq->load_last += delta;
1803
1804 /* update entity weight now that we are on_rq again */
1805 update_cfs_shares(cfs_rq);
1806 }
1807#endif
1808
1809 return 0;
1810}
1811
1812static int tg_throttle_down(struct task_group *tg, void *data)
1813{
1814 struct rq *rq = data;
1815 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1816
1817 /* group is entering throttled state, record last load */
1818 if (!cfs_rq->throttle_count)
1819 update_cfs_load(cfs_rq, 0);
1820 cfs_rq->throttle_count++;
1821
1822 return 0;
1823}
1824
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001825static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001826{
1827 struct rq *rq = rq_of(cfs_rq);
1828 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1829 struct sched_entity *se;
1830 long task_delta, dequeue = 1;
1831
1832 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1833
1834 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001835 rcu_read_lock();
1836 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1837 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001838
1839 task_delta = cfs_rq->h_nr_running;
1840 for_each_sched_entity(se) {
1841 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1842 /* throttled entity or throttle-on-deactivate */
1843 if (!se->on_rq)
1844 break;
1845
1846 if (dequeue)
1847 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1848 qcfs_rq->h_nr_running -= task_delta;
1849
1850 if (qcfs_rq->load.weight)
1851 dequeue = 0;
1852 }
1853
1854 if (!se)
1855 rq->nr_running -= task_delta;
1856
1857 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001858 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001859 raw_spin_lock(&cfs_b->lock);
1860 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1861 raw_spin_unlock(&cfs_b->lock);
1862}
1863
Peter Zijlstra029632f2011-10-25 10:00:11 +02001864void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001865{
1866 struct rq *rq = rq_of(cfs_rq);
1867 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1868 struct sched_entity *se;
1869 int enqueue = 1;
1870 long task_delta;
1871
1872 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1873
1874 cfs_rq->throttled = 0;
1875 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001876 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001877 list_del_rcu(&cfs_rq->throttled_list);
1878 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001879 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001880
Paul Turner64660c82011-07-21 09:43:36 -07001881 update_rq_clock(rq);
1882 /* update hierarchical throttle state */
1883 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1884
Paul Turner671fd9d2011-07-21 09:43:34 -07001885 if (!cfs_rq->load.weight)
1886 return;
1887
1888 task_delta = cfs_rq->h_nr_running;
1889 for_each_sched_entity(se) {
1890 if (se->on_rq)
1891 enqueue = 0;
1892
1893 cfs_rq = cfs_rq_of(se);
1894 if (enqueue)
1895 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1896 cfs_rq->h_nr_running += task_delta;
1897
1898 if (cfs_rq_throttled(cfs_rq))
1899 break;
1900 }
1901
1902 if (!se)
1903 rq->nr_running += task_delta;
1904
1905 /* determine whether we need to wake up potentially idle cpu */
1906 if (rq->curr == rq->idle && rq->cfs.nr_running)
1907 resched_task(rq->curr);
1908}
1909
1910static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1911 u64 remaining, u64 expires)
1912{
1913 struct cfs_rq *cfs_rq;
1914 u64 runtime = remaining;
1915
1916 rcu_read_lock();
1917 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1918 throttled_list) {
1919 struct rq *rq = rq_of(cfs_rq);
1920
1921 raw_spin_lock(&rq->lock);
1922 if (!cfs_rq_throttled(cfs_rq))
1923 goto next;
1924
1925 runtime = -cfs_rq->runtime_remaining + 1;
1926 if (runtime > remaining)
1927 runtime = remaining;
1928 remaining -= runtime;
1929
1930 cfs_rq->runtime_remaining += runtime;
1931 cfs_rq->runtime_expires = expires;
1932
1933 /* we check whether we're throttled above */
1934 if (cfs_rq->runtime_remaining > 0)
1935 unthrottle_cfs_rq(cfs_rq);
1936
1937next:
1938 raw_spin_unlock(&rq->lock);
1939
1940 if (!remaining)
1941 break;
1942 }
1943 rcu_read_unlock();
1944
1945 return remaining;
1946}
1947
Paul Turner58088ad2011-07-21 09:43:31 -07001948/*
1949 * Responsible for refilling a task_group's bandwidth and unthrottling its
1950 * cfs_rqs as appropriate. If there has been no activity within the last
1951 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1952 * used to track this state.
1953 */
1954static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1955{
Paul Turner671fd9d2011-07-21 09:43:34 -07001956 u64 runtime, runtime_expires;
1957 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001958
1959 raw_spin_lock(&cfs_b->lock);
1960 /* no need to continue the timer with no bandwidth constraint */
1961 if (cfs_b->quota == RUNTIME_INF)
1962 goto out_unlock;
1963
Paul Turner671fd9d2011-07-21 09:43:34 -07001964 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1965 /* idle depends on !throttled (for the case of a large deficit) */
1966 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001967 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001968
Paul Turnera9cf55b2011-07-21 09:43:32 -07001969 /* if we're going inactive then everything else can be deferred */
1970 if (idle)
1971 goto out_unlock;
1972
1973 __refill_cfs_bandwidth_runtime(cfs_b);
1974
Paul Turner671fd9d2011-07-21 09:43:34 -07001975 if (!throttled) {
1976 /* mark as potentially idle for the upcoming period */
1977 cfs_b->idle = 1;
1978 goto out_unlock;
1979 }
Paul Turner58088ad2011-07-21 09:43:31 -07001980
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001981 /* account preceding periods in which throttling occurred */
1982 cfs_b->nr_throttled += overrun;
1983
Paul Turner671fd9d2011-07-21 09:43:34 -07001984 /*
1985 * There are throttled entities so we must first use the new bandwidth
1986 * to unthrottle them before making it generally available. This
1987 * ensures that all existing debts will be paid before a new cfs_rq is
1988 * allowed to run.
1989 */
1990 runtime = cfs_b->runtime;
1991 runtime_expires = cfs_b->runtime_expires;
1992 cfs_b->runtime = 0;
1993
1994 /*
1995 * This check is repeated as we are holding onto the new bandwidth
1996 * while we unthrottle. This can potentially race with an unthrottled
1997 * group trying to acquire new bandwidth from the global pool.
1998 */
1999 while (throttled && runtime > 0) {
2000 raw_spin_unlock(&cfs_b->lock);
2001 /* we can't nest cfs_b->lock while distributing bandwidth */
2002 runtime = distribute_cfs_runtime(cfs_b, runtime,
2003 runtime_expires);
2004 raw_spin_lock(&cfs_b->lock);
2005
2006 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2007 }
2008
2009 /* return (any) remaining runtime */
2010 cfs_b->runtime = runtime;
2011 /*
2012 * While we are ensured activity in the period following an
2013 * unthrottle, this also covers the case in which the new bandwidth is
2014 * insufficient to cover the existing bandwidth deficit. (Forcing the
2015 * timer to remain active while there are any throttled entities.)
2016 */
2017 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002018out_unlock:
2019 if (idle)
2020 cfs_b->timer_active = 0;
2021 raw_spin_unlock(&cfs_b->lock);
2022
2023 return idle;
2024}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002025
Paul Turnerd8b49862011-07-21 09:43:41 -07002026/* a cfs_rq won't donate quota below this amount */
2027static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2028/* minimum remaining period time to redistribute slack quota */
2029static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2030/* how long we wait to gather additional slack before distributing */
2031static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2032
2033/* are we near the end of the current quota period? */
2034static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2035{
2036 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2037 u64 remaining;
2038
2039 /* if the call-back is running a quota refresh is already occurring */
2040 if (hrtimer_callback_running(refresh_timer))
2041 return 1;
2042
2043 /* is a quota refresh about to occur? */
2044 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2045 if (remaining < min_expire)
2046 return 1;
2047
2048 return 0;
2049}
2050
2051static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2052{
2053 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2054
2055 /* if there's a quota refresh soon don't bother with slack */
2056 if (runtime_refresh_within(cfs_b, min_left))
2057 return;
2058
2059 start_bandwidth_timer(&cfs_b->slack_timer,
2060 ns_to_ktime(cfs_bandwidth_slack_period));
2061}
2062
2063/* we know any runtime found here is valid as update_curr() precedes return */
2064static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2065{
2066 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2067 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2068
2069 if (slack_runtime <= 0)
2070 return;
2071
2072 raw_spin_lock(&cfs_b->lock);
2073 if (cfs_b->quota != RUNTIME_INF &&
2074 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2075 cfs_b->runtime += slack_runtime;
2076
2077 /* we are under rq->lock, defer unthrottling using a timer */
2078 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2079 !list_empty(&cfs_b->throttled_cfs_rq))
2080 start_cfs_slack_bandwidth(cfs_b);
2081 }
2082 raw_spin_unlock(&cfs_b->lock);
2083
2084 /* even if it's not valid for return we don't want to try again */
2085 cfs_rq->runtime_remaining -= slack_runtime;
2086}
2087
2088static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2089{
Paul Turner56f570e2011-11-07 20:26:33 -08002090 if (!cfs_bandwidth_used())
2091 return;
2092
Paul Turnerfccfdc62011-11-07 20:26:34 -08002093 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002094 return;
2095
2096 __return_cfs_rq_runtime(cfs_rq);
2097}
2098
2099/*
2100 * This is done with a timer (instead of inline with bandwidth return) since
2101 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2102 */
2103static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2104{
2105 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2106 u64 expires;
2107
2108 /* confirm we're still not at a refresh boundary */
2109 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2110 return;
2111
2112 raw_spin_lock(&cfs_b->lock);
2113 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2114 runtime = cfs_b->runtime;
2115 cfs_b->runtime = 0;
2116 }
2117 expires = cfs_b->runtime_expires;
2118 raw_spin_unlock(&cfs_b->lock);
2119
2120 if (!runtime)
2121 return;
2122
2123 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2124
2125 raw_spin_lock(&cfs_b->lock);
2126 if (expires == cfs_b->runtime_expires)
2127 cfs_b->runtime = runtime;
2128 raw_spin_unlock(&cfs_b->lock);
2129}
2130
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002131/*
2132 * When a group wakes up we want to make sure that its quota is not already
2133 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2134 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2135 */
2136static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2137{
Paul Turner56f570e2011-11-07 20:26:33 -08002138 if (!cfs_bandwidth_used())
2139 return;
2140
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002141 /* an active group must be handled by the update_curr()->put() path */
2142 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2143 return;
2144
2145 /* ensure the group is not already throttled */
2146 if (cfs_rq_throttled(cfs_rq))
2147 return;
2148
2149 /* update runtime allocation */
2150 account_cfs_rq_runtime(cfs_rq, 0);
2151 if (cfs_rq->runtime_remaining <= 0)
2152 throttle_cfs_rq(cfs_rq);
2153}
2154
2155/* conditionally throttle active cfs_rq's from put_prev_entity() */
2156static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2157{
Paul Turner56f570e2011-11-07 20:26:33 -08002158 if (!cfs_bandwidth_used())
2159 return;
2160
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002161 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2162 return;
2163
2164 /*
2165 * it's possible for a throttled entity to be forced into a running
2166 * state (e.g. set_curr_task), in this case we're finished.
2167 */
2168 if (cfs_rq_throttled(cfs_rq))
2169 return;
2170
2171 throttle_cfs_rq(cfs_rq);
2172}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002173
2174static inline u64 default_cfs_period(void);
2175static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2176static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2177
2178static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2179{
2180 struct cfs_bandwidth *cfs_b =
2181 container_of(timer, struct cfs_bandwidth, slack_timer);
2182 do_sched_cfs_slack_timer(cfs_b);
2183
2184 return HRTIMER_NORESTART;
2185}
2186
2187static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2188{
2189 struct cfs_bandwidth *cfs_b =
2190 container_of(timer, struct cfs_bandwidth, period_timer);
2191 ktime_t now;
2192 int overrun;
2193 int idle = 0;
2194
2195 for (;;) {
2196 now = hrtimer_cb_get_time(timer);
2197 overrun = hrtimer_forward(timer, now, cfs_b->period);
2198
2199 if (!overrun)
2200 break;
2201
2202 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2203 }
2204
2205 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2206}
2207
2208void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2209{
2210 raw_spin_lock_init(&cfs_b->lock);
2211 cfs_b->runtime = 0;
2212 cfs_b->quota = RUNTIME_INF;
2213 cfs_b->period = ns_to_ktime(default_cfs_period());
2214
2215 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2216 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2217 cfs_b->period_timer.function = sched_cfs_period_timer;
2218 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2219 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2220}
2221
2222static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2223{
2224 cfs_rq->runtime_enabled = 0;
2225 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2226}
2227
2228/* requires cfs_b->lock, may release to reprogram timer */
2229void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2230{
2231 /*
2232 * The timer may be active because we're trying to set a new bandwidth
2233 * period or because we're racing with the tear-down path
2234 * (timer_active==0 becomes visible before the hrtimer call-back
2235 * terminates). In either case we ensure that it's re-programmed
2236 */
2237 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2238 raw_spin_unlock(&cfs_b->lock);
2239 /* ensure cfs_b->lock is available while we wait */
2240 hrtimer_cancel(&cfs_b->period_timer);
2241
2242 raw_spin_lock(&cfs_b->lock);
2243 /* if someone else restarted the timer then we're done */
2244 if (cfs_b->timer_active)
2245 return;
2246 }
2247
2248 cfs_b->timer_active = 1;
2249 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2250}
2251
2252static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2253{
2254 hrtimer_cancel(&cfs_b->period_timer);
2255 hrtimer_cancel(&cfs_b->slack_timer);
2256}
2257
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002258static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002259{
2260 struct cfs_rq *cfs_rq;
2261
2262 for_each_leaf_cfs_rq(rq, cfs_rq) {
2263 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2264
2265 if (!cfs_rq->runtime_enabled)
2266 continue;
2267
2268 /*
2269 * clock_task is not advancing so we just need to make sure
2270 * there's some valid quota amount
2271 */
2272 cfs_rq->runtime_remaining = cfs_b->quota;
2273 if (cfs_rq_throttled(cfs_rq))
2274 unthrottle_cfs_rq(cfs_rq);
2275 }
2276}
2277
2278#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002279static __always_inline
2280void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002281static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2282static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002283static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002284
2285static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2286{
2287 return 0;
2288}
Paul Turner64660c82011-07-21 09:43:36 -07002289
2290static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2291{
2292 return 0;
2293}
2294
2295static inline int throttled_lb_pair(struct task_group *tg,
2296 int src_cpu, int dest_cpu)
2297{
2298 return 0;
2299}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002300
2301void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2302
2303#ifdef CONFIG_FAIR_GROUP_SCHED
2304static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002305#endif
2306
Peter Zijlstra029632f2011-10-25 10:00:11 +02002307static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2308{
2309 return NULL;
2310}
2311static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002312static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002313
2314#endif /* CONFIG_CFS_BANDWIDTH */
2315
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002316/**************************************************
2317 * CFS operations on tasks:
2318 */
2319
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002320#ifdef CONFIG_SCHED_HRTICK
2321static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2322{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002323 struct sched_entity *se = &p->se;
2324 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2325
2326 WARN_ON(task_rq(p) != rq);
2327
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002328 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002329 u64 slice = sched_slice(cfs_rq, se);
2330 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2331 s64 delta = slice - ran;
2332
2333 if (delta < 0) {
2334 if (rq->curr == p)
2335 resched_task(p);
2336 return;
2337 }
2338
2339 /*
2340 * Don't schedule slices shorter than 10000ns, that just
2341 * doesn't make sense. Rely on vruntime for fairness.
2342 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002343 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002344 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002345
Peter Zijlstra31656512008-07-18 18:01:23 +02002346 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002347 }
2348}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002349
2350/*
2351 * called from enqueue/dequeue and updates the hrtick when the
2352 * current task is from our class and nr_running is low enough
2353 * to matter.
2354 */
2355static void hrtick_update(struct rq *rq)
2356{
2357 struct task_struct *curr = rq->curr;
2358
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002359 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002360 return;
2361
2362 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2363 hrtick_start_fair(rq, curr);
2364}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302365#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002366static inline void
2367hrtick_start_fair(struct rq *rq, struct task_struct *p)
2368{
2369}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002370
2371static inline void hrtick_update(struct rq *rq)
2372{
2373}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002374#endif
2375
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002376/*
2377 * The enqueue_task method is called before nr_running is
2378 * increased. Here we update the fair scheduling stats and
2379 * then put the task into the rbtree:
2380 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002381static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002382enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002383{
2384 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002385 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002386
2387 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002388 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002389 break;
2390 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002391 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002392
2393 /*
2394 * end evaluation on encountering a throttled cfs_rq
2395 *
2396 * note: in the case of encountering a throttled cfs_rq we will
2397 * post the final h_nr_running increment below.
2398 */
2399 if (cfs_rq_throttled(cfs_rq))
2400 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002401 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002402
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002403 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002404 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002405
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002406 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002407 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002408 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002409
Paul Turner85dac902011-07-21 09:43:33 -07002410 if (cfs_rq_throttled(cfs_rq))
2411 break;
2412
Paul Turnerd6b55912010-11-15 15:47:09 -08002413 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002414 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002415 }
2416
Paul Turner85dac902011-07-21 09:43:33 -07002417 if (!se)
2418 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002419 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002420}
2421
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002422static void set_next_buddy(struct sched_entity *se);
2423
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002424/*
2425 * The dequeue_task method is called before nr_running is
2426 * decreased. We remove the task from the rbtree and
2427 * update the fair scheduling stats:
2428 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002429static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002430{
2431 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002432 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002433 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002434
2435 for_each_sched_entity(se) {
2436 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002437 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002438
2439 /*
2440 * end evaluation on encountering a throttled cfs_rq
2441 *
2442 * note: in the case of encountering a throttled cfs_rq we will
2443 * post the final h_nr_running decrement below.
2444 */
2445 if (cfs_rq_throttled(cfs_rq))
2446 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002447 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002448
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002449 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002450 if (cfs_rq->load.weight) {
2451 /*
2452 * Bias pick_next to pick a task from this cfs_rq, as
2453 * p is sleeping when it is within its sched_slice.
2454 */
2455 if (task_sleep && parent_entity(se))
2456 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002457
2458 /* avoid re-evaluating load for this entity */
2459 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002460 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002461 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002462 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002463 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002464
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002465 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002466 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002467 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002468
Paul Turner85dac902011-07-21 09:43:33 -07002469 if (cfs_rq_throttled(cfs_rq))
2470 break;
2471
Paul Turnerd6b55912010-11-15 15:47:09 -08002472 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002473 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002474 }
2475
Paul Turner85dac902011-07-21 09:43:33 -07002476 if (!se)
2477 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002478 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002479}
2480
Gregory Haskinse7693a32008-01-25 21:08:09 +01002481#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002482/* Used instead of source_load when we know the type == 0 */
2483static unsigned long weighted_cpuload(const int cpu)
2484{
2485 return cpu_rq(cpu)->load.weight;
2486}
2487
2488/*
2489 * Return a low guess at the load of a migration-source cpu weighted
2490 * according to the scheduling class and "nice" value.
2491 *
2492 * We want to under-estimate the load of migration sources, to
2493 * balance conservatively.
2494 */
2495static unsigned long source_load(int cpu, int type)
2496{
2497 struct rq *rq = cpu_rq(cpu);
2498 unsigned long total = weighted_cpuload(cpu);
2499
2500 if (type == 0 || !sched_feat(LB_BIAS))
2501 return total;
2502
2503 return min(rq->cpu_load[type-1], total);
2504}
2505
2506/*
2507 * Return a high guess at the load of a migration-target cpu weighted
2508 * according to the scheduling class and "nice" value.
2509 */
2510static unsigned long target_load(int cpu, int type)
2511{
2512 struct rq *rq = cpu_rq(cpu);
2513 unsigned long total = weighted_cpuload(cpu);
2514
2515 if (type == 0 || !sched_feat(LB_BIAS))
2516 return total;
2517
2518 return max(rq->cpu_load[type-1], total);
2519}
2520
2521static unsigned long power_of(int cpu)
2522{
2523 return cpu_rq(cpu)->cpu_power;
2524}
2525
2526static unsigned long cpu_avg_load_per_task(int cpu)
2527{
2528 struct rq *rq = cpu_rq(cpu);
2529 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2530
2531 if (nr_running)
2532 return rq->load.weight / nr_running;
2533
2534 return 0;
2535}
2536
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002537
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002538static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002539{
2540 struct sched_entity *se = &p->se;
2541 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002542 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002543
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002544#ifndef CONFIG_64BIT
2545 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002546
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002547 do {
2548 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2549 smp_rmb();
2550 min_vruntime = cfs_rq->min_vruntime;
2551 } while (min_vruntime != min_vruntime_copy);
2552#else
2553 min_vruntime = cfs_rq->min_vruntime;
2554#endif
2555
2556 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002557}
2558
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002559#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002560/*
2561 * effective_load() calculates the load change as seen from the root_task_group
2562 *
2563 * Adding load to a group doesn't make a group heavier, but can cause movement
2564 * of group shares between cpus. Assuming the shares were perfectly aligned one
2565 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002566 *
2567 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2568 * on this @cpu and results in a total addition (subtraction) of @wg to the
2569 * total group weight.
2570 *
2571 * Given a runqueue weight distribution (rw_i) we can compute a shares
2572 * distribution (s_i) using:
2573 *
2574 * s_i = rw_i / \Sum rw_j (1)
2575 *
2576 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2577 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2578 * shares distribution (s_i):
2579 *
2580 * rw_i = { 2, 4, 1, 0 }
2581 * s_i = { 2/7, 4/7, 1/7, 0 }
2582 *
2583 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2584 * task used to run on and the CPU the waker is running on), we need to
2585 * compute the effect of waking a task on either CPU and, in case of a sync
2586 * wakeup, compute the effect of the current task going to sleep.
2587 *
2588 * So for a change of @wl to the local @cpu with an overall group weight change
2589 * of @wl we can compute the new shares distribution (s'_i) using:
2590 *
2591 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2592 *
2593 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2594 * differences in waking a task to CPU 0. The additional task changes the
2595 * weight and shares distributions like:
2596 *
2597 * rw'_i = { 3, 4, 1, 0 }
2598 * s'_i = { 3/8, 4/8, 1/8, 0 }
2599 *
2600 * We can then compute the difference in effective weight by using:
2601 *
2602 * dw_i = S * (s'_i - s_i) (3)
2603 *
2604 * Where 'S' is the group weight as seen by its parent.
2605 *
2606 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2607 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2608 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002609 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002610static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002611{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002612 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002613
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002614 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002615 return wl;
2616
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002617 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002618 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002619
Paul Turner977dda72011-01-14 17:57:50 -08002620 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002621
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002622 /*
2623 * W = @wg + \Sum rw_j
2624 */
2625 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002626
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002627 /*
2628 * w = rw_i + @wl
2629 */
2630 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002631
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002632 /*
2633 * wl = S * s'_i; see (2)
2634 */
2635 if (W > 0 && w < W)
2636 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002637 else
2638 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002639
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002640 /*
2641 * Per the above, wl is the new se->load.weight value; since
2642 * those are clipped to [MIN_SHARES, ...) do so now. See
2643 * calc_cfs_shares().
2644 */
Paul Turner977dda72011-01-14 17:57:50 -08002645 if (wl < MIN_SHARES)
2646 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002647
2648 /*
2649 * wl = dw_i = S * (s'_i - s_i); see (3)
2650 */
Paul Turner977dda72011-01-14 17:57:50 -08002651 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002652
2653 /*
2654 * Recursively apply this logic to all parent groups to compute
2655 * the final effective load change on the root group. Since
2656 * only the @tg group gets extra weight, all parent groups can
2657 * only redistribute existing shares. @wl is the shift in shares
2658 * resulting from this level per the above.
2659 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002660 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002661 }
2662
2663 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002664}
2665#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002666
Peter Zijlstra83378262008-06-27 13:41:37 +02002667static inline unsigned long effective_load(struct task_group *tg, int cpu,
2668 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002669{
Peter Zijlstra83378262008-06-27 13:41:37 +02002670 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002671}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002672
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002673#endif
2674
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002675static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002676{
Paul Turnere37b6a72011-01-21 20:44:59 -08002677 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002678 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002679 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002680 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002681 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002682 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002683
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002684 idx = sd->wake_idx;
2685 this_cpu = smp_processor_id();
2686 prev_cpu = task_cpu(p);
2687 load = source_load(prev_cpu, idx);
2688 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002689
2690 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002691 * If sync wakeup then subtract the (maximum possible)
2692 * effect of the currently running task from the load
2693 * of the current CPU:
2694 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002695 if (sync) {
2696 tg = task_group(current);
2697 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002698
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002699 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002700 load += effective_load(tg, prev_cpu, 0, -weight);
2701 }
2702
2703 tg = task_group(p);
2704 weight = p->se.load.weight;
2705
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002706 /*
2707 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002708 * due to the sync cause above having dropped this_load to 0, we'll
2709 * always have an imbalance, but there's really nothing you can do
2710 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002711 *
2712 * Otherwise check if either cpus are near enough in load to allow this
2713 * task to be woken on this_cpu.
2714 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002715 if (this_load > 0) {
2716 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002717
2718 this_eff_load = 100;
2719 this_eff_load *= power_of(prev_cpu);
2720 this_eff_load *= this_load +
2721 effective_load(tg, this_cpu, weight, weight);
2722
2723 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2724 prev_eff_load *= power_of(this_cpu);
2725 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2726
2727 balanced = this_eff_load <= prev_eff_load;
2728 } else
2729 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002730
2731 /*
2732 * If the currently running task will sleep within
2733 * a reasonable amount of time then attract this newly
2734 * woken task:
2735 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002736 if (sync && balanced)
2737 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002738
Lucas De Marchi41acab82010-03-10 23:37:45 -03002739 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002740 tl_per_task = cpu_avg_load_per_task(this_cpu);
2741
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002742 if (balanced ||
2743 (this_load <= load &&
2744 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002745 /*
2746 * This domain has SD_WAKE_AFFINE and
2747 * p is cache cold in this domain, and
2748 * there is no bad imbalance.
2749 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002750 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002751 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002752
2753 return 1;
2754 }
2755 return 0;
2756}
2757
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002758/*
2759 * find_idlest_group finds and returns the least busy CPU group within the
2760 * domain.
2761 */
2762static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002763find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002764 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002765{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002766 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002767 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002768 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002769
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002770 do {
2771 unsigned long load, avg_load;
2772 int local_group;
2773 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002774
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002775 /* Skip over this group if it has no CPUs allowed */
2776 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002777 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002778 continue;
2779
2780 local_group = cpumask_test_cpu(this_cpu,
2781 sched_group_cpus(group));
2782
2783 /* Tally up the load of all CPUs in the group */
2784 avg_load = 0;
2785
2786 for_each_cpu(i, sched_group_cpus(group)) {
2787 /* Bias balancing toward cpus of our domain */
2788 if (local_group)
2789 load = source_load(i, load_idx);
2790 else
2791 load = target_load(i, load_idx);
2792
2793 avg_load += load;
2794 }
2795
2796 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002797 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002798
2799 if (local_group) {
2800 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002801 } else if (avg_load < min_load) {
2802 min_load = avg_load;
2803 idlest = group;
2804 }
2805 } while (group = group->next, group != sd->groups);
2806
2807 if (!idlest || 100*this_load < imbalance*min_load)
2808 return NULL;
2809 return idlest;
2810}
2811
2812/*
2813 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2814 */
2815static int
2816find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2817{
2818 unsigned long load, min_load = ULONG_MAX;
2819 int idlest = -1;
2820 int i;
2821
2822 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002823 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002824 load = weighted_cpuload(i);
2825
2826 if (load < min_load || (load == min_load && i == this_cpu)) {
2827 min_load = load;
2828 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002829 }
2830 }
2831
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002832 return idlest;
2833}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002834
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002835/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002836 * Try and locate an idle CPU in the sched_domain.
2837 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002838static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002839{
2840 int cpu = smp_processor_id();
2841 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002842 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07002843 struct sched_group *sg;
2844 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002845
2846 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002847 * If the task is going to be woken-up on this cpu and if it is
2848 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002849 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002850 if (target == cpu && idle_cpu(cpu))
2851 return cpu;
2852
2853 /*
2854 * If the task is going to be woken-up on the cpu where it previously
2855 * ran and if it is currently idle, then it the right target.
2856 */
2857 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002858 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002859
2860 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07002861 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002862 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002863 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002864 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07002865 sg = sd->groups;
2866 do {
2867 if (!cpumask_intersects(sched_group_cpus(sg),
2868 tsk_cpus_allowed(p)))
2869 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02002870
Linus Torvalds37407ea2012-09-16 12:29:43 -07002871 for_each_cpu(i, sched_group_cpus(sg)) {
2872 if (!idle_cpu(i))
2873 goto next;
2874 }
2875
2876 target = cpumask_first_and(sched_group_cpus(sg),
2877 tsk_cpus_allowed(p));
2878 goto done;
2879next:
2880 sg = sg->next;
2881 } while (sg != sd->groups);
2882 }
2883done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002884 return target;
2885}
2886
2887/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002888 * sched_balance_self: balance the current task (running on cpu) in domains
2889 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2890 * SD_BALANCE_EXEC.
2891 *
2892 * Balance, ie. select the least loaded group.
2893 *
2894 * Returns the target CPU number, or the same CPU if no balancing is needed.
2895 *
2896 * preempt must be disabled.
2897 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002898static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002899select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002900{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002901 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002902 int cpu = smp_processor_id();
2903 int prev_cpu = task_cpu(p);
2904 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002905 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002906 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002907
Peter Zijlstra29baa742012-04-23 12:11:21 +02002908 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01002909 return prev_cpu;
2910
Peter Zijlstra0763a662009-09-14 19:37:39 +02002911 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002912 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002913 want_affine = 1;
2914 new_cpu = prev_cpu;
2915 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002916
Peter Zijlstradce840a2011-04-07 14:09:50 +02002917 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002918 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01002919 if (!(tmp->flags & SD_LOAD_BALANCE))
2920 continue;
2921
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002922 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002923 * If both cpu and prev_cpu are part of this domain,
2924 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002925 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002926 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2927 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2928 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08002929 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002930 }
2931
Alex Shif03542a2012-07-26 08:55:34 +08002932 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002933 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002934 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002935
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002936 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08002937 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002938 prev_cpu = cpu;
2939
2940 new_cpu = select_idle_sibling(p, prev_cpu);
2941 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002942 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002943
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002944 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002945 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002946 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002947 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002948
Peter Zijlstra0763a662009-09-14 19:37:39 +02002949 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002950 sd = sd->child;
2951 continue;
2952 }
2953
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002954 if (sd_flag & SD_BALANCE_WAKE)
2955 load_idx = sd->wake_idx;
2956
2957 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002958 if (!group) {
2959 sd = sd->child;
2960 continue;
2961 }
2962
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002963 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002964 if (new_cpu == -1 || new_cpu == cpu) {
2965 /* Now try balancing at a lower domain level of cpu */
2966 sd = sd->child;
2967 continue;
2968 }
2969
2970 /* Now try balancing at a lower domain level of new_cpu */
2971 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002972 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002973 sd = NULL;
2974 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002975 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002976 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002977 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002978 sd = tmp;
2979 }
2980 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002981 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002982unlock:
2983 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002984
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002985 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002986}
2987#endif /* CONFIG_SMP */
2988
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002989static unsigned long
2990wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002991{
2992 unsigned long gran = sysctl_sched_wakeup_granularity;
2993
2994 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002995 * Since its curr running now, convert the gran from real-time
2996 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002997 *
2998 * By using 'se' instead of 'curr' we penalize light tasks, so
2999 * they get preempted easier. That is, if 'se' < 'curr' then
3000 * the resulting gran will be larger, therefore penalizing the
3001 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3002 * be smaller, again penalizing the lighter task.
3003 *
3004 * This is especially important for buddies when the leftmost
3005 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003006 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003007 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003008}
3009
3010/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003011 * Should 'se' preempt 'curr'.
3012 *
3013 * |s1
3014 * |s2
3015 * |s3
3016 * g
3017 * |<--->|c
3018 *
3019 * w(c, s1) = -1
3020 * w(c, s2) = 0
3021 * w(c, s3) = 1
3022 *
3023 */
3024static int
3025wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3026{
3027 s64 gran, vdiff = curr->vruntime - se->vruntime;
3028
3029 if (vdiff <= 0)
3030 return -1;
3031
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003032 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003033 if (vdiff > gran)
3034 return 1;
3035
3036 return 0;
3037}
3038
Peter Zijlstra02479092008-11-04 21:25:10 +01003039static void set_last_buddy(struct sched_entity *se)
3040{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003041 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3042 return;
3043
3044 for_each_sched_entity(se)
3045 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003046}
3047
3048static void set_next_buddy(struct sched_entity *se)
3049{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003050 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3051 return;
3052
3053 for_each_sched_entity(se)
3054 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003055}
3056
Rik van Rielac53db52011-02-01 09:51:03 -05003057static void set_skip_buddy(struct sched_entity *se)
3058{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003059 for_each_sched_entity(se)
3060 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003061}
3062
Peter Zijlstra464b7522008-10-24 11:06:15 +02003063/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003064 * Preempt the current task with a newly woken task if needed:
3065 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003066static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003067{
3068 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003069 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003070 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003071 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003072 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003073
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003074 if (unlikely(se == pse))
3075 return;
3076
Paul Turner5238cdd2011-07-21 09:43:37 -07003077 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003078 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003079 * unconditionally check_prempt_curr() after an enqueue (which may have
3080 * lead to a throttle). This both saves work and prevents false
3081 * next-buddy nomination below.
3082 */
3083 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3084 return;
3085
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003086 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003087 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003088 next_buddy_marked = 1;
3089 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003090
Bharata B Raoaec0a512008-08-28 14:42:49 +05303091 /*
3092 * We can come here with TIF_NEED_RESCHED already set from new task
3093 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003094 *
3095 * Note: this also catches the edge-case of curr being in a throttled
3096 * group (e.g. via set_curr_task), since update_curr() (in the
3097 * enqueue of curr) will have resulted in resched being set. This
3098 * prevents us from potentially nominating it as a false LAST_BUDDY
3099 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303100 */
3101 if (test_tsk_need_resched(curr))
3102 return;
3103
Darren Harta2f5c9a2011-02-22 13:04:33 -08003104 /* Idle tasks are by definition preempted by non-idle tasks. */
3105 if (unlikely(curr->policy == SCHED_IDLE) &&
3106 likely(p->policy != SCHED_IDLE))
3107 goto preempt;
3108
Ingo Molnar91c234b2007-10-15 17:00:18 +02003109 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003110 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3111 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003112 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003113 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003114 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003115
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003116 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003117 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003118 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003119 if (wakeup_preempt_entity(se, pse) == 1) {
3120 /*
3121 * Bias pick_next to pick the sched entity that is
3122 * triggering this preemption.
3123 */
3124 if (!next_buddy_marked)
3125 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003126 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003127 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003128
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003129 return;
3130
3131preempt:
3132 resched_task(curr);
3133 /*
3134 * Only set the backward buddy when the current task is still
3135 * on the rq. This can happen when a wakeup gets interleaved
3136 * with schedule on the ->pre_schedule() or idle_balance()
3137 * point, either of which can * drop the rq lock.
3138 *
3139 * Also, during early boot the idle thread is in the fair class,
3140 * for obvious reasons its a bad idea to schedule back to it.
3141 */
3142 if (unlikely(!se->on_rq || curr == rq->idle))
3143 return;
3144
3145 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3146 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003147}
3148
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003149static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003150{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003151 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003152 struct cfs_rq *cfs_rq = &rq->cfs;
3153 struct sched_entity *se;
3154
Tim Blechmann36ace272009-11-24 11:55:45 +01003155 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003156 return NULL;
3157
3158 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003159 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003160 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003161 cfs_rq = group_cfs_rq(se);
3162 } while (cfs_rq);
3163
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003164 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003165 if (hrtick_enabled(rq))
3166 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003167
3168 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003169}
3170
3171/*
3172 * Account for a descheduled task:
3173 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003174static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003175{
3176 struct sched_entity *se = &prev->se;
3177 struct cfs_rq *cfs_rq;
3178
3179 for_each_sched_entity(se) {
3180 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003181 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003182 }
3183}
3184
Rik van Rielac53db52011-02-01 09:51:03 -05003185/*
3186 * sched_yield() is very simple
3187 *
3188 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3189 */
3190static void yield_task_fair(struct rq *rq)
3191{
3192 struct task_struct *curr = rq->curr;
3193 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3194 struct sched_entity *se = &curr->se;
3195
3196 /*
3197 * Are we the only task in the tree?
3198 */
3199 if (unlikely(rq->nr_running == 1))
3200 return;
3201
3202 clear_buddies(cfs_rq, se);
3203
3204 if (curr->policy != SCHED_BATCH) {
3205 update_rq_clock(rq);
3206 /*
3207 * Update run-time statistics of the 'current'.
3208 */
3209 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003210 /*
3211 * Tell update_rq_clock() that we've just updated,
3212 * so we don't do microscopic update in schedule()
3213 * and double the fastpath cost.
3214 */
3215 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003216 }
3217
3218 set_skip_buddy(se);
3219}
3220
Mike Galbraithd95f4122011-02-01 09:50:51 -05003221static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3222{
3223 struct sched_entity *se = &p->se;
3224
Paul Turner5238cdd2011-07-21 09:43:37 -07003225 /* throttled hierarchies are not runnable */
3226 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003227 return false;
3228
3229 /* Tell the scheduler that we'd really like pse to run next. */
3230 set_next_buddy(se);
3231
Mike Galbraithd95f4122011-02-01 09:50:51 -05003232 yield_task_fair(rq);
3233
3234 return true;
3235}
3236
Peter Williams681f3e62007-10-24 18:23:51 +02003237#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003238/**************************************************
3239 * Fair scheduling class load-balancing methods:
3240 */
3241
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003242static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3243
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003244#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003245#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303246#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003247
3248struct lb_env {
3249 struct sched_domain *sd;
3250
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003251 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303252 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003253
3254 int dst_cpu;
3255 struct rq *dst_rq;
3256
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303257 struct cpumask *dst_grpmask;
3258 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003259 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003260 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08003261 /* The set of CPUs under consideration for load-balancing */
3262 struct cpumask *cpus;
3263
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003264 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003265
3266 unsigned int loop;
3267 unsigned int loop_break;
3268 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003269};
3270
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003271/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003272 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003273 * Both runqueues must be locked.
3274 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003275static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003276{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003277 deactivate_task(env->src_rq, p, 0);
3278 set_task_cpu(p, env->dst_cpu);
3279 activate_task(env->dst_rq, p, 0);
3280 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003281}
3282
3283/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003284 * Is this task likely cache-hot:
3285 */
3286static int
3287task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3288{
3289 s64 delta;
3290
3291 if (p->sched_class != &fair_sched_class)
3292 return 0;
3293
3294 if (unlikely(p->policy == SCHED_IDLE))
3295 return 0;
3296
3297 /*
3298 * Buddy candidates are cache hot:
3299 */
3300 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3301 (&p->se == cfs_rq_of(&p->se)->next ||
3302 &p->se == cfs_rq_of(&p->se)->last))
3303 return 1;
3304
3305 if (sysctl_sched_migration_cost == -1)
3306 return 1;
3307 if (sysctl_sched_migration_cost == 0)
3308 return 0;
3309
3310 delta = now - p->se.exec_start;
3311
3312 return delta < (s64)sysctl_sched_migration_cost;
3313}
3314
3315/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003316 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3317 */
3318static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003319int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003320{
3321 int tsk_cache_hot = 0;
3322 /*
3323 * We do not migrate tasks that are:
3324 * 1) running (obviously), or
3325 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3326 * 3) are cache-hot on their current CPU.
3327 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003328 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303329 int new_dst_cpu;
3330
Lucas De Marchi41acab82010-03-10 23:37:45 -03003331 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303332
3333 /*
3334 * Remember if this task can be migrated to any other cpu in
3335 * our sched_group. We may want to revisit it if we couldn't
3336 * meet load balance goals by pulling other tasks on src_cpu.
3337 *
3338 * Also avoid computing new_dst_cpu if we have already computed
3339 * one in current iteration.
3340 */
3341 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3342 return 0;
3343
3344 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3345 tsk_cpus_allowed(p));
3346 if (new_dst_cpu < nr_cpu_ids) {
3347 env->flags |= LBF_SOME_PINNED;
3348 env->new_dst_cpu = new_dst_cpu;
3349 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003350 return 0;
3351 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303352
3353 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003354 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003355
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003356 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003357 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003358 return 0;
3359 }
3360
3361 /*
3362 * Aggressive migration if:
3363 * 1) task is cache cold, or
3364 * 2) too many balance attempts have failed.
3365 */
3366
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003367 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003368 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003369 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003370#ifdef CONFIG_SCHEDSTATS
3371 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003372 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003373 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003374 }
3375#endif
3376 return 1;
3377 }
3378
3379 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003380 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003381 return 0;
3382 }
3383 return 1;
3384}
3385
Peter Zijlstra897c3952009-12-17 17:45:42 +01003386/*
3387 * move_one_task tries to move exactly one task from busiest to this_rq, as
3388 * part of active balancing operations within "domain".
3389 * Returns 1 if successful and 0 otherwise.
3390 *
3391 * Called with both runqueues locked.
3392 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003393static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003394{
3395 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003396
Peter Zijlstra367456c2012-02-20 21:49:09 +01003397 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3398 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3399 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003400
Peter Zijlstra367456c2012-02-20 21:49:09 +01003401 if (!can_migrate_task(p, env))
3402 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003403
Peter Zijlstra367456c2012-02-20 21:49:09 +01003404 move_task(p, env);
3405 /*
3406 * Right now, this is only the second place move_task()
3407 * is called, so we can safely collect move_task()
3408 * stats here rather than inside move_task().
3409 */
3410 schedstat_inc(env->sd, lb_gained[env->idle]);
3411 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003412 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003413 return 0;
3414}
3415
Peter Zijlstra367456c2012-02-20 21:49:09 +01003416static unsigned long task_h_load(struct task_struct *p);
3417
Peter Zijlstraeb953082012-04-17 13:38:40 +02003418static const unsigned int sched_nr_migrate_break = 32;
3419
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003420/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003421 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003422 * this_rq, as part of a balancing operation within domain "sd".
3423 * Returns 1 if successful and 0 otherwise.
3424 *
3425 * Called with both runqueues locked.
3426 */
3427static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003428{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003429 struct list_head *tasks = &env->src_rq->cfs_tasks;
3430 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003431 unsigned long load;
3432 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003433
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003434 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003435 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003436
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003437 while (!list_empty(tasks)) {
3438 p = list_first_entry(tasks, struct task_struct, se.group_node);
3439
Peter Zijlstra367456c2012-02-20 21:49:09 +01003440 env->loop++;
3441 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003442 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003443 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003444
3445 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003446 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003447 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003448 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003449 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003450 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003451
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003452 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003453 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003454
Peter Zijlstra367456c2012-02-20 21:49:09 +01003455 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003456
Peter Zijlstraeb953082012-04-17 13:38:40 +02003457 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003458 goto next;
3459
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003460 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003461 goto next;
3462
3463 if (!can_migrate_task(p, env))
3464 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003465
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003466 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003467 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003468 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003469
3470#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003471 /*
3472 * NEWIDLE balancing is a source of latency, so preemptible
3473 * kernels will stop after the first task is pulled to minimize
3474 * the critical section.
3475 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003476 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003477 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003478#endif
3479
Peter Zijlstraee00e662009-12-17 17:25:20 +01003480 /*
3481 * We only want to steal up to the prescribed amount of
3482 * weighted load.
3483 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003484 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003485 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003486
Peter Zijlstra367456c2012-02-20 21:49:09 +01003487 continue;
3488next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003489 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003490 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003491
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003492 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003493 * Right now, this is one of only two places move_task() is called,
3494 * so we can safely collect move_task() stats here rather than
3495 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003496 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003497 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003498
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003499 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003500}
3501
Peter Zijlstra230059de2009-12-17 17:47:12 +01003502#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003503/*
3504 * update tg->load_weight by folding this cpu's load_avg
3505 */
Paul Turner67e86252010-11-15 15:47:05 -08003506static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003507{
3508 struct cfs_rq *cfs_rq;
3509 unsigned long flags;
3510 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003511
3512 if (!tg->se[cpu])
3513 return 0;
3514
3515 rq = cpu_rq(cpu);
3516 cfs_rq = tg->cfs_rq[cpu];
3517
3518 raw_spin_lock_irqsave(&rq->lock, flags);
3519
3520 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003521 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003522
3523 /*
3524 * We need to update shares after updating tg->load_weight in
3525 * order to adjust the weight of groups with long running tasks.
3526 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003527 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003528
3529 raw_spin_unlock_irqrestore(&rq->lock, flags);
3530
3531 return 0;
3532}
3533
3534static void update_shares(int cpu)
3535{
3536 struct cfs_rq *cfs_rq;
3537 struct rq *rq = cpu_rq(cpu);
3538
3539 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003540 /*
3541 * Iterates the task_group tree in a bottom up fashion, see
3542 * list_add_leaf_cfs_rq() for details.
3543 */
Paul Turner64660c82011-07-21 09:43:36 -07003544 for_each_leaf_cfs_rq(rq, cfs_rq) {
3545 /* throttled entities do not contribute to load */
3546 if (throttled_hierarchy(cfs_rq))
3547 continue;
3548
Paul Turner67e86252010-11-15 15:47:05 -08003549 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003550 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003551 rcu_read_unlock();
3552}
3553
Peter Zijlstra9763b672011-07-13 13:09:25 +02003554/*
3555 * Compute the cpu's hierarchical load factor for each task group.
3556 * This needs to be done in a top-down fashion because the load of a child
3557 * group is a fraction of its parents load.
3558 */
3559static int tg_load_down(struct task_group *tg, void *data)
3560{
3561 unsigned long load;
3562 long cpu = (long)data;
3563
3564 if (!tg->parent) {
3565 load = cpu_rq(cpu)->load.weight;
3566 } else {
3567 load = tg->parent->cfs_rq[cpu]->h_load;
3568 load *= tg->se[cpu]->load.weight;
3569 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3570 }
3571
3572 tg->cfs_rq[cpu]->h_load = load;
3573
3574 return 0;
3575}
3576
3577static void update_h_load(long cpu)
3578{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003579 struct rq *rq = cpu_rq(cpu);
3580 unsigned long now = jiffies;
3581
3582 if (rq->h_load_throttle == now)
3583 return;
3584
3585 rq->h_load_throttle = now;
3586
Peter Zijlstra367456c2012-02-20 21:49:09 +01003587 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003588 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003589 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003590}
3591
Peter Zijlstra367456c2012-02-20 21:49:09 +01003592static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003593{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003594 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3595 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003596
Peter Zijlstra367456c2012-02-20 21:49:09 +01003597 load = p->se.load.weight;
3598 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003599
Peter Zijlstra367456c2012-02-20 21:49:09 +01003600 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003601}
3602#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003603static inline void update_shares(int cpu)
3604{
3605}
3606
Peter Zijlstra367456c2012-02-20 21:49:09 +01003607static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003608{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003609}
3610
3611static unsigned long task_h_load(struct task_struct *p)
3612{
3613 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003614}
3615#endif
3616
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003617/********** Helpers for find_busiest_group ************************/
3618/*
3619 * sd_lb_stats - Structure to store the statistics of a sched_domain
3620 * during load balancing.
3621 */
3622struct sd_lb_stats {
3623 struct sched_group *busiest; /* Busiest group in this sd */
3624 struct sched_group *this; /* Local group in this sd */
3625 unsigned long total_load; /* Total load of all groups in sd */
3626 unsigned long total_pwr; /* Total power of all groups in sd */
3627 unsigned long avg_load; /* Average load across all groups in sd */
3628
3629 /** Statistics of this group */
3630 unsigned long this_load;
3631 unsigned long this_load_per_task;
3632 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003633 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003634 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003635
3636 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003637 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003638 unsigned long max_load;
3639 unsigned long busiest_load_per_task;
3640 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003641 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003642 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003643 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003644
3645 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003646};
3647
3648/*
3649 * sg_lb_stats - stats of a sched_group required for load_balancing
3650 */
3651struct sg_lb_stats {
3652 unsigned long avg_load; /*Avg load across the CPUs of the group */
3653 unsigned long group_load; /* Total load over the CPUs of the group */
3654 unsigned long sum_nr_running; /* Nr tasks running in the group */
3655 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3656 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003657 unsigned long idle_cpus;
3658 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003659 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003660 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003661};
3662
3663/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003664 * get_sd_load_idx - Obtain the load index for a given sched domain.
3665 * @sd: The sched_domain whose load_idx is to be obtained.
3666 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3667 */
3668static inline int get_sd_load_idx(struct sched_domain *sd,
3669 enum cpu_idle_type idle)
3670{
3671 int load_idx;
3672
3673 switch (idle) {
3674 case CPU_NOT_IDLE:
3675 load_idx = sd->busy_idx;
3676 break;
3677
3678 case CPU_NEWLY_IDLE:
3679 load_idx = sd->newidle_idx;
3680 break;
3681 default:
3682 load_idx = sd->idle_idx;
3683 break;
3684 }
3685
3686 return load_idx;
3687}
3688
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003689unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3690{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003691 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003692}
3693
3694unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3695{
3696 return default_scale_freq_power(sd, cpu);
3697}
3698
3699unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3700{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003701 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003702 unsigned long smt_gain = sd->smt_gain;
3703
3704 smt_gain /= weight;
3705
3706 return smt_gain;
3707}
3708
3709unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3710{
3711 return default_scale_smt_power(sd, cpu);
3712}
3713
3714unsigned long scale_rt_power(int cpu)
3715{
3716 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003717 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003718
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003719 /*
3720 * Since we're reading these variables without serialization make sure
3721 * we read them once before doing sanity checks on them.
3722 */
3723 age_stamp = ACCESS_ONCE(rq->age_stamp);
3724 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003725
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003726 total = sched_avg_period() + (rq->clock - age_stamp);
3727
3728 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003729 /* Ensures that power won't end up being negative */
3730 available = 0;
3731 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003732 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003733 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003734
Nikhil Rao1399fa72011-05-18 10:09:39 -07003735 if (unlikely((s64)total < SCHED_POWER_SCALE))
3736 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003737
Nikhil Rao1399fa72011-05-18 10:09:39 -07003738 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003739
3740 return div_u64(available, total);
3741}
3742
3743static void update_cpu_power(struct sched_domain *sd, int cpu)
3744{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003745 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003746 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003747 struct sched_group *sdg = sd->groups;
3748
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003749 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3750 if (sched_feat(ARCH_POWER))
3751 power *= arch_scale_smt_power(sd, cpu);
3752 else
3753 power *= default_scale_smt_power(sd, cpu);
3754
Nikhil Rao1399fa72011-05-18 10:09:39 -07003755 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003756 }
3757
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003758 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003759
3760 if (sched_feat(ARCH_POWER))
3761 power *= arch_scale_freq_power(sd, cpu);
3762 else
3763 power *= default_scale_freq_power(sd, cpu);
3764
Nikhil Rao1399fa72011-05-18 10:09:39 -07003765 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003766
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003767 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003768 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003769
3770 if (!power)
3771 power = 1;
3772
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003773 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003774 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003775}
3776
Peter Zijlstra029632f2011-10-25 10:00:11 +02003777void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003778{
3779 struct sched_domain *child = sd->child;
3780 struct sched_group *group, *sdg = sd->groups;
3781 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003782 unsigned long interval;
3783
3784 interval = msecs_to_jiffies(sd->balance_interval);
3785 interval = clamp(interval, 1UL, max_load_balance_interval);
3786 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003787
3788 if (!child) {
3789 update_cpu_power(sd, cpu);
3790 return;
3791 }
3792
3793 power = 0;
3794
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003795 if (child->flags & SD_OVERLAP) {
3796 /*
3797 * SD_OVERLAP domains cannot assume that child groups
3798 * span the current group.
3799 */
3800
3801 for_each_cpu(cpu, sched_group_cpus(sdg))
3802 power += power_of(cpu);
3803 } else {
3804 /*
3805 * !SD_OVERLAP domains can assume that child groups
3806 * span the current group.
3807 */
3808
3809 group = child->groups;
3810 do {
3811 power += group->sgp->power;
3812 group = group->next;
3813 } while (group != child->groups);
3814 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003815
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003816 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003817}
3818
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003819/*
3820 * Try and fix up capacity for tiny siblings, this is needed when
3821 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3822 * which on its own isn't powerful enough.
3823 *
3824 * See update_sd_pick_busiest() and check_asym_packing().
3825 */
3826static inline int
3827fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3828{
3829 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003830 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003831 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003832 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003833 return 0;
3834
3835 /*
3836 * If ~90% of the cpu_power is still there, we're good.
3837 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003838 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003839 return 1;
3840
3841 return 0;
3842}
3843
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003844/**
3845 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003846 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003847 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003848 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003849 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003850 * @balance: Should we balance.
3851 * @sgs: variable to hold the statistics for this group.
3852 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003853static inline void update_sg_lb_stats(struct lb_env *env,
3854 struct sched_group *group, int load_idx,
Michael Wangb94031302012-07-12 16:10:13 +08003855 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003856{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003857 unsigned long nr_running, max_nr_running, min_nr_running;
3858 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003859 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003860 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003861 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003862
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003863 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02003864 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003865
3866 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003867 max_cpu_load = 0;
3868 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003869 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003870 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003871
Michael Wangb94031302012-07-12 16:10:13 +08003872 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003873 struct rq *rq = cpu_rq(i);
3874
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003875 nr_running = rq->nr_running;
3876
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003877 /* Bias balancing toward cpus of our domain */
3878 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02003879 if (idle_cpu(i) && !first_idle_cpu &&
3880 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003881 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003882 balance_cpu = i;
3883 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003884
3885 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003886 } else {
3887 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003888 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003889 max_cpu_load = load;
3890 if (min_cpu_load > load)
3891 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003892
3893 if (nr_running > max_nr_running)
3894 max_nr_running = nr_running;
3895 if (min_nr_running > nr_running)
3896 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003897 }
3898
3899 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003900 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003901 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003902 if (idle_cpu(i))
3903 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003904 }
3905
3906 /*
3907 * First idle cpu or the first cpu(busiest) in this sched group
3908 * is eligible for doing load balancing at this and above
3909 * domains. In the newly idle case, we will allow all the cpu's
3910 * to do the newly idle load balance.
3911 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003912 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003913 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003914 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01003915 *balance = 0;
3916 return;
3917 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003918 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01003919 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003920 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003921 }
3922
3923 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003924 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003925
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003926 /*
3927 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003928 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003929 *
3930 * APZ: with cgroup the avg task weight can vary wildly and
3931 * might not be a suitable number - should we keep a
3932 * normalized nr_running number somewhere that negates
3933 * the hierarchy?
3934 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003935 if (sgs->sum_nr_running)
3936 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003937
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003938 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
3939 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940 sgs->group_imb = 1;
3941
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003942 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003943 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003944 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003945 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003946 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003947
3948 if (sgs->group_capacity > sgs->sum_nr_running)
3949 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003950}
3951
3952/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003953 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07003954 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10003955 * @sds: sched_domain statistics
3956 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003957 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10003958 *
3959 * Determine if @sg is a busier group than the previously selected
3960 * busiest group.
3961 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003962static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10003963 struct sd_lb_stats *sds,
3964 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003965 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10003966{
3967 if (sgs->avg_load <= sds->max_load)
3968 return false;
3969
3970 if (sgs->sum_nr_running > sgs->group_capacity)
3971 return true;
3972
3973 if (sgs->group_imb)
3974 return true;
3975
3976 /*
3977 * ASYM_PACKING needs to move all the work to the lowest
3978 * numbered CPUs in the group, therefore mark all groups
3979 * higher than ourself as busy.
3980 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003981 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3982 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10003983 if (!sds->busiest)
3984 return true;
3985
3986 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3987 return true;
3988 }
3989
3990 return false;
3991}
3992
3993/**
Hui Kang461819a2011-10-11 23:00:59 -04003994 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003995 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003996 * @balance: Should we balance.
3997 * @sds: variable to hold the statistics for this sched_domain.
3998 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003999static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004000 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004001{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004002 struct sched_domain *child = env->sd->child;
4003 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004004 struct sg_lb_stats sgs;
4005 int load_idx, prefer_sibling = 0;
4006
4007 if (child && child->flags & SD_PREFER_SIBLING)
4008 prefer_sibling = 1;
4009
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004010 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004011
4012 do {
4013 int local_group;
4014
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004015 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004016 memset(&sgs, 0, sizeof(sgs));
Michael Wangb94031302012-07-12 16:10:13 +08004017 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004018
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004019 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004020 return;
4021
4022 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004023 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004024
4025 /*
4026 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004027 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004028 * and move all the excess tasks away. We lower the capacity
4029 * of a group only if the local group has the capacity to fit
4030 * these excess tasks, i.e. nr_running < group_capacity. The
4031 * extra check prevents the case where you always pull from the
4032 * heaviest group when it is already under-utilized (possible
4033 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004034 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004035 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004036 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4037
4038 if (local_group) {
4039 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004040 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004041 sds->this_nr_running = sgs.sum_nr_running;
4042 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004043 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004044 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004045 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004046 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004047 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004048 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004049 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004050 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004051 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004052 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004053 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004054 sds->group_imb = sgs.group_imb;
4055 }
4056
Michael Neuling532cb4c2010-06-08 14:57:02 +10004057 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004058 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004059}
4060
Michael Neuling532cb4c2010-06-08 14:57:02 +10004061/**
4062 * check_asym_packing - Check to see if the group is packed into the
4063 * sched doman.
4064 *
4065 * This is primarily intended to used at the sibling level. Some
4066 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4067 * case of POWER7, it can move to lower SMT modes only when higher
4068 * threads are idle. When in lower SMT modes, the threads will
4069 * perform better since they share less core resources. Hence when we
4070 * have idle threads, we want them to be the higher ones.
4071 *
4072 * This packing function is run on idle threads. It checks to see if
4073 * the busiest CPU in this domain (core in the P7 case) has a higher
4074 * CPU number than the packing function is being run on. Here we are
4075 * assuming lower CPU number will be equivalent to lower a SMT thread
4076 * number.
4077 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004078 * Returns 1 when packing is required and a task should be moved to
4079 * this CPU. The amount of the imbalance is returned in *imbalance.
4080 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004081 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004082 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004083 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004084static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004085{
4086 int busiest_cpu;
4087
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004088 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004089 return 0;
4090
4091 if (!sds->busiest)
4092 return 0;
4093
4094 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004095 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004096 return 0;
4097
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004098 env->imbalance = DIV_ROUND_CLOSEST(
4099 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4100
Michael Neuling532cb4c2010-06-08 14:57:02 +10004101 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004102}
4103
4104/**
4105 * fix_small_imbalance - Calculate the minor imbalance that exists
4106 * amongst the groups of a sched_domain, during
4107 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004108 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004109 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004110 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004111static inline
4112void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004113{
4114 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4115 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004116 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004117
4118 if (sds->this_nr_running) {
4119 sds->this_load_per_task /= sds->this_nr_running;
4120 if (sds->busiest_load_per_task >
4121 sds->this_load_per_task)
4122 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004123 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004124 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004125 cpu_avg_load_per_task(env->dst_cpu);
4126 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004127
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004128 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004129 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004130 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004131
4132 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4133 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004134 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004135 return;
4136 }
4137
4138 /*
4139 * OK, we don't have enough imbalance to justify moving tasks,
4140 * however we may be able to increase total CPU power used by
4141 * moving them.
4142 */
4143
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004144 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004145 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004146 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004147 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004148 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004149
4150 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004151 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004152 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004153 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004154 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004155 min(sds->busiest_load_per_task, sds->max_load - tmp);
4156
4157 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004158 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004159 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004160 tmp = (sds->max_load * sds->busiest->sgp->power) /
4161 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004162 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004163 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004164 sds->this->sgp->power;
4165 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004166 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004167 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004168
4169 /* Move if we gain throughput */
4170 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004171 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004172}
4173
4174/**
4175 * calculate_imbalance - Calculate the amount of imbalance present within the
4176 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004177 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004178 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004179 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004180static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004181{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004182 unsigned long max_pull, load_above_capacity = ~0UL;
4183
4184 sds->busiest_load_per_task /= sds->busiest_nr_running;
4185 if (sds->group_imb) {
4186 sds->busiest_load_per_task =
4187 min(sds->busiest_load_per_task, sds->avg_load);
4188 }
4189
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004190 /*
4191 * In the presence of smp nice balancing, certain scenarios can have
4192 * max load less than avg load(as we skip the groups at or below
4193 * its cpu_power, while calculating max_load..)
4194 */
4195 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004196 env->imbalance = 0;
4197 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004198 }
4199
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004200 if (!sds->group_imb) {
4201 /*
4202 * Don't want to pull so many tasks that a group would go idle.
4203 */
4204 load_above_capacity = (sds->busiest_nr_running -
4205 sds->busiest_group_capacity);
4206
Nikhil Rao1399fa72011-05-18 10:09:39 -07004207 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004208
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004209 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004210 }
4211
4212 /*
4213 * We're trying to get all the cpus to the average_load, so we don't
4214 * want to push ourselves above the average load, nor do we wish to
4215 * reduce the max loaded cpu below the average load. At the same time,
4216 * we also don't want to reduce the group load below the group capacity
4217 * (so that we can implement power-savings policies etc). Thus we look
4218 * for the minimum possible imbalance.
4219 * Be careful of negative numbers as they'll appear as very large values
4220 * with unsigned longs.
4221 */
4222 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004223
4224 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004225 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004226 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004227 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004228
4229 /*
4230 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004231 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004232 * a think about bumping its value to force at least one task to be
4233 * moved
4234 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004235 if (env->imbalance < sds->busiest_load_per_task)
4236 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004237
4238}
Nikhil Raofab47622010-10-15 13:12:29 -07004239
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004240/******* find_busiest_group() helpers end here *********************/
4241
4242/**
4243 * find_busiest_group - Returns the busiest group within the sched_domain
4244 * if there is an imbalance. If there isn't an imbalance, and
4245 * the user has opted for power-savings, it returns a group whose
4246 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4247 * such a group exists.
4248 *
4249 * Also calculates the amount of weighted load which should be moved
4250 * to restore balance.
4251 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004252 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004253 * @balance: Pointer to a variable indicating if this_cpu
4254 * is the appropriate cpu to perform load balancing at this_level.
4255 *
4256 * Returns: - the busiest group if imbalance exists.
4257 * - If no imbalance and user has opted for power-savings balance,
4258 * return the least loaded group whose CPUs can be
4259 * put to idle by rebalancing its tasks onto our group.
4260 */
4261static struct sched_group *
Michael Wangb94031302012-07-12 16:10:13 +08004262find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004263{
4264 struct sd_lb_stats sds;
4265
4266 memset(&sds, 0, sizeof(sds));
4267
4268 /*
4269 * Compute the various statistics relavent for load balancing at
4270 * this level.
4271 */
Michael Wangb94031302012-07-12 16:10:13 +08004272 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004273
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004274 /*
4275 * this_cpu is not the appropriate cpu to perform load balancing at
4276 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004277 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004278 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004279 goto ret;
4280
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004281 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4282 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004283 return sds.busiest;
4284
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004285 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004286 if (!sds.busiest || sds.busiest_nr_running == 0)
4287 goto out_balanced;
4288
Nikhil Rao1399fa72011-05-18 10:09:39 -07004289 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004290
Peter Zijlstra866ab432011-02-21 18:56:47 +01004291 /*
4292 * If the busiest group is imbalanced the below checks don't
4293 * work because they assumes all things are equal, which typically
4294 * isn't true due to cpus_allowed constraints and the like.
4295 */
4296 if (sds.group_imb)
4297 goto force_balance;
4298
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004299 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004300 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004301 !sds.busiest_has_capacity)
4302 goto force_balance;
4303
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004304 /*
4305 * If the local group is more busy than the selected busiest group
4306 * don't try and pull any tasks.
4307 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004308 if (sds.this_load >= sds.max_load)
4309 goto out_balanced;
4310
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004311 /*
4312 * Don't pull any tasks if this group is already above the domain
4313 * average load.
4314 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004315 if (sds.this_load >= sds.avg_load)
4316 goto out_balanced;
4317
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004318 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004319 /*
4320 * This cpu is idle. If the busiest group load doesn't
4321 * have more tasks than the number of available cpu's and
4322 * there is no imbalance between this and busiest group
4323 * wrt to idle cpu's, it is balanced.
4324 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004325 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004326 sds.busiest_nr_running <= sds.busiest_group_weight)
4327 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004328 } else {
4329 /*
4330 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4331 * imbalance_pct to be conservative.
4332 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004333 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004334 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004335 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004336
Nikhil Raofab47622010-10-15 13:12:29 -07004337force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004338 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004339 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004340 return sds.busiest;
4341
4342out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004343ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004344 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004345 return NULL;
4346}
4347
4348/*
4349 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4350 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004351static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08004352 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004353{
4354 struct rq *busiest = NULL, *rq;
4355 unsigned long max_load = 0;
4356 int i;
4357
4358 for_each_cpu(i, sched_group_cpus(group)) {
4359 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004360 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4361 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004362 unsigned long wl;
4363
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004364 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004365 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004366
Michael Wangb94031302012-07-12 16:10:13 +08004367 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004368 continue;
4369
4370 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004371 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004372
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004373 /*
4374 * When comparing with imbalance, use weighted_cpuload()
4375 * which is not scaled with the cpu power.
4376 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004377 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004378 continue;
4379
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004380 /*
4381 * For the load comparisons with the other cpu's, consider
4382 * the weighted_cpuload() scaled with the cpu power, so that
4383 * the load can be moved away from the cpu that is potentially
4384 * running at a lower capacity.
4385 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004386 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004387
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004388 if (wl > max_load) {
4389 max_load = wl;
4390 busiest = rq;
4391 }
4392 }
4393
4394 return busiest;
4395}
4396
4397/*
4398 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4399 * so long as it is large enough.
4400 */
4401#define MAX_PINNED_INTERVAL 512
4402
4403/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004404DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004405
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004406static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004407{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004408 struct sched_domain *sd = env->sd;
4409
4410 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004411
4412 /*
4413 * ASYM_PACKING needs to force migrate tasks from busy but
4414 * higher numbered CPUs in order to pack all tasks in the
4415 * lowest numbered CPUs.
4416 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004417 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004418 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004419 }
4420
4421 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4422}
4423
Tejun Heo969c7922010-05-06 18:49:21 +02004424static int active_load_balance_cpu_stop(void *data);
4425
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004426/*
4427 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4428 * tasks if there is an imbalance.
4429 */
4430static int load_balance(int this_cpu, struct rq *this_rq,
4431 struct sched_domain *sd, enum cpu_idle_type idle,
4432 int *balance)
4433{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304434 int ld_moved, cur_ld_moved, active_balance = 0;
4435 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004436 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004437 struct rq *busiest;
4438 unsigned long flags;
4439 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4440
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004441 struct lb_env env = {
4442 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004443 .dst_cpu = this_cpu,
4444 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304445 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004446 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004447 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08004448 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004449 };
4450
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004451 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304452 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004453
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004454 schedstat_inc(sd, lb_count[idle]);
4455
4456redo:
Michael Wangb94031302012-07-12 16:10:13 +08004457 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004458
4459 if (*balance == 0)
4460 goto out_balanced;
4461
4462 if (!group) {
4463 schedstat_inc(sd, lb_nobusyg[idle]);
4464 goto out_balanced;
4465 }
4466
Michael Wangb94031302012-07-12 16:10:13 +08004467 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004468 if (!busiest) {
4469 schedstat_inc(sd, lb_nobusyq[idle]);
4470 goto out_balanced;
4471 }
4472
Michael Wang78feefc2012-08-06 16:41:59 +08004473 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004474
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004475 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004476
4477 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304478 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004479 if (busiest->nr_running > 1) {
4480 /*
4481 * Attempt to move tasks. If find_busiest_group has found
4482 * an imbalance but busiest->nr_running <= 1, the group is
4483 * still unbalanced. ld_moved simply stays zero, so it is
4484 * correctly treated as an imbalance.
4485 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004486 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004487 env.src_cpu = busiest->cpu;
4488 env.src_rq = busiest;
4489 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004490
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004491 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004492more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004493 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004494 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304495
4496 /*
4497 * cur_ld_moved - load moved in current iteration
4498 * ld_moved - cumulative load moved across iterations
4499 */
4500 cur_ld_moved = move_tasks(&env);
4501 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004502 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004503 local_irq_restore(flags);
4504
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004505 if (env.flags & LBF_NEED_BREAK) {
4506 env.flags &= ~LBF_NEED_BREAK;
4507 goto more_balance;
4508 }
4509
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004510 /*
4511 * some other cpu did the load balance for us.
4512 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304513 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4514 resched_cpu(env.dst_cpu);
4515
4516 /*
4517 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4518 * us and move them to an alternate dst_cpu in our sched_group
4519 * where they can run. The upper limit on how many times we
4520 * iterate on same src_cpu is dependent on number of cpus in our
4521 * sched_group.
4522 *
4523 * This changes load balance semantics a bit on who can move
4524 * load to a given_cpu. In addition to the given_cpu itself
4525 * (or a ilb_cpu acting on its behalf where given_cpu is
4526 * nohz-idle), we now have balance_cpu in a position to move
4527 * load to given_cpu. In rare situations, this may cause
4528 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4529 * _independently_ and at _same_ time to move some load to
4530 * given_cpu) causing exceess load to be moved to given_cpu.
4531 * This however should not happen so much in practice and
4532 * moreover subsequent load balance cycles should correct the
4533 * excess load moved.
4534 */
4535 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4536 lb_iterations++ < max_lb_iterations) {
4537
Michael Wang78feefc2012-08-06 16:41:59 +08004538 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304539 env.dst_cpu = env.new_dst_cpu;
4540 env.flags &= ~LBF_SOME_PINNED;
4541 env.loop = 0;
4542 env.loop_break = sched_nr_migrate_break;
4543 /*
4544 * Go back to "more_balance" rather than "redo" since we
4545 * need to continue with same src_cpu.
4546 */
4547 goto more_balance;
4548 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004549
4550 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004551 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004552 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304553 if (!cpumask_empty(cpus)) {
4554 env.loop = 0;
4555 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004556 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304557 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004558 goto out_balanced;
4559 }
4560 }
4561
4562 if (!ld_moved) {
4563 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004564 /*
4565 * Increment the failure counter only on periodic balance.
4566 * We do not want newidle balance, which can be very
4567 * frequent, pollute the failure counter causing
4568 * excessive cache_hot migrations and active balances.
4569 */
4570 if (idle != CPU_NEWLY_IDLE)
4571 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004572
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004573 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004574 raw_spin_lock_irqsave(&busiest->lock, flags);
4575
Tejun Heo969c7922010-05-06 18:49:21 +02004576 /* don't kick the active_load_balance_cpu_stop,
4577 * if the curr task on busiest cpu can't be
4578 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004579 */
4580 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004581 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004582 raw_spin_unlock_irqrestore(&busiest->lock,
4583 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004584 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004585 goto out_one_pinned;
4586 }
4587
Tejun Heo969c7922010-05-06 18:49:21 +02004588 /*
4589 * ->active_balance synchronizes accesses to
4590 * ->active_balance_work. Once set, it's cleared
4591 * only after active load balance is finished.
4592 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004593 if (!busiest->active_balance) {
4594 busiest->active_balance = 1;
4595 busiest->push_cpu = this_cpu;
4596 active_balance = 1;
4597 }
4598 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004599
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004600 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004601 stop_one_cpu_nowait(cpu_of(busiest),
4602 active_load_balance_cpu_stop, busiest,
4603 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004604 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004605
4606 /*
4607 * We've kicked active balancing, reset the failure
4608 * counter.
4609 */
4610 sd->nr_balance_failed = sd->cache_nice_tries+1;
4611 }
4612 } else
4613 sd->nr_balance_failed = 0;
4614
4615 if (likely(!active_balance)) {
4616 /* We were unbalanced, so reset the balancing interval */
4617 sd->balance_interval = sd->min_interval;
4618 } else {
4619 /*
4620 * If we've begun active balancing, start to back off. This
4621 * case may not be covered by the all_pinned logic if there
4622 * is only 1 task on the busy runqueue (because we don't call
4623 * move_tasks).
4624 */
4625 if (sd->balance_interval < sd->max_interval)
4626 sd->balance_interval *= 2;
4627 }
4628
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004629 goto out;
4630
4631out_balanced:
4632 schedstat_inc(sd, lb_balanced[idle]);
4633
4634 sd->nr_balance_failed = 0;
4635
4636out_one_pinned:
4637 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004638 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004639 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004640 (sd->balance_interval < sd->max_interval))
4641 sd->balance_interval *= 2;
4642
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004643 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004644out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004645 return ld_moved;
4646}
4647
4648/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004649 * idle_balance is called by schedule() if this_cpu is about to become
4650 * idle. Attempts to pull tasks from other CPUs.
4651 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004652void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004653{
4654 struct sched_domain *sd;
4655 int pulled_task = 0;
4656 unsigned long next_balance = jiffies + HZ;
4657
4658 this_rq->idle_stamp = this_rq->clock;
4659
4660 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4661 return;
4662
Peter Zijlstraf492e122009-12-23 15:29:42 +01004663 /*
4664 * Drop the rq->lock, but keep IRQ/preempt disabled.
4665 */
4666 raw_spin_unlock(&this_rq->lock);
4667
Paul Turnerc66eaf62010-11-15 15:47:07 -08004668 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004669 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004670 for_each_domain(this_cpu, sd) {
4671 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004672 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004673
4674 if (!(sd->flags & SD_LOAD_BALANCE))
4675 continue;
4676
Peter Zijlstraf492e122009-12-23 15:29:42 +01004677 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004678 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004679 pulled_task = load_balance(this_cpu, this_rq,
4680 sd, CPU_NEWLY_IDLE, &balance);
4681 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004682
4683 interval = msecs_to_jiffies(sd->balance_interval);
4684 if (time_after(next_balance, sd->last_balance + interval))
4685 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004686 if (pulled_task) {
4687 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004688 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004689 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004690 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004691 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004692
4693 raw_spin_lock(&this_rq->lock);
4694
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004695 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4696 /*
4697 * We are going idle. next_balance may be set based on
4698 * a busy processor. So reset next_balance.
4699 */
4700 this_rq->next_balance = next_balance;
4701 }
4702}
4703
4704/*
Tejun Heo969c7922010-05-06 18:49:21 +02004705 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4706 * running tasks off the busiest CPU onto idle CPUs. It requires at
4707 * least 1 task to be running on each physical CPU where possible, and
4708 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004709 */
Tejun Heo969c7922010-05-06 18:49:21 +02004710static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004711{
Tejun Heo969c7922010-05-06 18:49:21 +02004712 struct rq *busiest_rq = data;
4713 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004714 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004715 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004716 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004717
4718 raw_spin_lock_irq(&busiest_rq->lock);
4719
4720 /* make sure the requested cpu hasn't gone down in the meantime */
4721 if (unlikely(busiest_cpu != smp_processor_id() ||
4722 !busiest_rq->active_balance))
4723 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004724
4725 /* Is there any task to move? */
4726 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004727 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004728
4729 /*
4730 * This condition is "impossible", if it occurs
4731 * we need to fix it. Originally reported by
4732 * Bjorn Helgaas on a 128-cpu setup.
4733 */
4734 BUG_ON(busiest_rq == target_rq);
4735
4736 /* move a task from busiest_rq to target_rq */
4737 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004738
4739 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004740 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004741 for_each_domain(target_cpu, sd) {
4742 if ((sd->flags & SD_LOAD_BALANCE) &&
4743 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4744 break;
4745 }
4746
4747 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004748 struct lb_env env = {
4749 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004750 .dst_cpu = target_cpu,
4751 .dst_rq = target_rq,
4752 .src_cpu = busiest_rq->cpu,
4753 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004754 .idle = CPU_IDLE,
4755 };
4756
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004757 schedstat_inc(sd, alb_count);
4758
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004759 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004760 schedstat_inc(sd, alb_pushed);
4761 else
4762 schedstat_inc(sd, alb_failed);
4763 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004764 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004765 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004766out_unlock:
4767 busiest_rq->active_balance = 0;
4768 raw_spin_unlock_irq(&busiest_rq->lock);
4769 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004770}
4771
4772#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004773/*
4774 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004775 * - When one of the busy CPUs notice that there may be an idle rebalancing
4776 * needed, they will kick the idle load balancer, which then does idle
4777 * load balancing for all the idle CPUs.
4778 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004779static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004780 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004781 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004782 unsigned long next_balance; /* in jiffy units */
4783} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004784
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004785static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004786{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004787 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004788
Suresh Siddha786d6dc72011-12-01 17:07:35 -08004789 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4790 return ilb;
4791
4792 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004793}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004794
4795/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004796 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4797 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4798 * CPU (if there is one).
4799 */
4800static void nohz_balancer_kick(int cpu)
4801{
4802 int ilb_cpu;
4803
4804 nohz.next_balance++;
4805
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004806 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004807
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004808 if (ilb_cpu >= nr_cpu_ids)
4809 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004810
Suresh Siddhacd490c52011-12-06 11:26:34 -08004811 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004812 return;
4813 /*
4814 * Use smp_send_reschedule() instead of resched_cpu().
4815 * This way we generate a sched IPI on the target cpu which
4816 * is idle. And the softirq performing nohz idle load balance
4817 * will be run before returning from the IPI.
4818 */
4819 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004820 return;
4821}
4822
Alex Shic1cc0172012-09-10 15:10:58 +08004823static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004824{
4825 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4826 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4827 atomic_dec(&nohz.nr_cpus);
4828 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4829 }
4830}
4831
Suresh Siddha69e1e812011-12-01 17:07:33 -08004832static inline void set_cpu_sd_state_busy(void)
4833{
4834 struct sched_domain *sd;
4835 int cpu = smp_processor_id();
4836
4837 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4838 return;
4839 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4840
4841 rcu_read_lock();
4842 for_each_domain(cpu, sd)
4843 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4844 rcu_read_unlock();
4845}
4846
4847void set_cpu_sd_state_idle(void)
4848{
4849 struct sched_domain *sd;
4850 int cpu = smp_processor_id();
4851
4852 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4853 return;
4854 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4855
4856 rcu_read_lock();
4857 for_each_domain(cpu, sd)
4858 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4859 rcu_read_unlock();
4860}
4861
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004862/*
Alex Shic1cc0172012-09-10 15:10:58 +08004863 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004864 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004865 */
Alex Shic1cc0172012-09-10 15:10:58 +08004866void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004867{
Suresh Siddha71325962012-01-19 18:28:57 -08004868 /*
4869 * If this cpu is going down, then nothing needs to be done.
4870 */
4871 if (!cpu_active(cpu))
4872 return;
4873
Alex Shic1cc0172012-09-10 15:10:58 +08004874 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
4875 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004876
Alex Shic1cc0172012-09-10 15:10:58 +08004877 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
4878 atomic_inc(&nohz.nr_cpus);
4879 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004880}
Suresh Siddha71325962012-01-19 18:28:57 -08004881
4882static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4883 unsigned long action, void *hcpu)
4884{
4885 switch (action & ~CPU_TASKS_FROZEN) {
4886 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08004887 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08004888 return NOTIFY_OK;
4889 default:
4890 return NOTIFY_DONE;
4891 }
4892}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004893#endif
4894
4895static DEFINE_SPINLOCK(balancing);
4896
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004897/*
4898 * Scale the max load_balance interval with the number of CPUs in the system.
4899 * This trades load-balance latency on larger machines for less cross talk.
4900 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004901void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004902{
4903 max_load_balance_interval = HZ*num_online_cpus()/10;
4904}
4905
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004906/*
4907 * It checks each scheduling domain to see if it is due to be balanced,
4908 * and initiates a balancing operation if so.
4909 *
4910 * Balancing parameters are set up in arch_init_sched_domains.
4911 */
4912static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4913{
4914 int balance = 1;
4915 struct rq *rq = cpu_rq(cpu);
4916 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004917 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004918 /* Earliest time when we have to do rebalance again */
4919 unsigned long next_balance = jiffies + 60*HZ;
4920 int update_next_balance = 0;
4921 int need_serialize;
4922
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004923 update_shares(cpu);
4924
Peter Zijlstradce840a2011-04-07 14:09:50 +02004925 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004926 for_each_domain(cpu, sd) {
4927 if (!(sd->flags & SD_LOAD_BALANCE))
4928 continue;
4929
4930 interval = sd->balance_interval;
4931 if (idle != CPU_IDLE)
4932 interval *= sd->busy_factor;
4933
4934 /* scale ms to jiffies */
4935 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004936 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004937
4938 need_serialize = sd->flags & SD_SERIALIZE;
4939
4940 if (need_serialize) {
4941 if (!spin_trylock(&balancing))
4942 goto out;
4943 }
4944
4945 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4946 if (load_balance(cpu, rq, sd, idle, &balance)) {
4947 /*
4948 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004949 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004950 */
4951 idle = CPU_NOT_IDLE;
4952 }
4953 sd->last_balance = jiffies;
4954 }
4955 if (need_serialize)
4956 spin_unlock(&balancing);
4957out:
4958 if (time_after(next_balance, sd->last_balance + interval)) {
4959 next_balance = sd->last_balance + interval;
4960 update_next_balance = 1;
4961 }
4962
4963 /*
4964 * Stop the load balance at this level. There is another
4965 * CPU in our sched group which is doing load balancing more
4966 * actively.
4967 */
4968 if (!balance)
4969 break;
4970 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004971 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004972
4973 /*
4974 * next_balance will be updated only when there is a need.
4975 * When the cpu is attached to null domain for ex, it will not be
4976 * updated.
4977 */
4978 if (likely(update_next_balance))
4979 rq->next_balance = next_balance;
4980}
4981
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004982#ifdef CONFIG_NO_HZ
4983/*
4984 * In CONFIG_NO_HZ case, the idle balance kickee will do the
4985 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4986 */
4987static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
4988{
4989 struct rq *this_rq = cpu_rq(this_cpu);
4990 struct rq *rq;
4991 int balance_cpu;
4992
Suresh Siddha1c792db2011-12-01 17:07:32 -08004993 if (idle != CPU_IDLE ||
4994 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
4995 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004996
4997 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08004998 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004999 continue;
5000
5001 /*
5002 * If this cpu gets work to do, stop the load balancing
5003 * work being done for other cpus. Next load
5004 * balancing owner will pick it up.
5005 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005006 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005007 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005008
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005009 rq = cpu_rq(balance_cpu);
5010
5011 raw_spin_lock_irq(&rq->lock);
5012 update_rq_clock(rq);
5013 update_idle_cpu_load(rq);
5014 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005015
5016 rebalance_domains(balance_cpu, CPU_IDLE);
5017
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005018 if (time_after(this_rq->next_balance, rq->next_balance))
5019 this_rq->next_balance = rq->next_balance;
5020 }
5021 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005022end:
5023 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005024}
5025
5026/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005027 * Current heuristic for kicking the idle load balancer in the presence
5028 * of an idle cpu is the system.
5029 * - This rq has more than one task.
5030 * - At any scheduler domain level, this cpu's scheduler group has multiple
5031 * busy cpu's exceeding the group's power.
5032 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5033 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005034 */
5035static inline int nohz_kick_needed(struct rq *rq, int cpu)
5036{
5037 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005038 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005039
Suresh Siddha1c792db2011-12-01 17:07:32 -08005040 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005041 return 0;
5042
Suresh Siddha1c792db2011-12-01 17:07:32 -08005043 /*
5044 * We may be recently in ticked or tickless idle mode. At the first
5045 * busy tick after returning from idle, we will update the busy stats.
5046 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005047 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005048 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005049
5050 /*
5051 * None are in tickless mode and hence no need for NOHZ idle load
5052 * balancing.
5053 */
5054 if (likely(!atomic_read(&nohz.nr_cpus)))
5055 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005056
5057 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005058 return 0;
5059
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005060 if (rq->nr_running >= 2)
5061 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005062
Peter Zijlstra067491b2011-12-07 14:32:08 +01005063 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005064 for_each_domain(cpu, sd) {
5065 struct sched_group *sg = sd->groups;
5066 struct sched_group_power *sgp = sg->sgp;
5067 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005068
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005069 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005070 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005071
5072 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5073 && (cpumask_first_and(nohz.idle_cpus_mask,
5074 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005075 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005076
5077 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5078 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005079 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005080 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005081 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005082
5083need_kick_unlock:
5084 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005085need_kick:
5086 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005087}
5088#else
5089static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5090#endif
5091
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005092/*
5093 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005094 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005095 */
5096static void run_rebalance_domains(struct softirq_action *h)
5097{
5098 int this_cpu = smp_processor_id();
5099 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005100 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005101 CPU_IDLE : CPU_NOT_IDLE;
5102
5103 rebalance_domains(this_cpu, idle);
5104
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005105 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005106 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005107 * balancing on behalf of the other idle cpus whose ticks are
5108 * stopped.
5109 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005110 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005111}
5112
5113static inline int on_null_domain(int cpu)
5114{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005115 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005116}
5117
5118/*
5119 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005120 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005121void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005122{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005123 /* Don't need to rebalance while attached to NULL domain */
5124 if (time_after_eq(jiffies, rq->next_balance) &&
5125 likely(!on_null_domain(cpu)))
5126 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005127#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005128 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005129 nohz_balancer_kick(cpu);
5130#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005131}
5132
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005133static void rq_online_fair(struct rq *rq)
5134{
5135 update_sysctl();
5136}
5137
5138static void rq_offline_fair(struct rq *rq)
5139{
5140 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005141
5142 /* Ensure any throttled groups are reachable by pick_next_task */
5143 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005144}
5145
Dhaval Giani55e12e52008-06-24 23:39:43 +05305146#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005147
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005148/*
5149 * scheduler tick hitting a task of our scheduling class:
5150 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005151static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005152{
5153 struct cfs_rq *cfs_rq;
5154 struct sched_entity *se = &curr->se;
5155
5156 for_each_sched_entity(se) {
5157 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005158 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005159 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02005160
5161 if (sched_feat_numa(NUMA))
5162 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005163}
5164
5165/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005166 * called on fork with the child task as argument from the parent's context
5167 * - child not yet on the tasklist
5168 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005169 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005170static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005171{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005172 struct cfs_rq *cfs_rq;
5173 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005174 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005175 struct rq *rq = this_rq();
5176 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005177
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005178 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005179
Peter Zijlstra861d0342010-08-19 13:31:43 +02005180 update_rq_clock(rq);
5181
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005182 cfs_rq = task_cfs_rq(current);
5183 curr = cfs_rq->curr;
5184
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005185 if (unlikely(task_cpu(p) != this_cpu)) {
5186 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005187 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005188 rcu_read_unlock();
5189 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005190
Ting Yang7109c442007-08-28 12:53:24 +02005191 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005192
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005193 if (curr)
5194 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005195 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005196
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005197 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005198 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005199 * Upon rescheduling, sched_class::put_prev_task() will place
5200 * 'current' within the tree based on its new key value.
5201 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005202 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305203 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005204 }
5205
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005206 se->vruntime -= cfs_rq->min_vruntime;
5207
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005208 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005209}
5210
Steven Rostedtcb469842008-01-25 21:08:22 +01005211/*
5212 * Priority of the task has changed. Check to see if we preempt
5213 * the current task.
5214 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005215static void
5216prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005217{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005218 if (!p->se.on_rq)
5219 return;
5220
Steven Rostedtcb469842008-01-25 21:08:22 +01005221 /*
5222 * Reschedule if we are currently running on this runqueue and
5223 * our priority decreased, or if we are not currently running on
5224 * this runqueue and our priority is higher than the current's
5225 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005226 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005227 if (p->prio > oldprio)
5228 resched_task(rq->curr);
5229 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005230 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005231}
5232
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005233static void switched_from_fair(struct rq *rq, struct task_struct *p)
5234{
5235 struct sched_entity *se = &p->se;
5236 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5237
5238 /*
5239 * Ensure the task's vruntime is normalized, so that when its
5240 * switched back to the fair class the enqueue_entity(.flags=0) will
5241 * do the right thing.
5242 *
5243 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5244 * have normalized the vruntime, if it was !on_rq, then only when
5245 * the task is sleeping will it still have non-normalized vruntime.
5246 */
5247 if (!se->on_rq && p->state != TASK_RUNNING) {
5248 /*
5249 * Fix up our vruntime so that the current sleep doesn't
5250 * cause 'unlimited' sleep bonus.
5251 */
5252 place_entity(cfs_rq, se, 0);
5253 se->vruntime -= cfs_rq->min_vruntime;
5254 }
5255}
5256
Steven Rostedtcb469842008-01-25 21:08:22 +01005257/*
5258 * We switched to the sched_fair class.
5259 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005260static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005261{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005262 if (!p->se.on_rq)
5263 return;
5264
Steven Rostedtcb469842008-01-25 21:08:22 +01005265 /*
5266 * We were most likely switched from sched_rt, so
5267 * kick off the schedule if running, otherwise just see
5268 * if we can still preempt the current task.
5269 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005270 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005271 resched_task(rq->curr);
5272 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005273 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005274}
5275
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005276/* Account for a task changing its policy or group.
5277 *
5278 * This routine is mostly called to set cfs_rq->curr field when a task
5279 * migrates between groups/classes.
5280 */
5281static void set_curr_task_fair(struct rq *rq)
5282{
5283 struct sched_entity *se = &rq->curr->se;
5284
Paul Turnerec12cb72011-07-21 09:43:30 -07005285 for_each_sched_entity(se) {
5286 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5287
5288 set_next_entity(cfs_rq, se);
5289 /* ensure bandwidth has been allocated on our new cfs_rq */
5290 account_cfs_rq_runtime(cfs_rq, 0);
5291 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005292}
5293
Peter Zijlstra029632f2011-10-25 10:00:11 +02005294void init_cfs_rq(struct cfs_rq *cfs_rq)
5295{
5296 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005297 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5298#ifndef CONFIG_64BIT
5299 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5300#endif
5301}
5302
Peter Zijlstra810b3812008-02-29 15:21:01 -05005303#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005304static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005305{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005306 /*
5307 * If the task was not on the rq at the time of this cgroup movement
5308 * it must have been asleep, sleeping tasks keep their ->vruntime
5309 * absolute on their old rq until wakeup (needed for the fair sleeper
5310 * bonus in place_entity()).
5311 *
5312 * If it was on the rq, we've just 'preempted' it, which does convert
5313 * ->vruntime to a relative base.
5314 *
5315 * Make sure both cases convert their relative position when migrating
5316 * to another cgroup's rq. This does somewhat interfere with the
5317 * fair sleeper stuff for the first placement, but who cares.
5318 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005319 /*
5320 * When !on_rq, vruntime of the task has usually NOT been normalized.
5321 * But there are some cases where it has already been normalized:
5322 *
5323 * - Moving a forked child which is waiting for being woken up by
5324 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005325 * - Moving a task which has been woken up by try_to_wake_up() and
5326 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005327 *
5328 * To prevent boost or penalty in the new cfs_rq caused by delta
5329 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5330 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005331 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005332 on_rq = 1;
5333
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005334 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005335 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5336 set_task_rq(p, task_cpu(p));
5337 if (!on_rq)
5338 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005339}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005340
5341void free_fair_sched_group(struct task_group *tg)
5342{
5343 int i;
5344
5345 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5346
5347 for_each_possible_cpu(i) {
5348 if (tg->cfs_rq)
5349 kfree(tg->cfs_rq[i]);
5350 if (tg->se)
5351 kfree(tg->se[i]);
5352 }
5353
5354 kfree(tg->cfs_rq);
5355 kfree(tg->se);
5356}
5357
5358int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5359{
5360 struct cfs_rq *cfs_rq;
5361 struct sched_entity *se;
5362 int i;
5363
5364 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5365 if (!tg->cfs_rq)
5366 goto err;
5367 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5368 if (!tg->se)
5369 goto err;
5370
5371 tg->shares = NICE_0_LOAD;
5372
5373 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5374
5375 for_each_possible_cpu(i) {
5376 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5377 GFP_KERNEL, cpu_to_node(i));
5378 if (!cfs_rq)
5379 goto err;
5380
5381 se = kzalloc_node(sizeof(struct sched_entity),
5382 GFP_KERNEL, cpu_to_node(i));
5383 if (!se)
5384 goto err_free_rq;
5385
5386 init_cfs_rq(cfs_rq);
5387 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5388 }
5389
5390 return 1;
5391
5392err_free_rq:
5393 kfree(cfs_rq);
5394err:
5395 return 0;
5396}
5397
5398void unregister_fair_sched_group(struct task_group *tg, int cpu)
5399{
5400 struct rq *rq = cpu_rq(cpu);
5401 unsigned long flags;
5402
5403 /*
5404 * Only empty task groups can be destroyed; so we can speculatively
5405 * check on_list without danger of it being re-added.
5406 */
5407 if (!tg->cfs_rq[cpu]->on_list)
5408 return;
5409
5410 raw_spin_lock_irqsave(&rq->lock, flags);
5411 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5412 raw_spin_unlock_irqrestore(&rq->lock, flags);
5413}
5414
5415void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5416 struct sched_entity *se, int cpu,
5417 struct sched_entity *parent)
5418{
5419 struct rq *rq = cpu_rq(cpu);
5420
5421 cfs_rq->tg = tg;
5422 cfs_rq->rq = rq;
5423#ifdef CONFIG_SMP
5424 /* allow initial update_cfs_load() to truncate */
5425 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005426#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005427 init_cfs_rq_runtime(cfs_rq);
5428
5429 tg->cfs_rq[cpu] = cfs_rq;
5430 tg->se[cpu] = se;
5431
5432 /* se could be NULL for root_task_group */
5433 if (!se)
5434 return;
5435
5436 if (!parent)
5437 se->cfs_rq = &rq->cfs;
5438 else
5439 se->cfs_rq = parent->my_q;
5440
5441 se->my_q = cfs_rq;
5442 update_load_set(&se->load, 0);
5443 se->parent = parent;
5444}
5445
5446static DEFINE_MUTEX(shares_mutex);
5447
5448int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5449{
5450 int i;
5451 unsigned long flags;
5452
5453 /*
5454 * We can't change the weight of the root cgroup.
5455 */
5456 if (!tg->se[0])
5457 return -EINVAL;
5458
5459 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5460
5461 mutex_lock(&shares_mutex);
5462 if (tg->shares == shares)
5463 goto done;
5464
5465 tg->shares = shares;
5466 for_each_possible_cpu(i) {
5467 struct rq *rq = cpu_rq(i);
5468 struct sched_entity *se;
5469
5470 se = tg->se[i];
5471 /* Propagate contribution to hierarchy */
5472 raw_spin_lock_irqsave(&rq->lock, flags);
5473 for_each_sched_entity(se)
5474 update_cfs_shares(group_cfs_rq(se));
5475 raw_spin_unlock_irqrestore(&rq->lock, flags);
5476 }
5477
5478done:
5479 mutex_unlock(&shares_mutex);
5480 return 0;
5481}
5482#else /* CONFIG_FAIR_GROUP_SCHED */
5483
5484void free_fair_sched_group(struct task_group *tg) { }
5485
5486int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5487{
5488 return 1;
5489}
5490
5491void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5492
5493#endif /* CONFIG_FAIR_GROUP_SCHED */
5494
Peter Zijlstra810b3812008-02-29 15:21:01 -05005495
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005496static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005497{
5498 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005499 unsigned int rr_interval = 0;
5500
5501 /*
5502 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5503 * idle runqueue:
5504 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005505 if (rq->cfs.load.weight)
5506 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005507
5508 return rr_interval;
5509}
5510
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005511/*
5512 * All the scheduling class methods:
5513 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005514const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005515 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005516 .enqueue_task = enqueue_task_fair,
5517 .dequeue_task = dequeue_task_fair,
5518 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005519 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005520
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005521 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005522
5523 .pick_next_task = pick_next_task_fair,
5524 .put_prev_task = put_prev_task_fair,
5525
Peter Williams681f3e62007-10-24 18:23:51 +02005526#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005527 .select_task_rq = select_task_rq_fair,
5528
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005529 .rq_online = rq_online_fair,
5530 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005531
5532 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005533#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005534
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005535 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005536 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005537 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005538
5539 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005540 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005541 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005542
Peter Williams0d721ce2009-09-21 01:31:53 +00005543 .get_rr_interval = get_rr_interval_fair,
5544
Peter Zijlstra810b3812008-02-29 15:21:01 -05005545#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005546 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005547#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005548};
5549
5550#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005551void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005552{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005553 struct cfs_rq *cfs_rq;
5554
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005555 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005556 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005557 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005558 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005559}
5560#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005561
5562__init void init_sched_fair_class(void)
5563{
5564#ifdef CONFIG_SMP
5565 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5566
5567#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005568 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005569 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005570 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005571#endif
5572#endif /* SMP */
5573
5574}