blob: 05d71b458fab7e9c16d3938c0924485b4d5378f9 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
29
30#include <trace/events/sched.h>
31
32#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010033
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034/*
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090036 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 *
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020039 * 'timeslice length' - timeslices in CFS are of variable length
40 * and have no persistent notion like in traditional, time-slice
41 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * (to see the precise effective timeslice length of your workload,
44 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Mike Galbraith21406922010-03-11 17:17:15 +010046unsigned int sysctl_sched_latency = 6000000ULL;
47unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020048
49/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010050 * The initial- and re-scaling of tunables is configurable
51 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
52 *
53 * Options are:
54 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
55 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
56 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
57 */
58enum sched_tunable_scaling sysctl_sched_tunable_scaling
59 = SCHED_TUNABLESCALING_LOG;
60
61/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010062 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090063 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020065unsigned int sysctl_sched_min_granularity = 750000ULL;
66unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067
68/*
69 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
70 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020071static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020074 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020075 * parent will (try to) run first.
76 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020078
79/*
Steve Muckled448aba2012-10-24 15:00:20 -070080 * Controls whether, when SD_SHARE_PKG_RESOURCES is on, if all
81 * tasks go to idle CPUs when woken. If this is off, note that the
82 * per-task flag PF_WAKE_ON_IDLE can still cause a task to go to an
83 * idle CPU upon being woken.
84 */
85unsigned int __read_mostly sysctl_sched_wake_to_idle;
86
87/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020088 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020089 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020090 *
91 * This option delays the preemption effects of decoupled workloads
92 * and reduces their over-scheduling. Synchronous workloads will still
93 * have immediate wakeup/sleep latencies.
94 */
Mike Galbraith172e0822009-09-09 15:41:37 +020095unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010096unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020097
Ingo Molnarda84d962007-10-15 17:00:18 +020098const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
99
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800100/*
101 * The exponential sliding window over which load is averaged for shares
102 * distribution.
103 * (default: 10msec)
104 */
105unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
106
Paul Turnerec12cb72011-07-21 09:43:30 -0700107#ifdef CONFIG_CFS_BANDWIDTH
108/*
109 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
110 * each time a cfs_rq requests quota.
111 *
112 * Note: in the case that the slice exceeds the runtime remaining (either due
113 * to consumption or the quota being specified to be smaller than the slice)
114 * we will always only issue the remaining available time.
115 *
116 * default: 5 msec, units: microseconds
117 */
118unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
119#endif
120
Peter Zijlstra029632f2011-10-25 10:00:11 +0200121/*
122 * Increase the granularity value when there are more CPUs,
123 * because with more CPUs the 'effective latency' as visible
124 * to users decreases. But the relationship is not linear,
125 * so pick a second-best guess by going with the log2 of the
126 * number of CPUs.
127 *
128 * This idea comes from the SD scheduler of Con Kolivas:
129 */
130static int get_update_sysctl_factor(void)
131{
132 unsigned int cpus = min_t(int, num_online_cpus(), 8);
133 unsigned int factor;
134
135 switch (sysctl_sched_tunable_scaling) {
136 case SCHED_TUNABLESCALING_NONE:
137 factor = 1;
138 break;
139 case SCHED_TUNABLESCALING_LINEAR:
140 factor = cpus;
141 break;
142 case SCHED_TUNABLESCALING_LOG:
143 default:
144 factor = 1 + ilog2(cpus);
145 break;
146 }
147
148 return factor;
149}
150
151static void update_sysctl(void)
152{
153 unsigned int factor = get_update_sysctl_factor();
154
155#define SET_SYSCTL(name) \
156 (sysctl_##name = (factor) * normalized_sysctl_##name)
157 SET_SYSCTL(sched_min_granularity);
158 SET_SYSCTL(sched_latency);
159 SET_SYSCTL(sched_wakeup_granularity);
160#undef SET_SYSCTL
161}
162
163void sched_init_granularity(void)
164{
165 update_sysctl();
166}
167
168#if BITS_PER_LONG == 32
169# define WMULT_CONST (~0UL)
170#else
171# define WMULT_CONST (1UL << 32)
172#endif
173
174#define WMULT_SHIFT 32
175
176/*
177 * Shift right and round:
178 */
179#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
180
181/*
182 * delta *= weight / lw
183 */
184static unsigned long
185calc_delta_mine(unsigned long delta_exec, unsigned long weight,
186 struct load_weight *lw)
187{
188 u64 tmp;
189
190 /*
191 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
192 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
193 * 2^SCHED_LOAD_RESOLUTION.
194 */
195 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
196 tmp = (u64)delta_exec * scale_load_down(weight);
197 else
198 tmp = (u64)delta_exec;
199
200 if (!lw->inv_weight) {
201 unsigned long w = scale_load_down(lw->weight);
202
203 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
204 lw->inv_weight = 1;
205 else if (unlikely(!w))
206 lw->inv_weight = WMULT_CONST;
207 else
208 lw->inv_weight = WMULT_CONST / w;
209 }
210
211 /*
212 * Check whether we'd overflow the 64-bit multiplication:
213 */
214 if (unlikely(tmp > WMULT_CONST))
215 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
216 WMULT_SHIFT/2);
217 else
218 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
219
220 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
221}
222
223
224const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200225
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200226/**************************************************************
227 * CFS operations on generic schedulable entities:
228 */
229
230#ifdef CONFIG_FAIR_GROUP_SCHED
231
232/* cpu runqueue to which this cfs_rq is attached */
233static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
234{
235 return cfs_rq->rq;
236}
237
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200238/* An entity is a task if it doesn't "own" a runqueue */
239#define entity_is_task(se) (!se->my_q)
240
Peter Zijlstra8f488942009-07-24 12:25:30 +0200241static inline struct task_struct *task_of(struct sched_entity *se)
242{
243#ifdef CONFIG_SCHED_DEBUG
244 WARN_ON_ONCE(!entity_is_task(se));
245#endif
246 return container_of(se, struct task_struct, se);
247}
248
Peter Zijlstrab7581492008-04-19 19:45:00 +0200249/* Walk up scheduling entities hierarchy */
250#define for_each_sched_entity(se) \
251 for (; se; se = se->parent)
252
253static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
254{
255 return p->se.cfs_rq;
256}
257
258/* runqueue on which this entity is (to be) queued */
259static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
260{
261 return se->cfs_rq;
262}
263
264/* runqueue "owned" by this group */
265static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
266{
267 return grp->my_q;
268}
269
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800270static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
271{
272 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800273 /*
274 * Ensure we either appear before our parent (if already
275 * enqueued) or force our parent to appear after us when it is
276 * enqueued. The fact that we always enqueue bottom-up
277 * reduces this to two cases.
278 */
279 if (cfs_rq->tg->parent &&
280 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
281 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800282 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800283 } else {
284 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
285 &rq_of(cfs_rq)->leaf_cfs_rq_list);
286 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800287
288 cfs_rq->on_list = 1;
289 }
290}
291
292static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
293{
294 if (cfs_rq->on_list) {
295 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
296 cfs_rq->on_list = 0;
297 }
298}
299
Peter Zijlstrab7581492008-04-19 19:45:00 +0200300/* Iterate thr' all leaf cfs_rq's on a runqueue */
301#define for_each_leaf_cfs_rq(rq, cfs_rq) \
302 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
303
304/* Do the two (enqueued) entities belong to the same group ? */
305static inline int
306is_same_group(struct sched_entity *se, struct sched_entity *pse)
307{
308 if (se->cfs_rq == pse->cfs_rq)
309 return 1;
310
311 return 0;
312}
313
314static inline struct sched_entity *parent_entity(struct sched_entity *se)
315{
316 return se->parent;
317}
318
Peter Zijlstra464b7522008-10-24 11:06:15 +0200319/* return depth at which a sched entity is present in the hierarchy */
320static inline int depth_se(struct sched_entity *se)
321{
322 int depth = 0;
323
324 for_each_sched_entity(se)
325 depth++;
326
327 return depth;
328}
329
330static void
331find_matching_se(struct sched_entity **se, struct sched_entity **pse)
332{
333 int se_depth, pse_depth;
334
335 /*
336 * preemption test can be made between sibling entities who are in the
337 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
338 * both tasks until we find their ancestors who are siblings of common
339 * parent.
340 */
341
342 /* First walk up until both entities are at same depth */
343 se_depth = depth_se(*se);
344 pse_depth = depth_se(*pse);
345
346 while (se_depth > pse_depth) {
347 se_depth--;
348 *se = parent_entity(*se);
349 }
350
351 while (pse_depth > se_depth) {
352 pse_depth--;
353 *pse = parent_entity(*pse);
354 }
355
356 while (!is_same_group(*se, *pse)) {
357 *se = parent_entity(*se);
358 *pse = parent_entity(*pse);
359 }
360}
361
Peter Zijlstra8f488942009-07-24 12:25:30 +0200362#else /* !CONFIG_FAIR_GROUP_SCHED */
363
364static inline struct task_struct *task_of(struct sched_entity *se)
365{
366 return container_of(se, struct task_struct, se);
367}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200368
369static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
370{
371 return container_of(cfs_rq, struct rq, cfs);
372}
373
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374#define entity_is_task(se) 1
375
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376#define for_each_sched_entity(se) \
377 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200378
Peter Zijlstrab7581492008-04-19 19:45:00 +0200379static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200380{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200381 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200382}
383
Peter Zijlstrab7581492008-04-19 19:45:00 +0200384static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
385{
386 struct task_struct *p = task_of(se);
387 struct rq *rq = task_rq(p);
388
389 return &rq->cfs;
390}
391
392/* runqueue "owned" by this group */
393static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
394{
395 return NULL;
396}
397
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800398static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
399{
400}
401
402static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
403{
404}
405
Peter Zijlstrab7581492008-04-19 19:45:00 +0200406#define for_each_leaf_cfs_rq(rq, cfs_rq) \
407 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
408
409static inline int
410is_same_group(struct sched_entity *se, struct sched_entity *pse)
411{
412 return 1;
413}
414
415static inline struct sched_entity *parent_entity(struct sched_entity *se)
416{
417 return NULL;
418}
419
Peter Zijlstra464b7522008-10-24 11:06:15 +0200420static inline void
421find_matching_se(struct sched_entity **se, struct sched_entity **pse)
422{
423}
424
Peter Zijlstrab7581492008-04-19 19:45:00 +0200425#endif /* CONFIG_FAIR_GROUP_SCHED */
426
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700427static __always_inline
428void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200429
430/**************************************************************
431 * Scheduling class tree data structure manipulation methods:
432 */
433
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200434static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200435{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200436 s64 delta = (s64)(vruntime - min_vruntime);
437 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200438 min_vruntime = vruntime;
439
440 return min_vruntime;
441}
442
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200443static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200444{
445 s64 delta = (s64)(vruntime - min_vruntime);
446 if (delta < 0)
447 min_vruntime = vruntime;
448
449 return min_vruntime;
450}
451
Fabio Checconi54fdc582009-07-16 12:32:27 +0200452static inline int entity_before(struct sched_entity *a,
453 struct sched_entity *b)
454{
455 return (s64)(a->vruntime - b->vruntime) < 0;
456}
457
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200458static void update_min_vruntime(struct cfs_rq *cfs_rq)
459{
460 u64 vruntime = cfs_rq->min_vruntime;
461
462 if (cfs_rq->curr)
463 vruntime = cfs_rq->curr->vruntime;
464
465 if (cfs_rq->rb_leftmost) {
466 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
467 struct sched_entity,
468 run_node);
469
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100470 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200471 vruntime = se->vruntime;
472 else
473 vruntime = min_vruntime(vruntime, se->vruntime);
474 }
475
476 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200477#ifndef CONFIG_64BIT
478 smp_wmb();
479 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
480#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200481}
482
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200483/*
484 * Enqueue an entity into the rb-tree:
485 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200486static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200487{
488 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
489 struct rb_node *parent = NULL;
490 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200491 int leftmost = 1;
492
493 /*
494 * Find the right place in the rbtree:
495 */
496 while (*link) {
497 parent = *link;
498 entry = rb_entry(parent, struct sched_entity, run_node);
499 /*
500 * We dont care about collisions. Nodes with
501 * the same key stay together.
502 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200503 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200504 link = &parent->rb_left;
505 } else {
506 link = &parent->rb_right;
507 leftmost = 0;
508 }
509 }
510
511 /*
512 * Maintain a cache of leftmost tree entries (it is frequently
513 * used):
514 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200515 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200516 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517
518 rb_link_node(&se->run_node, parent, link);
519 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200520}
521
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200522static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200523{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100524 if (cfs_rq->rb_leftmost == &se->run_node) {
525 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100526
527 next_node = rb_next(&se->run_node);
528 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100529 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200530
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200532}
533
Peter Zijlstra029632f2011-10-25 10:00:11 +0200534struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200535{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100536 struct rb_node *left = cfs_rq->rb_leftmost;
537
538 if (!left)
539 return NULL;
540
541 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542}
543
Rik van Rielac53db52011-02-01 09:51:03 -0500544static struct sched_entity *__pick_next_entity(struct sched_entity *se)
545{
546 struct rb_node *next = rb_next(&se->run_node);
547
548 if (!next)
549 return NULL;
550
551 return rb_entry(next, struct sched_entity, run_node);
552}
553
554#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200555struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200556{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100557 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200558
Balbir Singh70eee742008-02-22 13:25:53 +0530559 if (!last)
560 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100561
562 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200563}
564
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200565/**************************************************************
566 * Scheduling class statistics methods:
567 */
568
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100569int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700570 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100571 loff_t *ppos)
572{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700573 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100574 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100575
576 if (ret || !write)
577 return ret;
578
579 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
580 sysctl_sched_min_granularity);
581
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100582#define WRT_SYSCTL(name) \
583 (normalized_sysctl_##name = sysctl_##name / (factor))
584 WRT_SYSCTL(sched_min_granularity);
585 WRT_SYSCTL(sched_latency);
586 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100587#undef WRT_SYSCTL
588
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100589 return 0;
590}
591#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200592
593/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200594 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200595 */
596static inline unsigned long
597calc_delta_fair(unsigned long delta, struct sched_entity *se)
598{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200599 if (unlikely(se->load.weight != NICE_0_LOAD))
600 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200601
602 return delta;
603}
604
605/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200606 * The idea is to set a period in which each task runs once.
607 *
608 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
609 * this period because otherwise the slices get too small.
610 *
611 * p = (nr <= nl) ? l : l*nr/nl
612 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200613static u64 __sched_period(unsigned long nr_running)
614{
615 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100616 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617
618 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100619 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200620 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200621 }
622
623 return period;
624}
625
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200626/*
627 * We calculate the wall-time slice from the period by taking a part
628 * proportional to the weight.
629 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200630 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200631 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200632static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200633{
Mike Galbraith0a582442009-01-02 12:16:42 +0100634 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200635
Mike Galbraith0a582442009-01-02 12:16:42 +0100636 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100637 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200638 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100639
640 cfs_rq = cfs_rq_of(se);
641 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200642
Mike Galbraith0a582442009-01-02 12:16:42 +0100643 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200644 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100645
646 update_load_add(&lw, se->load.weight);
647 load = &lw;
648 }
649 slice = calc_delta_mine(slice, se->load.weight, load);
650 }
651 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652}
653
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200654/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200655 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200656 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200657 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200658 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200659static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200660{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200661 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200662}
663
Paul Turnerd6b55912010-11-15 15:47:09 -0800664static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800665static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800666
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200667/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200668 * Update the current task's runtime statistics. Skip current tasks that
669 * are not in our scheduling class.
670 */
671static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200672__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
673 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200674{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200675 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200676
Lucas De Marchi41acab82010-03-10 23:37:45 -0300677 schedstat_set(curr->statistics.exec_max,
678 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200679
680 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200681 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200682 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100683
Ingo Molnare9acbff2007-10-15 17:00:04 +0200684 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200685 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800686
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100687#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800688 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800689#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690}
691
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200692static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693{
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200694 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700695 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200696 unsigned long delta_exec;
697
698 if (unlikely(!curr))
699 return;
700
701 /*
702 * Get the amount of time the current task was running
703 * since the last time we changed load (this cannot
704 * overflow on 32 bits):
705 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200706 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100707 if (!delta_exec)
708 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200709
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200710 __update_curr(cfs_rq, curr, delta_exec);
711 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100712
713 if (entity_is_task(curr)) {
714 struct task_struct *curtask = task_of(curr);
715
Ingo Molnarf977bb42009-09-13 18:15:54 +0200716 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100717 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700718 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100719 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700720
721 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200722}
723
724static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200725update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300727 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200728}
729
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200730/*
731 * Task is being enqueued - update stats:
732 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200733static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200734{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735 /*
736 * Are we enqueueing a waiting task? (for current tasks
737 * a dequeue/enqueue event is a NOP)
738 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200739 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200740 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200741}
742
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200743static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200744update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200745{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300746 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
747 rq_of(cfs_rq)->clock - se->statistics.wait_start));
748 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
749 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
750 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200751#ifdef CONFIG_SCHEDSTATS
752 if (entity_is_task(se)) {
753 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300754 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200755 }
756#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300757 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200758}
759
760static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200761update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200762{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200763 /*
764 * Mark the end of the wait period if dequeueing a
765 * waiting task:
766 */
Ingo Molnar429d43bc2007-10-15 17:00:03 +0200767 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200768 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200769}
770
771/*
772 * We are picking a new current task - update its stats:
773 */
774static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200775update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200776{
777 /*
778 * We are starting a new run period:
779 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700780 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200781}
782
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200783/**************************************************
784 * Scheduling class queueing methods:
785 */
786
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200787static void
788account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
789{
790 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200791 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200792 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100793#ifdef CONFIG_SMP
794 if (entity_is_task(se))
Peter Zijlstraeb953082012-04-17 13:38:40 +0200795 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100796#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200797 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200798}
799
800static void
801account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
802{
803 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200804 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +0200805 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +0100806 if (entity_is_task(se))
Bharata B Raob87f1722008-09-25 09:53:54 +0530807 list_del_init(&se->group_node);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200808 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200809}
810
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800811#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner64660c82011-07-21 09:43:36 -0700812/* we need this in update_cfs_load and load-balance functions below */
813static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800814# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800815static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
816 int global_update)
817{
818 struct task_group *tg = cfs_rq->tg;
819 long load_avg;
820
821 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
822 load_avg -= cfs_rq->load_contribution;
823
824 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
825 atomic_add(load_avg, &tg->load_weight);
826 cfs_rq->load_contribution += load_avg;
827 }
828}
829
830static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800831{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800832 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800833 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800834 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800835
Paul Turner64660c82011-07-21 09:43:36 -0700836 if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800837 return;
838
Paul Turner05ca62c2011-01-21 20:45:02 -0800839 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800840 delta = now - cfs_rq->load_stamp;
841
Paul Turnere33078b2010-11-15 15:47:04 -0800842 /* truncate load history at 4 idle periods */
843 if (cfs_rq->load_stamp > cfs_rq->load_last &&
844 now - cfs_rq->load_last > 4 * period) {
845 cfs_rq->load_period = 0;
846 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800847 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800848 }
849
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800850 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800851 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800852 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800853 if (load) {
854 cfs_rq->load_last = now;
855 cfs_rq->load_avg += delta * load;
856 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800857
Paul Turnerd6b55912010-11-15 15:47:09 -0800858 /* consider updating load contribution on each fold or truncate */
859 if (global_update || cfs_rq->load_period > period
860 || !cfs_rq->load_period)
861 update_cfs_rq_load_contribution(cfs_rq, global_update);
862
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800863 while (cfs_rq->load_period > period) {
864 /*
865 * Inline assembly required to prevent the compiler
866 * optimising this loop into a divmod call.
867 * See __iter_div_u64_rem() for another example of this.
868 */
869 asm("" : "+rm" (cfs_rq->load_period));
870 cfs_rq->load_period /= 2;
871 cfs_rq->load_avg /= 2;
872 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800873
Paul Turnere33078b2010-11-15 15:47:04 -0800874 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
875 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800876}
877
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200878static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
879{
880 long tg_weight;
881
882 /*
883 * Use this CPU's actual weight instead of the last load_contribution
884 * to gain a more accurate current total weight. See
885 * update_cfs_rq_load_contribution().
886 */
887 tg_weight = atomic_read(&tg->load_weight);
888 tg_weight -= cfs_rq->load_contribution;
889 tg_weight += cfs_rq->load.weight;
890
891 return tg_weight;
892}
893
Paul Turner6d5ab292011-01-21 20:45:01 -0800894static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800895{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200896 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800897
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200898 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -0800899 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800900
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800901 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +0200902 if (tg_weight)
903 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800904
905 if (shares < MIN_SHARES)
906 shares = MIN_SHARES;
907 if (shares > tg->shares)
908 shares = tg->shares;
909
910 return shares;
911}
912
913static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
914{
915 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
916 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800917 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800918 }
919}
920# else /* CONFIG_SMP */
921static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
922{
923}
924
Paul Turner6d5ab292011-01-21 20:45:01 -0800925static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800926{
927 return tg->shares;
928}
929
930static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
931{
932}
933# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800934static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
935 unsigned long weight)
936{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800937 if (se->on_rq) {
938 /* commit outstanding execution time */
939 if (cfs_rq->curr == se)
940 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800941 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800942 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800943
944 update_load_set(&se->load, weight);
945
946 if (se->on_rq)
947 account_entity_enqueue(cfs_rq, se);
948}
949
Paul Turner6d5ab292011-01-21 20:45:01 -0800950static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800951{
952 struct task_group *tg;
953 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800954 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800955
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800956 tg = cfs_rq->tg;
957 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -0700958 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800959 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800960#ifndef CONFIG_SMP
961 if (likely(se->load.weight == tg->shares))
962 return;
963#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800964 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800965
966 reweight_entity(cfs_rq_of(se), se, shares);
967}
968#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800969static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800970{
971}
972
Paul Turner6d5ab292011-01-21 20:45:01 -0800973static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800974{
975}
Paul Turner43365bd2010-12-15 19:10:17 -0800976
977static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
978{
979}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800980#endif /* CONFIG_FAIR_GROUP_SCHED */
981
Ingo Molnar2396af62007-08-09 11:16:48 +0200982static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200983{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +0200985 struct task_struct *tsk = NULL;
986
987 if (entity_is_task(se))
988 tsk = task_of(se);
989
Lucas De Marchi41acab82010-03-10 23:37:45 -0300990 if (se->statistics.sleep_start) {
991 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200992
993 if ((s64)delta < 0)
994 delta = 0;
995
Lucas De Marchi41acab82010-03-10 23:37:45 -0300996 if (unlikely(delta > se->statistics.sleep_max))
997 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200998
Peter Zijlstra8c79a042012-01-30 14:51:37 +0100999 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001000 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01001001
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001002 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02001003 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001004 trace_sched_stat_sleep(tsk, delta);
1005 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001006 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03001007 if (se->statistics.block_start) {
1008 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001009
1010 if ((s64)delta < 0)
1011 delta = 0;
1012
Lucas De Marchi41acab82010-03-10 23:37:45 -03001013 if (unlikely(delta > se->statistics.block_max))
1014 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001015
Peter Zijlstra8c79a042012-01-30 14:51:37 +01001016 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001017 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02001018
Peter Zijlstrae4143142009-07-23 20:13:26 +02001019 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001020 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001021 se->statistics.iowait_sum += delta;
1022 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02001023 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001024 }
1025
Andrew Vaginb781a602011-11-28 12:03:35 +03001026 trace_sched_stat_blocked(tsk, delta);
1027
Peter Zijlstrae4143142009-07-23 20:13:26 +02001028 /*
1029 * Blocking time is in units of nanosecs, so shift by
1030 * 20 to get a milliseconds-range estimation of the
1031 * amount of time that the task spent sleeping:
1032 */
1033 if (unlikely(prof_on == SLEEP_PROFILING)) {
1034 profile_hits(SLEEP_PROFILING,
1035 (void *)get_wchan(tsk),
1036 delta >> 20);
1037 }
1038 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02001039 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040 }
1041#endif
1042}
1043
Peter Zijlstraddc97292007-10-15 17:00:10 +02001044static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
1045{
1046#ifdef CONFIG_SCHED_DEBUG
1047 s64 d = se->vruntime - cfs_rq->min_vruntime;
1048
1049 if (d < 0)
1050 d = -d;
1051
1052 if (d > 3*sysctl_sched_latency)
1053 schedstat_inc(cfs_rq, nr_spread_over);
1054#endif
1055}
1056
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001057static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001058place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
1059{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02001060 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001061
Peter Zijlstra2cb86002007-11-09 22:39:37 +01001062 /*
1063 * The 'current' period is already promised to the current tasks,
1064 * however the extra weight of the new task will slow them down a
1065 * little, place the new task so that it fits in the slot that
1066 * stays open at the end.
1067 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02001068 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02001069 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001070
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001071 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01001072 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001073 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001074
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001075 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001076 * Halve their sleep time's effect, to allow
1077 * for a gentler effect of sleepers:
1078 */
1079 if (sched_feat(GENTLE_FAIR_SLEEPERS))
1080 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02001081
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02001082 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001083 }
1084
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001085 /* ensure we never gain time by being placed backwards. */
1086 vruntime = max_vruntime(se->vruntime, vruntime);
1087
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001088 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001089}
1090
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001091static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
1092
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001093static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001094enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001095{
1096 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001097 * Update the normalized vruntime before updating min_vruntime
1098 * through callig update_curr().
1099 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001100 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001101 se->vruntime += cfs_rq->min_vruntime;
1102
1103 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001104 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001105 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001106 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08001107 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +02001108 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -08001109 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001110
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001111 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001112 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02001113 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02001114 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001115
Ingo Molnard2417e52007-08-09 11:16:47 +02001116 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001117 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001118 if (se != cfs_rq->curr)
1119 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001120 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001121
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001122 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08001123 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001124 check_enqueue_throttle(cfs_rq);
1125 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001126}
1127
Rik van Riel2c13c9192011-02-01 09:48:37 -05001128static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01001129{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001130 for_each_sched_entity(se) {
1131 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1132 if (cfs_rq->last == se)
1133 cfs_rq->last = NULL;
1134 else
1135 break;
1136 }
1137}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001138
Rik van Riel2c13c9192011-02-01 09:48:37 -05001139static void __clear_buddies_next(struct sched_entity *se)
1140{
1141 for_each_sched_entity(se) {
1142 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1143 if (cfs_rq->next == se)
1144 cfs_rq->next = NULL;
1145 else
1146 break;
1147 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001148}
1149
Rik van Rielac53db52011-02-01 09:51:03 -05001150static void __clear_buddies_skip(struct sched_entity *se)
1151{
1152 for_each_sched_entity(se) {
1153 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1154 if (cfs_rq->skip == se)
1155 cfs_rq->skip = NULL;
1156 else
1157 break;
1158 }
1159}
1160
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001161static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1162{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001163 if (cfs_rq->last == se)
1164 __clear_buddies_last(se);
1165
1166 if (cfs_rq->next == se)
1167 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001168
1169 if (cfs_rq->skip == se)
1170 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001171}
1172
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001173static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07001174
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001175static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001176dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001177{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001178 /*
1179 * Update run-time statistics of the 'current'.
1180 */
1181 update_curr(cfs_rq);
1182
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001183 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001184 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001185#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001186 if (entity_is_task(se)) {
1187 struct task_struct *tsk = task_of(se);
1188
1189 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001190 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001191 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001192 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001193 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001194#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001195 }
1196
Peter Zijlstra2002c692008-11-11 11:52:33 +01001197 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001198
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001199 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001200 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001201 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001202 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001203 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001204
1205 /*
1206 * Normalize the entity after updating the min_vruntime because the
1207 * update can refer to the ->curr item and we need to reflect this
1208 * movement in our normalized position.
1209 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001210 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001211 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001212
Paul Turnerd8b49862011-07-21 09:43:41 -07001213 /* return excess runtime on last dequeue */
1214 return_cfs_rq_runtime(cfs_rq);
1215
Peter Zijlstra1e876232011-05-17 16:21:10 -07001216 update_min_vruntime(cfs_rq);
1217 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001218}
1219
1220/*
1221 * Preempt the current task with a newly woken task if needed:
1222 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001223static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001224check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001225{
Peter Zijlstra11697832007-09-05 14:32:49 +02001226 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001227 struct sched_entity *se;
1228 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02001229
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02001230 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001231 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001232 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001233 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001234 /*
1235 * The current task ran long enough, ensure it doesn't get
1236 * re-elected due to buddy favours.
1237 */
1238 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001239 return;
1240 }
1241
1242 /*
1243 * Ensure that a task that missed wakeup preemption by a
1244 * narrow margin doesn't have to wait for a full slice.
1245 * This also mitigates buddy induced latencies under load.
1246 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001247 if (delta_exec < sysctl_sched_min_granularity)
1248 return;
1249
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001250 se = __pick_first_entity(cfs_rq);
1251 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001252
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001253 if (delta < 0)
1254 return;
Mike Galbraithd7d829442011-01-05 05:41:17 +01001255
Wang Xingchaof4cfb332011-09-16 13:35:52 -04001256 if (delta > ideal_runtime)
1257 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001258}
1259
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001260static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001261set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001262{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001263 /* 'current' is not kept within the tree. */
1264 if (se->on_rq) {
1265 /*
1266 * Any task has to be enqueued before it get to execute on
1267 * a CPU. So account for the time it spent waiting on the
1268 * runqueue.
1269 */
1270 update_stats_wait_end(cfs_rq, se);
1271 __dequeue_entity(cfs_rq, se);
1272 }
1273
Ingo Molnar79303e92007-08-09 11:16:47 +02001274 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001275 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001276#ifdef CONFIG_SCHEDSTATS
1277 /*
1278 * Track our maximum slice length, if the CPU's load is at
1279 * least twice that of our own weight (i.e. dont track it
1280 * when there are only lesser-weight tasks around):
1281 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001282 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001283 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001284 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1285 }
1286#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001287 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001288}
1289
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001290static int
1291wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1292
Rik van Rielac53db52011-02-01 09:51:03 -05001293/*
1294 * Pick the next process, keeping these things in mind, in this order:
1295 * 1) keep things fair between processes/task groups
1296 * 2) pick the "next" process, since someone really wants that to run
1297 * 3) pick the "last" process, for cache locality
1298 * 4) do not run the "skip" process, if something else is available
1299 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001300static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001301{
Rik van Rielac53db52011-02-01 09:51:03 -05001302 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001303 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001304
Rik van Rielac53db52011-02-01 09:51:03 -05001305 /*
1306 * Avoid running the skip buddy, if running something else can
1307 * be done without getting too unfair.
1308 */
1309 if (cfs_rq->skip == se) {
1310 struct sched_entity *second = __pick_next_entity(se);
1311 if (second && wakeup_preempt_entity(second, left) < 1)
1312 se = second;
1313 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001314
Mike Galbraithf685cea2009-10-23 23:09:22 +02001315 /*
1316 * Prefer last buddy, try to return the CPU to a preempted task.
1317 */
1318 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1319 se = cfs_rq->last;
1320
Rik van Rielac53db52011-02-01 09:51:03 -05001321 /*
1322 * Someone really wants this to run. If it's not unfair, run it.
1323 */
1324 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1325 se = cfs_rq->next;
1326
Mike Galbraithf685cea2009-10-23 23:09:22 +02001327 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001328
1329 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001330}
1331
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001332static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
1333
Ingo Molnarab6cde22007-08-09 11:16:48 +02001334static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001335{
1336 /*
1337 * If still on the runqueue then deactivate_task()
1338 * was not called and update_curr() has to be done:
1339 */
1340 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001341 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001342
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001343 /* throttle cfs_rqs exceeding runtime */
1344 check_cfs_rq_runtime(cfs_rq);
1345
Peter Zijlstraddc97292007-10-15 17:00:10 +02001346 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001347 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001348 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001349 /* Put 'current' back into the tree. */
1350 __enqueue_entity(cfs_rq, prev);
1351 }
Ingo Molnar429d43bc2007-10-15 17:00:03 +02001352 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001353}
1354
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001355static void
1356entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001357{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001358 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001359 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001360 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001361 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001362
Paul Turner43365bd2010-12-15 19:10:17 -08001363 /*
1364 * Update share accounting for long-running entities.
1365 */
1366 update_entity_shares_tick(cfs_rq);
1367
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001368#ifdef CONFIG_SCHED_HRTICK
1369 /*
1370 * queued ticks are scheduled to match the slice, so don't bother
1371 * validating it and just reschedule.
1372 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001373 if (queued) {
1374 resched_task(rq_of(cfs_rq)->curr);
1375 return;
1376 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001377 /*
1378 * don't let the period tick interfere with the hrtick preemption
1379 */
1380 if (!sched_feat(DOUBLE_TICK) &&
1381 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1382 return;
1383#endif
1384
Yong Zhang2c2efae2011-07-29 16:20:33 +08001385 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001386 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001387}
1388
Paul Turnerab84d312011-07-21 09:43:28 -07001389
1390/**************************************************
1391 * CFS bandwidth control machinery
1392 */
1393
1394#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02001395
1396#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001397static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001398
1399static inline bool cfs_bandwidth_used(void)
1400{
Ingo Molnarc5905af2012-02-24 08:31:31 +01001401 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001402}
1403
1404void account_cfs_bandwidth_used(int enabled, int was_enabled)
1405{
1406 /* only need to count groups transitioning between enabled/!enabled */
1407 if (enabled && !was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001408 static_key_slow_inc(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001409 else if (!enabled && was_enabled)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001410 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001411}
1412#else /* HAVE_JUMP_LABEL */
1413static bool cfs_bandwidth_used(void)
1414{
1415 return true;
1416}
1417
1418void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
1419#endif /* HAVE_JUMP_LABEL */
1420
Paul Turnerab84d312011-07-21 09:43:28 -07001421/*
1422 * default period for cfs group bandwidth.
1423 * default: 0.1s, units: nanoseconds
1424 */
1425static inline u64 default_cfs_period(void)
1426{
1427 return 100000000ULL;
1428}
Paul Turnerec12cb72011-07-21 09:43:30 -07001429
1430static inline u64 sched_cfs_bandwidth_slice(void)
1431{
1432 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1433}
1434
Paul Turnera9cf55b2011-07-21 09:43:32 -07001435/*
1436 * Replenish runtime according to assigned quota and update expiration time.
1437 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1438 * additional synchronization around rq->lock.
1439 *
1440 * requires cfs_b->lock
1441 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001442void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001443{
1444 u64 now;
1445
1446 if (cfs_b->quota == RUNTIME_INF)
1447 return;
1448
1449 now = sched_clock_cpu(smp_processor_id());
1450 cfs_b->runtime = cfs_b->quota;
1451 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1452}
1453
Peter Zijlstra029632f2011-10-25 10:00:11 +02001454static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1455{
1456 return &tg->cfs_bandwidth;
1457}
1458
Paul Turner85dac902011-07-21 09:43:33 -07001459/* returns 0 on failure to allocate runtime */
1460static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001461{
1462 struct task_group *tg = cfs_rq->tg;
1463 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001464 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001465
1466 /* note: this is a positive sum as runtime_remaining <= 0 */
1467 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1468
1469 raw_spin_lock(&cfs_b->lock);
1470 if (cfs_b->quota == RUNTIME_INF)
1471 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001472 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001473 /*
1474 * If the bandwidth pool has become inactive, then at least one
1475 * period must have elapsed since the last consumption.
1476 * Refresh the global state and ensure bandwidth timer becomes
1477 * active.
1478 */
1479 if (!cfs_b->timer_active) {
1480 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001481 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001482 }
Paul Turner58088ad2011-07-21 09:43:31 -07001483
1484 if (cfs_b->runtime > 0) {
1485 amount = min(cfs_b->runtime, min_amount);
1486 cfs_b->runtime -= amount;
1487 cfs_b->idle = 0;
1488 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001489 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001490 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001491 raw_spin_unlock(&cfs_b->lock);
1492
1493 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001494 /*
1495 * we may have advanced our local expiration to account for allowed
1496 * spread between our sched_clock and the one on which runtime was
1497 * issued.
1498 */
1499 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1500 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001501
1502 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001503}
1504
1505/*
1506 * Note: This depends on the synchronization provided by sched_clock and the
1507 * fact that rq->clock snapshots this value.
1508 */
1509static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1510{
1511 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1512 struct rq *rq = rq_of(cfs_rq);
1513
1514 /* if the deadline is ahead of our clock, nothing to do */
1515 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1516 return;
1517
1518 if (cfs_rq->runtime_remaining < 0)
1519 return;
1520
1521 /*
1522 * If the local deadline has passed we have to consider the
1523 * possibility that our sched_clock is 'fast' and the global deadline
1524 * has not truly expired.
1525 *
1526 * Fortunately we can check determine whether this the case by checking
1527 * whether the global deadline has advanced.
1528 */
1529
1530 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1531 /* extend local deadline, drift is bounded above by 2 ticks */
1532 cfs_rq->runtime_expires += TICK_NSEC;
1533 } else {
1534 /* global deadline is ahead, expiration has passed */
1535 cfs_rq->runtime_remaining = 0;
1536 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001537}
1538
1539static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1540 unsigned long delta_exec)
1541{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001542 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001543 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001544 expire_cfs_rq_runtime(cfs_rq);
1545
1546 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001547 return;
1548
Paul Turner85dac902011-07-21 09:43:33 -07001549 /*
1550 * if we're unable to extend our runtime we resched so that the active
1551 * hierarchy can be throttled
1552 */
1553 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1554 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001555}
1556
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001557static __always_inline
1558void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001559{
Paul Turner56f570e2011-11-07 20:26:33 -08001560 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001561 return;
1562
1563 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1564}
1565
Paul Turner85dac902011-07-21 09:43:33 -07001566static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1567{
Paul Turner56f570e2011-11-07 20:26:33 -08001568 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001569}
1570
Paul Turner64660c82011-07-21 09:43:36 -07001571/* check whether cfs_rq, or any parent, is throttled */
1572static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1573{
Paul Turner56f570e2011-11-07 20:26:33 -08001574 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001575}
1576
1577/*
1578 * Ensure that neither of the group entities corresponding to src_cpu or
1579 * dest_cpu are members of a throttled hierarchy when performing group
1580 * load-balance operations.
1581 */
1582static inline int throttled_lb_pair(struct task_group *tg,
1583 int src_cpu, int dest_cpu)
1584{
1585 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1586
1587 src_cfs_rq = tg->cfs_rq[src_cpu];
1588 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1589
1590 return throttled_hierarchy(src_cfs_rq) ||
1591 throttled_hierarchy(dest_cfs_rq);
1592}
1593
1594/* updated child weight may affect parent so we have to do this bottom up */
1595static int tg_unthrottle_up(struct task_group *tg, void *data)
1596{
1597 struct rq *rq = data;
1598 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1599
1600 cfs_rq->throttle_count--;
1601#ifdef CONFIG_SMP
1602 if (!cfs_rq->throttle_count) {
1603 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1604
1605 /* leaving throttled state, advance shares averaging windows */
1606 cfs_rq->load_stamp += delta;
1607 cfs_rq->load_last += delta;
1608
1609 /* update entity weight now that we are on_rq again */
1610 update_cfs_shares(cfs_rq);
1611 }
1612#endif
1613
1614 return 0;
1615}
1616
1617static int tg_throttle_down(struct task_group *tg, void *data)
1618{
1619 struct rq *rq = data;
1620 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1621
1622 /* group is entering throttled state, record last load */
1623 if (!cfs_rq->throttle_count)
1624 update_cfs_load(cfs_rq, 0);
1625 cfs_rq->throttle_count++;
1626
1627 return 0;
1628}
1629
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001630static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001631{
1632 struct rq *rq = rq_of(cfs_rq);
1633 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1634 struct sched_entity *se;
1635 long task_delta, dequeue = 1;
1636
1637 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1638
1639 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001640 rcu_read_lock();
1641 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1642 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001643
1644 task_delta = cfs_rq->h_nr_running;
1645 for_each_sched_entity(se) {
1646 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1647 /* throttled entity or throttle-on-deactivate */
1648 if (!se->on_rq)
1649 break;
1650
1651 if (dequeue)
1652 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1653 qcfs_rq->h_nr_running -= task_delta;
1654
1655 if (qcfs_rq->load.weight)
1656 dequeue = 0;
1657 }
1658
1659 if (!se)
1660 rq->nr_running -= task_delta;
1661
1662 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001663 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001664 raw_spin_lock(&cfs_b->lock);
1665 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Ben Segalldfb473b2013-10-16 11:16:32 -07001666 if (!cfs_b->timer_active)
1667 __start_cfs_bandwidth(cfs_b);
Paul Turner85dac902011-07-21 09:43:33 -07001668 raw_spin_unlock(&cfs_b->lock);
1669}
1670
Peter Zijlstra029632f2011-10-25 10:00:11 +02001671void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001672{
1673 struct rq *rq = rq_of(cfs_rq);
1674 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1675 struct sched_entity *se;
1676 int enqueue = 1;
1677 long task_delta;
1678
1679 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1680
1681 cfs_rq->throttled = 0;
1682 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001683 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001684 list_del_rcu(&cfs_rq->throttled_list);
1685 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001686 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001687
Paul Turner64660c82011-07-21 09:43:36 -07001688 update_rq_clock(rq);
1689 /* update hierarchical throttle state */
1690 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1691
Paul Turner671fd9d2011-07-21 09:43:34 -07001692 if (!cfs_rq->load.weight)
1693 return;
1694
1695 task_delta = cfs_rq->h_nr_running;
1696 for_each_sched_entity(se) {
1697 if (se->on_rq)
1698 enqueue = 0;
1699
1700 cfs_rq = cfs_rq_of(se);
1701 if (enqueue)
1702 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1703 cfs_rq->h_nr_running += task_delta;
1704
1705 if (cfs_rq_throttled(cfs_rq))
1706 break;
1707 }
1708
1709 if (!se)
1710 rq->nr_running += task_delta;
1711
1712 /* determine whether we need to wake up potentially idle cpu */
1713 if (rq->curr == rq->idle && rq->cfs.nr_running)
1714 resched_task(rq->curr);
1715}
1716
1717static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1718 u64 remaining, u64 expires)
1719{
1720 struct cfs_rq *cfs_rq;
1721 u64 runtime = remaining;
1722
1723 rcu_read_lock();
1724 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1725 throttled_list) {
1726 struct rq *rq = rq_of(cfs_rq);
1727
1728 raw_spin_lock(&rq->lock);
1729 if (!cfs_rq_throttled(cfs_rq))
1730 goto next;
1731
1732 runtime = -cfs_rq->runtime_remaining + 1;
1733 if (runtime > remaining)
1734 runtime = remaining;
1735 remaining -= runtime;
1736
1737 cfs_rq->runtime_remaining += runtime;
1738 cfs_rq->runtime_expires = expires;
1739
1740 /* we check whether we're throttled above */
1741 if (cfs_rq->runtime_remaining > 0)
1742 unthrottle_cfs_rq(cfs_rq);
1743
1744next:
1745 raw_spin_unlock(&rq->lock);
1746
1747 if (!remaining)
1748 break;
1749 }
1750 rcu_read_unlock();
1751
1752 return remaining;
1753}
1754
Paul Turner58088ad2011-07-21 09:43:31 -07001755/*
1756 * Responsible for refilling a task_group's bandwidth and unthrottling its
1757 * cfs_rqs as appropriate. If there has been no activity within the last
1758 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1759 * used to track this state.
1760 */
1761static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1762{
Paul Turner671fd9d2011-07-21 09:43:34 -07001763 u64 runtime, runtime_expires;
1764 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001765
1766 raw_spin_lock(&cfs_b->lock);
1767 /* no need to continue the timer with no bandwidth constraint */
1768 if (cfs_b->quota == RUNTIME_INF)
1769 goto out_unlock;
1770
Paul Turner671fd9d2011-07-21 09:43:34 -07001771 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1772 /* idle depends on !throttled (for the case of a large deficit) */
1773 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001774 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001775
Paul Turnera9cf55b2011-07-21 09:43:32 -07001776 /* if we're going inactive then everything else can be deferred */
1777 if (idle)
1778 goto out_unlock;
1779
1780 __refill_cfs_bandwidth_runtime(cfs_b);
1781
Paul Turner671fd9d2011-07-21 09:43:34 -07001782 if (!throttled) {
1783 /* mark as potentially idle for the upcoming period */
1784 cfs_b->idle = 1;
1785 goto out_unlock;
1786 }
Paul Turner58088ad2011-07-21 09:43:31 -07001787
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001788 /* account preceding periods in which throttling occurred */
1789 cfs_b->nr_throttled += overrun;
1790
Paul Turner671fd9d2011-07-21 09:43:34 -07001791 /*
1792 * There are throttled entities so we must first use the new bandwidth
1793 * to unthrottle them before making it generally available. This
1794 * ensures that all existing debts will be paid before a new cfs_rq is
1795 * allowed to run.
1796 */
1797 runtime = cfs_b->runtime;
1798 runtime_expires = cfs_b->runtime_expires;
1799 cfs_b->runtime = 0;
1800
1801 /*
1802 * This check is repeated as we are holding onto the new bandwidth
1803 * while we unthrottle. This can potentially race with an unthrottled
1804 * group trying to acquire new bandwidth from the global pool.
1805 */
1806 while (throttled && runtime > 0) {
1807 raw_spin_unlock(&cfs_b->lock);
1808 /* we can't nest cfs_b->lock while distributing bandwidth */
1809 runtime = distribute_cfs_runtime(cfs_b, runtime,
1810 runtime_expires);
1811 raw_spin_lock(&cfs_b->lock);
1812
1813 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1814 }
1815
1816 /* return (any) remaining runtime */
1817 cfs_b->runtime = runtime;
1818 /*
1819 * While we are ensured activity in the period following an
1820 * unthrottle, this also covers the case in which the new bandwidth is
1821 * insufficient to cover the existing bandwidth deficit. (Forcing the
1822 * timer to remain active while there are any throttled entities.)
1823 */
1824 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07001825out_unlock:
1826 if (idle)
1827 cfs_b->timer_active = 0;
1828 raw_spin_unlock(&cfs_b->lock);
1829
1830 return idle;
1831}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001832
Paul Turnerd8b49862011-07-21 09:43:41 -07001833/* a cfs_rq won't donate quota below this amount */
1834static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
1835/* minimum remaining period time to redistribute slack quota */
1836static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
1837/* how long we wait to gather additional slack before distributing */
1838static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
1839
1840/* are we near the end of the current quota period? */
1841static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
1842{
1843 struct hrtimer *refresh_timer = &cfs_b->period_timer;
1844 u64 remaining;
1845
1846 /* if the call-back is running a quota refresh is already occurring */
1847 if (hrtimer_callback_running(refresh_timer))
1848 return 1;
1849
1850 /* is a quota refresh about to occur? */
1851 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
1852 if (remaining < min_expire)
1853 return 1;
1854
1855 return 0;
1856}
1857
1858static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
1859{
1860 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
1861
1862 /* if there's a quota refresh soon don't bother with slack */
1863 if (runtime_refresh_within(cfs_b, min_left))
1864 return;
1865
1866 start_bandwidth_timer(&cfs_b->slack_timer,
1867 ns_to_ktime(cfs_bandwidth_slack_period));
1868}
1869
1870/* we know any runtime found here is valid as update_curr() precedes return */
1871static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1872{
1873 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1874 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
1875
1876 if (slack_runtime <= 0)
1877 return;
1878
1879 raw_spin_lock(&cfs_b->lock);
1880 if (cfs_b->quota != RUNTIME_INF &&
1881 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
1882 cfs_b->runtime += slack_runtime;
1883
1884 /* we are under rq->lock, defer unthrottling using a timer */
1885 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
1886 !list_empty(&cfs_b->throttled_cfs_rq))
1887 start_cfs_slack_bandwidth(cfs_b);
1888 }
1889 raw_spin_unlock(&cfs_b->lock);
1890
1891 /* even if it's not valid for return we don't want to try again */
1892 cfs_rq->runtime_remaining -= slack_runtime;
1893}
1894
1895static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1896{
Paul Turner56f570e2011-11-07 20:26:33 -08001897 if (!cfs_bandwidth_used())
1898 return;
1899
Paul Turnerfccfdc62011-11-07 20:26:34 -08001900 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07001901 return;
1902
1903 __return_cfs_rq_runtime(cfs_rq);
1904}
1905
1906/*
1907 * This is done with a timer (instead of inline with bandwidth return) since
1908 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
1909 */
1910static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
1911{
1912 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
1913 u64 expires;
1914
1915 /* confirm we're still not at a refresh boundary */
1916 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
1917 return;
1918
1919 raw_spin_lock(&cfs_b->lock);
1920 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
1921 runtime = cfs_b->runtime;
1922 cfs_b->runtime = 0;
1923 }
1924 expires = cfs_b->runtime_expires;
1925 raw_spin_unlock(&cfs_b->lock);
1926
1927 if (!runtime)
1928 return;
1929
1930 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
1931
1932 raw_spin_lock(&cfs_b->lock);
1933 if (expires == cfs_b->runtime_expires)
1934 cfs_b->runtime = runtime;
1935 raw_spin_unlock(&cfs_b->lock);
1936}
1937
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001938/*
1939 * When a group wakes up we want to make sure that its quota is not already
1940 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
1941 * runtime as update_curr() throttling can not not trigger until it's on-rq.
1942 */
1943static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
1944{
Paul Turner56f570e2011-11-07 20:26:33 -08001945 if (!cfs_bandwidth_used())
1946 return;
1947
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001948 /* an active group must be handled by the update_curr()->put() path */
1949 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
1950 return;
1951
1952 /* ensure the group is not already throttled */
1953 if (cfs_rq_throttled(cfs_rq))
1954 return;
1955
1956 /* update runtime allocation */
1957 account_cfs_rq_runtime(cfs_rq, 0);
1958 if (cfs_rq->runtime_remaining <= 0)
1959 throttle_cfs_rq(cfs_rq);
1960}
1961
1962/* conditionally throttle active cfs_rq's from put_prev_entity() */
1963static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1964{
Paul Turner56f570e2011-11-07 20:26:33 -08001965 if (!cfs_bandwidth_used())
1966 return;
1967
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001968 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
1969 return;
1970
1971 /*
1972 * it's possible for a throttled entity to be forced into a running
1973 * state (e.g. set_curr_task), in this case we're finished.
1974 */
1975 if (cfs_rq_throttled(cfs_rq))
1976 return;
1977
1978 throttle_cfs_rq(cfs_rq);
1979}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001980
1981static inline u64 default_cfs_period(void);
1982static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
1983static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
1984
1985static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
1986{
1987 struct cfs_bandwidth *cfs_b =
1988 container_of(timer, struct cfs_bandwidth, slack_timer);
1989 do_sched_cfs_slack_timer(cfs_b);
1990
1991 return HRTIMER_NORESTART;
1992}
1993
1994static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
1995{
1996 struct cfs_bandwidth *cfs_b =
1997 container_of(timer, struct cfs_bandwidth, period_timer);
1998 ktime_t now;
1999 int overrun;
2000 int idle = 0;
2001
2002 for (;;) {
2003 now = hrtimer_cb_get_time(timer);
2004 overrun = hrtimer_forward(timer, now, cfs_b->period);
2005
2006 if (!overrun)
2007 break;
2008
2009 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2010 }
2011
2012 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2013}
2014
2015void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2016{
2017 raw_spin_lock_init(&cfs_b->lock);
2018 cfs_b->runtime = 0;
2019 cfs_b->quota = RUNTIME_INF;
2020 cfs_b->period = ns_to_ktime(default_cfs_period());
2021
2022 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2023 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2024 cfs_b->period_timer.function = sched_cfs_period_timer;
2025 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2026 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2027}
2028
2029static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2030{
2031 cfs_rq->runtime_enabled = 0;
2032 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2033}
2034
2035/* requires cfs_b->lock, may release to reprogram timer */
2036void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2037{
2038 /*
2039 * The timer may be active because we're trying to set a new bandwidth
2040 * period or because we're racing with the tear-down path
2041 * (timer_active==0 becomes visible before the hrtimer call-back
2042 * terminates). In either case we ensure that it's re-programmed
2043 */
2044 while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
2045 raw_spin_unlock(&cfs_b->lock);
2046 /* ensure cfs_b->lock is available while we wait */
2047 hrtimer_cancel(&cfs_b->period_timer);
2048
2049 raw_spin_lock(&cfs_b->lock);
2050 /* if someone else restarted the timer then we're done */
2051 if (cfs_b->timer_active)
2052 return;
2053 }
2054
2055 cfs_b->timer_active = 1;
2056 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2057}
2058
2059static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2060{
2061 hrtimer_cancel(&cfs_b->period_timer);
2062 hrtimer_cancel(&cfs_b->slack_timer);
2063}
2064
2065void unthrottle_offline_cfs_rqs(struct rq *rq)
2066{
2067 struct cfs_rq *cfs_rq;
2068
2069 for_each_leaf_cfs_rq(rq, cfs_rq) {
2070 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2071
2072 if (!cfs_rq->runtime_enabled)
2073 continue;
2074
2075 /*
2076 * clock_task is not advancing so we just need to make sure
2077 * there's some valid quota amount
2078 */
2079 cfs_rq->runtime_remaining = cfs_b->quota;
2080 if (cfs_rq_throttled(cfs_rq))
2081 unthrottle_cfs_rq(cfs_rq);
2082 }
2083}
2084
2085#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002086static __always_inline
2087void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002088static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2089static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002090static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002091
2092static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2093{
2094 return 0;
2095}
Paul Turner64660c82011-07-21 09:43:36 -07002096
2097static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2098{
2099 return 0;
2100}
2101
2102static inline int throttled_lb_pair(struct task_group *tg,
2103 int src_cpu, int dest_cpu)
2104{
2105 return 0;
2106}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002107
2108void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2109
2110#ifdef CONFIG_FAIR_GROUP_SCHED
2111static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002112#endif
2113
Peter Zijlstra029632f2011-10-25 10:00:11 +02002114static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2115{
2116 return NULL;
2117}
2118static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2119void unthrottle_offline_cfs_rqs(struct rq *rq) {}
2120
2121#endif /* CONFIG_CFS_BANDWIDTH */
2122
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002123/**************************************************
2124 * CFS operations on tasks:
2125 */
2126
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002127#ifdef CONFIG_SCHED_HRTICK
2128static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2129{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002130 struct sched_entity *se = &p->se;
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002131 struct cfs_rq *cfs_rq = &rq->cfs;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002132
2133 WARN_ON(task_rq(p) != rq);
2134
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002135 if (cfs_rq->h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002136 u64 slice = sched_slice(cfs_rq, se);
2137 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2138 s64 delta = slice - ran;
2139
2140 if (delta < 0) {
2141 if (rq->curr == p)
2142 resched_task(p);
2143 return;
2144 }
2145
2146 /*
2147 * Don't schedule slices shorter than 10000ns, that just
2148 * doesn't make sense. Rely on vruntime for fairness.
2149 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002150 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002151 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002152
Peter Zijlstra31656512008-07-18 18:01:23 +02002153 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002154 }
2155}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002156
2157/*
2158 * called from enqueue/dequeue and updates the hrtick when the
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002159 * current task is from our class.
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002160 */
2161static void hrtick_update(struct rq *rq)
2162{
2163 struct task_struct *curr = rq->curr;
2164
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002165 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002166 return;
2167
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002168 hrtick_start_fair(rq, curr);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002169}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302170#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002171static inline void
2172hrtick_start_fair(struct rq *rq, struct task_struct *p)
2173{
2174}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002175
2176static inline void hrtick_update(struct rq *rq)
2177{
2178}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002179#endif
2180
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002181/*
2182 * The enqueue_task method is called before nr_running is
2183 * increased. Here we update the fair scheduling stats and
2184 * then put the task into the rbtree:
2185 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002186static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002187enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002188{
2189 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002190 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002191
2192 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002193 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002194 break;
2195 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002196 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002197
2198 /*
2199 * end evaluation on encountering a throttled cfs_rq
2200 *
2201 * note: in the case of encountering a throttled cfs_rq we will
2202 * post the final h_nr_running increment below.
2203 */
2204 if (cfs_rq_throttled(cfs_rq))
2205 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002206 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002207
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002208 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002209 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002210
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002211 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002212 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002213 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002214
Paul Turner85dac902011-07-21 09:43:33 -07002215 if (cfs_rq_throttled(cfs_rq))
2216 break;
2217
Paul Turnerd6b55912010-11-15 15:47:09 -08002218 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002219 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002220 }
2221
Paul Turner85dac902011-07-21 09:43:33 -07002222 if (!se)
2223 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002224 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002225}
2226
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002227static void set_next_buddy(struct sched_entity *se);
2228
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002229/*
2230 * The dequeue_task method is called before nr_running is
2231 * decreased. We remove the task from the rbtree and
2232 * update the fair scheduling stats:
2233 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002234static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002235{
2236 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002237 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002238 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002239
2240 for_each_sched_entity(se) {
2241 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002242 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002243
2244 /*
2245 * end evaluation on encountering a throttled cfs_rq
2246 *
2247 * note: in the case of encountering a throttled cfs_rq we will
2248 * post the final h_nr_running decrement below.
2249 */
2250 if (cfs_rq_throttled(cfs_rq))
2251 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002252 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002253
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002254 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002255 if (cfs_rq->load.weight) {
2256 /*
2257 * Bias pick_next to pick a task from this cfs_rq, as
2258 * p is sleeping when it is within its sched_slice.
2259 */
2260 if (task_sleep && parent_entity(se))
2261 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002262
2263 /* avoid re-evaluating load for this entity */
2264 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002265 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002266 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002267 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002268 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002269
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002270 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002271 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002272 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002273
Paul Turner85dac902011-07-21 09:43:33 -07002274 if (cfs_rq_throttled(cfs_rq))
2275 break;
2276
Paul Turnerd6b55912010-11-15 15:47:09 -08002277 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002278 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002279 }
2280
Paul Turner85dac902011-07-21 09:43:33 -07002281 if (!se)
2282 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002283 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002284}
2285
Gregory Haskinse7693a32008-01-25 21:08:09 +01002286#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002287/* Used instead of source_load when we know the type == 0 */
2288static unsigned long weighted_cpuload(const int cpu)
2289{
2290 return cpu_rq(cpu)->load.weight;
2291}
2292
2293/*
2294 * Return a low guess at the load of a migration-source cpu weighted
2295 * according to the scheduling class and "nice" value.
2296 *
2297 * We want to under-estimate the load of migration sources, to
2298 * balance conservatively.
2299 */
2300static unsigned long source_load(int cpu, int type)
2301{
2302 struct rq *rq = cpu_rq(cpu);
2303 unsigned long total = weighted_cpuload(cpu);
2304
2305 if (type == 0 || !sched_feat(LB_BIAS))
2306 return total;
2307
2308 return min(rq->cpu_load[type-1], total);
2309}
2310
2311/*
2312 * Return a high guess at the load of a migration-target cpu weighted
2313 * according to the scheduling class and "nice" value.
2314 */
2315static unsigned long target_load(int cpu, int type)
2316{
2317 struct rq *rq = cpu_rq(cpu);
2318 unsigned long total = weighted_cpuload(cpu);
2319
2320 if (type == 0 || !sched_feat(LB_BIAS))
2321 return total;
2322
2323 return max(rq->cpu_load[type-1], total);
2324}
2325
2326static unsigned long power_of(int cpu)
2327{
2328 return cpu_rq(cpu)->cpu_power;
2329}
2330
2331static unsigned long cpu_avg_load_per_task(int cpu)
2332{
2333 struct rq *rq = cpu_rq(cpu);
2334 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2335
2336 if (nr_running)
2337 return rq->load.weight / nr_running;
2338
2339 return 0;
2340}
2341
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002342
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002343static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002344{
2345 struct sched_entity *se = &p->se;
2346 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002347 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002348
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002349#ifndef CONFIG_64BIT
2350 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002351
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002352 do {
2353 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2354 smp_rmb();
2355 min_vruntime = cfs_rq->min_vruntime;
2356 } while (min_vruntime != min_vruntime_copy);
2357#else
2358 min_vruntime = cfs_rq->min_vruntime;
2359#endif
2360
2361 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002362}
2363
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002364#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002365/*
2366 * effective_load() calculates the load change as seen from the root_task_group
2367 *
2368 * Adding load to a group doesn't make a group heavier, but can cause movement
2369 * of group shares between cpus. Assuming the shares were perfectly aligned one
2370 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002371 *
2372 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2373 * on this @cpu and results in a total addition (subtraction) of @wg to the
2374 * total group weight.
2375 *
2376 * Given a runqueue weight distribution (rw_i) we can compute a shares
2377 * distribution (s_i) using:
2378 *
2379 * s_i = rw_i / \Sum rw_j (1)
2380 *
2381 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2382 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2383 * shares distribution (s_i):
2384 *
2385 * rw_i = { 2, 4, 1, 0 }
2386 * s_i = { 2/7, 4/7, 1/7, 0 }
2387 *
2388 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2389 * task used to run on and the CPU the waker is running on), we need to
2390 * compute the effect of waking a task on either CPU and, in case of a sync
2391 * wakeup, compute the effect of the current task going to sleep.
2392 *
2393 * So for a change of @wl to the local @cpu with an overall group weight change
2394 * of @wl we can compute the new shares distribution (s'_i) using:
2395 *
2396 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2397 *
2398 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2399 * differences in waking a task to CPU 0. The additional task changes the
2400 * weight and shares distributions like:
2401 *
2402 * rw'_i = { 3, 4, 1, 0 }
2403 * s'_i = { 3/8, 4/8, 1/8, 0 }
2404 *
2405 * We can then compute the difference in effective weight by using:
2406 *
2407 * dw_i = S * (s'_i - s_i) (3)
2408 *
2409 * Where 'S' is the group weight as seen by its parent.
2410 *
2411 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2412 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2413 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002414 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002415static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002416{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002417 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002418
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002419 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002420 return wl;
2421
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002422 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002423 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002424
Paul Turner977dda72011-01-14 17:57:50 -08002425 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002426
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002427 /*
2428 * W = @wg + \Sum rw_j
2429 */
2430 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002431
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002432 /*
2433 * w = rw_i + @wl
2434 */
2435 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002436
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002437 /*
2438 * wl = S * s'_i; see (2)
2439 */
2440 if (W > 0 && w < W)
2441 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002442 else
2443 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002444
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002445 /*
2446 * Per the above, wl is the new se->load.weight value; since
2447 * those are clipped to [MIN_SHARES, ...) do so now. See
2448 * calc_cfs_shares().
2449 */
Paul Turner977dda72011-01-14 17:57:50 -08002450 if (wl < MIN_SHARES)
2451 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002452
2453 /*
2454 * wl = dw_i = S * (s'_i - s_i); see (3)
2455 */
Paul Turner977dda72011-01-14 17:57:50 -08002456 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002457
2458 /*
2459 * Recursively apply this logic to all parent groups to compute
2460 * the final effective load change on the root group. Since
2461 * only the @tg group gets extra weight, all parent groups can
2462 * only redistribute existing shares. @wl is the shift in shares
2463 * resulting from this level per the above.
2464 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002465 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002466 }
2467
2468 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002469}
2470#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002471
Peter Zijlstra83378262008-06-27 13:41:37 +02002472static inline unsigned long effective_load(struct task_group *tg, int cpu,
2473 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002474{
Peter Zijlstra83378262008-06-27 13:41:37 +02002475 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002476}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002477
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002478#endif
2479
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002480static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002481{
Paul Turnere37b6a72011-01-21 20:44:59 -08002482 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002483 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002484 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002485 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002486 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002487 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002488
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002489 idx = sd->wake_idx;
2490 this_cpu = smp_processor_id();
2491 prev_cpu = task_cpu(p);
2492 load = source_load(prev_cpu, idx);
2493 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002494
2495 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002496 * If sync wakeup then subtract the (maximum possible)
2497 * effect of the currently running task from the load
2498 * of the current CPU:
2499 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002500 if (sync) {
2501 tg = task_group(current);
2502 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002503
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002504 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002505 load += effective_load(tg, prev_cpu, 0, -weight);
2506 }
2507
2508 tg = task_group(p);
2509 weight = p->se.load.weight;
2510
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002511 /*
2512 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002513 * due to the sync cause above having dropped this_load to 0, we'll
2514 * always have an imbalance, but there's really nothing you can do
2515 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002516 *
2517 * Otherwise check if either cpus are near enough in load to allow this
2518 * task to be woken on this_cpu.
2519 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002520 if (this_load > 0) {
2521 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002522
2523 this_eff_load = 100;
2524 this_eff_load *= power_of(prev_cpu);
2525 this_eff_load *= this_load +
2526 effective_load(tg, this_cpu, weight, weight);
2527
2528 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2529 prev_eff_load *= power_of(this_cpu);
2530 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2531
2532 balanced = this_eff_load <= prev_eff_load;
2533 } else
2534 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002535
2536 /*
2537 * If the currently running task will sleep within
2538 * a reasonable amount of time then attract this newly
2539 * woken task:
2540 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002541 if (sync && balanced)
2542 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002543
Lucas De Marchi41acab82010-03-10 23:37:45 -03002544 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002545 tl_per_task = cpu_avg_load_per_task(this_cpu);
2546
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002547 if (balanced ||
2548 (this_load <= load &&
2549 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002550 /*
2551 * This domain has SD_WAKE_AFFINE and
2552 * p is cache cold in this domain, and
2553 * there is no bad imbalance.
2554 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002555 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002556 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002557
2558 return 1;
2559 }
2560 return 0;
2561}
2562
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002563/*
2564 * find_idlest_group finds and returns the least busy CPU group within the
2565 * domain.
2566 */
2567static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002568find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002569 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002570{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002571 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002572 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002573 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002574
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002575 do {
2576 unsigned long load, avg_load;
2577 int local_group;
2578 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002579
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002580 /* Skip over this group if it has no CPUs allowed */
2581 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002582 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002583 continue;
2584
2585 local_group = cpumask_test_cpu(this_cpu,
2586 sched_group_cpus(group));
2587
2588 /* Tally up the load of all CPUs in the group */
2589 avg_load = 0;
2590
2591 for_each_cpu(i, sched_group_cpus(group)) {
2592 /* Bias balancing toward cpus of our domain */
2593 if (local_group)
2594 load = source_load(i, load_idx);
2595 else
2596 load = target_load(i, load_idx);
2597
2598 avg_load += load;
2599 }
2600
2601 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002602 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002603
2604 if (local_group) {
2605 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002606 } else if (avg_load < min_load) {
2607 min_load = avg_load;
2608 idlest = group;
2609 }
2610 } while (group = group->next, group != sd->groups);
2611
2612 if (!idlest || 100*this_load < imbalance*min_load)
2613 return NULL;
2614 return idlest;
2615}
2616
2617/*
2618 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2619 */
2620static int
2621find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2622{
2623 unsigned long load, min_load = ULONG_MAX;
2624 int idlest = -1;
2625 int i;
2626
2627 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002628 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002629 load = weighted_cpuload(i);
2630
2631 if (load < min_load || (load == min_load && i == this_cpu)) {
2632 min_load = load;
2633 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002634 }
2635 }
2636
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002637 return idlest;
2638}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002639
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002640/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002641 * Try and locate an idle CPU in the sched_domain.
2642 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002643static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002644{
2645 int cpu = smp_processor_id();
2646 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002647 struct sched_domain *sd;
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002648 struct sched_group *sg;
Suresh Siddha77e81362011-11-17 11:08:23 -08002649 int i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002650
2651 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002652 * If the task is going to be woken-up on this cpu and if it is
2653 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002654 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002655 if (target == cpu && idle_cpu(cpu))
2656 return cpu;
2657
2658 /*
2659 * If the task is going to be woken-up on the cpu where it previously
2660 * ran and if it is currently idle, then it the right target.
2661 */
2662 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002663 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002664
Steve Muckled448aba2012-10-24 15:00:20 -07002665 if (!sysctl_sched_wake_to_idle &&
2666 !(current->flags & PF_WAKE_UP_IDLE) &&
Steve Muckleff96cd22012-09-10 11:45:02 -07002667 !(p->flags & PF_WAKE_UP_IDLE))
2668 return target;
2669
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002670 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002671 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002672 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002673 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002674 for_each_lower_domain(sd) {
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002675 sg = sd->groups;
2676 do {
2677 if (!cpumask_intersects(sched_group_cpus(sg),
2678 tsk_cpus_allowed(p)))
2679 goto next;
2680
2681 for_each_cpu(i, sched_group_cpus(sg)) {
2682 if (!idle_cpu(i))
2683 goto next;
2684 }
2685
2686 target = cpumask_first_and(sched_group_cpus(sg),
2687 tsk_cpus_allowed(p));
2688 goto done;
2689next:
2690 sg = sg->next;
2691 } while (sg != sd->groups);
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002692 }
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002693done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002694 return target;
2695}
2696
2697/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002698 * sched_balance_self: balance the current task (running on cpu) in domains
2699 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2700 * SD_BALANCE_EXEC.
2701 *
2702 * Balance, ie. select the least loaded group.
2703 *
2704 * Returns the target CPU number, or the same CPU if no balancing is needed.
2705 *
2706 * preempt must be disabled.
2707 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002708static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002709select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002710{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002711 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002712 int cpu = smp_processor_id();
2713 int prev_cpu = task_cpu(p);
2714 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002715 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002716 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002717 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002718
Mike Galbraith76854c72011-11-22 15:18:24 +01002719 if (p->rt.nr_cpus_allowed == 1)
2720 return prev_cpu;
2721
Peter Zijlstra0763a662009-09-14 19:37:39 +02002722 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002723 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002724 want_affine = 1;
2725 new_cpu = prev_cpu;
2726 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002727
Peter Zijlstradce840a2011-04-07 14:09:50 +02002728 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002729 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002730 if (!(tmp->flags & SD_LOAD_BALANCE))
2731 continue;
2732
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002733 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02002734 * If power savings logic is enabled for a domain, see if we
2735 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002736 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02002737 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02002738 unsigned long power = 0;
2739 unsigned long nr_running = 0;
2740 unsigned long capacity;
2741 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002742
Peter Zijlstraae154be2009-09-10 14:40:57 +02002743 for_each_cpu(i, sched_domain_span(tmp)) {
2744 power += power_of(i);
2745 nr_running += cpu_rq(i)->cfs.nr_running;
2746 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002747
Nikhil Rao1399fa72011-05-18 10:09:39 -07002748 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002749
Peter Zijlstra59abf022009-09-16 08:28:30 +02002750 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2751 nr_running /= 2;
2752
2753 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002754 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002755 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002756
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002757 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002758 * If both cpu and prev_cpu are part of this domain,
2759 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002760 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002761 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2762 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2763 affine_sd = tmp;
2764 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002765 }
2766
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002767 if (!want_sd && !want_affine)
2768 break;
2769
Peter Zijlstra0763a662009-09-14 19:37:39 +02002770 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002771 continue;
2772
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002773 if (want_sd)
2774 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002775 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002776
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002777 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002778 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002779 prev_cpu = cpu;
2780
2781 new_cpu = select_idle_sibling(p, prev_cpu);
2782 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002783 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002784
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002785 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002786 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002787 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002788 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002789
Peter Zijlstra0763a662009-09-14 19:37:39 +02002790 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002791 sd = sd->child;
2792 continue;
2793 }
2794
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002795 if (sd_flag & SD_BALANCE_WAKE)
2796 load_idx = sd->wake_idx;
2797
2798 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002799 if (!group) {
2800 sd = sd->child;
2801 continue;
2802 }
2803
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002804 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002805 if (new_cpu == -1 || new_cpu == cpu) {
2806 /* Now try balancing at a lower domain level of cpu */
2807 sd = sd->child;
2808 continue;
2809 }
2810
2811 /* Now try balancing at a lower domain level of new_cpu */
2812 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002813 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002814 sd = NULL;
2815 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002816 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002817 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002818 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002819 sd = tmp;
2820 }
2821 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002822 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002823unlock:
2824 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002825
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002826 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002827}
2828#endif /* CONFIG_SMP */
2829
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002830static unsigned long
2831wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002832{
2833 unsigned long gran = sysctl_sched_wakeup_granularity;
2834
2835 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002836 * Since its curr running now, convert the gran from real-time
2837 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002838 *
2839 * By using 'se' instead of 'curr' we penalize light tasks, so
2840 * they get preempted easier. That is, if 'se' < 'curr' then
2841 * the resulting gran will be larger, therefore penalizing the
2842 * lighter, if otoh 'se' > 'curr' then the resulting gran will
2843 * be smaller, again penalizing the lighter task.
2844 *
2845 * This is especially important for buddies when the leftmost
2846 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002847 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08002848 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002849}
2850
2851/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02002852 * Should 'se' preempt 'curr'.
2853 *
2854 * |s1
2855 * |s2
2856 * |s3
2857 * g
2858 * |<--->|c
2859 *
2860 * w(c, s1) = -1
2861 * w(c, s2) = 0
2862 * w(c, s3) = 1
2863 *
2864 */
2865static int
2866wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2867{
2868 s64 gran, vdiff = curr->vruntime - se->vruntime;
2869
2870 if (vdiff <= 0)
2871 return -1;
2872
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002873 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02002874 if (vdiff > gran)
2875 return 1;
2876
2877 return 0;
2878}
2879
Peter Zijlstra02479092008-11-04 21:25:10 +01002880static void set_last_buddy(struct sched_entity *se)
2881{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002882 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2883 return;
2884
2885 for_each_sched_entity(se)
2886 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002887}
2888
2889static void set_next_buddy(struct sched_entity *se)
2890{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002891 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2892 return;
2893
2894 for_each_sched_entity(se)
2895 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002896}
2897
Rik van Rielac53db52011-02-01 09:51:03 -05002898static void set_skip_buddy(struct sched_entity *se)
2899{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002900 for_each_sched_entity(se)
2901 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05002902}
2903
Peter Zijlstra464b7522008-10-24 11:06:15 +02002904/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002905 * Preempt the current task with a newly woken task if needed:
2906 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02002907static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002908{
2909 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02002910 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002911 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002912 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002913 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002914
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002915 if (unlikely(se == pse))
2916 return;
2917
Paul Turner5238cdd2011-07-21 09:43:37 -07002918 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01002919 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07002920 * unconditionally check_prempt_curr() after an enqueue (which may have
2921 * lead to a throttle). This both saves work and prevents false
2922 * next-buddy nomination below.
2923 */
2924 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
2925 return;
2926
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002927 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02002928 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002929 next_buddy_marked = 1;
2930 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02002931
Bharata B Raoaec0a512008-08-28 14:42:49 +05302932 /*
2933 * We can come here with TIF_NEED_RESCHED already set from new task
2934 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07002935 *
2936 * Note: this also catches the edge-case of curr being in a throttled
2937 * group (e.g. via set_curr_task), since update_curr() (in the
2938 * enqueue of curr) will have resulted in resched being set. This
2939 * prevents us from potentially nominating it as a false LAST_BUDDY
2940 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05302941 */
2942 if (test_tsk_need_resched(curr))
2943 return;
2944
Darren Harta2f5c9a2011-02-22 13:04:33 -08002945 /* Idle tasks are by definition preempted by non-idle tasks. */
2946 if (unlikely(curr->policy == SCHED_IDLE) &&
2947 likely(p->policy != SCHED_IDLE))
2948 goto preempt;
2949
Ingo Molnar91c234b2007-10-15 17:00:18 +02002950 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08002951 * Batch and idle tasks do not preempt non-idle tasks (their preemption
2952 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02002953 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01002954 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02002955 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002956
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002957 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07002958 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002959 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002960 if (wakeup_preempt_entity(se, pse) == 1) {
2961 /*
2962 * Bias pick_next to pick the sched entity that is
2963 * triggering this preemption.
2964 */
2965 if (!next_buddy_marked)
2966 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002967 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002968 }
Jupyung Leea65ac742009-11-17 18:51:40 +09002969
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002970 return;
2971
2972preempt:
2973 resched_task(curr);
2974 /*
2975 * Only set the backward buddy when the current task is still
2976 * on the rq. This can happen when a wakeup gets interleaved
2977 * with schedule on the ->pre_schedule() or idle_balance()
2978 * point, either of which can * drop the rq lock.
2979 *
2980 * Also, during early boot the idle thread is in the fair class,
2981 * for obvious reasons its a bad idea to schedule back to it.
2982 */
2983 if (unlikely(!se->on_rq || curr == rq->idle))
2984 return;
2985
2986 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
2987 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002988}
2989
Ingo Molnarfb8d4722007-08-09 11:16:48 +02002990static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002991{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002992 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002993 struct cfs_rq *cfs_rq = &rq->cfs;
2994 struct sched_entity *se;
2995
Tim Blechmann36ace272009-11-24 11:55:45 +01002996 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002997 return NULL;
2998
2999 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003000 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003001 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003002 cfs_rq = group_cfs_rq(se);
3003 } while (cfs_rq);
3004
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003005 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003006 if (hrtick_enabled(rq))
3007 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003008
3009 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003010}
3011
3012/*
3013 * Account for a descheduled task:
3014 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003015static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003016{
3017 struct sched_entity *se = &prev->se;
3018 struct cfs_rq *cfs_rq;
3019
3020 for_each_sched_entity(se) {
3021 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003022 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003023 }
3024}
3025
Rik van Rielac53db52011-02-01 09:51:03 -05003026/*
3027 * sched_yield() is very simple
3028 *
3029 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3030 */
3031static void yield_task_fair(struct rq *rq)
3032{
3033 struct task_struct *curr = rq->curr;
3034 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3035 struct sched_entity *se = &curr->se;
3036
3037 /*
3038 * Are we the only task in the tree?
3039 */
3040 if (unlikely(rq->nr_running == 1))
3041 return;
3042
3043 clear_buddies(cfs_rq, se);
3044
3045 if (curr->policy != SCHED_BATCH) {
3046 update_rq_clock(rq);
3047 /*
3048 * Update run-time statistics of the 'current'.
3049 */
3050 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003051 /*
3052 * Tell update_rq_clock() that we've just updated,
3053 * so we don't do microscopic update in schedule()
3054 * and double the fastpath cost.
3055 */
3056 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003057 }
3058
3059 set_skip_buddy(se);
3060}
3061
Mike Galbraithd95f4122011-02-01 09:50:51 -05003062static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3063{
3064 struct sched_entity *se = &p->se;
3065
Paul Turner5238cdd2011-07-21 09:43:37 -07003066 /* throttled hierarchies are not runnable */
3067 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003068 return false;
3069
3070 /* Tell the scheduler that we'd really like pse to run next. */
3071 set_next_buddy(se);
3072
Mike Galbraithd95f4122011-02-01 09:50:51 -05003073 yield_task_fair(rq);
3074
3075 return true;
3076}
3077
Peter Williams681f3e62007-10-24 18:23:51 +02003078#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003079/**************************************************
3080 * Fair scheduling class load-balancing methods:
3081 */
3082
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003083static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3084
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003085#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003086#define LBF_NEED_BREAK 0x02
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003087
3088struct lb_env {
3089 struct sched_domain *sd;
3090
3091 int src_cpu;
3092 struct rq *src_rq;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003093
3094 int dst_cpu;
3095 struct rq *dst_rq;
3096
3097 enum cpu_idle_type idle;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003098 long load_move;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003099 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003100
3101 unsigned int loop;
3102 unsigned int loop_break;
3103 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003104};
3105
Steve Muckle8f77c282013-03-11 16:33:42 -07003106static DEFINE_PER_CPU(bool, dbs_boost_needed);
3107
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003108/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003109 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003110 * Both runqueues must be locked.
3111 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003112static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003113{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003114 deactivate_task(env->src_rq, p, 0);
3115 set_task_cpu(p, env->dst_cpu);
3116 activate_task(env->dst_rq, p, 0);
3117 check_preempt_curr(env->dst_rq, p, 0);
Steve Muckle8f77c282013-03-11 16:33:42 -07003118 if (task_notify_on_migrate(p))
3119 per_cpu(dbs_boost_needed, env->dst_cpu) = true;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003120}
3121
3122/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003123 * Is this task likely cache-hot:
3124 */
3125static int
3126task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3127{
3128 s64 delta;
3129
3130 if (p->sched_class != &fair_sched_class)
3131 return 0;
3132
3133 if (unlikely(p->policy == SCHED_IDLE))
3134 return 0;
3135
3136 /*
3137 * Buddy candidates are cache hot:
3138 */
3139 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3140 (&p->se == cfs_rq_of(&p->se)->next ||
3141 &p->se == cfs_rq_of(&p->se)->last))
3142 return 1;
3143
3144 if (sysctl_sched_migration_cost == -1)
3145 return 1;
3146 if (sysctl_sched_migration_cost == 0)
3147 return 0;
3148
3149 delta = now - p->se.exec_start;
3150
3151 return delta < (s64)sysctl_sched_migration_cost;
3152}
3153
3154/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003155 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3156 */
3157static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003158int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003159{
3160 int tsk_cache_hot = 0;
3161 /*
3162 * We do not migrate tasks that are:
3163 * 1) running (obviously), or
3164 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3165 * 3) are cache-hot on their current CPU.
3166 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003167 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003168 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003169 return 0;
3170 }
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003171 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003172
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003173 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003174 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003175 return 0;
3176 }
3177
3178 /*
3179 * Aggressive migration if:
3180 * 1) task is cache cold, or
3181 * 2) too many balance attempts have failed.
3182 */
3183
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003184 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003185 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003186 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003187#ifdef CONFIG_SCHEDSTATS
3188 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003189 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003190 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003191 }
3192#endif
3193 return 1;
3194 }
3195
3196 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003197 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003198 return 0;
3199 }
3200 return 1;
3201}
3202
Peter Zijlstra897c3952009-12-17 17:45:42 +01003203/*
3204 * move_one_task tries to move exactly one task from busiest to this_rq, as
3205 * part of active balancing operations within "domain".
3206 * Returns 1 if successful and 0 otherwise.
3207 *
3208 * Called with both runqueues locked.
3209 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003210static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003211{
3212 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003213
Peter Zijlstra367456c2012-02-20 21:49:09 +01003214 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3215 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3216 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003217
Peter Zijlstra367456c2012-02-20 21:49:09 +01003218 if (!can_migrate_task(p, env))
3219 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003220
Peter Zijlstra367456c2012-02-20 21:49:09 +01003221 move_task(p, env);
3222 /*
3223 * Right now, this is only the second place move_task()
3224 * is called, so we can safely collect move_task()
3225 * stats here rather than inside move_task().
3226 */
3227 schedstat_inc(env->sd, lb_gained[env->idle]);
3228 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003229 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003230 return 0;
3231}
3232
Peter Zijlstra367456c2012-02-20 21:49:09 +01003233static unsigned long task_h_load(struct task_struct *p);
3234
Peter Zijlstraeb953082012-04-17 13:38:40 +02003235static const unsigned int sched_nr_migrate_break = 32;
3236
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003237/*
3238 * move_tasks tries to move up to load_move weighted load from busiest to
3239 * this_rq, as part of a balancing operation within domain "sd".
3240 * Returns 1 if successful and 0 otherwise.
3241 *
3242 * Called with both runqueues locked.
3243 */
3244static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003245{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003246 struct list_head *tasks = &env->src_rq->cfs_tasks;
3247 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003248 unsigned long load;
3249 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003250
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003251 if (env->load_move <= 0)
3252 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003253
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003254 while (!list_empty(tasks)) {
3255 p = list_first_entry(tasks, struct task_struct, se.group_node);
3256
Peter Zijlstra367456c2012-02-20 21:49:09 +01003257 env->loop++;
3258 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003259 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003260 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003261
3262 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003263 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003264 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003265 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003266 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003267 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003268
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003269 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003270 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003271
Peter Zijlstra367456c2012-02-20 21:49:09 +01003272 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003273
Peter Zijlstraeb953082012-04-17 13:38:40 +02003274 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003275 goto next;
3276
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003277 if ((load / 2) > env->load_move)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003278 goto next;
3279
3280 if (!can_migrate_task(p, env))
3281 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003282
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003283 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003284 pulled++;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003285 env->load_move -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003286
3287#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003288 /*
3289 * NEWIDLE balancing is a source of latency, so preemptible
3290 * kernels will stop after the first task is pulled to minimize
3291 * the critical section.
3292 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003293 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003294 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003295#endif
3296
Peter Zijlstraee00e662009-12-17 17:25:20 +01003297 /*
3298 * We only want to steal up to the prescribed amount of
3299 * weighted load.
3300 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003301 if (env->load_move <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003302 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003303
Peter Zijlstra367456c2012-02-20 21:49:09 +01003304 continue;
3305next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003306 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003307 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003308
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003309 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003310 * Right now, this is one of only two places move_task() is called,
3311 * so we can safely collect move_task() stats here rather than
3312 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003313 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003314 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003315
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003316 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003317}
3318
Peter Zijlstra230059de2009-12-17 17:47:12 +01003319#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003320/*
3321 * update tg->load_weight by folding this cpu's load_avg
3322 */
Paul Turner67e86252010-11-15 15:47:05 -08003323static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003324{
3325 struct cfs_rq *cfs_rq;
3326 unsigned long flags;
3327 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003328
3329 if (!tg->se[cpu])
3330 return 0;
3331
3332 rq = cpu_rq(cpu);
3333 cfs_rq = tg->cfs_rq[cpu];
3334
3335 raw_spin_lock_irqsave(&rq->lock, flags);
3336
3337 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003338 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003339
3340 /*
3341 * We need to update shares after updating tg->load_weight in
3342 * order to adjust the weight of groups with long running tasks.
3343 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003344 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003345
3346 raw_spin_unlock_irqrestore(&rq->lock, flags);
3347
3348 return 0;
3349}
3350
3351static void update_shares(int cpu)
3352{
3353 struct cfs_rq *cfs_rq;
3354 struct rq *rq = cpu_rq(cpu);
3355
3356 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003357 /*
3358 * Iterates the task_group tree in a bottom up fashion, see
3359 * list_add_leaf_cfs_rq() for details.
3360 */
Paul Turner64660c82011-07-21 09:43:36 -07003361 for_each_leaf_cfs_rq(rq, cfs_rq) {
3362 /* throttled entities do not contribute to load */
3363 if (throttled_hierarchy(cfs_rq))
3364 continue;
3365
Paul Turner67e86252010-11-15 15:47:05 -08003366 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003367 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003368 rcu_read_unlock();
3369}
3370
Peter Zijlstra9763b672011-07-13 13:09:25 +02003371/*
3372 * Compute the cpu's hierarchical load factor for each task group.
3373 * This needs to be done in a top-down fashion because the load of a child
3374 * group is a fraction of its parents load.
3375 */
3376static int tg_load_down(struct task_group *tg, void *data)
3377{
3378 unsigned long load;
3379 long cpu = (long)data;
3380
3381 if (!tg->parent) {
3382 load = cpu_rq(cpu)->load.weight;
3383 } else {
3384 load = tg->parent->cfs_rq[cpu]->h_load;
3385 load *= tg->se[cpu]->load.weight;
3386 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3387 }
3388
3389 tg->cfs_rq[cpu]->h_load = load;
3390
3391 return 0;
3392}
3393
3394static void update_h_load(long cpu)
3395{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003396 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003397 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003398 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003399}
3400
Peter Zijlstra367456c2012-02-20 21:49:09 +01003401static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003402{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003403 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3404 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003405
Peter Zijlstra367456c2012-02-20 21:49:09 +01003406 load = p->se.load.weight;
3407 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003408
Peter Zijlstra367456c2012-02-20 21:49:09 +01003409 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003410}
3411#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003412static inline void update_shares(int cpu)
3413{
3414}
3415
Peter Zijlstra367456c2012-02-20 21:49:09 +01003416static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003417{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003418}
3419
3420static unsigned long task_h_load(struct task_struct *p)
3421{
3422 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003423}
3424#endif
3425
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003426/********** Helpers for find_busiest_group ************************/
3427/*
3428 * sd_lb_stats - Structure to store the statistics of a sched_domain
3429 * during load balancing.
3430 */
3431struct sd_lb_stats {
3432 struct sched_group *busiest; /* Busiest group in this sd */
3433 struct sched_group *this; /* Local group in this sd */
3434 unsigned long total_load; /* Total load of all groups in sd */
3435 unsigned long total_pwr; /* Total power of all groups in sd */
3436 unsigned long avg_load; /* Average load across all groups in sd */
3437
3438 /** Statistics of this group */
3439 unsigned long this_load;
3440 unsigned long this_load_per_task;
3441 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003442 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003443 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003444
3445 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003446 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003447 unsigned long max_load;
3448 unsigned long busiest_load_per_task;
3449 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003450 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003451 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003452 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003453
3454 int group_imb; /* Is there imbalance in this sd */
3455#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3456 int power_savings_balance; /* Is powersave balance needed for this sd */
3457 struct sched_group *group_min; /* Least loaded group in sd */
3458 struct sched_group *group_leader; /* Group which relieves group_min */
3459 unsigned long min_load_per_task; /* load_per_task in group_min */
3460 unsigned long leader_nr_running; /* Nr running of group_leader */
3461 unsigned long min_nr_running; /* Nr running of group_min */
3462#endif
3463};
3464
3465/*
3466 * sg_lb_stats - stats of a sched_group required for load_balancing
3467 */
3468struct sg_lb_stats {
3469 unsigned long avg_load; /*Avg load across the CPUs of the group */
3470 unsigned long group_load; /* Total load over the CPUs of the group */
3471 unsigned long sum_nr_running; /* Nr tasks running in the group */
3472 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3473 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003474 unsigned long idle_cpus;
3475 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003476 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003477 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003478};
3479
3480/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003481 * get_sd_load_idx - Obtain the load index for a given sched domain.
3482 * @sd: The sched_domain whose load_idx is to be obtained.
3483 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3484 */
3485static inline int get_sd_load_idx(struct sched_domain *sd,
3486 enum cpu_idle_type idle)
3487{
3488 int load_idx;
3489
3490 switch (idle) {
3491 case CPU_NOT_IDLE:
3492 load_idx = sd->busy_idx;
3493 break;
3494
3495 case CPU_NEWLY_IDLE:
3496 load_idx = sd->newidle_idx;
3497 break;
3498 default:
3499 load_idx = sd->idle_idx;
3500 break;
3501 }
3502
3503 return load_idx;
3504}
3505
3506
3507#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3508/**
3509 * init_sd_power_savings_stats - Initialize power savings statistics for
3510 * the given sched_domain, during load balancing.
3511 *
3512 * @sd: Sched domain whose power-savings statistics are to be initialized.
3513 * @sds: Variable containing the statistics for sd.
3514 * @idle: Idle status of the CPU at which we're performing load-balancing.
3515 */
3516static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3517 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3518{
3519 /*
3520 * Busy processors will not participate in power savings
3521 * balance.
3522 */
3523 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3524 sds->power_savings_balance = 0;
3525 else {
3526 sds->power_savings_balance = 1;
3527 sds->min_nr_running = ULONG_MAX;
3528 sds->leader_nr_running = 0;
3529 }
3530}
3531
3532/**
3533 * update_sd_power_savings_stats - Update the power saving stats for a
3534 * sched_domain while performing load balancing.
3535 *
3536 * @group: sched_group belonging to the sched_domain under consideration.
3537 * @sds: Variable containing the statistics of the sched_domain
3538 * @local_group: Does group contain the CPU for which we're performing
3539 * load balancing ?
3540 * @sgs: Variable containing the statistics of the group.
3541 */
3542static inline void update_sd_power_savings_stats(struct sched_group *group,
3543 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3544{
3545
3546 if (!sds->power_savings_balance)
3547 return;
3548
3549 /*
3550 * If the local group is idle or completely loaded
3551 * no need to do power savings balance at this domain
3552 */
3553 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3554 !sds->this_nr_running))
3555 sds->power_savings_balance = 0;
3556
3557 /*
3558 * If a group is already running at full capacity or idle,
3559 * don't include that group in power savings calculations
3560 */
3561 if (!sds->power_savings_balance ||
3562 sgs->sum_nr_running >= sgs->group_capacity ||
3563 !sgs->sum_nr_running)
3564 return;
3565
3566 /*
3567 * Calculate the group which has the least non-idle load.
3568 * This is the group from where we need to pick up the load
3569 * for saving power
3570 */
3571 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3572 (sgs->sum_nr_running == sds->min_nr_running &&
3573 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3574 sds->group_min = group;
3575 sds->min_nr_running = sgs->sum_nr_running;
3576 sds->min_load_per_task = sgs->sum_weighted_load /
3577 sgs->sum_nr_running;
3578 }
3579
3580 /*
3581 * Calculate the group which is almost near its
3582 * capacity but still has some space to pick up some load
3583 * from other group and save more power
3584 */
3585 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3586 return;
3587
3588 if (sgs->sum_nr_running > sds->leader_nr_running ||
3589 (sgs->sum_nr_running == sds->leader_nr_running &&
3590 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3591 sds->group_leader = group;
3592 sds->leader_nr_running = sgs->sum_nr_running;
3593 }
3594}
3595
3596/**
3597 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3598 * @sds: Variable containing the statistics of the sched_domain
3599 * under consideration.
3600 * @this_cpu: Cpu at which we're currently performing load-balancing.
3601 * @imbalance: Variable to store the imbalance.
3602 *
3603 * Description:
3604 * Check if we have potential to perform some power-savings balance.
3605 * If yes, set the busiest group to be the least loaded group in the
3606 * sched_domain, so that it's CPUs can be put to idle.
3607 *
3608 * Returns 1 if there is potential to perform power-savings balance.
3609 * Else returns 0.
3610 */
3611static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3612 int this_cpu, unsigned long *imbalance)
3613{
3614 if (!sds->power_savings_balance)
3615 return 0;
3616
3617 if (sds->this != sds->group_leader ||
3618 sds->group_leader == sds->group_min)
3619 return 0;
3620
3621 *imbalance = sds->min_load_per_task;
3622 sds->busiest = sds->group_min;
3623
3624 return 1;
3625
3626}
3627#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3628static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3629 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3630{
3631 return;
3632}
3633
3634static inline void update_sd_power_savings_stats(struct sched_group *group,
3635 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3636{
3637 return;
3638}
3639
3640static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3641 int this_cpu, unsigned long *imbalance)
3642{
3643 return 0;
3644}
3645#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3646
3647
3648unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3649{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003650 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003651}
3652
3653unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3654{
3655 return default_scale_freq_power(sd, cpu);
3656}
3657
3658unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3659{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003660 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003661 unsigned long smt_gain = sd->smt_gain;
3662
3663 smt_gain /= weight;
3664
3665 return smt_gain;
3666}
3667
3668unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3669{
3670 return default_scale_smt_power(sd, cpu);
3671}
3672
3673unsigned long scale_rt_power(int cpu)
3674{
3675 struct rq *rq = cpu_rq(cpu);
3676 u64 total, available;
3677
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003678 total = sched_avg_period() + (rq->clock - rq->age_stamp);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003679
3680 if (unlikely(total < rq->rt_avg)) {
3681 /* Ensures that power won't end up being negative */
3682 available = 0;
3683 } else {
3684 available = total - rq->rt_avg;
3685 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003686
Nikhil Rao1399fa72011-05-18 10:09:39 -07003687 if (unlikely((s64)total < SCHED_POWER_SCALE))
3688 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003689
Nikhil Rao1399fa72011-05-18 10:09:39 -07003690 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003691
3692 return div_u64(available, total);
3693}
3694
3695static void update_cpu_power(struct sched_domain *sd, int cpu)
3696{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003697 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003698 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003699 struct sched_group *sdg = sd->groups;
3700
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003701 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3702 if (sched_feat(ARCH_POWER))
3703 power *= arch_scale_smt_power(sd, cpu);
3704 else
3705 power *= default_scale_smt_power(sd, cpu);
3706
Nikhil Rao1399fa72011-05-18 10:09:39 -07003707 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003708 }
3709
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003710 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003711
3712 if (sched_feat(ARCH_POWER))
3713 power *= arch_scale_freq_power(sd, cpu);
3714 else
3715 power *= default_scale_freq_power(sd, cpu);
3716
Nikhil Rao1399fa72011-05-18 10:09:39 -07003717 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003718
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003719 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003720 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003721
3722 if (!power)
3723 power = 1;
3724
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003725 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003726 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003727}
3728
Peter Zijlstra029632f2011-10-25 10:00:11 +02003729void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003730{
3731 struct sched_domain *child = sd->child;
3732 struct sched_group *group, *sdg = sd->groups;
3733 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003734 unsigned long interval;
3735
3736 interval = msecs_to_jiffies(sd->balance_interval);
3737 interval = clamp(interval, 1UL, max_load_balance_interval);
3738 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003739
3740 if (!child) {
3741 update_cpu_power(sd, cpu);
3742 return;
3743 }
3744
3745 power = 0;
3746
3747 group = child->groups;
3748 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003749 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003750 group = group->next;
3751 } while (group != child->groups);
3752
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003753 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003754}
3755
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003756/*
3757 * Try and fix up capacity for tiny siblings, this is needed when
3758 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3759 * which on its own isn't powerful enough.
3760 *
3761 * See update_sd_pick_busiest() and check_asym_packing().
3762 */
3763static inline int
3764fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3765{
3766 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003767 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003768 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003769 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003770 return 0;
3771
3772 /*
3773 * If ~90% of the cpu_power is still there, we're good.
3774 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003775 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003776 return 1;
3777
3778 return 0;
3779}
3780
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003781/**
3782 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3783 * @sd: The sched_domain whose statistics are to be updated.
3784 * @group: sched_group whose statistics are to be updated.
3785 * @this_cpu: Cpu for which load balance is currently performed.
3786 * @idle: Idle status of this_cpu
3787 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003788 * @local_group: Does group contain this_cpu.
3789 * @cpus: Set of cpus considered for load balancing.
3790 * @balance: Should we balance.
3791 * @sgs: variable to hold the statistics for this group.
3792 */
3793static inline void update_sg_lb_stats(struct sched_domain *sd,
3794 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003795 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003796 int local_group, const struct cpumask *cpus,
3797 int *balance, struct sg_lb_stats *sgs)
3798{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003799 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003800 int i;
3801 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003802 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003803
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003804 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003805 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003806
3807 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003808 max_cpu_load = 0;
3809 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003810 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003811
3812 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3813 struct rq *rq = cpu_rq(i);
3814
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003815 /* Bias balancing toward cpus of our domain */
3816 if (local_group) {
3817 if (idle_cpu(i) && !first_idle_cpu) {
3818 first_idle_cpu = 1;
3819 balance_cpu = i;
3820 }
3821
3822 load = target_load(i, load_idx);
3823 } else {
3824 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003825 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003826 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003827 max_nr_running = rq->nr_running;
3828 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003829 if (min_cpu_load > load)
3830 min_cpu_load = load;
3831 }
3832
3833 sgs->group_load += load;
3834 sgs->sum_nr_running += rq->nr_running;
3835 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003836 if (idle_cpu(i))
3837 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003838 }
3839
3840 /*
3841 * First idle cpu or the first cpu(busiest) in this sched group
3842 * is eligible for doing load balancing at this and above
3843 * domains. In the newly idle case, we will allow all the cpu's
3844 * to do the newly idle load balance.
3845 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003846 if (local_group) {
3847 if (idle != CPU_NEWLY_IDLE) {
3848 if (balance_cpu != this_cpu) {
3849 *balance = 0;
3850 return;
3851 }
3852 update_group_power(sd, this_cpu);
3853 } else if (time_after_eq(jiffies, group->sgp->next_update))
3854 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003855 }
3856
3857 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003858 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003859
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003860 /*
3861 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003862 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003863 *
3864 * APZ: with cgroup the avg task weight can vary wildly and
3865 * might not be a suitable number - should we keep a
3866 * normalized nr_running number somewhere that negates
3867 * the hierarchy?
3868 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003869 if (sgs->sum_nr_running)
3870 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003871
Peter Zijlstra866ab432011-02-21 18:56:47 +01003872 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003873 sgs->group_imb = 1;
3874
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003875 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003876 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003877 if (!sgs->group_capacity)
3878 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003879 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003880
3881 if (sgs->group_capacity > sgs->sum_nr_running)
3882 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003883}
3884
3885/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003886 * update_sd_pick_busiest - return 1 on busiest group
3887 * @sd: sched_domain whose statistics are to be checked
3888 * @sds: sched_domain statistics
3889 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003890 * @sgs: sched_group statistics
3891 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10003892 *
3893 * Determine if @sg is a busier group than the previously selected
3894 * busiest group.
3895 */
3896static bool update_sd_pick_busiest(struct sched_domain *sd,
3897 struct sd_lb_stats *sds,
3898 struct sched_group *sg,
3899 struct sg_lb_stats *sgs,
3900 int this_cpu)
3901{
3902 if (sgs->avg_load <= sds->max_load)
3903 return false;
3904
3905 if (sgs->sum_nr_running > sgs->group_capacity)
3906 return true;
3907
3908 if (sgs->group_imb)
3909 return true;
3910
3911 /*
3912 * ASYM_PACKING needs to move all the work to the lowest
3913 * numbered CPUs in the group, therefore mark all groups
3914 * higher than ourself as busy.
3915 */
3916 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3917 this_cpu < group_first_cpu(sg)) {
3918 if (!sds->busiest)
3919 return true;
3920
3921 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3922 return true;
3923 }
3924
3925 return false;
3926}
3927
3928/**
Hui Kang461819a2011-10-11 23:00:59 -04003929 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003930 * @sd: sched_domain whose statistics are to be updated.
3931 * @this_cpu: Cpu for which load balance is currently performed.
3932 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003933 * @cpus: Set of cpus considered for load balancing.
3934 * @balance: Should we balance.
3935 * @sds: variable to hold the statistics for this sched_domain.
3936 */
3937static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003938 enum cpu_idle_type idle, const struct cpumask *cpus,
3939 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003940{
3941 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003942 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943 struct sg_lb_stats sgs;
3944 int load_idx, prefer_sibling = 0;
3945
3946 if (child && child->flags & SD_PREFER_SIBLING)
3947 prefer_sibling = 1;
3948
3949 init_sd_power_savings_stats(sd, sds, idle);
3950 load_idx = get_sd_load_idx(sd, idle);
3951
3952 do {
3953 int local_group;
3954
Michael Neuling532cb4c2010-06-08 14:57:02 +10003955 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003956 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003957 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003958 local_group, cpus, balance, &sgs);
3959
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003960 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003961 return;
3962
3963 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003964 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003965
3966 /*
3967 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10003968 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07003969 * and move all the excess tasks away. We lower the capacity
3970 * of a group only if the local group has the capacity to fit
3971 * these excess tasks, i.e. nr_running < group_capacity. The
3972 * extra check prevents the case where you always pull from the
3973 * heaviest group when it is already under-utilized (possible
3974 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003975 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07003976 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003977 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3978
3979 if (local_group) {
3980 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003981 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003982 sds->this_nr_running = sgs.sum_nr_running;
3983 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003984 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003985 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003986 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003987 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003988 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003989 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003990 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003991 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003992 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07003993 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003994 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003995 sds->group_imb = sgs.group_imb;
3996 }
3997
Michael Neuling532cb4c2010-06-08 14:57:02 +10003998 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
3999 sg = sg->next;
4000 } while (sg != sd->groups);
4001}
4002
Michael Neuling532cb4c2010-06-08 14:57:02 +10004003/**
4004 * check_asym_packing - Check to see if the group is packed into the
4005 * sched doman.
4006 *
4007 * This is primarily intended to used at the sibling level. Some
4008 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4009 * case of POWER7, it can move to lower SMT modes only when higher
4010 * threads are idle. When in lower SMT modes, the threads will
4011 * perform better since they share less core resources. Hence when we
4012 * have idle threads, we want them to be the higher ones.
4013 *
4014 * This packing function is run on idle threads. It checks to see if
4015 * the busiest CPU in this domain (core in the P7 case) has a higher
4016 * CPU number than the packing function is being run on. Here we are
4017 * assuming lower CPU number will be equivalent to lower a SMT thread
4018 * number.
4019 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004020 * Returns 1 when packing is required and a task should be moved to
4021 * this CPU. The amount of the imbalance is returned in *imbalance.
4022 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10004023 * @sd: The sched_domain whose packing is to be checked.
4024 * @sds: Statistics of the sched_domain which is to be packed
4025 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4026 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004027 */
4028static int check_asym_packing(struct sched_domain *sd,
4029 struct sd_lb_stats *sds,
4030 int this_cpu, unsigned long *imbalance)
4031{
4032 int busiest_cpu;
4033
4034 if (!(sd->flags & SD_ASYM_PACKING))
4035 return 0;
4036
4037 if (!sds->busiest)
4038 return 0;
4039
4040 busiest_cpu = group_first_cpu(sds->busiest);
4041 if (this_cpu > busiest_cpu)
4042 return 0;
4043
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004044 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004045 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004046 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004047}
4048
4049/**
4050 * fix_small_imbalance - Calculate the minor imbalance that exists
4051 * amongst the groups of a sched_domain, during
4052 * load balancing.
4053 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4054 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4055 * @imbalance: Variable to store the imbalance.
4056 */
4057static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4058 int this_cpu, unsigned long *imbalance)
4059{
4060 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4061 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004062 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004063
4064 if (sds->this_nr_running) {
4065 sds->this_load_per_task /= sds->this_nr_running;
4066 if (sds->busiest_load_per_task >
4067 sds->this_load_per_task)
4068 imbn = 1;
4069 } else
4070 sds->this_load_per_task =
4071 cpu_avg_load_per_task(this_cpu);
4072
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004073 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004074 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004075 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004076
4077 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4078 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004079 *imbalance = sds->busiest_load_per_task;
4080 return;
4081 }
4082
4083 /*
4084 * OK, we don't have enough imbalance to justify moving tasks,
4085 * however we may be able to increase total CPU power used by
4086 * moving them.
4087 */
4088
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004089 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004090 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004091 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004092 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004093 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004094
4095 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004096 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004097 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004099 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004100 min(sds->busiest_load_per_task, sds->max_load - tmp);
4101
4102 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004103 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004104 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004105 tmp = (sds->max_load * sds->busiest->sgp->power) /
4106 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004108 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004109 sds->this->sgp->power;
4110 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004111 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004112 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004113
4114 /* Move if we gain throughput */
4115 if (pwr_move > pwr_now)
4116 *imbalance = sds->busiest_load_per_task;
4117}
4118
4119/**
4120 * calculate_imbalance - Calculate the amount of imbalance present within the
4121 * groups of a given sched_domain during load balance.
4122 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4123 * @this_cpu: Cpu for which currently load balance is being performed.
4124 * @imbalance: The variable to store the imbalance.
4125 */
4126static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4127 unsigned long *imbalance)
4128{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004129 unsigned long max_pull, load_above_capacity = ~0UL;
4130
4131 sds->busiest_load_per_task /= sds->busiest_nr_running;
4132 if (sds->group_imb) {
4133 sds->busiest_load_per_task =
4134 min(sds->busiest_load_per_task, sds->avg_load);
4135 }
4136
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004137 /*
4138 * In the presence of smp nice balancing, certain scenarios can have
4139 * max load less than avg load(as we skip the groups at or below
4140 * its cpu_power, while calculating max_load..)
4141 */
4142 if (sds->max_load < sds->avg_load) {
4143 *imbalance = 0;
4144 return fix_small_imbalance(sds, this_cpu, imbalance);
4145 }
4146
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004147 if (!sds->group_imb) {
4148 /*
4149 * Don't want to pull so many tasks that a group would go idle.
4150 */
4151 load_above_capacity = (sds->busiest_nr_running -
4152 sds->busiest_group_capacity);
4153
Nikhil Rao1399fa72011-05-18 10:09:39 -07004154 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004155
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004156 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004157 }
4158
4159 /*
4160 * We're trying to get all the cpus to the average_load, so we don't
4161 * want to push ourselves above the average load, nor do we wish to
4162 * reduce the max loaded cpu below the average load. At the same time,
4163 * we also don't want to reduce the group load below the group capacity
4164 * (so that we can implement power-savings policies etc). Thus we look
4165 * for the minimum possible imbalance.
4166 * Be careful of negative numbers as they'll appear as very large values
4167 * with unsigned longs.
4168 */
4169 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004170
4171 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004172 *imbalance = min(max_pull * sds->busiest->sgp->power,
4173 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004174 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004175
4176 /*
4177 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004178 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004179 * a think about bumping its value to force at least one task to be
4180 * moved
4181 */
4182 if (*imbalance < sds->busiest_load_per_task)
4183 return fix_small_imbalance(sds, this_cpu, imbalance);
4184
4185}
Nikhil Raofab47622010-10-15 13:12:29 -07004186
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004187/******* find_busiest_group() helpers end here *********************/
4188
4189/**
4190 * find_busiest_group - Returns the busiest group within the sched_domain
4191 * if there is an imbalance. If there isn't an imbalance, and
4192 * the user has opted for power-savings, it returns a group whose
4193 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4194 * such a group exists.
4195 *
4196 * Also calculates the amount of weighted load which should be moved
4197 * to restore balance.
4198 *
4199 * @sd: The sched_domain whose busiest group is to be returned.
4200 * @this_cpu: The cpu for which load balancing is currently being performed.
4201 * @imbalance: Variable which stores amount of weighted load which should
4202 * be moved to restore balance/put a group to idle.
4203 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004204 * @cpus: The set of CPUs under consideration for load-balancing.
4205 * @balance: Pointer to a variable indicating if this_cpu
4206 * is the appropriate cpu to perform load balancing at this_level.
4207 *
4208 * Returns: - the busiest group if imbalance exists.
4209 * - If no imbalance and user has opted for power-savings balance,
4210 * return the least loaded group whose CPUs can be
4211 * put to idle by rebalancing its tasks onto our group.
4212 */
4213static struct sched_group *
4214find_busiest_group(struct sched_domain *sd, int this_cpu,
4215 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004216 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004217{
4218 struct sd_lb_stats sds;
4219
4220 memset(&sds, 0, sizeof(sds));
4221
4222 /*
4223 * Compute the various statistics relavent for load balancing at
4224 * this level.
4225 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004226 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004227
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004228 /*
4229 * this_cpu is not the appropriate cpu to perform load balancing at
4230 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004231 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004232 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004233 goto ret;
4234
Michael Neuling532cb4c2010-06-08 14:57:02 +10004235 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
4236 check_asym_packing(sd, &sds, this_cpu, imbalance))
4237 return sds.busiest;
4238
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004239 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004240 if (!sds.busiest || sds.busiest_nr_running == 0)
4241 goto out_balanced;
4242
Nikhil Rao1399fa72011-05-18 10:09:39 -07004243 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004244
Peter Zijlstra866ab432011-02-21 18:56:47 +01004245 /*
4246 * If the busiest group is imbalanced the below checks don't
4247 * work because they assumes all things are equal, which typically
4248 * isn't true due to cpus_allowed constraints and the like.
4249 */
4250 if (sds.group_imb)
4251 goto force_balance;
4252
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004253 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07004254 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4255 !sds.busiest_has_capacity)
4256 goto force_balance;
4257
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004258 /*
4259 * If the local group is more busy than the selected busiest group
4260 * don't try and pull any tasks.
4261 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004262 if (sds.this_load >= sds.max_load)
4263 goto out_balanced;
4264
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004265 /*
4266 * Don't pull any tasks if this group is already above the domain
4267 * average load.
4268 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004269 if (sds.this_load >= sds.avg_load)
4270 goto out_balanced;
4271
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004272 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004273 /*
4274 * This cpu is idle. If the busiest group load doesn't
4275 * have more tasks than the number of available cpu's and
4276 * there is no imbalance between this and busiest group
4277 * wrt to idle cpu's, it is balanced.
4278 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004279 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004280 sds.busiest_nr_running <= sds.busiest_group_weight)
4281 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004282 } else {
4283 /*
4284 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4285 * imbalance_pct to be conservative.
4286 */
4287 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4288 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004289 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004290
Nikhil Raofab47622010-10-15 13:12:29 -07004291force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004292 /* Looks like there is an imbalance. Compute it */
4293 calculate_imbalance(&sds, this_cpu, imbalance);
4294 return sds.busiest;
4295
4296out_balanced:
4297 /*
4298 * There is no obvious imbalance. But check if we can do some balancing
4299 * to save power.
4300 */
4301 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4302 return sds.busiest;
4303ret:
4304 *imbalance = 0;
4305 return NULL;
4306}
4307
4308/*
4309 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4310 */
4311static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004312find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4313 enum cpu_idle_type idle, unsigned long imbalance,
4314 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004315{
4316 struct rq *busiest = NULL, *rq;
4317 unsigned long max_load = 0;
4318 int i;
4319
4320 for_each_cpu(i, sched_group_cpus(group)) {
4321 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004322 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4323 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004324 unsigned long wl;
4325
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004326 if (!capacity)
4327 capacity = fix_small_capacity(sd, group);
4328
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004329 if (!cpumask_test_cpu(i, cpus))
4330 continue;
4331
4332 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004333 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004334
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004335 /*
4336 * When comparing with imbalance, use weighted_cpuload()
4337 * which is not scaled with the cpu power.
4338 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004339 if (capacity && rq->nr_running == 1 && wl > imbalance)
4340 continue;
4341
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004342 /*
4343 * For the load comparisons with the other cpu's, consider
4344 * the weighted_cpuload() scaled with the cpu power, so that
4345 * the load can be moved away from the cpu that is potentially
4346 * running at a lower capacity.
4347 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004348 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004349
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004350 if (wl > max_load) {
4351 max_load = wl;
4352 busiest = rq;
4353 }
4354 }
4355
4356 return busiest;
4357}
4358
4359/*
4360 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4361 * so long as it is large enough.
4362 */
4363#define MAX_PINNED_INTERVAL 512
4364
4365/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004366DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004367
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004368static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004369 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004370{
4371 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004372
4373 /*
4374 * ASYM_PACKING needs to force migrate tasks from busy but
4375 * higher numbered CPUs in order to pack all tasks in the
4376 * lowest numbered CPUs.
4377 */
4378 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
4379 return 1;
4380
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004381 /*
4382 * The only task running in a non-idle cpu can be moved to this
4383 * cpu in an attempt to completely freeup the other CPU
4384 * package.
4385 *
4386 * The package power saving logic comes from
4387 * find_busiest_group(). If there are no imbalance, then
4388 * f_b_g() will return NULL. However when sched_mc={1,2} then
4389 * f_b_g() will select a group from which a running task may be
4390 * pulled to this cpu in order to make the other package idle.
4391 * If there is no opportunity to make a package idle and if
4392 * there are no imbalance, then f_b_g() will return NULL and no
4393 * action will be taken in load_balance_newidle().
4394 *
4395 * Under normal task pull operation due to imbalance, there
4396 * will be more than one task in the source run queue and
4397 * move_tasks() will succeed. ld_moved will be true and this
4398 * active balance code will not be triggered.
4399 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004400 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4401 return 0;
4402 }
4403
4404 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4405}
4406
Tejun Heo969c7922010-05-06 18:49:21 +02004407static int active_load_balance_cpu_stop(void *data);
4408
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004409/*
4410 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4411 * tasks if there is an imbalance.
4412 */
4413static int load_balance(int this_cpu, struct rq *this_rq,
4414 struct sched_domain *sd, enum cpu_idle_type idle,
4415 int *balance)
4416{
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004417 int ld_moved, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004418 struct sched_group *group;
4419 unsigned long imbalance;
4420 struct rq *busiest;
4421 unsigned long flags;
4422 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4423
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004424 struct lb_env env = {
4425 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004426 .dst_cpu = this_cpu,
4427 .dst_rq = this_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004428 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004429 .loop_break = sched_nr_migrate_break,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004430 };
4431
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004432 cpumask_copy(cpus, cpu_active_mask);
4433
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004434 schedstat_inc(sd, lb_count[idle]);
4435
4436redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004437 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004438 cpus, balance);
4439
4440 if (*balance == 0)
4441 goto out_balanced;
4442
4443 if (!group) {
4444 schedstat_inc(sd, lb_nobusyg[idle]);
4445 goto out_balanced;
4446 }
4447
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004448 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004449 if (!busiest) {
4450 schedstat_inc(sd, lb_nobusyq[idle]);
4451 goto out_balanced;
4452 }
4453
4454 BUG_ON(busiest == this_rq);
4455
4456 schedstat_add(sd, lb_imbalance[idle], imbalance);
4457
4458 ld_moved = 0;
4459 if (busiest->nr_running > 1) {
4460 /*
4461 * Attempt to move tasks. If find_busiest_group has found
4462 * an imbalance but busiest->nr_running <= 1, the group is
4463 * still unbalanced. ld_moved simply stays zero, so it is
4464 * correctly treated as an imbalance.
4465 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004466 env.flags |= LBF_ALL_PINNED;
Peter Zijlstraeb953082012-04-17 13:38:40 +02004467 env.load_move = imbalance;
4468 env.src_cpu = busiest->cpu;
4469 env.src_rq = busiest;
4470 env.loop_max = min_t(unsigned long, sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004471
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004472more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004473 local_irq_save(flags);
4474 double_rq_lock(this_rq, busiest);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004475 if (!env.loop)
4476 update_h_load(env.src_cpu);
4477 ld_moved += move_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004478 double_rq_unlock(this_rq, busiest);
4479 local_irq_restore(flags);
4480
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004481 if (env.flags & LBF_NEED_BREAK) {
4482 env.flags &= ~LBF_NEED_BREAK;
4483 goto more_balance;
4484 }
4485
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004486 /*
4487 * some other cpu did the load balance for us.
4488 */
4489 if (ld_moved && this_cpu != smp_processor_id())
4490 resched_cpu(this_cpu);
4491
4492 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004493 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004494 cpumask_clear_cpu(cpu_of(busiest), cpus);
4495 if (!cpumask_empty(cpus))
4496 goto redo;
4497 goto out_balanced;
4498 }
4499 }
4500
4501 if (!ld_moved) {
4502 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004503 /*
4504 * Increment the failure counter only on periodic balance.
4505 * We do not want newidle balance, which can be very
4506 * frequent, pollute the failure counter causing
4507 * excessive cache_hot migrations and active balances.
4508 */
4509 if (idle != CPU_NEWLY_IDLE)
4510 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004511
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004512 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004513 raw_spin_lock_irqsave(&busiest->lock, flags);
4514
Tejun Heo969c7922010-05-06 18:49:21 +02004515 /* don't kick the active_load_balance_cpu_stop,
4516 * if the curr task on busiest cpu can't be
4517 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004518 */
4519 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004520 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004521 raw_spin_unlock_irqrestore(&busiest->lock,
4522 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004523 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004524 goto out_one_pinned;
4525 }
4526
Tejun Heo969c7922010-05-06 18:49:21 +02004527 /*
4528 * ->active_balance synchronizes accesses to
4529 * ->active_balance_work. Once set, it's cleared
4530 * only after active load balance is finished.
4531 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004532 if (!busiest->active_balance) {
4533 busiest->active_balance = 1;
4534 busiest->push_cpu = this_cpu;
4535 active_balance = 1;
4536 }
4537 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004538
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004539 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02004540 stop_one_cpu_nowait(cpu_of(busiest),
4541 active_load_balance_cpu_stop, busiest,
4542 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004543
4544 /*
4545 * We've kicked active balancing, reset the failure
4546 * counter.
4547 */
4548 sd->nr_balance_failed = sd->cache_nice_tries+1;
4549 }
Steve Muckle8f77c282013-03-11 16:33:42 -07004550 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004551 sd->nr_balance_failed = 0;
Steve Muckle8f77c282013-03-11 16:33:42 -07004552 if (per_cpu(dbs_boost_needed, this_cpu)) {
4553 per_cpu(dbs_boost_needed, this_cpu) = false;
4554 atomic_notifier_call_chain(&migration_notifier_head,
4555 this_cpu,
4556 (void *)cpu_of(busiest));
4557 }
4558 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004559 if (likely(!active_balance)) {
4560 /* We were unbalanced, so reset the balancing interval */
4561 sd->balance_interval = sd->min_interval;
4562 } else {
4563 /*
4564 * If we've begun active balancing, start to back off. This
4565 * case may not be covered by the all_pinned logic if there
4566 * is only 1 task on the busy runqueue (because we don't call
4567 * move_tasks).
4568 */
4569 if (sd->balance_interval < sd->max_interval)
4570 sd->balance_interval *= 2;
4571 }
4572
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004573 goto out;
4574
4575out_balanced:
4576 schedstat_inc(sd, lb_balanced[idle]);
4577
4578 sd->nr_balance_failed = 0;
4579
4580out_one_pinned:
4581 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004582 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004583 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004584 (sd->balance_interval < sd->max_interval))
4585 sd->balance_interval *= 2;
4586
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004587 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004588out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004589 return ld_moved;
4590}
4591
4592/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004593 * idle_balance is called by schedule() if this_cpu is about to become
4594 * idle. Attempts to pull tasks from other CPUs.
4595 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004596void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004597{
4598 struct sched_domain *sd;
4599 int pulled_task = 0;
4600 unsigned long next_balance = jiffies + HZ;
4601
4602 this_rq->idle_stamp = this_rq->clock;
4603
4604 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4605 return;
4606
Peter Zijlstraf492e122009-12-23 15:29:42 +01004607 /*
4608 * Drop the rq->lock, but keep IRQ/preempt disabled.
4609 */
4610 raw_spin_unlock(&this_rq->lock);
4611
Paul Turnerc66eaf62010-11-15 15:47:07 -08004612 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004613 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004614 for_each_domain(this_cpu, sd) {
4615 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004616 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004617
4618 if (!(sd->flags & SD_LOAD_BALANCE))
4619 continue;
4620
Peter Zijlstraf492e122009-12-23 15:29:42 +01004621 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004622 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004623 pulled_task = load_balance(this_cpu, this_rq,
4624 sd, CPU_NEWLY_IDLE, &balance);
4625 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004626
4627 interval = msecs_to_jiffies(sd->balance_interval);
4628 if (time_after(next_balance, sd->last_balance + interval))
4629 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004630 if (pulled_task) {
4631 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004632 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004633 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004634 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004635 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004636
4637 raw_spin_lock(&this_rq->lock);
4638
Srivatsa Vaddagirice1a0412013-03-07 12:14:53 -08004639 if (!pulled_task || time_after(jiffies, this_rq->next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004640 /*
4641 * We are going idle. next_balance may be set based on
4642 * a busy processor. So reset next_balance.
4643 */
4644 this_rq->next_balance = next_balance;
4645 }
4646}
4647
4648/*
Tejun Heo969c7922010-05-06 18:49:21 +02004649 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4650 * running tasks off the busiest CPU onto idle CPUs. It requires at
4651 * least 1 task to be running on each physical CPU where possible, and
4652 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004653 */
Tejun Heo969c7922010-05-06 18:49:21 +02004654static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004655{
Tejun Heo969c7922010-05-06 18:49:21 +02004656 struct rq *busiest_rq = data;
4657 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004658 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004659 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004660 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004661
4662 raw_spin_lock_irq(&busiest_rq->lock);
4663
4664 /* make sure the requested cpu hasn't gone down in the meantime */
4665 if (unlikely(busiest_cpu != smp_processor_id() ||
4666 !busiest_rq->active_balance))
4667 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004668
4669 /* Is there any task to move? */
4670 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004671 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004672
4673 /*
4674 * This condition is "impossible", if it occurs
4675 * we need to fix it. Originally reported by
4676 * Bjorn Helgaas on a 128-cpu setup.
4677 */
4678 BUG_ON(busiest_rq == target_rq);
4679
4680 /* move a task from busiest_rq to target_rq */
4681 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004682
4683 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004684 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004685 for_each_domain(target_cpu, sd) {
4686 if ((sd->flags & SD_LOAD_BALANCE) &&
4687 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4688 break;
4689 }
4690
4691 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004692 struct lb_env env = {
4693 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004694 .dst_cpu = target_cpu,
4695 .dst_rq = target_rq,
4696 .src_cpu = busiest_rq->cpu,
4697 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004698 .idle = CPU_IDLE,
4699 };
4700
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004701 schedstat_inc(sd, alb_count);
4702
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004703 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004704 schedstat_inc(sd, alb_pushed);
4705 else
4706 schedstat_inc(sd, alb_failed);
4707 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004708 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004709 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004710out_unlock:
4711 busiest_rq->active_balance = 0;
4712 raw_spin_unlock_irq(&busiest_rq->lock);
Steve Muckle8f77c282013-03-11 16:33:42 -07004713 if (per_cpu(dbs_boost_needed, target_cpu)) {
4714 per_cpu(dbs_boost_needed, target_cpu) = false;
4715 atomic_notifier_call_chain(&migration_notifier_head,
4716 target_cpu,
4717 (void *)cpu_of(busiest_rq));
4718 }
Tejun Heo969c7922010-05-06 18:49:21 +02004719 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004720}
4721
4722#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004723/*
4724 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004725 * - When one of the busy CPUs notice that there may be an idle rebalancing
4726 * needed, they will kick the idle load balancer, which then does idle
4727 * load balancing for all the idle CPUs.
4728 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004729static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004730 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004731 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004732 unsigned long next_balance; /* in jiffy units */
4733} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004734
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004735#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4736/**
4737 * lowest_flag_domain - Return lowest sched_domain containing flag.
4738 * @cpu: The cpu whose lowest level of sched domain is to
4739 * be returned.
4740 * @flag: The flag to check for the lowest sched_domain
4741 * for the given cpu.
4742 *
4743 * Returns the lowest sched_domain of a cpu which contains the given flag.
4744 */
4745static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4746{
4747 struct sched_domain *sd;
4748
4749 for_each_domain(cpu, sd)
Hillf Danton08354712011-06-16 21:55:19 -04004750 if (sd->flags & flag)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004751 break;
4752
4753 return sd;
4754}
4755
4756/**
4757 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4758 * @cpu: The cpu whose domains we're iterating over.
4759 * @sd: variable holding the value of the power_savings_sd
4760 * for cpu.
4761 * @flag: The flag to filter the sched_domains to be iterated.
4762 *
4763 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4764 * set, starting from the lowest sched_domain to the highest.
4765 */
4766#define for_each_flag_domain(cpu, sd, flag) \
4767 for (sd = lowest_flag_domain(cpu, flag); \
4768 (sd && (sd->flags & flag)); sd = sd->parent)
4769
4770/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004771 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4772 * @cpu: The cpu which is nominating a new idle_load_balancer.
4773 *
4774 * Returns: Returns the id of the idle load balancer if it exists,
4775 * Else, returns >= nr_cpu_ids.
4776 *
4777 * This algorithm picks the idle load balancer such that it belongs to a
4778 * semi-idle powersavings sched_domain. The idea is to try and avoid
4779 * completely idle packages/cores just for the purpose of idle load balancing
4780 * when there are other idle cpu's which are better suited for that job.
4781 */
4782static int find_new_ilb(int cpu)
4783{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004784 int ilb = cpumask_first(nohz.idle_cpus_mask);
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004785 struct sched_group *ilbg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004786 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004787
4788 /*
4789 * Have idle load balancer selection from semi-idle packages only
4790 * when power-aware load balancing is enabled
4791 */
4792 if (!(sched_smt_power_savings || sched_mc_power_savings))
4793 goto out_done;
4794
4795 /*
4796 * Optimize for the case when we have no idle CPUs or only one
4797 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4798 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004799 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004800 goto out_done;
4801
Peter Zijlstradce840a2011-04-07 14:09:50 +02004802 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004803 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004804 ilbg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004805
4806 do {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004807 if (ilbg->group_weight !=
4808 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4809 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4810 sched_group_cpus(ilbg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02004811 goto unlock;
4812 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004813
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004814 ilbg = ilbg->next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004815
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004816 } while (ilbg != sd->groups);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004817 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004818unlock:
4819 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004820
4821out_done:
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004822 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4823 return ilb;
4824
4825 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004826}
4827#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4828static inline int find_new_ilb(int call_cpu)
4829{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004830 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004831}
4832#endif
4833
4834/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004835 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4836 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4837 * CPU (if there is one).
4838 */
4839static void nohz_balancer_kick(int cpu)
4840{
4841 int ilb_cpu;
4842
4843 nohz.next_balance++;
4844
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004845 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004846
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004847 if (ilb_cpu >= nr_cpu_ids)
4848 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004849
Suresh Siddhacd490c52011-12-06 11:26:34 -08004850 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004851 return;
4852 /*
4853 * Use smp_send_reschedule() instead of resched_cpu().
4854 * This way we generate a sched IPI on the target cpu which
4855 * is idle. And the softirq performing nohz idle load balance
4856 * will be run before returning from the IPI.
4857 */
4858 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004859 return;
4860}
4861
Suresh Siddha71325962012-01-19 18:28:57 -08004862static inline void clear_nohz_tick_stopped(int cpu)
4863{
4864 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4865 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4866 atomic_dec(&nohz.nr_cpus);
4867 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4868 }
4869}
4870
Suresh Siddha69e1e812011-12-01 17:07:33 -08004871static inline void set_cpu_sd_state_busy(void)
4872{
4873 struct sched_domain *sd;
4874 int cpu = smp_processor_id();
4875
4876 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4877 return;
4878 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4879
4880 rcu_read_lock();
4881 for_each_domain(cpu, sd)
4882 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4883 rcu_read_unlock();
4884}
4885
4886void set_cpu_sd_state_idle(void)
4887{
4888 struct sched_domain *sd;
4889 int cpu = smp_processor_id();
4890
4891 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4892 return;
4893 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4894
4895 rcu_read_lock();
4896 for_each_domain(cpu, sd)
4897 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4898 rcu_read_unlock();
4899}
4900
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004901/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004902 * This routine will record that this cpu is going idle with tick stopped.
4903 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004904 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004905void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004906{
4907 int cpu = smp_processor_id();
4908
Suresh Siddha71325962012-01-19 18:28:57 -08004909 /*
4910 * If this cpu is going down, then nothing needs to be done.
4911 */
4912 if (!cpu_active(cpu))
4913 return;
4914
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004915 if (stop_tick) {
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004916 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004917 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004918
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004919 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004920 atomic_inc(&nohz.nr_cpus);
Suresh Siddha1c792db2011-12-01 17:07:32 -08004921 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004922 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004923 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004924}
Suresh Siddha71325962012-01-19 18:28:57 -08004925
4926static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4927 unsigned long action, void *hcpu)
4928{
4929 switch (action & ~CPU_TASKS_FROZEN) {
4930 case CPU_DYING:
4931 clear_nohz_tick_stopped(smp_processor_id());
4932 return NOTIFY_OK;
4933 default:
4934 return NOTIFY_DONE;
4935 }
4936}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004937#endif
4938
4939static DEFINE_SPINLOCK(balancing);
4940
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004941/*
4942 * Scale the max load_balance interval with the number of CPUs in the system.
4943 * This trades load-balance latency on larger machines for less cross talk.
4944 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004945void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004946{
4947 max_load_balance_interval = HZ*num_online_cpus()/10;
4948}
4949
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004950/*
4951 * It checks each scheduling domain to see if it is due to be balanced,
4952 * and initiates a balancing operation if so.
4953 *
4954 * Balancing parameters are set up in arch_init_sched_domains.
4955 */
4956static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4957{
4958 int balance = 1;
4959 struct rq *rq = cpu_rq(cpu);
4960 unsigned long interval;
4961 struct sched_domain *sd;
4962 /* Earliest time when we have to do rebalance again */
4963 unsigned long next_balance = jiffies + 60*HZ;
4964 int update_next_balance = 0;
4965 int need_serialize;
4966
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004967 update_shares(cpu);
4968
Peter Zijlstradce840a2011-04-07 14:09:50 +02004969 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004970 for_each_domain(cpu, sd) {
4971 if (!(sd->flags & SD_LOAD_BALANCE))
4972 continue;
4973
4974 interval = sd->balance_interval;
4975 if (idle != CPU_IDLE)
4976 interval *= sd->busy_factor;
4977
4978 /* scale ms to jiffies */
4979 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004980 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004981
4982 need_serialize = sd->flags & SD_SERIALIZE;
4983
4984 if (need_serialize) {
4985 if (!spin_trylock(&balancing))
4986 goto out;
4987 }
4988
4989 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4990 if (load_balance(cpu, rq, sd, idle, &balance)) {
4991 /*
4992 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004993 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004994 */
4995 idle = CPU_NOT_IDLE;
4996 }
4997 sd->last_balance = jiffies;
4998 }
4999 if (need_serialize)
5000 spin_unlock(&balancing);
5001out:
5002 if (time_after(next_balance, sd->last_balance + interval)) {
5003 next_balance = sd->last_balance + interval;
5004 update_next_balance = 1;
5005 }
5006
5007 /*
5008 * Stop the load balance at this level. There is another
5009 * CPU in our sched group which is doing load balancing more
5010 * actively.
5011 */
5012 if (!balance)
5013 break;
5014 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005015 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005016
5017 /*
5018 * next_balance will be updated only when there is a need.
5019 * When the cpu is attached to null domain for ex, it will not be
5020 * updated.
5021 */
5022 if (likely(update_next_balance))
5023 rq->next_balance = next_balance;
5024}
5025
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005026#ifdef CONFIG_NO_HZ
5027/*
5028 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5029 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5030 */
5031static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5032{
5033 struct rq *this_rq = cpu_rq(this_cpu);
5034 struct rq *rq;
5035 int balance_cpu;
5036
Suresh Siddha1c792db2011-12-01 17:07:32 -08005037 if (idle != CPU_IDLE ||
5038 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5039 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005040
5041 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005042 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005043 continue;
5044
5045 /*
5046 * If this cpu gets work to do, stop the load balancing
5047 * work being done for other cpus. Next load
5048 * balancing owner will pick it up.
5049 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005050 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005051 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005052
5053 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02005054 update_rq_clock(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005055 update_cpu_load(this_rq);
5056 raw_spin_unlock_irq(&this_rq->lock);
5057
5058 rebalance_domains(balance_cpu, CPU_IDLE);
5059
5060 rq = cpu_rq(balance_cpu);
5061 if (time_after(this_rq->next_balance, rq->next_balance))
5062 this_rq->next_balance = rq->next_balance;
5063 }
5064 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005065end:
5066 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005067}
5068
5069/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005070 * Current heuristic for kicking the idle load balancer in the presence
5071 * of an idle cpu is the system.
5072 * - This rq has more than one task.
5073 * - At any scheduler domain level, this cpu's scheduler group has multiple
5074 * busy cpu's exceeding the group's power.
5075 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5076 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005077 */
5078static inline int nohz_kick_needed(struct rq *rq, int cpu)
5079{
5080 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005081 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005082
Suresh Siddha1c792db2011-12-01 17:07:32 -08005083 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005084 return 0;
5085
Suresh Siddha1c792db2011-12-01 17:07:32 -08005086 /*
5087 * We may be recently in ticked or tickless idle mode. At the first
5088 * busy tick after returning from idle, we will update the busy stats.
5089 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005090 set_cpu_sd_state_busy();
Suresh Siddha71325962012-01-19 18:28:57 -08005091 clear_nohz_tick_stopped(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005092
5093 /*
5094 * None are in tickless mode and hence no need for NOHZ idle load
5095 * balancing.
5096 */
5097 if (likely(!atomic_read(&nohz.nr_cpus)))
5098 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005099
5100 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005101 return 0;
5102
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005103 if (rq->nr_running >= 2)
5104 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005105
Peter Zijlstra067491b2011-12-07 14:32:08 +01005106 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005107 for_each_domain(cpu, sd) {
5108 struct sched_group *sg = sd->groups;
5109 struct sched_group_power *sgp = sg->sgp;
5110 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005111
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005112 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005113 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005114
5115 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5116 && (cpumask_first_and(nohz.idle_cpus_mask,
5117 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005118 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005119
5120 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5121 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005122 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005123 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005124 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005125
5126need_kick_unlock:
5127 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005128need_kick:
5129 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005130}
5131#else
5132static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5133#endif
5134
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005135/*
5136 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005137 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005138 */
5139static void run_rebalance_domains(struct softirq_action *h)
5140{
5141 int this_cpu = smp_processor_id();
5142 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005143 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005144 CPU_IDLE : CPU_NOT_IDLE;
5145
5146 rebalance_domains(this_cpu, idle);
5147
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005148 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005149 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005150 * balancing on behalf of the other idle cpus whose ticks are
5151 * stopped.
5152 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005153 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005154}
5155
5156static inline int on_null_domain(int cpu)
5157{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005158 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005159}
5160
5161/*
5162 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005163 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005164void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005165{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005166 /* Don't need to rebalance while attached to NULL domain */
5167 if (time_after_eq(jiffies, rq->next_balance) &&
5168 likely(!on_null_domain(cpu)))
5169 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005170#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005171 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005172 nohz_balancer_kick(cpu);
5173#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005174}
5175
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005176static void rq_online_fair(struct rq *rq)
5177{
5178 update_sysctl();
5179}
5180
5181static void rq_offline_fair(struct rq *rq)
5182{
5183 update_sysctl();
5184}
5185
Dhaval Giani55e12e52008-06-24 23:39:43 +05305186#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005187
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005188/*
5189 * scheduler tick hitting a task of our scheduling class:
5190 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005191static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005192{
5193 struct cfs_rq *cfs_rq;
5194 struct sched_entity *se = &curr->se;
5195
5196 for_each_sched_entity(se) {
5197 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005198 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005199 }
5200}
5201
5202/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005203 * called on fork with the child task as argument from the parent's context
5204 * - child not yet on the tasklist
5205 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005206 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005207static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005208{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005209 struct cfs_rq *cfs_rq;
5210 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005211 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005212 struct rq *rq = this_rq();
5213 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005214
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005215 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005216
Peter Zijlstra861d0342010-08-19 13:31:43 +02005217 update_rq_clock(rq);
5218
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005219 cfs_rq = task_cfs_rq(current);
5220 curr = cfs_rq->curr;
5221
Daisuke Nishimura628753322013-09-10 18:16:36 +09005222 /*
5223 * Not only the cpu but also the task_group of the parent might have
5224 * been changed after parent->se.parent,cfs_rq were copied to
5225 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
5226 * of child point to valid ones.
5227 */
5228 rcu_read_lock();
5229 __set_task_cpu(p, this_cpu);
5230 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005231
Ting Yang7109c442007-08-28 12:53:24 +02005232 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005233
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005234 if (curr)
5235 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005236 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005237
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005238 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005239 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005240 * Upon rescheduling, sched_class::put_prev_task() will place
5241 * 'current' within the tree based on its new key value.
5242 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005243 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305244 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005245 }
5246
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005247 se->vruntime -= cfs_rq->min_vruntime;
5248
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005249 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005250}
5251
Steven Rostedtcb469842008-01-25 21:08:22 +01005252/*
5253 * Priority of the task has changed. Check to see if we preempt
5254 * the current task.
5255 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005256static void
5257prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005258{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005259 if (!p->se.on_rq)
5260 return;
5261
Steven Rostedtcb469842008-01-25 21:08:22 +01005262 /*
5263 * Reschedule if we are currently running on this runqueue and
5264 * our priority decreased, or if we are not currently running on
5265 * this runqueue and our priority is higher than the current's
5266 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005267 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005268 if (p->prio > oldprio)
5269 resched_task(rq->curr);
5270 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005271 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005272}
5273
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005274static void switched_from_fair(struct rq *rq, struct task_struct *p)
5275{
5276 struct sched_entity *se = &p->se;
5277 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5278
5279 /*
5280 * Ensure the task's vruntime is normalized, so that when its
5281 * switched back to the fair class the enqueue_entity(.flags=0) will
5282 * do the right thing.
5283 *
5284 * If it was on_rq, then the dequeue_entity(.flags=0) will already
5285 * have normalized the vruntime, if it was !on_rq, then only when
5286 * the task is sleeping will it still have non-normalized vruntime.
5287 */
5288 if (!se->on_rq && p->state != TASK_RUNNING) {
5289 /*
5290 * Fix up our vruntime so that the current sleep doesn't
5291 * cause 'unlimited' sleep bonus.
5292 */
5293 place_entity(cfs_rq, se, 0);
5294 se->vruntime -= cfs_rq->min_vruntime;
5295 }
5296}
5297
Steven Rostedtcb469842008-01-25 21:08:22 +01005298/*
5299 * We switched to the sched_fair class.
5300 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005301static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005302{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005303 if (!p->se.on_rq)
5304 return;
5305
Steven Rostedtcb469842008-01-25 21:08:22 +01005306 /*
5307 * We were most likely switched from sched_rt, so
5308 * kick off the schedule if running, otherwise just see
5309 * if we can still preempt the current task.
5310 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005311 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005312 resched_task(rq->curr);
5313 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005314 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005315}
5316
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005317/* Account for a task changing its policy or group.
5318 *
5319 * This routine is mostly called to set cfs_rq->curr field when a task
5320 * migrates between groups/classes.
5321 */
5322static void set_curr_task_fair(struct rq *rq)
5323{
5324 struct sched_entity *se = &rq->curr->se;
5325
Paul Turnerec12cb72011-07-21 09:43:30 -07005326 for_each_sched_entity(se) {
5327 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5328
5329 set_next_entity(cfs_rq, se);
5330 /* ensure bandwidth has been allocated on our new cfs_rq */
5331 account_cfs_rq_runtime(cfs_rq, 0);
5332 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005333}
5334
Peter Zijlstra029632f2011-10-25 10:00:11 +02005335void init_cfs_rq(struct cfs_rq *cfs_rq)
5336{
5337 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005338 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5339#ifndef CONFIG_64BIT
5340 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5341#endif
5342}
5343
Peter Zijlstra810b3812008-02-29 15:21:01 -05005344#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005345static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005346{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005347 /*
5348 * If the task was not on the rq at the time of this cgroup movement
5349 * it must have been asleep, sleeping tasks keep their ->vruntime
5350 * absolute on their old rq until wakeup (needed for the fair sleeper
5351 * bonus in place_entity()).
5352 *
5353 * If it was on the rq, we've just 'preempted' it, which does convert
5354 * ->vruntime to a relative base.
5355 *
5356 * Make sure both cases convert their relative position when migrating
5357 * to another cgroup's rq. This does somewhat interfere with the
5358 * fair sleeper stuff for the first placement, but who cares.
5359 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005360 /*
5361 * When !on_rq, vruntime of the task has usually NOT been normalized.
5362 * But there are some cases where it has already been normalized:
5363 *
5364 * - Moving a forked child which is waiting for being woken up by
5365 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005366 * - Moving a task which has been woken up by try_to_wake_up() and
5367 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005368 *
5369 * To prevent boost or penalty in the new cfs_rq caused by delta
5370 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5371 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005372 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005373 on_rq = 1;
5374
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005375 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005376 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5377 set_task_rq(p, task_cpu(p));
5378 if (!on_rq)
5379 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005380}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005381
5382void free_fair_sched_group(struct task_group *tg)
5383{
5384 int i;
5385
5386 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5387
5388 for_each_possible_cpu(i) {
5389 if (tg->cfs_rq)
5390 kfree(tg->cfs_rq[i]);
5391 if (tg->se)
5392 kfree(tg->se[i]);
5393 }
5394
5395 kfree(tg->cfs_rq);
5396 kfree(tg->se);
5397}
5398
5399int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5400{
5401 struct cfs_rq *cfs_rq;
5402 struct sched_entity *se;
5403 int i;
5404
5405 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5406 if (!tg->cfs_rq)
5407 goto err;
5408 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5409 if (!tg->se)
5410 goto err;
5411
5412 tg->shares = NICE_0_LOAD;
5413
5414 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5415
5416 for_each_possible_cpu(i) {
5417 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5418 GFP_KERNEL, cpu_to_node(i));
5419 if (!cfs_rq)
5420 goto err;
5421
5422 se = kzalloc_node(sizeof(struct sched_entity),
5423 GFP_KERNEL, cpu_to_node(i));
5424 if (!se)
5425 goto err_free_rq;
5426
5427 init_cfs_rq(cfs_rq);
5428 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5429 }
5430
5431 return 1;
5432
5433err_free_rq:
5434 kfree(cfs_rq);
5435err:
5436 return 0;
5437}
5438
5439void unregister_fair_sched_group(struct task_group *tg, int cpu)
5440{
5441 struct rq *rq = cpu_rq(cpu);
5442 unsigned long flags;
5443
5444 /*
5445 * Only empty task groups can be destroyed; so we can speculatively
5446 * check on_list without danger of it being re-added.
5447 */
5448 if (!tg->cfs_rq[cpu]->on_list)
5449 return;
5450
5451 raw_spin_lock_irqsave(&rq->lock, flags);
5452 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5453 raw_spin_unlock_irqrestore(&rq->lock, flags);
5454}
5455
5456void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5457 struct sched_entity *se, int cpu,
5458 struct sched_entity *parent)
5459{
5460 struct rq *rq = cpu_rq(cpu);
5461
5462 cfs_rq->tg = tg;
5463 cfs_rq->rq = rq;
5464#ifdef CONFIG_SMP
5465 /* allow initial update_cfs_load() to truncate */
5466 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005467#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005468 init_cfs_rq_runtime(cfs_rq);
5469
5470 tg->cfs_rq[cpu] = cfs_rq;
5471 tg->se[cpu] = se;
5472
5473 /* se could be NULL for root_task_group */
5474 if (!se)
5475 return;
5476
5477 if (!parent)
5478 se->cfs_rq = &rq->cfs;
5479 else
5480 se->cfs_rq = parent->my_q;
5481
5482 se->my_q = cfs_rq;
5483 update_load_set(&se->load, 0);
5484 se->parent = parent;
5485}
5486
5487static DEFINE_MUTEX(shares_mutex);
5488
5489int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5490{
5491 int i;
5492 unsigned long flags;
5493
5494 /*
5495 * We can't change the weight of the root cgroup.
5496 */
5497 if (!tg->se[0])
5498 return -EINVAL;
5499
5500 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5501
5502 mutex_lock(&shares_mutex);
5503 if (tg->shares == shares)
5504 goto done;
5505
5506 tg->shares = shares;
5507 for_each_possible_cpu(i) {
5508 struct rq *rq = cpu_rq(i);
5509 struct sched_entity *se;
5510
5511 se = tg->se[i];
5512 /* Propagate contribution to hierarchy */
5513 raw_spin_lock_irqsave(&rq->lock, flags);
5514 for_each_sched_entity(se)
5515 update_cfs_shares(group_cfs_rq(se));
5516 raw_spin_unlock_irqrestore(&rq->lock, flags);
5517 }
5518
5519done:
5520 mutex_unlock(&shares_mutex);
5521 return 0;
5522}
5523#else /* CONFIG_FAIR_GROUP_SCHED */
5524
5525void free_fair_sched_group(struct task_group *tg) { }
5526
5527int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5528{
5529 return 1;
5530}
5531
5532void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5533
5534#endif /* CONFIG_FAIR_GROUP_SCHED */
5535
Peter Zijlstra810b3812008-02-29 15:21:01 -05005536
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005537static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005538{
5539 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005540 unsigned int rr_interval = 0;
5541
5542 /*
5543 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5544 * idle runqueue:
5545 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005546 if (rq->cfs.load.weight)
Zhu Yanhai1836cd12013-01-08 12:56:52 +08005547 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005548
5549 return rr_interval;
5550}
5551
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005552/*
5553 * All the scheduling class methods:
5554 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005555const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005556 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005557 .enqueue_task = enqueue_task_fair,
5558 .dequeue_task = dequeue_task_fair,
5559 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005560 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005561
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005562 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005563
5564 .pick_next_task = pick_next_task_fair,
5565 .put_prev_task = put_prev_task_fair,
5566
Peter Williams681f3e62007-10-24 18:23:51 +02005567#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005568 .select_task_rq = select_task_rq_fair,
5569
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005570 .rq_online = rq_online_fair,
5571 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005572
5573 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005574#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005575
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005576 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005577 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005578 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005579
5580 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005581 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005582 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005583
Peter Williams0d721ce2009-09-21 01:31:53 +00005584 .get_rr_interval = get_rr_interval_fair,
5585
Peter Zijlstra810b3812008-02-29 15:21:01 -05005586#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005587 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005588#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005589};
5590
5591#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005592void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005593{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005594 struct cfs_rq *cfs_rq;
5595
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005596 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005597 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005598 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005599 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005600}
5601#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005602
5603__init void init_sched_fair_class(void)
5604{
5605#ifdef CONFIG_SMP
5606 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5607
5608#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005609 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005610 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005611 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005612#endif
5613#endif /* SMP */
5614
5615}