blob: 7a02a2082e95154d5094ded219b20f76ac98a525 [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 Zijlstra6d0f0eb2007-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
Mel Gorman1a687c22012-11-22 11:16:36 +0000814 if (!sched_feat_numa(NUMA))
815 return;
816
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200817 /* FIXME: Allocate task-specific structure for placement policy here */
818
Mel Gormanfb003b82012-11-15 09:01:14 +0000819 /*
Mel Gormanb8593bf2012-11-21 01:18:23 +0000820 * If pages are properly placed (did not migrate) then scan slower.
821 * This is reset periodically in case of phase changes
Mel Gormanfb003b82012-11-15 09:01:14 +0000822 */
Mel Gormanb8593bf2012-11-21 01:18:23 +0000823 if (!migrated)
824 p->numa_scan_period = min(sysctl_numa_balancing_scan_period_max,
825 p->numa_scan_period + jiffies_to_msecs(10));
Mel Gormanfb003b82012-11-15 09:01:14 +0000826
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200827 task_numa_placement(p);
828}
829
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200830static void reset_ptenuma_scan(struct task_struct *p)
831{
832 ACCESS_ONCE(p->mm->numa_scan_seq)++;
833 p->mm->numa_scan_offset = 0;
834}
835
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200836/*
837 * The expensive part of numa migration is done from task_work context.
838 * Triggered from task_tick_numa().
839 */
840void task_numa_work(struct callback_head *work)
841{
842 unsigned long migrate, next_scan, now = jiffies;
843 struct task_struct *p = current;
844 struct mm_struct *mm = p->mm;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200845 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +0000846 unsigned long start, end;
847 long pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200848
849 WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
850
851 work->next = work; /* protect against double add */
852 /*
853 * Who cares about NUMA placement when they're dying.
854 *
855 * NOTE: make sure not to dereference p->mm before this check,
856 * exit_task_work() happens _after_ exit_mm() so we could be called
857 * without p->mm even though we still had it when we enqueued this
858 * work.
859 */
860 if (p->flags & PF_EXITING)
861 return;
862
863 /*
Mel Gormanb8593bf2012-11-21 01:18:23 +0000864 * Reset the scan period if enough time has gone by. Objective is that
865 * scanning will be reduced if pages are properly placed. As tasks
866 * can enter different phases this needs to be re-examined. Lacking
867 * proper tracking of reference behaviour, this blunt hammer is used.
868 */
869 migrate = mm->numa_next_reset;
870 if (time_after(now, migrate)) {
871 p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
872 next_scan = now + msecs_to_jiffies(sysctl_numa_balancing_scan_period_reset);
873 xchg(&mm->numa_next_reset, next_scan);
874 }
875
876 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200877 * Enforce maximal scan/migration frequency..
878 */
879 migrate = mm->numa_next_scan;
880 if (time_before(now, migrate))
881 return;
882
883 if (p->numa_scan_period == 0)
884 p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
885
Mel Gormanfb003b82012-11-15 09:01:14 +0000886 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200887 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
888 return;
889
Mel Gormane14808b2012-11-19 10:59:15 +0000890 /*
891 * Do not set pte_numa if the current running node is rate-limited.
892 * This loses statistics on the fault but if we are unwilling to
893 * migrate to this node, it is less likely we can do useful work
894 */
895 if (migrate_ratelimited(numa_node_id()))
896 return;
897
Mel Gorman9f406042012-11-14 18:34:32 +0000898 start = mm->numa_scan_offset;
899 pages = sysctl_numa_balancing_scan_size;
900 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
901 if (!pages)
902 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200903
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200904 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +0000905 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200906 if (!vma) {
907 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +0000908 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200909 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200910 }
Mel Gorman9f406042012-11-14 18:34:32 +0000911 for (; vma; vma = vma->vm_next) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200912 if (!vma_migratable(vma))
913 continue;
914
915 /* Skip small VMAs. They are not likely to be of relevance */
916 if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < HPAGE_PMD_NR)
917 continue;
918
Mel Gorman9f406042012-11-14 18:34:32 +0000919 do {
920 start = max(start, vma->vm_start);
921 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
922 end = min(end, vma->vm_end);
923 pages -= change_prot_numa(vma, start, end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200924
Mel Gorman9f406042012-11-14 18:34:32 +0000925 start = end;
926 if (pages <= 0)
927 goto out;
928 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200929 }
930
Mel Gorman9f406042012-11-14 18:34:32 +0000931out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200932 /*
933 * It is possible to reach the end of the VMA list but the last few VMAs are
934 * not guaranteed to the vma_migratable. If they are not, we would find the
935 * !migratable VMA on the next scan but not reset the scanner to the start
936 * so check it now.
937 */
938 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +0000939 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200940 else
941 reset_ptenuma_scan(p);
942 up_read(&mm->mmap_sem);
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200943}
944
945/*
946 * Drive the periodic memory faults..
947 */
948void task_tick_numa(struct rq *rq, struct task_struct *curr)
949{
950 struct callback_head *work = &curr->numa_work;
951 u64 period, now;
952
953 /*
954 * We don't care about NUMA placement if we don't have memory.
955 */
956 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
957 return;
958
959 /*
960 * Using runtime rather than walltime has the dual advantage that
961 * we (mostly) drive the selection from busy threads and that the
962 * task needs to have done some actual work before we bother with
963 * NUMA placement.
964 */
965 now = curr->se.sum_exec_runtime;
966 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
967
968 if (now - curr->node_stamp > period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +0200969 if (!curr->node_stamp)
970 curr->numa_scan_period = sysctl_numa_balancing_scan_period_min;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200971 curr->node_stamp = now;
972
973 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
974 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
975 task_work_add(curr, work, true);
976 }
977 }
978}
979#else
980static void task_tick_numa(struct rq *rq, struct task_struct *curr)
981{
982}
983#endif /* CONFIG_NUMA_BALANCING */
984
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200985static void
986account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
987{
988 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200989 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200990 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100991#ifdef CONFIG_SMP
992 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200993 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100994#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200995 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200996}
997
998static void
999account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
1000{
1001 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001002 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02001003 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01001004 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +05301005 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001006 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001007}
1008
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001009#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -07001010/* we need this in update_cfs_load and load-balance functions below */
1011static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001012# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -08001013static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
1014 int global_update)
1015{
1016 struct task_group *tg = cfs_rq->tg;
1017 long load_avg;
1018
1019 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
1020 load_avg -= cfs_rq->load_contribution;
1021
1022 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
1023 atomic_add(load_avg, &tg->load_weight);
1024 cfs_rq->load_contribution += load_avg;
1025 }
1026}
1027
1028static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001029{
Paul Turnera7a4f8a2010-11-15 15:47:06 -08001030 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001031 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -08001032 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001033
Paul Turner64660c82011-07-21 09:43:36 -07001034 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001035 return;
1036
Paul Turner05ca62c2011-01-21 20:45:02 -08001037 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001038 delta = now - cfs_rq->load_stamp;
1039
Paul Turnere33078b2010-11-15 15:47:04 -08001040 /* truncate load history at 4 idle periods */
1041 if (cfs_rq->load_stamp > cfs_rq->load_last &&
1042 now - cfs_rq->load_last > 4 * period) {
1043 cfs_rq->load_period = 0;
1044 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -08001045 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -08001046 }
1047
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001048 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -08001049 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001050 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -08001051 if (load) {
1052 cfs_rq->load_last = now;
1053 cfs_rq->load_avg += delta * load;
1054 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001055
Paul Turnerd6b55912010-11-15 15:47:09 -08001056 /* consider updating load contribution on each fold or truncate */
1057 if (global_update || cfs_rq->load_period > period
1058 || !cfs_rq->load_period)
1059 update_cfs_rq_load_contribution(cfs_rq, global_update);
1060
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001061 while (cfs_rq->load_period > period) {
1062 /*
1063 * Inline assembly required to prevent the compiler
1064 * optimising this loop into a divmod call.
1065 * See __iter_div_u64_rem() for another example of this.
1066 */
1067 asm("" : "+rm" (cfs_rq->load_period));
1068 cfs_rq->load_period /= 2;
1069 cfs_rq->load_avg /= 2;
1070 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001071
Paul Turnere33078b2010-11-15 15:47:04 -08001072 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
1073 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001074}
1075
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001076static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
1077{
1078 long tg_weight;
1079
1080 /*
1081 * Use this CPU's actual weight instead of the last load_contribution
1082 * to gain a more accurate current total weight. See
1083 * update_cfs_rq_load_contribution().
1084 */
1085 tg_weight = atomic_read(&tg->load_weight);
1086 tg_weight -= cfs_rq->load_contribution;
1087 tg_weight += cfs_rq->load.weight;
1088
1089 return tg_weight;
1090}
1091
Paul Turner6d5ab292011-01-21 20:45:01 -08001092static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001093{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001094 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001095
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001096 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -08001097 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001098
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001099 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001100 if (tg_weight)
1101 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001102
1103 if (shares < MIN_SHARES)
1104 shares = MIN_SHARES;
1105 if (shares > tg->shares)
1106 shares = tg->shares;
1107
1108 return shares;
1109}
1110
1111static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1112{
1113 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
1114 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08001115 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001116 }
1117}
1118# else /* CONFIG_SMP */
1119static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
1120{
1121}
1122
Paul Turner6d5ab292011-01-21 20:45:01 -08001123static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001124{
1125 return tg->shares;
1126}
1127
1128static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1129{
1130}
1131# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001132static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
1133 unsigned long weight)
1134{
Paul Turner19e5eeb2010-12-15 19:10:18 -08001135 if (se->on_rq) {
1136 /* commit outstanding execution time */
1137 if (cfs_rq->curr == se)
1138 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001139 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08001140 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001141
1142 update_load_set(&se->load, weight);
1143
1144 if (se->on_rq)
1145 account_entity_enqueue(cfs_rq, se);
1146}
1147
Paul Turner6d5ab292011-01-21 20:45:01 -08001148static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001149{
1150 struct task_group *tg;
1151 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001152 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001153
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001154 tg = cfs_rq->tg;
1155 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07001156 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001157 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001158#ifndef CONFIG_SMP
1159 if (likely(se->load.weight == tg->shares))
1160 return;
1161#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08001162 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001163
1164 reweight_entity(cfs_rq_of(se), se, shares);
1165}
1166#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -08001167static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001168{
1169}
1170
Paul Turner6d5ab292011-01-21 20:45:01 -08001171static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001172{
1173}
Paul Turner43365bd2010-12-15 19:10:17 -08001174
1175static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
1176{
1177}
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001178#endif /* CONFIG_FAIR_GROUP_SCHED */
1179
Ingo Molnar2396af62007-08-09 11:16:48 +02001180static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001181{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001182#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02001183 struct task_struct *tsk = NULL;
1184
1185 if (entity_is_task(se))
1186 tsk = task_of(se);
1187
Lucas De Marchi41acab82010-03-10 23:37:45 -03001188 if (se->statistics.sleep_start) {
1189 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001190
1191 if ((s64)delta < 0)
1192 delta = 0;
1193
Lucas De Marchi41acab82010-03-10 23:37:45 -03001194 if (unlikely(delta > se->statistics.sleep_max))
1195 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001196
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001197 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001198 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001199
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001200 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001201 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001202 trace_sched_stat_sleep(tsk, delta);
1203 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001204 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001205 if (se->statistics.block_start) {
1206 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001207
1208 if ((s64)delta < 0)
1209 delta = 0;
1210
Lucas De Marchi41acab82010-03-10 23:37:45 -03001211 if (unlikely(delta > se->statistics.block_max))
1212 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001213
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001214 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001215 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001216
Peter Zijlstrae4143142009-07-23 20:13:26 +02001217 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001218 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001219 se->statistics.iowait_sum += delta;
1220 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001221 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001222 }
1223
Andrew Vaginb781a602011-11-28 12:03:35 +03001224 trace_sched_stat_blocked(tsk, delta);
1225
Peter Zijlstrae4143142009-07-23 20:13:26 +02001226 /*
1227 * Blocking time is in units of nanosecs, so shift by
1228 * 20 to get a milliseconds-range estimation of the
1229 * amount of time that the task spent sleeping:
1230 */
1231 if (unlikely(prof_on == SLEEP_PROFILING)) {
1232 profile_hits(SLEEP_PROFILING,
1233 (void *)get_wchan(tsk),
1234 delta >> 20);
1235 }
1236 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001237 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001238 }
1239#endif
1240}
1241
Peter Zijlstraddc97292007-10-15 17:00:10 +02001242static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1243{
1244#ifdef CONFIG_SCHED_DEBUG
1245 s64 d = se->vruntime - cfs_rq->min_vruntime;
1246
1247 if (d < 0)
1248 d = -d;
1249
1250 if (d > 3*sysctl_sched_latency)
1251 schedstat_inc(cfs_rq, nr_spread_over);
1252#endif
1253}
1254
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001255static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001256place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1257{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001258 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001259
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001260 /*
1261 * The 'current' period is already promised to the current tasks,
1262 * however the extra weight of the new task will slow them down a
1263 * little, place the new task so that it fits in the slot that
1264 * stays open at the end.
1265 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001266 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001267 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001268
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001269 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001270 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001271 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001272
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001273 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001274 * Halve their sleep time's effect, to allow
1275 * for a gentler effect of sleepers:
1276 */
1277 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1278 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001279
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001280 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001281 }
1282
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001283 /* ensure we never gain time by being placed backwards. */
1284 vruntime = max_vruntime(se->vruntime, vruntime);
1285
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001286 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001287}
1288
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001289static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1290
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001291static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001292enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001293{
1294 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001295 * Update the normalized vruntime before updating min_vruntime
1296 * through callig update_curr().
1297 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001298 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001299 se->vruntime += cfs_rq->min_vruntime;
1300
1301 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001302 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001303 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001304 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001305 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001306 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001307 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001308
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001309 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001310 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001311 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001312 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001313
Ingo Molnard2417e52007-08-09 11:16:47 +02001314 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001315 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001316 if (se != cfs_rq->curr)
1317 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001318 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001319
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001320 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001321 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001322 check_enqueue_throttle(cfs_rq);
1323 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001324}
1325
Rik van Riel2c13c9192011-02-01 09:48:37 -05001326static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001327{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001328 for_each_sched_entity(se) {
1329 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1330 if (cfs_rq->last == se)
1331 cfs_rq->last = NULL;
1332 else
1333 break;
1334 }
1335}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001336
Rik van Riel2c13c9192011-02-01 09:48:37 -05001337static void __clear_buddies_next(struct sched_entity *se)
1338{
1339 for_each_sched_entity(se) {
1340 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1341 if (cfs_rq->next == se)
1342 cfs_rq->next = NULL;
1343 else
1344 break;
1345 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001346}
1347
Rik van Rielac53db52011-02-01 09:51:03 -05001348static void __clear_buddies_skip(struct sched_entity *se)
1349{
1350 for_each_sched_entity(se) {
1351 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1352 if (cfs_rq->skip == se)
1353 cfs_rq->skip = NULL;
1354 else
1355 break;
1356 }
1357}
1358
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001359static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1360{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001361 if (cfs_rq->last == se)
1362 __clear_buddies_last(se);
1363
1364 if (cfs_rq->next == se)
1365 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001366
1367 if (cfs_rq->skip == se)
1368 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001369}
1370
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001371static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001372
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001373static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001374dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001375{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001376 /*
1377 * Update run-time statistics of the 'current'.
1378 */
1379 update_curr(cfs_rq);
1380
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001381 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001382 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001383#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001384 if (entity_is_task(se)) {
1385 struct task_struct *tsk = task_of(se);
1386
1387 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001388 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001389 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001390 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001391 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001392#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001393 }
1394
Peter Zijlstra2002c692008-11-11 11:52:33 +01001395 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001396
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001397 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001398 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001399 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001400 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001401 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001402
1403 /*
1404 * Normalize the entity after updating the min_vruntime because the
1405 * update can refer to the ->curr item and we need to reflect this
1406 * movement in our normalized position.
1407 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001408 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001409 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001410
Paul Turnerd8b49862011-07-21 09:43:41 -07001411 /* return excess runtime on last dequeue */
1412 return_cfs_rq_runtime(cfs_rq);
1413
Peter Zijlstra1e876232011-05-17 16:21:10 -07001414 update_min_vruntime(cfs_rq);
1415 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001416}
1417
1418/*
1419 * Preempt the current task with a newly woken task if needed:
1420 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001421static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001422check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001423{
Peter Zijlstra11697832007-09-05 14:32:49 +02001424 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001425 struct sched_entity *se;
1426 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001427
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001428 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001429 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001430 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001431 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001432 /*
1433 * The current task ran long enough, ensure it doesn't get
1434 * re-elected due to buddy favours.
1435 */
1436 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001437 return;
1438 }
1439
1440 /*
1441 * Ensure that a task that missed wakeup preemption by a
1442 * narrow margin doesn't have to wait for a full slice.
1443 * This also mitigates buddy induced latencies under load.
1444 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001445 if (delta_exec < sysctl_sched_min_granularity)
1446 return;
1447
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001448 se = __pick_first_entity(cfs_rq);
1449 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001450
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001451 if (delta < 0)
1452 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01001453
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001454 if (delta > ideal_runtime)
1455 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001456}
1457
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001458static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001459set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001460{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001461 /* 'current' is not kept within the tree. */
1462 if (se->on_rq) {
1463 /*
1464 * Any task has to be enqueued before it get to execute on
1465 * a CPU. So account for the time it spent waiting on the
1466 * runqueue.
1467 */
1468 update_stats_wait_end(cfs_rq, se);
1469 __dequeue_entity(cfs_rq, se);
1470 }
1471
Ingo Molnar79303e92007-08-09 11:16:47 +02001472 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001473 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001474#ifdef CONFIG_SCHEDSTATS
1475 /*
1476 * Track our maximum slice length, if the CPU's load is at
1477 * least twice that of our own weight (i.e. dont track it
1478 * when there are only lesser-weight tasks around):
1479 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001480 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001481 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001482 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1483 }
1484#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001485 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001486}
1487
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001488static int
1489wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1490
Rik van Rielac53db52011-02-01 09:51:03 -05001491/*
1492 * Pick the next process, keeping these things in mind, in this order:
1493 * 1) keep things fair between processes/task groups
1494 * 2) pick the "next" process, since someone really wants that to run
1495 * 3) pick the "last" process, for cache locality
1496 * 4) do not run the "skip" process, if something else is available
1497 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001498static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001499{
Rik van Rielac53db52011-02-01 09:51:03 -05001500 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001501 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001502
Rik van Rielac53db52011-02-01 09:51:03 -05001503 /*
1504 * Avoid running the skip buddy, if running something else can
1505 * be done without getting too unfair.
1506 */
1507 if (cfs_rq->skip == se) {
1508 struct sched_entity *second = __pick_next_entity(se);
1509 if (second && wakeup_preempt_entity(second, left) < 1)
1510 se = second;
1511 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001512
Mike Galbraithf685cea2009-10-23 23:09:22 +02001513 /*
1514 * Prefer last buddy, try to return the CPU to a preempted task.
1515 */
1516 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1517 se = cfs_rq->last;
1518
Rik van Rielac53db52011-02-01 09:51:03 -05001519 /*
1520 * Someone really wants this to run. If it's not unfair, run it.
1521 */
1522 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1523 se = cfs_rq->next;
1524
Mike Galbraithf685cea2009-10-23 23:09:22 +02001525 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001526
1527 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001528}
1529
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001530static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1531
Ingo Molnarab6cde22007-08-09 11:16:48 +02001532static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001533{
1534 /*
1535 * If still on the runqueue then deactivate_task()
1536 * was not called and update_curr() has to be done:
1537 */
1538 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001539 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001540
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001541 /* throttle cfs_rqs exceeding runtime */
1542 check_cfs_rq_runtime(cfs_rq);
1543
Peter Zijlstraddc97292007-10-15 17:00:10 +02001544 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001545 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001546 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001547 /* Put 'current' back into the tree. */
1548 __enqueue_entity(cfs_rq, prev);
1549 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001550 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001551}
1552
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001553static void
1554entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001555{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001556 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001557 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001558 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001559 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001560
Paul Turner43365bd2010-12-15 19:10:17 -08001561 /*
1562 * Update share accounting for long-running entities.
1563 */
1564 update_entity_shares_tick(cfs_rq);
1565
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001566#ifdef CONFIG_SCHED_HRTICK
1567 /*
1568 * queued ticks are scheduled to match the slice, so don't bother
1569 * validating it and just reschedule.
1570 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001571 if (queued) {
1572 resched_task(rq_of(cfs_rq)->curr);
1573 return;
1574 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001575 /*
1576 * don't let the period tick interfere with the hrtick preemption
1577 */
1578 if (!sched_feat(DOUBLE_TICK) &&
1579 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1580 return;
1581#endif
1582
Yong Zhang2c2efae2011-07-29 16:20:33 +08001583 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001584 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001585}
1586
Paul Turnerab84d312011-07-21 09:43:28 -07001587
1588/**************************************************
1589 * CFS bandwidth control machinery
1590 */
1591
1592#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001593
1594#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001595static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001596
1597static inline bool cfs_bandwidth_used(void)
1598{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001599 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001600}
1601
1602void account_cfs_bandwidth_used(int enabled, int was_enabled)
1603{
1604 /* only need to count groups transitioning between enabled/!enabled */
1605 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001606 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001607 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001608 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001609}
1610#else /* HAVE_JUMP_LABEL */
1611static bool cfs_bandwidth_used(void)
1612{
1613 return true;
1614}
1615
1616void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1617#endif /* HAVE_JUMP_LABEL */
1618
Paul Turnerab84d312011-07-21 09:43:28 -07001619/*
1620 * default period for cfs group bandwidth.
1621 * default: 0.1s, units: nanoseconds
1622 */
1623static inline u64 default_cfs_period(void)
1624{
1625 return 100000000ULL;
1626}
Paul Turnerec12cb72011-07-21 09:43:30 -07001627
1628static inline u64 sched_cfs_bandwidth_slice(void)
1629{
1630 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1631}
1632
Paul Turnera9cf55b2011-07-21 09:43:32 -07001633/*
1634 * Replenish runtime according to assigned quota and update expiration time.
1635 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1636 * additional synchronization around rq->lock.
1637 *
1638 * requires cfs_b->lock
1639 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001640void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001641{
1642 u64 now;
1643
1644 if (cfs_b->quota == RUNTIME_INF)
1645 return;
1646
1647 now = sched_clock_cpu(smp_processor_id());
1648 cfs_b->runtime = cfs_b->quota;
1649 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1650}
1651
Peter Zijlstra029632f2011-10-25 10:00:11 +02001652static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1653{
1654 return &tg->cfs_bandwidth;
1655}
1656
Paul Turner85dac902011-07-21 09:43:33 -07001657/* returns 0 on failure to allocate runtime */
1658static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001659{
1660 struct task_group *tg = cfs_rq->tg;
1661 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001662 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001663
1664 /* note: this is a positive sum as runtime_remaining <= 0 */
1665 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1666
1667 raw_spin_lock(&cfs_b->lock);
1668 if (cfs_b->quota == RUNTIME_INF)
1669 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001670 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001671 /*
1672 * If the bandwidth pool has become inactive, then at least one
1673 * period must have elapsed since the last consumption.
1674 * Refresh the global state and ensure bandwidth timer becomes
1675 * active.
1676 */
1677 if (!cfs_b->timer_active) {
1678 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001679 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001680 }
Paul Turner58088ad2011-07-21 09:43:31 -07001681
1682 if (cfs_b->runtime > 0) {
1683 amount = min(cfs_b->runtime, min_amount);
1684 cfs_b->runtime -= amount;
1685 cfs_b->idle = 0;
1686 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001687 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001688 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001689 raw_spin_unlock(&cfs_b->lock);
1690
1691 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001692 /*
1693 * we may have advanced our local expiration to account for allowed
1694 * spread between our sched_clock and the one on which runtime was
1695 * issued.
1696 */
1697 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1698 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001699
1700 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001701}
1702
1703/*
1704 * Note: This depends on the synchronization provided by sched_clock and the
1705 * fact that rq->clock snapshots this value.
1706 */
1707static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1708{
1709 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1710 struct rq *rq = rq_of(cfs_rq);
1711
1712 /* if the deadline is ahead of our clock, nothing to do */
1713 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1714 return;
1715
1716 if (cfs_rq->runtime_remaining < 0)
1717 return;
1718
1719 /*
1720 * If the local deadline has passed we have to consider the
1721 * possibility that our sched_clock is 'fast' and the global deadline
1722 * has not truly expired.
1723 *
1724 * Fortunately we can check determine whether this the case by checking
1725 * whether the global deadline has advanced.
1726 */
1727
1728 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1729 /* extend local deadline, drift is bounded above by 2 ticks */
1730 cfs_rq->runtime_expires += TICK_NSEC;
1731 } else {
1732 /* global deadline is ahead, expiration has passed */
1733 cfs_rq->runtime_remaining = 0;
1734 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001735}
1736
1737static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1738 unsigned long delta_exec)
1739{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001740 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001741 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001742 expire_cfs_rq_runtime(cfs_rq);
1743
1744 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001745 return;
1746
Paul Turner85dac902011-07-21 09:43:33 -07001747 /*
1748 * if we're unable to extend our runtime we resched so that the active
1749 * hierarchy can be throttled
1750 */
1751 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1752 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001753}
1754
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001755static __always_inline
1756void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001757{
Paul Turner56f570e2011-11-07 20:26:33 -08001758 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001759 return;
1760
1761 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1762}
1763
Paul Turner85dac902011-07-21 09:43:33 -07001764static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1765{
Paul Turner56f570e2011-11-07 20:26:33 -08001766 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001767}
1768
Paul Turner64660c82011-07-21 09:43:36 -07001769/* check whether cfs_rq, or any parent, is throttled */
1770static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1771{
Paul Turner56f570e2011-11-07 20:26:33 -08001772 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001773}
1774
1775/*
1776 * Ensure that neither of the group entities corresponding to src_cpu or
1777 * dest_cpu are members of a throttled hierarchy when performing group
1778 * load-balance operations.
1779 */
1780static inline int throttled_lb_pair(struct task_group *tg,
1781 int src_cpu, int dest_cpu)
1782{
1783 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1784
1785 src_cfs_rq = tg->cfs_rq[src_cpu];
1786 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1787
1788 return throttled_hierarchy(src_cfs_rq) ||
1789 throttled_hierarchy(dest_cfs_rq);
1790}
1791
1792/* updated child weight may affect parent so we have to do this bottom up */
1793static int tg_unthrottle_up(struct task_group *tg, void *data)
1794{
1795 struct rq *rq = data;
1796 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1797
1798 cfs_rq->throttle_count--;
1799#ifdef CONFIG_SMP
1800 if (!cfs_rq->throttle_count) {
1801 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1802
1803 /* leaving throttled state, advance shares averaging windows */
1804 cfs_rq->load_stamp += delta;
1805 cfs_rq->load_last += delta;
1806
1807 /* update entity weight now that we are on_rq again */
1808 update_cfs_shares(cfs_rq);
1809 }
1810#endif
1811
1812 return 0;
1813}
1814
1815static int tg_throttle_down(struct task_group *tg, void *data)
1816{
1817 struct rq *rq = data;
1818 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1819
1820 /* group is entering throttled state, record last load */
1821 if (!cfs_rq->throttle_count)
1822 update_cfs_load(cfs_rq, 0);
1823 cfs_rq->throttle_count++;
1824
1825 return 0;
1826}
1827
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001828static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001829{
1830 struct rq *rq = rq_of(cfs_rq);
1831 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1832 struct sched_entity *se;
1833 long task_delta, dequeue = 1;
1834
1835 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1836
1837 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001838 rcu_read_lock();
1839 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1840 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001841
1842 task_delta = cfs_rq->h_nr_running;
1843 for_each_sched_entity(se) {
1844 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1845 /* throttled entity or throttle-on-deactivate */
1846 if (!se->on_rq)
1847 break;
1848
1849 if (dequeue)
1850 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1851 qcfs_rq->h_nr_running -= task_delta;
1852
1853 if (qcfs_rq->load.weight)
1854 dequeue = 0;
1855 }
1856
1857 if (!se)
1858 rq->nr_running -= task_delta;
1859
1860 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001861 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001862 raw_spin_lock(&cfs_b->lock);
1863 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
1864 raw_spin_unlock(&cfs_b->lock);
1865}
1866
Peter Zijlstra029632f2011-10-25 10:00:11 +02001867void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001868{
1869 struct rq *rq = rq_of(cfs_rq);
1870 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1871 struct sched_entity *se;
1872 int enqueue = 1;
1873 long task_delta;
1874
1875 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1876
1877 cfs_rq->throttled = 0;
1878 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001879 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001880 list_del_rcu(&cfs_rq->throttled_list);
1881 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001882 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001883
Paul Turner64660c82011-07-21 09:43:36 -07001884 update_rq_clock(rq);
1885 /* update hierarchical throttle state */
1886 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1887
Paul Turner671fd9d2011-07-21 09:43:34 -07001888 if (!cfs_rq->load.weight)
1889 return;
1890
1891 task_delta = cfs_rq->h_nr_running;
1892 for_each_sched_entity(se) {
1893 if (se->on_rq)
1894 enqueue = 0;
1895
1896 cfs_rq = cfs_rq_of(se);
1897 if (enqueue)
1898 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1899 cfs_rq->h_nr_running += task_delta;
1900
1901 if (cfs_rq_throttled(cfs_rq))
1902 break;
1903 }
1904
1905 if (!se)
1906 rq->nr_running += task_delta;
1907
1908 /* determine whether we need to wake up potentially idle cpu */
1909 if (rq->curr == rq->idle && rq->cfs.nr_running)
1910 resched_task(rq->curr);
1911}
1912
1913static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1914 u64 remaining, u64 expires)
1915{
1916 struct cfs_rq *cfs_rq;
1917 u64 runtime = remaining;
1918
1919 rcu_read_lock();
1920 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1921 throttled_list) {
1922 struct rq *rq = rq_of(cfs_rq);
1923
1924 raw_spin_lock(&rq->lock);
1925 if (!cfs_rq_throttled(cfs_rq))
1926 goto next;
1927
1928 runtime = -cfs_rq->runtime_remaining + 1;
1929 if (runtime > remaining)
1930 runtime = remaining;
1931 remaining -= runtime;
1932
1933 cfs_rq->runtime_remaining += runtime;
1934 cfs_rq->runtime_expires = expires;
1935
1936 /* we check whether we're throttled above */
1937 if (cfs_rq->runtime_remaining > 0)
1938 unthrottle_cfs_rq(cfs_rq);
1939
1940next:
1941 raw_spin_unlock(&rq->lock);
1942
1943 if (!remaining)
1944 break;
1945 }
1946 rcu_read_unlock();
1947
1948 return remaining;
1949}
1950
Paul Turner58088ad2011-07-21 09:43:31 -07001951/*
1952 * Responsible for refilling a task_group's bandwidth and unthrottling its
1953 * cfs_rqs as appropriate. If there has been no activity within the last
1954 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1955 * used to track this state.
1956 */
1957static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1958{
Paul Turner671fd9d2011-07-21 09:43:34 -07001959 u64 runtime, runtime_expires;
1960 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001961
1962 raw_spin_lock(&cfs_b->lock);
1963 /* no need to continue the timer with no bandwidth constraint */
1964 if (cfs_b->quota == RUNTIME_INF)
1965 goto out_unlock;
1966
Paul Turner671fd9d2011-07-21 09:43:34 -07001967 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1968 /* idle depends on !throttled (for the case of a large deficit) */
1969 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001970 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001971
Paul Turnera9cf55b2011-07-21 09:43:32 -07001972 /* if we're going inactive then everything else can be deferred */
1973 if (idle)
1974 goto out_unlock;
1975
1976 __refill_cfs_bandwidth_runtime(cfs_b);
1977
Paul Turner671fd9d2011-07-21 09:43:34 -07001978 if (!throttled) {
1979 /* mark as potentially idle for the upcoming period */
1980 cfs_b->idle = 1;
1981 goto out_unlock;
1982 }
Paul Turner58088ad2011-07-21 09:43:31 -07001983
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001984 /* account preceding periods in which throttling occurred */
1985 cfs_b->nr_throttled += overrun;
1986
Paul Turner671fd9d2011-07-21 09:43:34 -07001987 /*
1988 * There are throttled entities so we must first use the new bandwidth
1989 * to unthrottle them before making it generally available. This
1990 * ensures that all existing debts will be paid before a new cfs_rq is
1991 * allowed to run.
1992 */
1993 runtime = cfs_b->runtime;
1994 runtime_expires = cfs_b->runtime_expires;
1995 cfs_b->runtime = 0;
1996
1997 /*
1998 * This check is repeated as we are holding onto the new bandwidth
1999 * while we unthrottle. This can potentially race with an unthrottled
2000 * group trying to acquire new bandwidth from the global pool.
2001 */
2002 while (throttled && runtime > 0) {
2003 raw_spin_unlock(&cfs_b->lock);
2004 /* we can't nest cfs_b->lock while distributing bandwidth */
2005 runtime = distribute_cfs_runtime(cfs_b, runtime,
2006 runtime_expires);
2007 raw_spin_lock(&cfs_b->lock);
2008
2009 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
2010 }
2011
2012 /* return (any) remaining runtime */
2013 cfs_b->runtime = runtime;
2014 /*
2015 * While we are ensured activity in the period following an
2016 * unthrottle, this also covers the case in which the new bandwidth is
2017 * insufficient to cover the existing bandwidth deficit. (Forcing the
2018 * timer to remain active while there are any throttled entities.)
2019 */
2020 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07002021out_unlock:
2022 if (idle)
2023 cfs_b->timer_active = 0;
2024 raw_spin_unlock(&cfs_b->lock);
2025
2026 return idle;
2027}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002028
Paul Turnerd8b49862011-07-21 09:43:41 -07002029/* a cfs_rq won't donate quota below this amount */
2030static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
2031/* minimum remaining period time to redistribute slack quota */
2032static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
2033/* how long we wait to gather additional slack before distributing */
2034static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
2035
2036/* are we near the end of the current quota period? */
2037static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
2038{
2039 struct hrtimer *refresh_timer = &cfs_b->period_timer;
2040 u64 remaining;
2041
2042 /* if the call-back is running a quota refresh is already occurring */
2043 if (hrtimer_callback_running(refresh_timer))
2044 return 1;
2045
2046 /* is a quota refresh about to occur? */
2047 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
2048 if (remaining < min_expire)
2049 return 1;
2050
2051 return 0;
2052}
2053
2054static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
2055{
2056 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
2057
2058 /* if there's a quota refresh soon don't bother with slack */
2059 if (runtime_refresh_within(cfs_b, min_left))
2060 return;
2061
2062 start_bandwidth_timer(&cfs_b->slack_timer,
2063 ns_to_ktime(cfs_bandwidth_slack_period));
2064}
2065
2066/* we know any runtime found here is valid as update_curr() precedes return */
2067static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2068{
2069 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2070 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
2071
2072 if (slack_runtime <= 0)
2073 return;
2074
2075 raw_spin_lock(&cfs_b->lock);
2076 if (cfs_b->quota != RUNTIME_INF &&
2077 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
2078 cfs_b->runtime += slack_runtime;
2079
2080 /* we are under rq->lock, defer unthrottling using a timer */
2081 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
2082 !list_empty(&cfs_b->throttled_cfs_rq))
2083 start_cfs_slack_bandwidth(cfs_b);
2084 }
2085 raw_spin_unlock(&cfs_b->lock);
2086
2087 /* even if it's not valid for return we don't want to try again */
2088 cfs_rq->runtime_remaining -= slack_runtime;
2089}
2090
2091static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2092{
Paul Turner56f570e2011-11-07 20:26:33 -08002093 if (!cfs_bandwidth_used())
2094 return;
2095
Paul Turnerfccfdc62011-11-07 20:26:34 -08002096 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07002097 return;
2098
2099 __return_cfs_rq_runtime(cfs_rq);
2100}
2101
2102/*
2103 * This is done with a timer (instead of inline with bandwidth return) since
2104 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
2105 */
2106static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
2107{
2108 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
2109 u64 expires;
2110
2111 /* confirm we're still not at a refresh boundary */
2112 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
2113 return;
2114
2115 raw_spin_lock(&cfs_b->lock);
2116 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
2117 runtime = cfs_b->runtime;
2118 cfs_b->runtime = 0;
2119 }
2120 expires = cfs_b->runtime_expires;
2121 raw_spin_unlock(&cfs_b->lock);
2122
2123 if (!runtime)
2124 return;
2125
2126 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
2127
2128 raw_spin_lock(&cfs_b->lock);
2129 if (expires == cfs_b->runtime_expires)
2130 cfs_b->runtime = runtime;
2131 raw_spin_unlock(&cfs_b->lock);
2132}
2133
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002134/*
2135 * When a group wakes up we want to make sure that its quota is not already
2136 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
2137 * runtime as update_curr() throttling can not not trigger until it's on-rq.
2138 */
2139static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
2140{
Paul Turner56f570e2011-11-07 20:26:33 -08002141 if (!cfs_bandwidth_used())
2142 return;
2143
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002144 /* an active group must be handled by the update_curr()->put() path */
2145 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
2146 return;
2147
2148 /* ensure the group is not already throttled */
2149 if (cfs_rq_throttled(cfs_rq))
2150 return;
2151
2152 /* update runtime allocation */
2153 account_cfs_rq_runtime(cfs_rq, 0);
2154 if (cfs_rq->runtime_remaining <= 0)
2155 throttle_cfs_rq(cfs_rq);
2156}
2157
2158/* conditionally throttle active cfs_rq's from put_prev_entity() */
2159static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2160{
Paul Turner56f570e2011-11-07 20:26:33 -08002161 if (!cfs_bandwidth_used())
2162 return;
2163
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002164 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
2165 return;
2166
2167 /*
2168 * it's possible for a throttled entity to be forced into a running
2169 * state (e.g. set_curr_task), in this case we're finished.
2170 */
2171 if (cfs_rq_throttled(cfs_rq))
2172 return;
2173
2174 throttle_cfs_rq(cfs_rq);
2175}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002176
2177static inline u64 default_cfs_period(void);
2178static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2179static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2180
2181static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2182{
2183 struct cfs_bandwidth *cfs_b =
2184 container_of(timer, struct cfs_bandwidth, slack_timer);
2185 do_sched_cfs_slack_timer(cfs_b);
2186
2187 return HRTIMER_NORESTART;
2188}
2189
2190static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2191{
2192 struct cfs_bandwidth *cfs_b =
2193 container_of(timer, struct cfs_bandwidth, period_timer);
2194 ktime_t now;
2195 int overrun;
2196 int idle = 0;
2197
2198 for (;;) {
2199 now = hrtimer_cb_get_time(timer);
2200 overrun = hrtimer_forward(timer, now, cfs_b->period);
2201
2202 if (!overrun)
2203 break;
2204
2205 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2206 }
2207
2208 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2209}
2210
2211void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2212{
2213 raw_spin_lock_init(&cfs_b->lock);
2214 cfs_b->runtime = 0;
2215 cfs_b->quota = RUNTIME_INF;
2216 cfs_b->period = ns_to_ktime(default_cfs_period());
2217
2218 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2219 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2220 cfs_b->period_timer.function = sched_cfs_period_timer;
2221 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2222 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2223}
2224
2225static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2226{
2227 cfs_rq->runtime_enabled = 0;
2228 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2229}
2230
2231/* requires cfs_b->lock, may release to reprogram timer */
2232void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2233{
2234 /*
2235 * The timer may be active because we're trying to set a new bandwidth
2236 * period or because we're racing with the tear-down path
2237 * (timer_active==0 becomes visible before the hrtimer call-back
2238 * terminates). In either case we ensure that it's re-programmed
2239 */
2240 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2241 raw_spin_unlock(&cfs_b->lock);
2242 /* ensure cfs_b->lock is available while we wait */
2243 hrtimer_cancel(&cfs_b->period_timer);
2244
2245 raw_spin_lock(&cfs_b->lock);
2246 /* if someone else restarted the timer then we're done */
2247 if (cfs_b->timer_active)
2248 return;
2249 }
2250
2251 cfs_b->timer_active = 1;
2252 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2253}
2254
2255static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2256{
2257 hrtimer_cancel(&cfs_b->period_timer);
2258 hrtimer_cancel(&cfs_b->slack_timer);
2259}
2260
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002261static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002262{
2263 struct cfs_rq *cfs_rq;
2264
2265 for_each_leaf_cfs_rq(rq, cfs_rq) {
2266 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2267
2268 if (!cfs_rq->runtime_enabled)
2269 continue;
2270
2271 /*
2272 * clock_task is not advancing so we just need to make sure
2273 * there's some valid quota amount
2274 */
2275 cfs_rq->runtime_remaining = cfs_b->quota;
2276 if (cfs_rq_throttled(cfs_rq))
2277 unthrottle_cfs_rq(cfs_rq);
2278 }
2279}
2280
2281#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002282static __always_inline
2283void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002284static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2285static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002286static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002287
2288static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2289{
2290 return 0;
2291}
Paul Turner64660c82011-07-21 09:43:36 -07002292
2293static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2294{
2295 return 0;
2296}
2297
2298static inline int throttled_lb_pair(struct task_group *tg,
2299 int src_cpu, int dest_cpu)
2300{
2301 return 0;
2302}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002303
2304void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2305
2306#ifdef CONFIG_FAIR_GROUP_SCHED
2307static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002308#endif
2309
Peter Zijlstra029632f2011-10-25 10:00:11 +02002310static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2311{
2312 return NULL;
2313}
2314static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07002315static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002316
2317#endif /* CONFIG_CFS_BANDWIDTH */
2318
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002319/**************************************************
2320 * CFS operations on tasks:
2321 */
2322
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002323#ifdef CONFIG_SCHED_HRTICK
2324static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2325{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002326 struct sched_entity *se = &p->se;
2327 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2328
2329 WARN_ON(task_rq(p) != rq);
2330
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002331 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002332 u64 slice = sched_slice(cfs_rq, se);
2333 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2334 s64 delta = slice - ran;
2335
2336 if (delta < 0) {
2337 if (rq->curr == p)
2338 resched_task(p);
2339 return;
2340 }
2341
2342 /*
2343 * Don't schedule slices shorter than 10000ns, that just
2344 * doesn't make sense. Rely on vruntime for fairness.
2345 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002346 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002347 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002348
Peter Zijlstra31656512008-07-18 18:01:23 +02002349 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002350 }
2351}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002352
2353/*
2354 * called from enqueue/dequeue and updates the hrtick when the
2355 * current task is from our class and nr_running is low enough
2356 * to matter.
2357 */
2358static void hrtick_update(struct rq *rq)
2359{
2360 struct task_struct *curr = rq->curr;
2361
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002362 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002363 return;
2364
2365 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
2366 hrtick_start_fair(rq, curr);
2367}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302368#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002369static inline void
2370hrtick_start_fair(struct rq *rq, struct task_struct *p)
2371{
2372}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002373
2374static inline void hrtick_update(struct rq *rq)
2375{
2376}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002377#endif
2378
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002379/*
2380 * The enqueue_task method is called before nr_running is
2381 * increased. Here we update the fair scheduling stats and
2382 * then put the task into the rbtree:
2383 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002384static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002385enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002386{
2387 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002388 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002389
2390 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002391 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002392 break;
2393 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002394 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002395
2396 /*
2397 * end evaluation on encountering a throttled cfs_rq
2398 *
2399 * note: in the case of encountering a throttled cfs_rq we will
2400 * post the final h_nr_running increment below.
2401 */
2402 if (cfs_rq_throttled(cfs_rq))
2403 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002404 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002405
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002406 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002407 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002408
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002409 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002410 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002411 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002412
Paul Turner85dac902011-07-21 09:43:33 -07002413 if (cfs_rq_throttled(cfs_rq))
2414 break;
2415
Paul Turnerd6b55912010-11-15 15:47:09 -08002416 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002417 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002418 }
2419
Paul Turner85dac902011-07-21 09:43:33 -07002420 if (!se)
2421 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002422 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002423}
2424
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002425static void set_next_buddy(struct sched_entity *se);
2426
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002427/*
2428 * The dequeue_task method is called before nr_running is
2429 * decreased. We remove the task from the rbtree and
2430 * update the fair scheduling stats:
2431 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002432static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002433{
2434 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002435 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002436 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002437
2438 for_each_sched_entity(se) {
2439 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002440 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002441
2442 /*
2443 * end evaluation on encountering a throttled cfs_rq
2444 *
2445 * note: in the case of encountering a throttled cfs_rq we will
2446 * post the final h_nr_running decrement below.
2447 */
2448 if (cfs_rq_throttled(cfs_rq))
2449 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002450 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002451
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002452 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002453 if (cfs_rq->load.weight) {
2454 /*
2455 * Bias pick_next to pick a task from this cfs_rq, as
2456 * p is sleeping when it is within its sched_slice.
2457 */
2458 if (task_sleep && parent_entity(se))
2459 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002460
2461 /* avoid re-evaluating load for this entity */
2462 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002463 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002464 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002465 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002466 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002467
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002468 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002469 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002470 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002471
Paul Turner85dac902011-07-21 09:43:33 -07002472 if (cfs_rq_throttled(cfs_rq))
2473 break;
2474
Paul Turnerd6b55912010-11-15 15:47:09 -08002475 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002476 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002477 }
2478
Paul Turner85dac902011-07-21 09:43:33 -07002479 if (!se)
2480 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002481 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002482}
2483
Gregory Haskinse7693a32008-01-25 21:08:09 +01002484#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002485/* Used instead of source_load when we know the type == 0 */
2486static unsigned long weighted_cpuload(const int cpu)
2487{
2488 return cpu_rq(cpu)->load.weight;
2489}
2490
2491/*
2492 * Return a low guess at the load of a migration-source cpu weighted
2493 * according to the scheduling class and "nice" value.
2494 *
2495 * We want to under-estimate the load of migration sources, to
2496 * balance conservatively.
2497 */
2498static unsigned long source_load(int cpu, int type)
2499{
2500 struct rq *rq = cpu_rq(cpu);
2501 unsigned long total = weighted_cpuload(cpu);
2502
2503 if (type == 0 || !sched_feat(LB_BIAS))
2504 return total;
2505
2506 return min(rq->cpu_load[type-1], total);
2507}
2508
2509/*
2510 * Return a high guess at the load of a migration-target cpu weighted
2511 * according to the scheduling class and "nice" value.
2512 */
2513static unsigned long target_load(int cpu, int type)
2514{
2515 struct rq *rq = cpu_rq(cpu);
2516 unsigned long total = weighted_cpuload(cpu);
2517
2518 if (type == 0 || !sched_feat(LB_BIAS))
2519 return total;
2520
2521 return max(rq->cpu_load[type-1], total);
2522}
2523
2524static unsigned long power_of(int cpu)
2525{
2526 return cpu_rq(cpu)->cpu_power;
2527}
2528
2529static unsigned long cpu_avg_load_per_task(int cpu)
2530{
2531 struct rq *rq = cpu_rq(cpu);
2532 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2533
2534 if (nr_running)
2535 return rq->load.weight / nr_running;
2536
2537 return 0;
2538}
2539
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002540
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002541static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002542{
2543 struct sched_entity *se = &p->se;
2544 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002545 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002546
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002547#ifndef CONFIG_64BIT
2548 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002549
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002550 do {
2551 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2552 smp_rmb();
2553 min_vruntime = cfs_rq->min_vruntime;
2554 } while (min_vruntime != min_vruntime_copy);
2555#else
2556 min_vruntime = cfs_rq->min_vruntime;
2557#endif
2558
2559 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002560}
2561
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002562#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002563/*
2564 * effective_load() calculates the load change as seen from the root_task_group
2565 *
2566 * Adding load to a group doesn't make a group heavier, but can cause movement
2567 * of group shares between cpus. Assuming the shares were perfectly aligned one
2568 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002569 *
2570 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2571 * on this @cpu and results in a total addition (subtraction) of @wg to the
2572 * total group weight.
2573 *
2574 * Given a runqueue weight distribution (rw_i) we can compute a shares
2575 * distribution (s_i) using:
2576 *
2577 * s_i = rw_i / \Sum rw_j (1)
2578 *
2579 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2580 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2581 * shares distribution (s_i):
2582 *
2583 * rw_i = { 2, 4, 1, 0 }
2584 * s_i = { 2/7, 4/7, 1/7, 0 }
2585 *
2586 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2587 * task used to run on and the CPU the waker is running on), we need to
2588 * compute the effect of waking a task on either CPU and, in case of a sync
2589 * wakeup, compute the effect of the current task going to sleep.
2590 *
2591 * So for a change of @wl to the local @cpu with an overall group weight change
2592 * of @wl we can compute the new shares distribution (s'_i) using:
2593 *
2594 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2595 *
2596 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2597 * differences in waking a task to CPU 0. The additional task changes the
2598 * weight and shares distributions like:
2599 *
2600 * rw'_i = { 3, 4, 1, 0 }
2601 * s'_i = { 3/8, 4/8, 1/8, 0 }
2602 *
2603 * We can then compute the difference in effective weight by using:
2604 *
2605 * dw_i = S * (s'_i - s_i) (3)
2606 *
2607 * Where 'S' is the group weight as seen by its parent.
2608 *
2609 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2610 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2611 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002612 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002613static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002614{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002615 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002616
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002617 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002618 return wl;
2619
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002620 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002621 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002622
Paul Turner977dda72011-01-14 17:57:50 -08002623 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002624
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002625 /*
2626 * W = @wg + \Sum rw_j
2627 */
2628 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002629
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002630 /*
2631 * w = rw_i + @wl
2632 */
2633 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002634
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002635 /*
2636 * wl = S * s'_i; see (2)
2637 */
2638 if (W > 0 && w < W)
2639 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002640 else
2641 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002642
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002643 /*
2644 * Per the above, wl is the new se->load.weight value; since
2645 * those are clipped to [MIN_SHARES, ...) do so now. See
2646 * calc_cfs_shares().
2647 */
Paul Turner977dda72011-01-14 17:57:50 -08002648 if (wl < MIN_SHARES)
2649 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002650
2651 /*
2652 * wl = dw_i = S * (s'_i - s_i); see (3)
2653 */
Paul Turner977dda72011-01-14 17:57:50 -08002654 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002655
2656 /*
2657 * Recursively apply this logic to all parent groups to compute
2658 * the final effective load change on the root group. Since
2659 * only the @tg group gets extra weight, all parent groups can
2660 * only redistribute existing shares. @wl is the shift in shares
2661 * resulting from this level per the above.
2662 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002663 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002664 }
2665
2666 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002667}
2668#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002669
Peter Zijlstra83378262008-06-27 13:41:37 +02002670static inline unsigned long effective_load(struct task_group *tg, int cpu,
2671 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002672{
Peter Zijlstra83378262008-06-27 13:41:37 +02002673 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002674}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002675
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002676#endif
2677
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002678static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002679{
Paul Turnere37b6a72011-01-21 20:44:59 -08002680 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002681 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002682 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002683 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002684 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002685 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002686
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002687 idx = sd->wake_idx;
2688 this_cpu = smp_processor_id();
2689 prev_cpu = task_cpu(p);
2690 load = source_load(prev_cpu, idx);
2691 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002692
2693 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002694 * If sync wakeup then subtract the (maximum possible)
2695 * effect of the currently running task from the load
2696 * of the current CPU:
2697 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002698 if (sync) {
2699 tg = task_group(current);
2700 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002701
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002702 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002703 load += effective_load(tg, prev_cpu, 0, -weight);
2704 }
2705
2706 tg = task_group(p);
2707 weight = p->se.load.weight;
2708
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002709 /*
2710 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002711 * due to the sync cause above having dropped this_load to 0, we'll
2712 * always have an imbalance, but there's really nothing you can do
2713 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002714 *
2715 * Otherwise check if either cpus are near enough in load to allow this
2716 * task to be woken on this_cpu.
2717 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002718 if (this_load > 0) {
2719 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002720
2721 this_eff_load = 100;
2722 this_eff_load *= power_of(prev_cpu);
2723 this_eff_load *= this_load +
2724 effective_load(tg, this_cpu, weight, weight);
2725
2726 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2727 prev_eff_load *= power_of(this_cpu);
2728 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2729
2730 balanced = this_eff_load <= prev_eff_load;
2731 } else
2732 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002733
2734 /*
2735 * If the currently running task will sleep within
2736 * a reasonable amount of time then attract this newly
2737 * woken task:
2738 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002739 if (sync && balanced)
2740 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002741
Lucas De Marchi41acab82010-03-10 23:37:45 -03002742 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002743 tl_per_task = cpu_avg_load_per_task(this_cpu);
2744
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002745 if (balanced ||
2746 (this_load <= load &&
2747 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002748 /*
2749 * This domain has SD_WAKE_AFFINE and
2750 * p is cache cold in this domain, and
2751 * there is no bad imbalance.
2752 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002753 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002754 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002755
2756 return 1;
2757 }
2758 return 0;
2759}
2760
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002761/*
2762 * find_idlest_group finds and returns the least busy CPU group within the
2763 * domain.
2764 */
2765static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002766find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002767 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002768{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002769 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002770 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002771 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002772
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002773 do {
2774 unsigned long load, avg_load;
2775 int local_group;
2776 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002777
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002778 /* Skip over this group if it has no CPUs allowed */
2779 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002780 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002781 continue;
2782
2783 local_group = cpumask_test_cpu(this_cpu,
2784 sched_group_cpus(group));
2785
2786 /* Tally up the load of all CPUs in the group */
2787 avg_load = 0;
2788
2789 for_each_cpu(i, sched_group_cpus(group)) {
2790 /* Bias balancing toward cpus of our domain */
2791 if (local_group)
2792 load = source_load(i, load_idx);
2793 else
2794 load = target_load(i, load_idx);
2795
2796 avg_load += load;
2797 }
2798
2799 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002800 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002801
2802 if (local_group) {
2803 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002804 } else if (avg_load < min_load) {
2805 min_load = avg_load;
2806 idlest = group;
2807 }
2808 } while (group = group->next, group != sd->groups);
2809
2810 if (!idlest || 100*this_load < imbalance*min_load)
2811 return NULL;
2812 return idlest;
2813}
2814
2815/*
2816 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2817 */
2818static int
2819find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2820{
2821 unsigned long load, min_load = ULONG_MAX;
2822 int idlest = -1;
2823 int i;
2824
2825 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002826 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002827 load = weighted_cpuload(i);
2828
2829 if (load < min_load || (load == min_load && i == this_cpu)) {
2830 min_load = load;
2831 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002832 }
2833 }
2834
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002835 return idlest;
2836}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002837
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002838/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002839 * Try and locate an idle CPU in the sched_domain.
2840 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002841static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002842{
2843 int cpu = smp_processor_id();
2844 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002845 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07002846 struct sched_group *sg;
2847 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002848
2849 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002850 * If the task is going to be woken-up on this cpu and if it is
2851 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002852 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002853 if (target == cpu && idle_cpu(cpu))
2854 return cpu;
2855
2856 /*
2857 * If the task is going to be woken-up on the cpu where it previously
2858 * ran and if it is currently idle, then it the right target.
2859 */
2860 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002861 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002862
2863 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07002864 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002865 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002866 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002867 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07002868 sg = sd->groups;
2869 do {
2870 if (!cpumask_intersects(sched_group_cpus(sg),
2871 tsk_cpus_allowed(p)))
2872 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02002873
Linus Torvalds37407ea2012-09-16 12:29:43 -07002874 for_each_cpu(i, sched_group_cpus(sg)) {
2875 if (!idle_cpu(i))
2876 goto next;
2877 }
2878
2879 target = cpumask_first_and(sched_group_cpus(sg),
2880 tsk_cpus_allowed(p));
2881 goto done;
2882next:
2883 sg = sg->next;
2884 } while (sg != sd->groups);
2885 }
2886done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002887 return target;
2888}
2889
2890/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002891 * sched_balance_self: balance the current task (running on cpu) in domains
2892 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2893 * SD_BALANCE_EXEC.
2894 *
2895 * Balance, ie. select the least loaded group.
2896 *
2897 * Returns the target CPU number, or the same CPU if no balancing is needed.
2898 *
2899 * preempt must be disabled.
2900 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002901static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002902select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002903{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002904 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002905 int cpu = smp_processor_id();
2906 int prev_cpu = task_cpu(p);
2907 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002908 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002909 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002910
Peter Zijlstra29baa742012-04-23 12:11:21 +02002911 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01002912 return prev_cpu;
2913
Peter Zijlstra0763a662009-09-14 19:37:39 +02002914 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002915 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002916 want_affine = 1;
2917 new_cpu = prev_cpu;
2918 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002919
Peter Zijlstradce840a2011-04-07 14:09:50 +02002920 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002921 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002922 if (!(tmp->flags & SD_LOAD_BALANCE))
2923 continue;
2924
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002925 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002926 * If both cpu and prev_cpu are part of this domain,
2927 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002928 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002929 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2930 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2931 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08002932 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002933 }
2934
Alex Shif03542a2012-07-26 08:55:34 +08002935 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002936 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002937 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002938
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002939 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08002940 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002941 prev_cpu = cpu;
2942
2943 new_cpu = select_idle_sibling(p, prev_cpu);
2944 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002945 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002946
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002947 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002948 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002949 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002950 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002951
Peter Zijlstra0763a662009-09-14 19:37:39 +02002952 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002953 sd = sd->child;
2954 continue;
2955 }
2956
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002957 if (sd_flag & SD_BALANCE_WAKE)
2958 load_idx = sd->wake_idx;
2959
2960 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002961 if (!group) {
2962 sd = sd->child;
2963 continue;
2964 }
2965
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002966 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002967 if (new_cpu == -1 || new_cpu == cpu) {
2968 /* Now try balancing at a lower domain level of cpu */
2969 sd = sd->child;
2970 continue;
2971 }
2972
2973 /* Now try balancing at a lower domain level of new_cpu */
2974 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002975 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002976 sd = NULL;
2977 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002978 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002979 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002980 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002981 sd = tmp;
2982 }
2983 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002984 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002985unlock:
2986 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002987
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002988 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002989}
2990#endif /* CONFIG_SMP */
2991
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002992static unsigned long
2993wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002994{
2995 unsigned long gran = sysctl_sched_wakeup_granularity;
2996
2997 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002998 * Since its curr running now, convert the gran from real-time
2999 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01003000 *
3001 * By using 'se' instead of 'curr' we penalize light tasks, so
3002 * they get preempted easier. That is, if 'se' < 'curr' then
3003 * the resulting gran will be larger, therefore penalizing the
3004 * lighter, if otoh 'se' > 'curr' then the resulting gran will
3005 * be smaller, again penalizing the lighter task.
3006 *
3007 * This is especially important for buddies when the leftmost
3008 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003009 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08003010 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02003011}
3012
3013/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02003014 * Should 'se' preempt 'curr'.
3015 *
3016 * |s1
3017 * |s2
3018 * |s3
3019 * g
3020 * |<--->|c
3021 *
3022 * w(c, s1) = -1
3023 * w(c, s2) = 0
3024 * w(c, s3) = 1
3025 *
3026 */
3027static int
3028wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
3029{
3030 s64 gran, vdiff = curr->vruntime - se->vruntime;
3031
3032 if (vdiff <= 0)
3033 return -1;
3034
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01003035 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02003036 if (vdiff > gran)
3037 return 1;
3038
3039 return 0;
3040}
3041
Peter Zijlstra02479092008-11-04 21:25:10 +01003042static void set_last_buddy(struct sched_entity *se)
3043{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003044 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3045 return;
3046
3047 for_each_sched_entity(se)
3048 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003049}
3050
3051static void set_next_buddy(struct sched_entity *se)
3052{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003053 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
3054 return;
3055
3056 for_each_sched_entity(se)
3057 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01003058}
3059
Rik van Rielac53db52011-02-01 09:51:03 -05003060static void set_skip_buddy(struct sched_entity *se)
3061{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07003062 for_each_sched_entity(se)
3063 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05003064}
3065
Peter Zijlstra464b7522008-10-24 11:06:15 +02003066/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003067 * Preempt the current task with a newly woken task if needed:
3068 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02003069static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003070{
3071 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02003072 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003073 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003074 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003075 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01003076
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01003077 if (unlikely(se == pse))
3078 return;
3079
Paul Turner5238cdd2011-07-21 09:43:37 -07003080 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003081 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07003082 * unconditionally check_prempt_curr() after an enqueue (which may have
3083 * lead to a throttle). This both saves work and prevents false
3084 * next-buddy nomination below.
3085 */
3086 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
3087 return;
3088
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003089 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02003090 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003091 next_buddy_marked = 1;
3092 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02003093
Bharata B Raoaec0a512008-08-28 14:42:49 +05303094 /*
3095 * We can come here with TIF_NEED_RESCHED already set from new task
3096 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07003097 *
3098 * Note: this also catches the edge-case of curr being in a throttled
3099 * group (e.g. via set_curr_task), since update_curr() (in the
3100 * enqueue of curr) will have resulted in resched being set. This
3101 * prevents us from potentially nominating it as a false LAST_BUDDY
3102 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05303103 */
3104 if (test_tsk_need_resched(curr))
3105 return;
3106
Darren Harta2f5c9a2011-02-22 13:04:33 -08003107 /* Idle tasks are by definition preempted by non-idle tasks. */
3108 if (unlikely(curr->policy == SCHED_IDLE) &&
3109 likely(p->policy != SCHED_IDLE))
3110 goto preempt;
3111
Ingo Molnar91c234b2007-10-15 17:00:18 +02003112 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08003113 * Batch and idle tasks do not preempt non-idle tasks (their preemption
3114 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02003115 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01003116 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02003117 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003118
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003119 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07003120 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003121 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003122 if (wakeup_preempt_entity(se, pse) == 1) {
3123 /*
3124 * Bias pick_next to pick the sched entity that is
3125 * triggering this preemption.
3126 */
3127 if (!next_buddy_marked)
3128 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003129 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003130 }
Jupyung Leea65ac742009-11-17 18:51:40 +09003131
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01003132 return;
3133
3134preempt:
3135 resched_task(curr);
3136 /*
3137 * Only set the backward buddy when the current task is still
3138 * on the rq. This can happen when a wakeup gets interleaved
3139 * with schedule on the ->pre_schedule() or idle_balance()
3140 * point, either of which can * drop the rq lock.
3141 *
3142 * Also, during early boot the idle thread is in the fair class,
3143 * for obvious reasons its a bad idea to schedule back to it.
3144 */
3145 if (unlikely(!se->on_rq || curr == rq->idle))
3146 return;
3147
3148 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
3149 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003150}
3151
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003152static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003153{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003154 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003155 struct cfs_rq *cfs_rq = &rq->cfs;
3156 struct sched_entity *se;
3157
Tim Blechmann36ace272009-11-24 11:55:45 +01003158 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003159 return NULL;
3160
3161 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003162 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003163 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003164 cfs_rq = group_cfs_rq(se);
3165 } while (cfs_rq);
3166
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003167 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003168 if (hrtick_enabled(rq))
3169 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003170
3171 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003172}
3173
3174/*
3175 * Account for a descheduled task:
3176 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003177static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003178{
3179 struct sched_entity *se = &prev->se;
3180 struct cfs_rq *cfs_rq;
3181
3182 for_each_sched_entity(se) {
3183 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003184 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003185 }
3186}
3187
Rik van Rielac53db52011-02-01 09:51:03 -05003188/*
3189 * sched_yield() is very simple
3190 *
3191 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3192 */
3193static void yield_task_fair(struct rq *rq)
3194{
3195 struct task_struct *curr = rq->curr;
3196 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3197 struct sched_entity *se = &curr->se;
3198
3199 /*
3200 * Are we the only task in the tree?
3201 */
3202 if (unlikely(rq->nr_running == 1))
3203 return;
3204
3205 clear_buddies(cfs_rq, se);
3206
3207 if (curr->policy != SCHED_BATCH) {
3208 update_rq_clock(rq);
3209 /*
3210 * Update run-time statistics of the 'current'.
3211 */
3212 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003213 /*
3214 * Tell update_rq_clock() that we've just updated,
3215 * so we don't do microscopic update in schedule()
3216 * and double the fastpath cost.
3217 */
3218 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003219 }
3220
3221 set_skip_buddy(se);
3222}
3223
Mike Galbraithd95f4122011-02-01 09:50:51 -05003224static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3225{
3226 struct sched_entity *se = &p->se;
3227
Paul Turner5238cdd2011-07-21 09:43:37 -07003228 /* throttled hierarchies are not runnable */
3229 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003230 return false;
3231
3232 /* Tell the scheduler that we'd really like pse to run next. */
3233 set_next_buddy(se);
3234
Mike Galbraithd95f4122011-02-01 09:50:51 -05003235 yield_task_fair(rq);
3236
3237 return true;
3238}
3239
Peter Williams681f3e62007-10-24 18:23:51 +02003240#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003241/**************************************************
3242 * Fair scheduling class load-balancing methods:
3243 */
3244
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003245static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3246
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003247#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003248#define LBF_NEED_BREAK 0x02
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303249#define LBF_SOME_PINNED 0x04
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003250
3251struct lb_env {
3252 struct sched_domain *sd;
3253
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003254 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05303255 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003256
3257 int dst_cpu;
3258 struct rq *dst_rq;
3259
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303260 struct cpumask *dst_grpmask;
3261 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003262 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003263 long imbalance;
Michael Wangb9403132012-07-12 16:10:13 +08003264 /* The set of CPUs under consideration for load-balancing */
3265 struct cpumask *cpus;
3266
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003267 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003268
3269 unsigned int loop;
3270 unsigned int loop_break;
3271 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003272};
3273
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003274/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003275 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003276 * Both runqueues must be locked.
3277 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003278static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003279{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003280 deactivate_task(env->src_rq, p, 0);
3281 set_task_cpu(p, env->dst_cpu);
3282 activate_task(env->dst_rq, p, 0);
3283 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003284}
3285
3286/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003287 * Is this task likely cache-hot:
3288 */
3289static int
3290task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3291{
3292 s64 delta;
3293
3294 if (p->sched_class != &fair_sched_class)
3295 return 0;
3296
3297 if (unlikely(p->policy == SCHED_IDLE))
3298 return 0;
3299
3300 /*
3301 * Buddy candidates are cache hot:
3302 */
3303 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3304 (&p->se == cfs_rq_of(&p->se)->next ||
3305 &p->se == cfs_rq_of(&p->se)->last))
3306 return 1;
3307
3308 if (sysctl_sched_migration_cost == -1)
3309 return 1;
3310 if (sysctl_sched_migration_cost == 0)
3311 return 0;
3312
3313 delta = now - p->se.exec_start;
3314
3315 return delta < (s64)sysctl_sched_migration_cost;
3316}
3317
3318/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003319 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3320 */
3321static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003322int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003323{
3324 int tsk_cache_hot = 0;
3325 /*
3326 * We do not migrate tasks that are:
3327 * 1) running (obviously), or
3328 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3329 * 3) are cache-hot on their current CPU.
3330 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003331 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303332 int new_dst_cpu;
3333
Lucas De Marchi41acab82010-03-10 23:37:45 -03003334 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303335
3336 /*
3337 * Remember if this task can be migrated to any other cpu in
3338 * our sched_group. We may want to revisit it if we couldn't
3339 * meet load balance goals by pulling other tasks on src_cpu.
3340 *
3341 * Also avoid computing new_dst_cpu if we have already computed
3342 * one in current iteration.
3343 */
3344 if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
3345 return 0;
3346
3347 new_dst_cpu = cpumask_first_and(env->dst_grpmask,
3348 tsk_cpus_allowed(p));
3349 if (new_dst_cpu < nr_cpu_ids) {
3350 env->flags |= LBF_SOME_PINNED;
3351 env->new_dst_cpu = new_dst_cpu;
3352 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003353 return 0;
3354 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05303355
3356 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003357 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003358
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003359 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003360 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003361 return 0;
3362 }
3363
3364 /*
3365 * Aggressive migration if:
3366 * 1) task is cache cold, or
3367 * 2) too many balance attempts have failed.
3368 */
3369
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003370 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003371 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003372 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003373#ifdef CONFIG_SCHEDSTATS
3374 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003375 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003376 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003377 }
3378#endif
3379 return 1;
3380 }
3381
3382 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003383 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003384 return 0;
3385 }
3386 return 1;
3387}
3388
Peter Zijlstra897c3952009-12-17 17:45:42 +01003389/*
3390 * move_one_task tries to move exactly one task from busiest to this_rq, as
3391 * part of active balancing operations within "domain".
3392 * Returns 1 if successful and 0 otherwise.
3393 *
3394 * Called with both runqueues locked.
3395 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003396static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003397{
3398 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003399
Peter Zijlstra367456c2012-02-20 21:49:09 +01003400 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3401 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3402 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003403
Peter Zijlstra367456c2012-02-20 21:49:09 +01003404 if (!can_migrate_task(p, env))
3405 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003406
Peter Zijlstra367456c2012-02-20 21:49:09 +01003407 move_task(p, env);
3408 /*
3409 * Right now, this is only the second place move_task()
3410 * is called, so we can safely collect move_task()
3411 * stats here rather than inside move_task().
3412 */
3413 schedstat_inc(env->sd, lb_gained[env->idle]);
3414 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003415 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003416 return 0;
3417}
3418
Peter Zijlstra367456c2012-02-20 21:49:09 +01003419static unsigned long task_h_load(struct task_struct *p);
3420
Peter Zijlstraeb953082012-04-17 13:38:40 +02003421static const unsigned int sched_nr_migrate_break = 32;
3422
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003423/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003424 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003425 * this_rq, as part of a balancing operation within domain "sd".
3426 * Returns 1 if successful and 0 otherwise.
3427 *
3428 * Called with both runqueues locked.
3429 */
3430static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003431{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003432 struct list_head *tasks = &env->src_rq->cfs_tasks;
3433 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003434 unsigned long load;
3435 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003436
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003437 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003438 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003439
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003440 while (!list_empty(tasks)) {
3441 p = list_first_entry(tasks, struct task_struct, se.group_node);
3442
Peter Zijlstra367456c2012-02-20 21:49:09 +01003443 env->loop++;
3444 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003445 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003446 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003447
3448 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003449 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003450 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003451 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003452 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003453 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003454
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003455 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003456 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003457
Peter Zijlstra367456c2012-02-20 21:49:09 +01003458 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003459
Peter Zijlstraeb953082012-04-17 13:38:40 +02003460 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003461 goto next;
3462
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003463 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003464 goto next;
3465
3466 if (!can_migrate_task(p, env))
3467 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003468
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003469 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003470 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003471 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003472
3473#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003474 /*
3475 * NEWIDLE balancing is a source of latency, so preemptible
3476 * kernels will stop after the first task is pulled to minimize
3477 * the critical section.
3478 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003479 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003480 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003481#endif
3482
Peter Zijlstraee00e662009-12-17 17:25:20 +01003483 /*
3484 * We only want to steal up to the prescribed amount of
3485 * weighted load.
3486 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003487 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003488 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003489
Peter Zijlstra367456c2012-02-20 21:49:09 +01003490 continue;
3491next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003492 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003493 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003494
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003495 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003496 * Right now, this is one of only two places move_task() is called,
3497 * so we can safely collect move_task() stats here rather than
3498 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003499 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003500 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003501
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003502 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003503}
3504
Peter Zijlstra230059de2009-12-17 17:47:12 +01003505#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003506/*
3507 * update tg->load_weight by folding this cpu's load_avg
3508 */
Paul Turner67e86252010-11-15 15:47:05 -08003509static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003510{
3511 struct cfs_rq *cfs_rq;
3512 unsigned long flags;
3513 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003514
3515 if (!tg->se[cpu])
3516 return 0;
3517
3518 rq = cpu_rq(cpu);
3519 cfs_rq = tg->cfs_rq[cpu];
3520
3521 raw_spin_lock_irqsave(&rq->lock, flags);
3522
3523 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003524 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003525
3526 /*
3527 * We need to update shares after updating tg->load_weight in
3528 * order to adjust the weight of groups with long running tasks.
3529 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003530 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003531
3532 raw_spin_unlock_irqrestore(&rq->lock, flags);
3533
3534 return 0;
3535}
3536
3537static void update_shares(int cpu)
3538{
3539 struct cfs_rq *cfs_rq;
3540 struct rq *rq = cpu_rq(cpu);
3541
3542 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003543 /*
3544 * Iterates the task_group tree in a bottom up fashion, see
3545 * list_add_leaf_cfs_rq() for details.
3546 */
Paul Turner64660c82011-07-21 09:43:36 -07003547 for_each_leaf_cfs_rq(rq, cfs_rq) {
3548 /* throttled entities do not contribute to load */
3549 if (throttled_hierarchy(cfs_rq))
3550 continue;
3551
Paul Turner67e86252010-11-15 15:47:05 -08003552 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003553 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003554 rcu_read_unlock();
3555}
3556
Peter Zijlstra9763b672011-07-13 13:09:25 +02003557/*
3558 * Compute the cpu's hierarchical load factor for each task group.
3559 * This needs to be done in a top-down fashion because the load of a child
3560 * group is a fraction of its parents load.
3561 */
3562static int tg_load_down(struct task_group *tg, void *data)
3563{
3564 unsigned long load;
3565 long cpu = (long)data;
3566
3567 if (!tg->parent) {
3568 load = cpu_rq(cpu)->load.weight;
3569 } else {
3570 load = tg->parent->cfs_rq[cpu]->h_load;
3571 load *= tg->se[cpu]->load.weight;
3572 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3573 }
3574
3575 tg->cfs_rq[cpu]->h_load = load;
3576
3577 return 0;
3578}
3579
3580static void update_h_load(long cpu)
3581{
Peter Zijlstraa35b6462012-08-08 21:46:40 +02003582 struct rq *rq = cpu_rq(cpu);
3583 unsigned long now = jiffies;
3584
3585 if (rq->h_load_throttle == now)
3586 return;
3587
3588 rq->h_load_throttle = now;
3589
Peter Zijlstra367456c2012-02-20 21:49:09 +01003590 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003591 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003592 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003593}
3594
Peter Zijlstra367456c2012-02-20 21:49:09 +01003595static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003596{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003597 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3598 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003599
Peter Zijlstra367456c2012-02-20 21:49:09 +01003600 load = p->se.load.weight;
3601 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003602
Peter Zijlstra367456c2012-02-20 21:49:09 +01003603 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003604}
3605#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003606static inline void update_shares(int cpu)
3607{
3608}
3609
Peter Zijlstra367456c2012-02-20 21:49:09 +01003610static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003611{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003612}
3613
3614static unsigned long task_h_load(struct task_struct *p)
3615{
3616 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003617}
3618#endif
3619
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003620/********** Helpers for find_busiest_group ************************/
3621/*
3622 * sd_lb_stats - Structure to store the statistics of a sched_domain
3623 * during load balancing.
3624 */
3625struct sd_lb_stats {
3626 struct sched_group *busiest; /* Busiest group in this sd */
3627 struct sched_group *this; /* Local group in this sd */
3628 unsigned long total_load; /* Total load of all groups in sd */
3629 unsigned long total_pwr; /* Total power of all groups in sd */
3630 unsigned long avg_load; /* Average load across all groups in sd */
3631
3632 /** Statistics of this group */
3633 unsigned long this_load;
3634 unsigned long this_load_per_task;
3635 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003636 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003637 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003638
3639 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003640 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003641 unsigned long max_load;
3642 unsigned long busiest_load_per_task;
3643 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003644 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003645 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003646 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003647
3648 int group_imb; /* Is there imbalance in this sd */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003649};
3650
3651/*
3652 * sg_lb_stats - stats of a sched_group required for load_balancing
3653 */
3654struct sg_lb_stats {
3655 unsigned long avg_load; /*Avg load across the CPUs of the group */
3656 unsigned long group_load; /* Total load over the CPUs of the group */
3657 unsigned long sum_nr_running; /* Nr tasks running in the group */
3658 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3659 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003660 unsigned long idle_cpus;
3661 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003662 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003663 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003664};
3665
3666/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003667 * get_sd_load_idx - Obtain the load index for a given sched domain.
3668 * @sd: The sched_domain whose load_idx is to be obtained.
3669 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3670 */
3671static inline int get_sd_load_idx(struct sched_domain *sd,
3672 enum cpu_idle_type idle)
3673{
3674 int load_idx;
3675
3676 switch (idle) {
3677 case CPU_NOT_IDLE:
3678 load_idx = sd->busy_idx;
3679 break;
3680
3681 case CPU_NEWLY_IDLE:
3682 load_idx = sd->newidle_idx;
3683 break;
3684 default:
3685 load_idx = sd->idle_idx;
3686 break;
3687 }
3688
3689 return load_idx;
3690}
3691
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003692unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3693{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003694 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003695}
3696
3697unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3698{
3699 return default_scale_freq_power(sd, cpu);
3700}
3701
3702unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3703{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003704 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003705 unsigned long smt_gain = sd->smt_gain;
3706
3707 smt_gain /= weight;
3708
3709 return smt_gain;
3710}
3711
3712unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3713{
3714 return default_scale_smt_power(sd, cpu);
3715}
3716
3717unsigned long scale_rt_power(int cpu)
3718{
3719 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003720 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003721
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003722 /*
3723 * Since we're reading these variables without serialization make sure
3724 * we read them once before doing sanity checks on them.
3725 */
3726 age_stamp = ACCESS_ONCE(rq->age_stamp);
3727 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003728
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003729 total = sched_avg_period() + (rq->clock - age_stamp);
3730
3731 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003732 /* Ensures that power won't end up being negative */
3733 available = 0;
3734 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02003735 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003736 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003737
Nikhil Rao1399fa72011-05-18 10:09:39 -07003738 if (unlikely((s64)total < SCHED_POWER_SCALE))
3739 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003740
Nikhil Rao1399fa72011-05-18 10:09:39 -07003741 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003742
3743 return div_u64(available, total);
3744}
3745
3746static void update_cpu_power(struct sched_domain *sd, int cpu)
3747{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003748 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003749 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003750 struct sched_group *sdg = sd->groups;
3751
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003752 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3753 if (sched_feat(ARCH_POWER))
3754 power *= arch_scale_smt_power(sd, cpu);
3755 else
3756 power *= default_scale_smt_power(sd, cpu);
3757
Nikhil Rao1399fa72011-05-18 10:09:39 -07003758 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003759 }
3760
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003761 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003762
3763 if (sched_feat(ARCH_POWER))
3764 power *= arch_scale_freq_power(sd, cpu);
3765 else
3766 power *= default_scale_freq_power(sd, cpu);
3767
Nikhil Rao1399fa72011-05-18 10:09:39 -07003768 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003769
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003770 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003771 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003772
3773 if (!power)
3774 power = 1;
3775
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003776 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003777 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003778}
3779
Peter Zijlstra029632f2011-10-25 10:00:11 +02003780void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003781{
3782 struct sched_domain *child = sd->child;
3783 struct sched_group *group, *sdg = sd->groups;
3784 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003785 unsigned long interval;
3786
3787 interval = msecs_to_jiffies(sd->balance_interval);
3788 interval = clamp(interval, 1UL, max_load_balance_interval);
3789 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003790
3791 if (!child) {
3792 update_cpu_power(sd, cpu);
3793 return;
3794 }
3795
3796 power = 0;
3797
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02003798 if (child->flags & SD_OVERLAP) {
3799 /*
3800 * SD_OVERLAP domains cannot assume that child groups
3801 * span the current group.
3802 */
3803
3804 for_each_cpu(cpu, sched_group_cpus(sdg))
3805 power += power_of(cpu);
3806 } else {
3807 /*
3808 * !SD_OVERLAP domains can assume that child groups
3809 * span the current group.
3810 */
3811
3812 group = child->groups;
3813 do {
3814 power += group->sgp->power;
3815 group = group->next;
3816 } while (group != child->groups);
3817 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003818
Peter Zijlstrac3decf02012-05-31 12:05:32 +02003819 sdg->sgp->power_orig = sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003820}
3821
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003822/*
3823 * Try and fix up capacity for tiny siblings, this is needed when
3824 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3825 * which on its own isn't powerful enough.
3826 *
3827 * See update_sd_pick_busiest() and check_asym_packing().
3828 */
3829static inline int
3830fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3831{
3832 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003833 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003834 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003835 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003836 return 0;
3837
3838 /*
3839 * If ~90% of the cpu_power is still there, we're good.
3840 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003841 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003842 return 1;
3843
3844 return 0;
3845}
3846
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003847/**
3848 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003849 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003850 * @group: sched_group whose statistics are to be updated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003851 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003852 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003853 * @balance: Should we balance.
3854 * @sgs: variable to hold the statistics for this group.
3855 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003856static inline void update_sg_lb_stats(struct lb_env *env,
3857 struct sched_group *group, int load_idx,
Michael Wangb9403132012-07-12 16:10:13 +08003858 int local_group, int *balance, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003859{
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003860 unsigned long nr_running, max_nr_running, min_nr_running;
3861 unsigned long load, max_cpu_load, min_cpu_load;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003862 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003863 unsigned long avg_load_per_task = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003864 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003865
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003866 if (local_group)
Peter Zijlstrac1174872012-05-31 14:47:33 +02003867 balance_cpu = group_balance_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003868
3869 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003870 max_cpu_load = 0;
3871 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003872 max_nr_running = 0;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003873 min_nr_running = ~0UL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003874
Michael Wangb9403132012-07-12 16:10:13 +08003875 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003876 struct rq *rq = cpu_rq(i);
3877
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003878 nr_running = rq->nr_running;
3879
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003880 /* Bias balancing toward cpus of our domain */
3881 if (local_group) {
Peter Zijlstrac1174872012-05-31 14:47:33 +02003882 if (idle_cpu(i) && !first_idle_cpu &&
3883 cpumask_test_cpu(i, sched_group_mask(group))) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003884 first_idle_cpu = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003885 balance_cpu = i;
3886 }
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003887
3888 load = target_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003889 } else {
3890 load = source_load(i, load_idx);
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003891 if (load > max_cpu_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003892 max_cpu_load = load;
3893 if (min_cpu_load > load)
3894 min_cpu_load = load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003895
3896 if (nr_running > max_nr_running)
3897 max_nr_running = nr_running;
3898 if (min_nr_running > nr_running)
3899 min_nr_running = nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003900 }
3901
3902 sgs->group_load += load;
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003903 sgs->sum_nr_running += nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003904 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003905 if (idle_cpu(i))
3906 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003907 }
3908
3909 /*
3910 * First idle cpu or the first cpu(busiest) in this sched group
3911 * is eligible for doing load balancing at this and above
3912 * domains. In the newly idle case, we will allow all the cpu's
3913 * to do the newly idle load balance.
3914 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003915 if (local_group) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003916 if (env->idle != CPU_NEWLY_IDLE) {
Peter Zijlstra04f733b2012-05-11 00:12:02 +02003917 if (balance_cpu != env->dst_cpu) {
Vincent Guittot4ec44122011-12-12 20:21:08 +01003918 *balance = 0;
3919 return;
3920 }
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003921 update_group_power(env->sd, env->dst_cpu);
Vincent Guittot4ec44122011-12-12 20:21:08 +01003922 } else if (time_after_eq(jiffies, group->sgp->next_update))
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003923 update_group_power(env->sd, env->dst_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003924 }
3925
3926 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003927 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003928
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003929 /*
3930 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003931 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003932 *
3933 * APZ: with cgroup the avg task weight can vary wildly and
3934 * might not be a suitable number - should we keep a
3935 * normalized nr_running number somewhere that negates
3936 * the hierarchy?
3937 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003938 if (sgs->sum_nr_running)
3939 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940
Peter Zijlstrae44bc5c2012-05-11 00:22:12 +02003941 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
3942 (max_nr_running - min_nr_running) > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943 sgs->group_imb = 1;
3944
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003945 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003946 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003947 if (!sgs->group_capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003948 sgs->group_capacity = fix_small_capacity(env->sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003949 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003950
3951 if (sgs->group_capacity > sgs->sum_nr_running)
3952 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003953}
3954
3955/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003956 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07003957 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10003958 * @sds: sched_domain statistics
3959 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003960 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10003961 *
3962 * Determine if @sg is a busier group than the previously selected
3963 * busiest group.
3964 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003965static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10003966 struct sd_lb_stats *sds,
3967 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003968 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10003969{
3970 if (sgs->avg_load <= sds->max_load)
3971 return false;
3972
3973 if (sgs->sum_nr_running > sgs->group_capacity)
3974 return true;
3975
3976 if (sgs->group_imb)
3977 return true;
3978
3979 /*
3980 * ASYM_PACKING needs to move all the work to the lowest
3981 * numbered CPUs in the group, therefore mark all groups
3982 * higher than ourself as busy.
3983 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02003984 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3985 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10003986 if (!sds->busiest)
3987 return true;
3988
3989 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3990 return true;
3991 }
3992
3993 return false;
3994}
3995
3996/**
Hui Kang461819a2011-10-11 23:00:59 -04003997 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07003998 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003999 * @balance: Should we balance.
4000 * @sds: variable to hold the statistics for this sched_domain.
4001 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004002static inline void update_sd_lb_stats(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004003 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004004{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004005 struct sched_domain *child = env->sd->child;
4006 struct sched_group *sg = env->sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004007 struct sg_lb_stats sgs;
4008 int load_idx, prefer_sibling = 0;
4009
4010 if (child && child->flags & SD_PREFER_SIBLING)
4011 prefer_sibling = 1;
4012
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004013 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004014
4015 do {
4016 int local_group;
4017
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004018 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004019 memset(&sgs, 0, sizeof(sgs));
Michael Wangb9403132012-07-12 16:10:13 +08004020 update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004021
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004022 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004023 return;
4024
4025 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004026 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004027
4028 /*
4029 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10004030 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07004031 * and move all the excess tasks away. We lower the capacity
4032 * of a group only if the local group has the capacity to fit
4033 * these excess tasks, i.e. nr_running < group_capacity. The
4034 * extra check prevents the case where you always pull from the
4035 * heaviest group when it is already under-utilized (possible
4036 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004037 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07004038 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004039 sgs.group_capacity = min(sgs.group_capacity, 1UL);
4040
4041 if (local_group) {
4042 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004043 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004044 sds->this_nr_running = sgs.sum_nr_running;
4045 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004046 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004047 sds->this_idle_cpus = sgs.idle_cpus;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004048 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004049 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004050 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004051 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004052 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004053 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004054 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004055 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004056 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004057 sds->group_imb = sgs.group_imb;
4058 }
4059
Michael Neuling532cb4c2010-06-08 14:57:02 +10004060 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004061 } while (sg != env->sd->groups);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004062}
4063
Michael Neuling532cb4c2010-06-08 14:57:02 +10004064/**
4065 * check_asym_packing - Check to see if the group is packed into the
4066 * sched doman.
4067 *
4068 * This is primarily intended to used at the sibling level. Some
4069 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4070 * case of POWER7, it can move to lower SMT modes only when higher
4071 * threads are idle. When in lower SMT modes, the threads will
4072 * perform better since they share less core resources. Hence when we
4073 * have idle threads, we want them to be the higher ones.
4074 *
4075 * This packing function is run on idle threads. It checks to see if
4076 * the busiest CPU in this domain (core in the P7 case) has a higher
4077 * CPU number than the packing function is being run on. Here we are
4078 * assuming lower CPU number will be equivalent to lower a SMT thread
4079 * number.
4080 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004081 * Returns 1 when packing is required and a task should be moved to
4082 * this CPU. The amount of the imbalance is returned in *imbalance.
4083 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004084 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004085 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10004086 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004087static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004088{
4089 int busiest_cpu;
4090
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004091 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004092 return 0;
4093
4094 if (!sds->busiest)
4095 return 0;
4096
4097 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004098 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004099 return 0;
4100
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004101 env->imbalance = DIV_ROUND_CLOSEST(
4102 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
4103
Michael Neuling532cb4c2010-06-08 14:57:02 +10004104 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004105}
4106
4107/**
4108 * fix_small_imbalance - Calculate the minor imbalance that exists
4109 * amongst the groups of a sched_domain, during
4110 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07004111 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004112 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004113 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004114static inline
4115void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004116{
4117 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4118 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004119 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004120
4121 if (sds->this_nr_running) {
4122 sds->this_load_per_task /= sds->this_nr_running;
4123 if (sds->busiest_load_per_task >
4124 sds->this_load_per_task)
4125 imbn = 1;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004126 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004127 sds->this_load_per_task =
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004128 cpu_avg_load_per_task(env->dst_cpu);
4129 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004130
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004131 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004132 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004133 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004134
4135 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4136 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004137 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004138 return;
4139 }
4140
4141 /*
4142 * OK, we don't have enough imbalance to justify moving tasks,
4143 * however we may be able to increase total CPU power used by
4144 * moving them.
4145 */
4146
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004147 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004148 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004149 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004150 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004151 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004152
4153 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004154 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004155 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004156 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004157 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004158 min(sds->busiest_load_per_task, sds->max_load - tmp);
4159
4160 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004161 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004162 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004163 tmp = (sds->max_load * sds->busiest->sgp->power) /
4164 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004165 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004166 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004167 sds->this->sgp->power;
4168 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004169 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004170 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004171
4172 /* Move if we gain throughput */
4173 if (pwr_move > pwr_now)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004174 env->imbalance = sds->busiest_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004175}
4176
4177/**
4178 * calculate_imbalance - Calculate the amount of imbalance present within the
4179 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004180 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004181 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004182 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004183static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004184{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004185 unsigned long max_pull, load_above_capacity = ~0UL;
4186
4187 sds->busiest_load_per_task /= sds->busiest_nr_running;
4188 if (sds->group_imb) {
4189 sds->busiest_load_per_task =
4190 min(sds->busiest_load_per_task, sds->avg_load);
4191 }
4192
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004193 /*
4194 * In the presence of smp nice balancing, certain scenarios can have
4195 * max load less than avg load(as we skip the groups at or below
4196 * its cpu_power, while calculating max_load..)
4197 */
4198 if (sds->max_load < sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004199 env->imbalance = 0;
4200 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004201 }
4202
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004203 if (!sds->group_imb) {
4204 /*
4205 * Don't want to pull so many tasks that a group would go idle.
4206 */
4207 load_above_capacity = (sds->busiest_nr_running -
4208 sds->busiest_group_capacity);
4209
Nikhil Rao1399fa72011-05-18 10:09:39 -07004210 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004211
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004212 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004213 }
4214
4215 /*
4216 * We're trying to get all the cpus to the average_load, so we don't
4217 * want to push ourselves above the average load, nor do we wish to
4218 * reduce the max loaded cpu below the average load. At the same time,
4219 * we also don't want to reduce the group load below the group capacity
4220 * (so that we can implement power-savings policies etc). Thus we look
4221 * for the minimum possible imbalance.
4222 * Be careful of negative numbers as they'll appear as very large values
4223 * with unsigned longs.
4224 */
4225 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004226
4227 /* How much load to actually move to equalise the imbalance */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004228 env->imbalance = min(max_pull * sds->busiest->sgp->power,
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004229 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004230 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004231
4232 /*
4233 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004234 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004235 * a think about bumping its value to force at least one task to be
4236 * moved
4237 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004238 if (env->imbalance < sds->busiest_load_per_task)
4239 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004240
4241}
Nikhil Raofab47622010-10-15 13:12:29 -07004242
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004243/******* find_busiest_group() helpers end here *********************/
4244
4245/**
4246 * find_busiest_group - Returns the busiest group within the sched_domain
4247 * if there is an imbalance. If there isn't an imbalance, and
4248 * the user has opted for power-savings, it returns a group whose
4249 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4250 * such a group exists.
4251 *
4252 * Also calculates the amount of weighted load which should be moved
4253 * to restore balance.
4254 *
Randy Dunlapcd968912012-06-08 13:18:33 -07004255 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004256 * @balance: Pointer to a variable indicating if this_cpu
4257 * is the appropriate cpu to perform load balancing at this_level.
4258 *
4259 * Returns: - the busiest group if imbalance exists.
4260 * - If no imbalance and user has opted for power-savings balance,
4261 * return the least loaded group whose CPUs can be
4262 * put to idle by rebalancing its tasks onto our group.
4263 */
4264static struct sched_group *
Michael Wangb9403132012-07-12 16:10:13 +08004265find_busiest_group(struct lb_env *env, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004266{
4267 struct sd_lb_stats sds;
4268
4269 memset(&sds, 0, sizeof(sds));
4270
4271 /*
4272 * Compute the various statistics relavent for load balancing at
4273 * this level.
4274 */
Michael Wangb9403132012-07-12 16:10:13 +08004275 update_sd_lb_stats(env, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004276
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004277 /*
4278 * this_cpu is not the appropriate cpu to perform load balancing at
4279 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004280 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004281 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004282 goto ret;
4283
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004284 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4285 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10004286 return sds.busiest;
4287
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004288 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004289 if (!sds.busiest || sds.busiest_nr_running == 0)
4290 goto out_balanced;
4291
Nikhil Rao1399fa72011-05-18 10:09:39 -07004292 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004293
Peter Zijlstra866ab432011-02-21 18:56:47 +01004294 /*
4295 * If the busiest group is imbalanced the below checks don't
4296 * work because they assumes all things are equal, which typically
4297 * isn't true due to cpus_allowed constraints and the like.
4298 */
4299 if (sds.group_imb)
4300 goto force_balance;
4301
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004302 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004303 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
Nikhil Raofab47622010-10-15 13:12:29 -07004304 !sds.busiest_has_capacity)
4305 goto force_balance;
4306
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004307 /*
4308 * If the local group is more busy than the selected busiest group
4309 * don't try and pull any tasks.
4310 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004311 if (sds.this_load >= sds.max_load)
4312 goto out_balanced;
4313
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004314 /*
4315 * Don't pull any tasks if this group is already above the domain
4316 * average load.
4317 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004318 if (sds.this_load >= sds.avg_load)
4319 goto out_balanced;
4320
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004321 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004322 /*
4323 * This cpu is idle. If the busiest group load doesn't
4324 * have more tasks than the number of available cpu's and
4325 * there is no imbalance between this and busiest group
4326 * wrt to idle cpu's, it is balanced.
4327 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004328 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004329 sds.busiest_nr_running <= sds.busiest_group_weight)
4330 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004331 } else {
4332 /*
4333 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4334 * imbalance_pct to be conservative.
4335 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004336 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004337 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004338 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004339
Nikhil Raofab47622010-10-15 13:12:29 -07004340force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004341 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004342 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004343 return sds.busiest;
4344
4345out_balanced:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004346ret:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004347 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004348 return NULL;
4349}
4350
4351/*
4352 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4353 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004354static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb9403132012-07-12 16:10:13 +08004355 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004356{
4357 struct rq *busiest = NULL, *rq;
4358 unsigned long max_load = 0;
4359 int i;
4360
4361 for_each_cpu(i, sched_group_cpus(group)) {
4362 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004363 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4364 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004365 unsigned long wl;
4366
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004367 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004368 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004369
Michael Wangb9403132012-07-12 16:10:13 +08004370 if (!cpumask_test_cpu(i, env->cpus))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004371 continue;
4372
4373 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004374 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004375
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004376 /*
4377 * When comparing with imbalance, use weighted_cpuload()
4378 * which is not scaled with the cpu power.
4379 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004380 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004381 continue;
4382
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004383 /*
4384 * For the load comparisons with the other cpu's, consider
4385 * the weighted_cpuload() scaled with the cpu power, so that
4386 * the load can be moved away from the cpu that is potentially
4387 * running at a lower capacity.
4388 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004389 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004390
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004391 if (wl > max_load) {
4392 max_load = wl;
4393 busiest = rq;
4394 }
4395 }
4396
4397 return busiest;
4398}
4399
4400/*
4401 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4402 * so long as it is large enough.
4403 */
4404#define MAX_PINNED_INTERVAL 512
4405
4406/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004407DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004408
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004409static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004410{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004411 struct sched_domain *sd = env->sd;
4412
4413 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004414
4415 /*
4416 * ASYM_PACKING needs to force migrate tasks from busy but
4417 * higher numbered CPUs in order to pack all tasks in the
4418 * lowest numbered CPUs.
4419 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004420 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10004421 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004422 }
4423
4424 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4425}
4426
Tejun Heo969c7922010-05-06 18:49:21 +02004427static int active_load_balance_cpu_stop(void *data);
4428
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004429/*
4430 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4431 * tasks if there is an imbalance.
4432 */
4433static int load_balance(int this_cpu, struct rq *this_rq,
4434 struct sched_domain *sd, enum cpu_idle_type idle,
4435 int *balance)
4436{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304437 int ld_moved, cur_ld_moved, active_balance = 0;
4438 int lb_iterations, max_lb_iterations;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004439 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004440 struct rq *busiest;
4441 unsigned long flags;
4442 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4443
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004444 struct lb_env env = {
4445 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004446 .dst_cpu = this_cpu,
4447 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304448 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004449 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004450 .loop_break = sched_nr_migrate_break,
Michael Wangb9403132012-07-12 16:10:13 +08004451 .cpus = cpus,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004452 };
4453
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004454 cpumask_copy(cpus, cpu_active_mask);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304455 max_lb_iterations = cpumask_weight(env.dst_grpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004456
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004457 schedstat_inc(sd, lb_count[idle]);
4458
4459redo:
Michael Wangb9403132012-07-12 16:10:13 +08004460 group = find_busiest_group(&env, balance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004461
4462 if (*balance == 0)
4463 goto out_balanced;
4464
4465 if (!group) {
4466 schedstat_inc(sd, lb_nobusyg[idle]);
4467 goto out_balanced;
4468 }
4469
Michael Wangb9403132012-07-12 16:10:13 +08004470 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004471 if (!busiest) {
4472 schedstat_inc(sd, lb_nobusyq[idle]);
4473 goto out_balanced;
4474 }
4475
Michael Wang78feefc2012-08-06 16:41:59 +08004476 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004477
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004478 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004479
4480 ld_moved = 0;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304481 lb_iterations = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004482 if (busiest->nr_running > 1) {
4483 /*
4484 * Attempt to move tasks. If find_busiest_group has found
4485 * an imbalance but busiest->nr_running <= 1, the group is
4486 * still unbalanced. ld_moved simply stays zero, so it is
4487 * correctly treated as an imbalance.
4488 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004489 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02004490 env.src_cpu = busiest->cpu;
4491 env.src_rq = busiest;
4492 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004493
Peter Zijlstraa35b6462012-08-08 21:46:40 +02004494 update_h_load(env.src_cpu);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004495more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004496 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08004497 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304498
4499 /*
4500 * cur_ld_moved - load moved in current iteration
4501 * ld_moved - cumulative load moved across iterations
4502 */
4503 cur_ld_moved = move_tasks(&env);
4504 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08004505 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004506 local_irq_restore(flags);
4507
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004508 if (env.flags & LBF_NEED_BREAK) {
4509 env.flags &= ~LBF_NEED_BREAK;
4510 goto more_balance;
4511 }
4512
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004513 /*
4514 * some other cpu did the load balance for us.
4515 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304516 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
4517 resched_cpu(env.dst_cpu);
4518
4519 /*
4520 * Revisit (affine) tasks on src_cpu that couldn't be moved to
4521 * us and move them to an alternate dst_cpu in our sched_group
4522 * where they can run. The upper limit on how many times we
4523 * iterate on same src_cpu is dependent on number of cpus in our
4524 * sched_group.
4525 *
4526 * This changes load balance semantics a bit on who can move
4527 * load to a given_cpu. In addition to the given_cpu itself
4528 * (or a ilb_cpu acting on its behalf where given_cpu is
4529 * nohz-idle), we now have balance_cpu in a position to move
4530 * load to given_cpu. In rare situations, this may cause
4531 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
4532 * _independently_ and at _same_ time to move some load to
4533 * given_cpu) causing exceess load to be moved to given_cpu.
4534 * This however should not happen so much in practice and
4535 * moreover subsequent load balance cycles should correct the
4536 * excess load moved.
4537 */
4538 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
4539 lb_iterations++ < max_lb_iterations) {
4540
Michael Wang78feefc2012-08-06 16:41:59 +08004541 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304542 env.dst_cpu = env.new_dst_cpu;
4543 env.flags &= ~LBF_SOME_PINNED;
4544 env.loop = 0;
4545 env.loop_break = sched_nr_migrate_break;
4546 /*
4547 * Go back to "more_balance" rather than "redo" since we
4548 * need to continue with same src_cpu.
4549 */
4550 goto more_balance;
4551 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004552
4553 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004554 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004555 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304556 if (!cpumask_empty(cpus)) {
4557 env.loop = 0;
4558 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004559 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05304560 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004561 goto out_balanced;
4562 }
4563 }
4564
4565 if (!ld_moved) {
4566 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004567 /*
4568 * Increment the failure counter only on periodic balance.
4569 * We do not want newidle balance, which can be very
4570 * frequent, pollute the failure counter causing
4571 * excessive cache_hot migrations and active balances.
4572 */
4573 if (idle != CPU_NEWLY_IDLE)
4574 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004575
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004576 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004577 raw_spin_lock_irqsave(&busiest->lock, flags);
4578
Tejun Heo969c7922010-05-06 18:49:21 +02004579 /* don't kick the active_load_balance_cpu_stop,
4580 * if the curr task on busiest cpu can't be
4581 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004582 */
4583 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004584 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004585 raw_spin_unlock_irqrestore(&busiest->lock,
4586 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004587 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004588 goto out_one_pinned;
4589 }
4590
Tejun Heo969c7922010-05-06 18:49:21 +02004591 /*
4592 * ->active_balance synchronizes accesses to
4593 * ->active_balance_work. Once set, it's cleared
4594 * only after active load balance is finished.
4595 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004596 if (!busiest->active_balance) {
4597 busiest->active_balance = 1;
4598 busiest->push_cpu = this_cpu;
4599 active_balance = 1;
4600 }
4601 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004602
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004603 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02004604 stop_one_cpu_nowait(cpu_of(busiest),
4605 active_load_balance_cpu_stop, busiest,
4606 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004607 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004608
4609 /*
4610 * We've kicked active balancing, reset the failure
4611 * counter.
4612 */
4613 sd->nr_balance_failed = sd->cache_nice_tries+1;
4614 }
4615 } else
4616 sd->nr_balance_failed = 0;
4617
4618 if (likely(!active_balance)) {
4619 /* We were unbalanced, so reset the balancing interval */
4620 sd->balance_interval = sd->min_interval;
4621 } else {
4622 /*
4623 * If we've begun active balancing, start to back off. This
4624 * case may not be covered by the all_pinned logic if there
4625 * is only 1 task on the busy runqueue (because we don't call
4626 * move_tasks).
4627 */
4628 if (sd->balance_interval < sd->max_interval)
4629 sd->balance_interval *= 2;
4630 }
4631
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004632 goto out;
4633
4634out_balanced:
4635 schedstat_inc(sd, lb_balanced[idle]);
4636
4637 sd->nr_balance_failed = 0;
4638
4639out_one_pinned:
4640 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004641 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004642 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004643 (sd->balance_interval < sd->max_interval))
4644 sd->balance_interval *= 2;
4645
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004646 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004648 return ld_moved;
4649}
4650
4651/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004652 * idle_balance is called by schedule() if this_cpu is about to become
4653 * idle. Attempts to pull tasks from other CPUs.
4654 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004655void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004656{
4657 struct sched_domain *sd;
4658 int pulled_task = 0;
4659 unsigned long next_balance = jiffies + HZ;
4660
4661 this_rq->idle_stamp = this_rq->clock;
4662
4663 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4664 return;
4665
Peter Zijlstraf492e122009-12-23 15:29:42 +01004666 /*
4667 * Drop the rq->lock, but keep IRQ/preempt disabled.
4668 */
4669 raw_spin_unlock(&this_rq->lock);
4670
Paul Turnerc66eaf62010-11-15 15:47:07 -08004671 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004672 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004673 for_each_domain(this_cpu, sd) {
4674 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004675 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004676
4677 if (!(sd->flags & SD_LOAD_BALANCE))
4678 continue;
4679
Peter Zijlstraf492e122009-12-23 15:29:42 +01004680 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004681 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004682 pulled_task = load_balance(this_cpu, this_rq,
4683 sd, CPU_NEWLY_IDLE, &balance);
4684 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004685
4686 interval = msecs_to_jiffies(sd->balance_interval);
4687 if (time_after(next_balance, sd->last_balance + interval))
4688 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004689 if (pulled_task) {
4690 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004691 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004692 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004693 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004694 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004695
4696 raw_spin_lock(&this_rq->lock);
4697
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004698 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4699 /*
4700 * We are going idle. next_balance may be set based on
4701 * a busy processor. So reset next_balance.
4702 */
4703 this_rq->next_balance = next_balance;
4704 }
4705}
4706
4707/*
Tejun Heo969c7922010-05-06 18:49:21 +02004708 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4709 * running tasks off the busiest CPU onto idle CPUs. It requires at
4710 * least 1 task to be running on each physical CPU where possible, and
4711 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004712 */
Tejun Heo969c7922010-05-06 18:49:21 +02004713static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004714{
Tejun Heo969c7922010-05-06 18:49:21 +02004715 struct rq *busiest_rq = data;
4716 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004717 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004718 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004719 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004720
4721 raw_spin_lock_irq(&busiest_rq->lock);
4722
4723 /* make sure the requested cpu hasn't gone down in the meantime */
4724 if (unlikely(busiest_cpu != smp_processor_id() ||
4725 !busiest_rq->active_balance))
4726 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004727
4728 /* Is there any task to move? */
4729 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004730 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004731
4732 /*
4733 * This condition is "impossible", if it occurs
4734 * we need to fix it. Originally reported by
4735 * Bjorn Helgaas on a 128-cpu setup.
4736 */
4737 BUG_ON(busiest_rq == target_rq);
4738
4739 /* move a task from busiest_rq to target_rq */
4740 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004741
4742 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004743 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004744 for_each_domain(target_cpu, sd) {
4745 if ((sd->flags & SD_LOAD_BALANCE) &&
4746 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4747 break;
4748 }
4749
4750 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004751 struct lb_env env = {
4752 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004753 .dst_cpu = target_cpu,
4754 .dst_rq = target_rq,
4755 .src_cpu = busiest_rq->cpu,
4756 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004757 .idle = CPU_IDLE,
4758 };
4759
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004760 schedstat_inc(sd, alb_count);
4761
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004762 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004763 schedstat_inc(sd, alb_pushed);
4764 else
4765 schedstat_inc(sd, alb_failed);
4766 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004767 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004768 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004769out_unlock:
4770 busiest_rq->active_balance = 0;
4771 raw_spin_unlock_irq(&busiest_rq->lock);
4772 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004773}
4774
4775#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004776/*
4777 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004778 * - When one of the busy CPUs notice that there may be an idle rebalancing
4779 * needed, they will kick the idle load balancer, which then does idle
4780 * load balancing for all the idle CPUs.
4781 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004782static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004783 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004784 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004785 unsigned long next_balance; /* in jiffy units */
4786} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004787
Peter Zijlstra8e7fbcb2012-01-09 11:28:35 +01004788static inline int find_new_ilb(int call_cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004789{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004790 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004791
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004792 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4793 return ilb;
4794
4795 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004796}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004797
4798/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004799 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4800 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4801 * CPU (if there is one).
4802 */
4803static void nohz_balancer_kick(int cpu)
4804{
4805 int ilb_cpu;
4806
4807 nohz.next_balance++;
4808
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004809 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004810
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004811 if (ilb_cpu >= nr_cpu_ids)
4812 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004813
Suresh Siddhacd490c52011-12-06 11:26:34 -08004814 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004815 return;
4816 /*
4817 * Use smp_send_reschedule() instead of resched_cpu().
4818 * This way we generate a sched IPI on the target cpu which
4819 * is idle. And the softirq performing nohz idle load balance
4820 * will be run before returning from the IPI.
4821 */
4822 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004823 return;
4824}
4825
Alex Shic1cc0172012-09-10 15:10:58 +08004826static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08004827{
4828 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4829 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4830 atomic_dec(&nohz.nr_cpus);
4831 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4832 }
4833}
4834
Suresh Siddha69e1e812011-12-01 17:07:33 -08004835static inline void set_cpu_sd_state_busy(void)
4836{
4837 struct sched_domain *sd;
4838 int cpu = smp_processor_id();
4839
4840 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4841 return;
4842 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4843
4844 rcu_read_lock();
4845 for_each_domain(cpu, sd)
4846 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4847 rcu_read_unlock();
4848}
4849
4850void set_cpu_sd_state_idle(void)
4851{
4852 struct sched_domain *sd;
4853 int cpu = smp_processor_id();
4854
4855 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4856 return;
4857 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4858
4859 rcu_read_lock();
4860 for_each_domain(cpu, sd)
4861 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4862 rcu_read_unlock();
4863}
4864
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004865/*
Alex Shic1cc0172012-09-10 15:10:58 +08004866 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004867 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004868 */
Alex Shic1cc0172012-09-10 15:10:58 +08004869void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004870{
Suresh Siddha71325962012-01-19 18:28:57 -08004871 /*
4872 * If this cpu is going down, then nothing needs to be done.
4873 */
4874 if (!cpu_active(cpu))
4875 return;
4876
Alex Shic1cc0172012-09-10 15:10:58 +08004877 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
4878 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004879
Alex Shic1cc0172012-09-10 15:10:58 +08004880 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
4881 atomic_inc(&nohz.nr_cpus);
4882 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004883}
Suresh Siddha71325962012-01-19 18:28:57 -08004884
4885static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4886 unsigned long action, void *hcpu)
4887{
4888 switch (action & ~CPU_TASKS_FROZEN) {
4889 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08004890 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08004891 return NOTIFY_OK;
4892 default:
4893 return NOTIFY_DONE;
4894 }
4895}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004896#endif
4897
4898static DEFINE_SPINLOCK(balancing);
4899
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004900/*
4901 * Scale the max load_balance interval with the number of CPUs in the system.
4902 * This trades load-balance latency on larger machines for less cross talk.
4903 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004904void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004905{
4906 max_load_balance_interval = HZ*num_online_cpus()/10;
4907}
4908
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004909/*
4910 * It checks each scheduling domain to see if it is due to be balanced,
4911 * and initiates a balancing operation if so.
4912 *
4913 * Balancing parameters are set up in arch_init_sched_domains.
4914 */
4915static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4916{
4917 int balance = 1;
4918 struct rq *rq = cpu_rq(cpu);
4919 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02004920 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004921 /* Earliest time when we have to do rebalance again */
4922 unsigned long next_balance = jiffies + 60*HZ;
4923 int update_next_balance = 0;
4924 int need_serialize;
4925
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004926 update_shares(cpu);
4927
Peter Zijlstradce840a2011-04-07 14:09:50 +02004928 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004929 for_each_domain(cpu, sd) {
4930 if (!(sd->flags & SD_LOAD_BALANCE))
4931 continue;
4932
4933 interval = sd->balance_interval;
4934 if (idle != CPU_IDLE)
4935 interval *= sd->busy_factor;
4936
4937 /* scale ms to jiffies */
4938 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004939 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004940
4941 need_serialize = sd->flags & SD_SERIALIZE;
4942
4943 if (need_serialize) {
4944 if (!spin_trylock(&balancing))
4945 goto out;
4946 }
4947
4948 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4949 if (load_balance(cpu, rq, sd, idle, &balance)) {
4950 /*
4951 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004952 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004953 */
4954 idle = CPU_NOT_IDLE;
4955 }
4956 sd->last_balance = jiffies;
4957 }
4958 if (need_serialize)
4959 spin_unlock(&balancing);
4960out:
4961 if (time_after(next_balance, sd->last_balance + interval)) {
4962 next_balance = sd->last_balance + interval;
4963 update_next_balance = 1;
4964 }
4965
4966 /*
4967 * Stop the load balance at this level. There is another
4968 * CPU in our sched group which is doing load balancing more
4969 * actively.
4970 */
4971 if (!balance)
4972 break;
4973 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004974 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004975
4976 /*
4977 * next_balance will be updated only when there is a need.
4978 * When the cpu is attached to null domain for ex, it will not be
4979 * updated.
4980 */
4981 if (likely(update_next_balance))
4982 rq->next_balance = next_balance;
4983}
4984
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004985#ifdef CONFIG_NO_HZ
4986/*
4987 * In CONFIG_NO_HZ case, the idle balance kickee will do the
4988 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4989 */
4990static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
4991{
4992 struct rq *this_rq = cpu_rq(this_cpu);
4993 struct rq *rq;
4994 int balance_cpu;
4995
Suresh Siddha1c792db2011-12-01 17:07:32 -08004996 if (idle != CPU_IDLE ||
4997 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
4998 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004999
5000 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005001 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005002 continue;
5003
5004 /*
5005 * If this cpu gets work to do, stop the load balancing
5006 * work being done for other cpus. Next load
5007 * balancing owner will pick it up.
5008 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005009 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005010 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005011
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02005012 rq = cpu_rq(balance_cpu);
5013
5014 raw_spin_lock_irq(&rq->lock);
5015 update_rq_clock(rq);
5016 update_idle_cpu_load(rq);
5017 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005018
5019 rebalance_domains(balance_cpu, CPU_IDLE);
5020
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005021 if (time_after(this_rq->next_balance, rq->next_balance))
5022 this_rq->next_balance = rq->next_balance;
5023 }
5024 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005025end:
5026 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005027}
5028
5029/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005030 * Current heuristic for kicking the idle load balancer in the presence
5031 * of an idle cpu is the system.
5032 * - This rq has more than one task.
5033 * - At any scheduler domain level, this cpu's scheduler group has multiple
5034 * busy cpu's exceeding the group's power.
5035 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5036 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005037 */
5038static inline int nohz_kick_needed(struct rq *rq, int cpu)
5039{
5040 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005041 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005042
Suresh Siddha1c792db2011-12-01 17:07:32 -08005043 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005044 return 0;
5045
Suresh Siddha1c792db2011-12-01 17:07:32 -08005046 /*
5047 * We may be recently in ticked or tickless idle mode. At the first
5048 * busy tick after returning from idle, we will update the busy stats.
5049 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005050 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08005051 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005052
5053 /*
5054 * None are in tickless mode and hence no need for NOHZ idle load
5055 * balancing.
5056 */
5057 if (likely(!atomic_read(&nohz.nr_cpus)))
5058 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005059
5060 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005061 return 0;
5062
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005063 if (rq->nr_running >= 2)
5064 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005065
Peter Zijlstra067491b2011-12-07 14:32:08 +01005066 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005067 for_each_domain(cpu, sd) {
5068 struct sched_group *sg = sd->groups;
5069 struct sched_group_power *sgp = sg->sgp;
5070 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005071
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005072 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005073 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005074
5075 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5076 && (cpumask_first_and(nohz.idle_cpus_mask,
5077 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005078 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005079
5080 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5081 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005082 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005083 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005085
5086need_kick_unlock:
5087 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005088need_kick:
5089 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005090}
5091#else
5092static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5093#endif
5094
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005095/*
5096 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005097 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005098 */
5099static void run_rebalance_domains(struct softirq_action *h)
5100{
5101 int this_cpu = smp_processor_id();
5102 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005103 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005104 CPU_IDLE : CPU_NOT_IDLE;
5105
5106 rebalance_domains(this_cpu, idle);
5107
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005108 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005109 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005110 * balancing on behalf of the other idle cpus whose ticks are
5111 * stopped.
5112 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005113 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005114}
5115
5116static inline int on_null_domain(int cpu)
5117{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005118 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005119}
5120
5121/*
5122 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005123 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005124void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005125{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005126 /* Don't need to rebalance while attached to NULL domain */
5127 if (time_after_eq(jiffies, rq->next_balance) &&
5128 likely(!on_null_domain(cpu)))
5129 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005130#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005131 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005132 nohz_balancer_kick(cpu);
5133#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005134}
5135
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005136static void rq_online_fair(struct rq *rq)
5137{
5138 update_sysctl();
5139}
5140
5141static void rq_offline_fair(struct rq *rq)
5142{
5143 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005144
5145 /* Ensure any throttled groups are reachable by pick_next_task */
5146 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005147}
5148
Dhaval Giani55e12e52008-06-24 23:39:43 +05305149#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005150
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005151/*
5152 * scheduler tick hitting a task of our scheduling class:
5153 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005154static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005155{
5156 struct cfs_rq *cfs_rq;
5157 struct sched_entity *se = &curr->se;
5158
5159 for_each_sched_entity(se) {
5160 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005161 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005162 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02005163
5164 if (sched_feat_numa(NUMA))
5165 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005166}
5167
5168/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005169 * called on fork with the child task as argument from the parent's context
5170 * - child not yet on the tasklist
5171 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005172 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005173static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005174{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005175 struct cfs_rq *cfs_rq;
5176 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005177 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005178 struct rq *rq = this_rq();
5179 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005180
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005181 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005182
Peter Zijlstra861d0342010-08-19 13:31:43 +02005183 update_rq_clock(rq);
5184
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005185 cfs_rq = task_cfs_rq(current);
5186 curr = cfs_rq->curr;
5187
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005188 if (unlikely(task_cpu(p) != this_cpu)) {
5189 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005190 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07005191 rcu_read_unlock();
5192 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005193
Ting Yang7109c442007-08-28 12:53:24 +02005194 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005195
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005196 if (curr)
5197 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005198 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005199
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005200 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005201 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005202 * Upon rescheduling, sched_class::put_prev_task() will place
5203 * 'current' within the tree based on its new key value.
5204 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005205 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305206 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005207 }
5208
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005209 se->vruntime -= cfs_rq->min_vruntime;
5210
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005211 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005212}
5213
Steven Rostedtcb469842008-01-25 21:08:22 +01005214/*
5215 * Priority of the task has changed. Check to see if we preempt
5216 * the current task.
5217 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005218static void
5219prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005220{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005221 if (!p->se.on_rq)
5222 return;
5223
Steven Rostedtcb469842008-01-25 21:08:22 +01005224 /*
5225 * Reschedule if we are currently running on this runqueue and
5226 * our priority decreased, or if we are not currently running on
5227 * this runqueue and our priority is higher than the current's
5228 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005229 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005230 if (p->prio > oldprio)
5231 resched_task(rq->curr);
5232 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005233 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005234}
5235
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005236static void switched_from_fair(struct rq *rq, struct task_struct *p)
5237{
5238 struct sched_entity *se = &p->se;
5239 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5240
5241 /*
5242 * Ensure the task's vruntime is normalized, so that when its
5243 * switched back to the fair class the enqueue_entity(.flags=0) will
5244 * do the right thing.
5245 *
5246 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5247 * have normalized the vruntime, if it was !on_rq, then only when
5248 * the task is sleeping will it still have non-normalized vruntime.
5249 */
5250 if (!se->on_rq && p->state != TASK_RUNNING) {
5251 /*
5252 * Fix up our vruntime so that the current sleep doesn't
5253 * cause 'unlimited' sleep bonus.
5254 */
5255 place_entity(cfs_rq, se, 0);
5256 se->vruntime -= cfs_rq->min_vruntime;
5257 }
5258}
5259
Steven Rostedtcb469842008-01-25 21:08:22 +01005260/*
5261 * We switched to the sched_fair class.
5262 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005263static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005264{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005265 if (!p->se.on_rq)
5266 return;
5267
Steven Rostedtcb469842008-01-25 21:08:22 +01005268 /*
5269 * We were most likely switched from sched_rt, so
5270 * kick off the schedule if running, otherwise just see
5271 * if we can still preempt the current task.
5272 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005273 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005274 resched_task(rq->curr);
5275 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005276 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005277}
5278
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005279/* Account for a task changing its policy or group.
5280 *
5281 * This routine is mostly called to set cfs_rq->curr field when a task
5282 * migrates between groups/classes.
5283 */
5284static void set_curr_task_fair(struct rq *rq)
5285{
5286 struct sched_entity *se = &rq->curr->se;
5287
Paul Turnerec12cb72011-07-21 09:43:30 -07005288 for_each_sched_entity(se) {
5289 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5290
5291 set_next_entity(cfs_rq, se);
5292 /* ensure bandwidth has been allocated on our new cfs_rq */
5293 account_cfs_rq_runtime(cfs_rq, 0);
5294 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005295}
5296
Peter Zijlstra029632f2011-10-25 10:00:11 +02005297void init_cfs_rq(struct cfs_rq *cfs_rq)
5298{
5299 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005300 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5301#ifndef CONFIG_64BIT
5302 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5303#endif
5304}
5305
Peter Zijlstra810b3812008-02-29 15:21:01 -05005306#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005307static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005308{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005309 /*
5310 * If the task was not on the rq at the time of this cgroup movement
5311 * it must have been asleep, sleeping tasks keep their ->vruntime
5312 * absolute on their old rq until wakeup (needed for the fair sleeper
5313 * bonus in place_entity()).
5314 *
5315 * If it was on the rq, we've just 'preempted' it, which does convert
5316 * ->vruntime to a relative base.
5317 *
5318 * Make sure both cases convert their relative position when migrating
5319 * to another cgroup's rq. This does somewhat interfere with the
5320 * fair sleeper stuff for the first placement, but who cares.
5321 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005322 /*
5323 * When !on_rq, vruntime of the task has usually NOT been normalized.
5324 * But there are some cases where it has already been normalized:
5325 *
5326 * - Moving a forked child which is waiting for being woken up by
5327 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005328 * - Moving a task which has been woken up by try_to_wake_up() and
5329 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005330 *
5331 * To prevent boost or penalty in the new cfs_rq caused by delta
5332 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5333 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005334 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005335 on_rq = 1;
5336
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005337 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005338 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5339 set_task_rq(p, task_cpu(p));
5340 if (!on_rq)
5341 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005342}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005343
5344void free_fair_sched_group(struct task_group *tg)
5345{
5346 int i;
5347
5348 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5349
5350 for_each_possible_cpu(i) {
5351 if (tg->cfs_rq)
5352 kfree(tg->cfs_rq[i]);
5353 if (tg->se)
5354 kfree(tg->se[i]);
5355 }
5356
5357 kfree(tg->cfs_rq);
5358 kfree(tg->se);
5359}
5360
5361int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5362{
5363 struct cfs_rq *cfs_rq;
5364 struct sched_entity *se;
5365 int i;
5366
5367 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5368 if (!tg->cfs_rq)
5369 goto err;
5370 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5371 if (!tg->se)
5372 goto err;
5373
5374 tg->shares = NICE_0_LOAD;
5375
5376 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5377
5378 for_each_possible_cpu(i) {
5379 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5380 GFP_KERNEL, cpu_to_node(i));
5381 if (!cfs_rq)
5382 goto err;
5383
5384 se = kzalloc_node(sizeof(struct sched_entity),
5385 GFP_KERNEL, cpu_to_node(i));
5386 if (!se)
5387 goto err_free_rq;
5388
5389 init_cfs_rq(cfs_rq);
5390 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5391 }
5392
5393 return 1;
5394
5395err_free_rq:
5396 kfree(cfs_rq);
5397err:
5398 return 0;
5399}
5400
5401void unregister_fair_sched_group(struct task_group *tg, int cpu)
5402{
5403 struct rq *rq = cpu_rq(cpu);
5404 unsigned long flags;
5405
5406 /*
5407 * Only empty task groups can be destroyed; so we can speculatively
5408 * check on_list without danger of it being re-added.
5409 */
5410 if (!tg->cfs_rq[cpu]->on_list)
5411 return;
5412
5413 raw_spin_lock_irqsave(&rq->lock, flags);
5414 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5415 raw_spin_unlock_irqrestore(&rq->lock, flags);
5416}
5417
5418void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5419 struct sched_entity *se, int cpu,
5420 struct sched_entity *parent)
5421{
5422 struct rq *rq = cpu_rq(cpu);
5423
5424 cfs_rq->tg = tg;
5425 cfs_rq->rq = rq;
5426#ifdef CONFIG_SMP
5427 /* allow initial update_cfs_load() to truncate */
5428 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005429#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005430 init_cfs_rq_runtime(cfs_rq);
5431
5432 tg->cfs_rq[cpu] = cfs_rq;
5433 tg->se[cpu] = se;
5434
5435 /* se could be NULL for root_task_group */
5436 if (!se)
5437 return;
5438
5439 if (!parent)
5440 se->cfs_rq = &rq->cfs;
5441 else
5442 se->cfs_rq = parent->my_q;
5443
5444 se->my_q = cfs_rq;
5445 update_load_set(&se->load, 0);
5446 se->parent = parent;
5447}
5448
5449static DEFINE_MUTEX(shares_mutex);
5450
5451int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5452{
5453 int i;
5454 unsigned long flags;
5455
5456 /*
5457 * We can't change the weight of the root cgroup.
5458 */
5459 if (!tg->se[0])
5460 return -EINVAL;
5461
5462 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5463
5464 mutex_lock(&shares_mutex);
5465 if (tg->shares == shares)
5466 goto done;
5467
5468 tg->shares = shares;
5469 for_each_possible_cpu(i) {
5470 struct rq *rq = cpu_rq(i);
5471 struct sched_entity *se;
5472
5473 se = tg->se[i];
5474 /* Propagate contribution to hierarchy */
5475 raw_spin_lock_irqsave(&rq->lock, flags);
5476 for_each_sched_entity(se)
5477 update_cfs_shares(group_cfs_rq(se));
5478 raw_spin_unlock_irqrestore(&rq->lock, flags);
5479 }
5480
5481done:
5482 mutex_unlock(&shares_mutex);
5483 return 0;
5484}
5485#else /* CONFIG_FAIR_GROUP_SCHED */
5486
5487void free_fair_sched_group(struct task_group *tg) { }
5488
5489int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5490{
5491 return 1;
5492}
5493
5494void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5495
5496#endif /* CONFIG_FAIR_GROUP_SCHED */
5497
Peter Zijlstra810b3812008-02-29 15:21:01 -05005498
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005499static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005500{
5501 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005502 unsigned int rr_interval = 0;
5503
5504 /*
5505 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5506 * idle runqueue:
5507 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005508 if (rq->cfs.load.weight)
5509 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005510
5511 return rr_interval;
5512}
5513
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005514/*
5515 * All the scheduling class methods:
5516 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005517const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005518 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005519 .enqueue_task = enqueue_task_fair,
5520 .dequeue_task = dequeue_task_fair,
5521 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005522 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005523
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005524 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005525
5526 .pick_next_task = pick_next_task_fair,
5527 .put_prev_task = put_prev_task_fair,
5528
Peter Williams681f3e62007-10-24 18:23:51 +02005529#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005530 .select_task_rq = select_task_rq_fair,
5531
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005532 .rq_online = rq_online_fair,
5533 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005534
5535 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005536#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005537
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005538 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005539 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005540 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005541
5542 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005543 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005544 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005545
Peter Williams0d721ce2009-09-21 01:31:53 +00005546 .get_rr_interval = get_rr_interval_fair,
5547
Peter Zijlstra810b3812008-02-29 15:21:01 -05005548#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005549 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005550#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005551};
5552
5553#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005554void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005555{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005556 struct cfs_rq *cfs_rq;
5557
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005558 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005559 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005560 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005561 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005562}
5563#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005564
5565__init void init_sched_fair_class(void)
5566{
5567#ifdef CONFIG_SMP
5568 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5569
5570#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005571 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005572 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005573 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005574#endif
5575#endif /* SMP */
5576
5577}