blob: 68f90861b23acb3fe11b2c5e9a7ace637e5b18a9 [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
Ben Segall16e74802013-10-16 11:16:12 -07001404void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001405{
Ben Segall16e74802013-10-16 11:16:12 -07001406 static_key_slow_inc(&__cfs_bandwidth_used);
1407}
1408
1409void cfs_bandwidth_usage_dec(void)
1410{
1411 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001412}
1413#else /* HAVE_JUMP_LABEL */
1414static bool cfs_bandwidth_used(void)
1415{
1416 return true;
1417}
1418
Ben Segall16e74802013-10-16 11:16:12 -07001419void cfs_bandwidth_usage_inc(void) {}
1420void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001421#endif /* HAVE_JUMP_LABEL */
1422
Paul Turnerab84d312011-07-21 09:43:28 -07001423/*
1424 * default period for cfs group bandwidth.
1425 * default: 0.1s, units: nanoseconds
1426 */
1427static inline u64 default_cfs_period(void)
1428{
1429 return 100000000ULL;
1430}
Paul Turnerec12cb72011-07-21 09:43:30 -07001431
1432static inline u64 sched_cfs_bandwidth_slice(void)
1433{
1434 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
1435}
1436
Paul Turnera9cf55b2011-07-21 09:43:32 -07001437/*
1438 * Replenish runtime according to assigned quota and update expiration time.
1439 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
1440 * additional synchronization around rq->lock.
1441 *
1442 * requires cfs_b->lock
1443 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001444void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07001445{
1446 u64 now;
1447
1448 if (cfs_b->quota == RUNTIME_INF)
1449 return;
1450
1451 now = sched_clock_cpu(smp_processor_id());
1452 cfs_b->runtime = cfs_b->quota;
1453 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
1454}
1455
Peter Zijlstra029632f2011-10-25 10:00:11 +02001456static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
1457{
1458 return &tg->cfs_bandwidth;
1459}
1460
Paul Turner85dac902011-07-21 09:43:33 -07001461/* returns 0 on failure to allocate runtime */
1462static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07001463{
1464 struct task_group *tg = cfs_rq->tg;
1465 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001466 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001467
1468 /* note: this is a positive sum as runtime_remaining <= 0 */
1469 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
1470
1471 raw_spin_lock(&cfs_b->lock);
1472 if (cfs_b->quota == RUNTIME_INF)
1473 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07001474 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07001475 /*
1476 * If the bandwidth pool has become inactive, then at least one
1477 * period must have elapsed since the last consumption.
1478 * Refresh the global state and ensure bandwidth timer becomes
1479 * active.
1480 */
1481 if (!cfs_b->timer_active) {
1482 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07001483 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07001484 }
Paul Turner58088ad2011-07-21 09:43:31 -07001485
1486 if (cfs_b->runtime > 0) {
1487 amount = min(cfs_b->runtime, min_amount);
1488 cfs_b->runtime -= amount;
1489 cfs_b->idle = 0;
1490 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001491 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07001492 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07001493 raw_spin_unlock(&cfs_b->lock);
1494
1495 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001496 /*
1497 * we may have advanced our local expiration to account for allowed
1498 * spread between our sched_clock and the one on which runtime was
1499 * issued.
1500 */
1501 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
1502 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07001503
1504 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001505}
1506
1507/*
1508 * Note: This depends on the synchronization provided by sched_clock and the
1509 * fact that rq->clock snapshots this value.
1510 */
1511static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1512{
1513 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1514 struct rq *rq = rq_of(cfs_rq);
1515
1516 /* if the deadline is ahead of our clock, nothing to do */
1517 if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
1518 return;
1519
1520 if (cfs_rq->runtime_remaining < 0)
1521 return;
1522
1523 /*
1524 * If the local deadline has passed we have to consider the
1525 * possibility that our sched_clock is 'fast' and the global deadline
1526 * has not truly expired.
1527 *
1528 * Fortunately we can check determine whether this the case by checking
1529 * whether the global deadline has advanced.
1530 */
1531
1532 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
1533 /* extend local deadline, drift is bounded above by 2 ticks */
1534 cfs_rq->runtime_expires += TICK_NSEC;
1535 } else {
1536 /* global deadline is ahead, expiration has passed */
1537 cfs_rq->runtime_remaining = 0;
1538 }
Paul Turnerec12cb72011-07-21 09:43:30 -07001539}
1540
1541static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
1542 unsigned long delta_exec)
1543{
Paul Turnera9cf55b2011-07-21 09:43:32 -07001544 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07001545 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07001546 expire_cfs_rq_runtime(cfs_rq);
1547
1548 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07001549 return;
1550
Paul Turner85dac902011-07-21 09:43:33 -07001551 /*
1552 * if we're unable to extend our runtime we resched so that the active
1553 * hierarchy can be throttled
1554 */
1555 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
1556 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07001557}
1558
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07001559static __always_inline
1560void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07001561{
Paul Turner56f570e2011-11-07 20:26:33 -08001562 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07001563 return;
1564
1565 __account_cfs_rq_runtime(cfs_rq, delta_exec);
1566}
1567
Paul Turner85dac902011-07-21 09:43:33 -07001568static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
1569{
Paul Turner56f570e2011-11-07 20:26:33 -08001570 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07001571}
1572
Paul Turner64660c82011-07-21 09:43:36 -07001573/* check whether cfs_rq, or any parent, is throttled */
1574static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
1575{
Paul Turner56f570e2011-11-07 20:26:33 -08001576 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07001577}
1578
1579/*
1580 * Ensure that neither of the group entities corresponding to src_cpu or
1581 * dest_cpu are members of a throttled hierarchy when performing group
1582 * load-balance operations.
1583 */
1584static inline int throttled_lb_pair(struct task_group *tg,
1585 int src_cpu, int dest_cpu)
1586{
1587 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
1588
1589 src_cfs_rq = tg->cfs_rq[src_cpu];
1590 dest_cfs_rq = tg->cfs_rq[dest_cpu];
1591
1592 return throttled_hierarchy(src_cfs_rq) ||
1593 throttled_hierarchy(dest_cfs_rq);
1594}
1595
1596/* updated child weight may affect parent so we have to do this bottom up */
1597static int tg_unthrottle_up(struct task_group *tg, void *data)
1598{
1599 struct rq *rq = data;
1600 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1601
1602 cfs_rq->throttle_count--;
1603#ifdef CONFIG_SMP
1604 if (!cfs_rq->throttle_count) {
1605 u64 delta = rq->clock_task - cfs_rq->load_stamp;
1606
1607 /* leaving throttled state, advance shares averaging windows */
1608 cfs_rq->load_stamp += delta;
1609 cfs_rq->load_last += delta;
1610
1611 /* update entity weight now that we are on_rq again */
1612 update_cfs_shares(cfs_rq);
1613 }
1614#endif
1615
1616 return 0;
1617}
1618
1619static int tg_throttle_down(struct task_group *tg, void *data)
1620{
1621 struct rq *rq = data;
1622 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
1623
1624 /* group is entering throttled state, record last load */
1625 if (!cfs_rq->throttle_count)
1626 update_cfs_load(cfs_rq, 0);
1627 cfs_rq->throttle_count++;
1628
1629 return 0;
1630}
1631
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001632static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07001633{
1634 struct rq *rq = rq_of(cfs_rq);
1635 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1636 struct sched_entity *se;
1637 long task_delta, dequeue = 1;
1638
1639 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1640
1641 /* account load preceding throttle */
Paul Turner64660c82011-07-21 09:43:36 -07001642 rcu_read_lock();
1643 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
1644 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07001645
1646 task_delta = cfs_rq->h_nr_running;
1647 for_each_sched_entity(se) {
1648 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
1649 /* throttled entity or throttle-on-deactivate */
1650 if (!se->on_rq)
1651 break;
1652
1653 if (dequeue)
1654 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
1655 qcfs_rq->h_nr_running -= task_delta;
1656
1657 if (qcfs_rq->load.weight)
1658 dequeue = 0;
1659 }
1660
1661 if (!se)
1662 rq->nr_running -= task_delta;
1663
1664 cfs_rq->throttled = 1;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001665 cfs_rq->throttled_timestamp = rq->clock;
Paul Turner85dac902011-07-21 09:43:33 -07001666 raw_spin_lock(&cfs_b->lock);
1667 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Ben Segalldfb473b2013-10-16 11:16:32 -07001668 if (!cfs_b->timer_active)
1669 __start_cfs_bandwidth(cfs_b);
Paul Turner85dac902011-07-21 09:43:33 -07001670 raw_spin_unlock(&cfs_b->lock);
1671}
1672
Peter Zijlstra029632f2011-10-25 10:00:11 +02001673void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07001674{
1675 struct rq *rq = rq_of(cfs_rq);
1676 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1677 struct sched_entity *se;
1678 int enqueue = 1;
1679 long task_delta;
1680
1681 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
1682
1683 cfs_rq->throttled = 0;
1684 raw_spin_lock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001685 cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp;
Paul Turner671fd9d2011-07-21 09:43:34 -07001686 list_del_rcu(&cfs_rq->throttled_list);
1687 raw_spin_unlock(&cfs_b->lock);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001688 cfs_rq->throttled_timestamp = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07001689
Paul Turner64660c82011-07-21 09:43:36 -07001690 update_rq_clock(rq);
1691 /* update hierarchical throttle state */
1692 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
1693
Paul Turner671fd9d2011-07-21 09:43:34 -07001694 if (!cfs_rq->load.weight)
1695 return;
1696
1697 task_delta = cfs_rq->h_nr_running;
1698 for_each_sched_entity(se) {
1699 if (se->on_rq)
1700 enqueue = 0;
1701
1702 cfs_rq = cfs_rq_of(se);
1703 if (enqueue)
1704 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
1705 cfs_rq->h_nr_running += task_delta;
1706
1707 if (cfs_rq_throttled(cfs_rq))
1708 break;
1709 }
1710
1711 if (!se)
1712 rq->nr_running += task_delta;
1713
1714 /* determine whether we need to wake up potentially idle cpu */
1715 if (rq->curr == rq->idle && rq->cfs.nr_running)
1716 resched_task(rq->curr);
1717}
1718
1719static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
1720 u64 remaining, u64 expires)
1721{
1722 struct cfs_rq *cfs_rq;
1723 u64 runtime = remaining;
1724
1725 rcu_read_lock();
1726 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
1727 throttled_list) {
1728 struct rq *rq = rq_of(cfs_rq);
1729
1730 raw_spin_lock(&rq->lock);
1731 if (!cfs_rq_throttled(cfs_rq))
1732 goto next;
1733
1734 runtime = -cfs_rq->runtime_remaining + 1;
1735 if (runtime > remaining)
1736 runtime = remaining;
1737 remaining -= runtime;
1738
1739 cfs_rq->runtime_remaining += runtime;
1740 cfs_rq->runtime_expires = expires;
1741
1742 /* we check whether we're throttled above */
1743 if (cfs_rq->runtime_remaining > 0)
1744 unthrottle_cfs_rq(cfs_rq);
1745
1746next:
1747 raw_spin_unlock(&rq->lock);
1748
1749 if (!remaining)
1750 break;
1751 }
1752 rcu_read_unlock();
1753
1754 return remaining;
1755}
1756
Paul Turner58088ad2011-07-21 09:43:31 -07001757/*
1758 * Responsible for refilling a task_group's bandwidth and unthrottling its
1759 * cfs_rqs as appropriate. If there has been no activity within the last
1760 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
1761 * used to track this state.
1762 */
1763static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
1764{
Paul Turner671fd9d2011-07-21 09:43:34 -07001765 u64 runtime, runtime_expires;
1766 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07001767
1768 raw_spin_lock(&cfs_b->lock);
1769 /* no need to continue the timer with no bandwidth constraint */
1770 if (cfs_b->quota == RUNTIME_INF)
1771 goto out_unlock;
1772
Paul Turner671fd9d2011-07-21 09:43:34 -07001773 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1774 /* idle depends on !throttled (for the case of a large deficit) */
1775 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001776 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07001777
Paul Turnera9cf55b2011-07-21 09:43:32 -07001778 /* if we're going inactive then everything else can be deferred */
1779 if (idle)
1780 goto out_unlock;
1781
Ben Segall03d35a32013-10-16 11:16:22 -07001782 /*
1783 * if we have relooped after returning idle once, we need to update our
1784 * status as actually running, so that other cpus doing
1785 * __start_cfs_bandwidth will stop trying to cancel us.
1786 */
1787 cfs_b->timer_active = 1;
1788
Paul Turnera9cf55b2011-07-21 09:43:32 -07001789 __refill_cfs_bandwidth_runtime(cfs_b);
1790
Paul Turner671fd9d2011-07-21 09:43:34 -07001791 if (!throttled) {
1792 /* mark as potentially idle for the upcoming period */
1793 cfs_b->idle = 1;
1794 goto out_unlock;
1795 }
Paul Turner58088ad2011-07-21 09:43:31 -07001796
Nikhil Raoe8da1b12011-07-21 09:43:40 -07001797 /* account preceding periods in which throttling occurred */
1798 cfs_b->nr_throttled += overrun;
1799
Paul Turner671fd9d2011-07-21 09:43:34 -07001800 /*
1801 * There are throttled entities so we must first use the new bandwidth
1802 * to unthrottle them before making it generally available. This
1803 * ensures that all existing debts will be paid before a new cfs_rq is
1804 * allowed to run.
1805 */
1806 runtime = cfs_b->runtime;
1807 runtime_expires = cfs_b->runtime_expires;
1808 cfs_b->runtime = 0;
1809
1810 /*
1811 * This check is repeated as we are holding onto the new bandwidth
1812 * while we unthrottle. This can potentially race with an unthrottled
1813 * group trying to acquire new bandwidth from the global pool.
1814 */
1815 while (throttled && runtime > 0) {
1816 raw_spin_unlock(&cfs_b->lock);
1817 /* we can't nest cfs_b->lock while distributing bandwidth */
1818 runtime = distribute_cfs_runtime(cfs_b, runtime,
1819 runtime_expires);
1820 raw_spin_lock(&cfs_b->lock);
1821
1822 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
1823 }
1824
1825 /* return (any) remaining runtime */
1826 cfs_b->runtime = runtime;
1827 /*
1828 * While we are ensured activity in the period following an
1829 * unthrottle, this also covers the case in which the new bandwidth is
1830 * insufficient to cover the existing bandwidth deficit. (Forcing the
1831 * timer to remain active while there are any throttled entities.)
1832 */
1833 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07001834out_unlock:
1835 if (idle)
1836 cfs_b->timer_active = 0;
1837 raw_spin_unlock(&cfs_b->lock);
1838
1839 return idle;
1840}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001841
Paul Turnerd8b49862011-07-21 09:43:41 -07001842/* a cfs_rq won't donate quota below this amount */
1843static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
1844/* minimum remaining period time to redistribute slack quota */
1845static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
1846/* how long we wait to gather additional slack before distributing */
1847static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
1848
Ben Segall9b318052013-10-16 11:16:17 -07001849/*
1850 * Are we near the end of the current quota period?
1851 *
1852 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
1853 * hrtimer base being cleared by __hrtimer_start_range_ns. In the case of
1854 * migrate_hrtimers, base is never cleared, so we are fine.
1855 */
Paul Turnerd8b49862011-07-21 09:43:41 -07001856static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
1857{
1858 struct hrtimer *refresh_timer = &cfs_b->period_timer;
1859 u64 remaining;
1860
1861 /* if the call-back is running a quota refresh is already occurring */
1862 if (hrtimer_callback_running(refresh_timer))
1863 return 1;
1864
1865 /* is a quota refresh about to occur? */
1866 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
1867 if (remaining < min_expire)
1868 return 1;
1869
1870 return 0;
1871}
1872
1873static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
1874{
1875 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
1876
1877 /* if there's a quota refresh soon don't bother with slack */
1878 if (runtime_refresh_within(cfs_b, min_left))
1879 return;
1880
1881 start_bandwidth_timer(&cfs_b->slack_timer,
1882 ns_to_ktime(cfs_bandwidth_slack_period));
1883}
1884
1885/* we know any runtime found here is valid as update_curr() precedes return */
1886static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1887{
1888 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
1889 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
1890
1891 if (slack_runtime <= 0)
1892 return;
1893
1894 raw_spin_lock(&cfs_b->lock);
1895 if (cfs_b->quota != RUNTIME_INF &&
1896 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
1897 cfs_b->runtime += slack_runtime;
1898
1899 /* we are under rq->lock, defer unthrottling using a timer */
1900 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
1901 !list_empty(&cfs_b->throttled_cfs_rq))
1902 start_cfs_slack_bandwidth(cfs_b);
1903 }
1904 raw_spin_unlock(&cfs_b->lock);
1905
1906 /* even if it's not valid for return we don't want to try again */
1907 cfs_rq->runtime_remaining -= slack_runtime;
1908}
1909
1910static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1911{
Paul Turner56f570e2011-11-07 20:26:33 -08001912 if (!cfs_bandwidth_used())
1913 return;
1914
Paul Turnerfccfdc62011-11-07 20:26:34 -08001915 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07001916 return;
1917
1918 __return_cfs_rq_runtime(cfs_rq);
1919}
1920
1921/*
1922 * This is done with a timer (instead of inline with bandwidth return) since
1923 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
1924 */
1925static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
1926{
1927 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
1928 u64 expires;
1929
1930 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07001931 raw_spin_lock(&cfs_b->lock);
Ben Segall9b318052013-10-16 11:16:17 -07001932 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
1933 raw_spin_unlock(&cfs_b->lock);
1934 return;
1935 }
1936
Paul Turnerd8b49862011-07-21 09:43:41 -07001937 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
1938 runtime = cfs_b->runtime;
1939 cfs_b->runtime = 0;
1940 }
1941 expires = cfs_b->runtime_expires;
1942 raw_spin_unlock(&cfs_b->lock);
1943
1944 if (!runtime)
1945 return;
1946
1947 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
1948
1949 raw_spin_lock(&cfs_b->lock);
1950 if (expires == cfs_b->runtime_expires)
1951 cfs_b->runtime = runtime;
1952 raw_spin_unlock(&cfs_b->lock);
1953}
1954
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001955/*
1956 * When a group wakes up we want to make sure that its quota is not already
1957 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
1958 * runtime as update_curr() throttling can not not trigger until it's on-rq.
1959 */
1960static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
1961{
Paul Turner56f570e2011-11-07 20:26:33 -08001962 if (!cfs_bandwidth_used())
1963 return;
1964
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001965 /* an active group must be handled by the update_curr()->put() path */
1966 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
1967 return;
1968
1969 /* ensure the group is not already throttled */
1970 if (cfs_rq_throttled(cfs_rq))
1971 return;
1972
1973 /* update runtime allocation */
1974 account_cfs_rq_runtime(cfs_rq, 0);
1975 if (cfs_rq->runtime_remaining <= 0)
1976 throttle_cfs_rq(cfs_rq);
1977}
1978
1979/* conditionally throttle active cfs_rq's from put_prev_entity() */
1980static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
1981{
Paul Turner56f570e2011-11-07 20:26:33 -08001982 if (!cfs_bandwidth_used())
1983 return;
1984
Paul Turnerd3d9dc32011-07-21 09:43:39 -07001985 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
1986 return;
1987
1988 /*
1989 * it's possible for a throttled entity to be forced into a running
1990 * state (e.g. set_curr_task), in this case we're finished.
1991 */
1992 if (cfs_rq_throttled(cfs_rq))
1993 return;
1994
1995 throttle_cfs_rq(cfs_rq);
1996}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001997
1998static inline u64 default_cfs_period(void);
1999static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
2000static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
2001
2002static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
2003{
2004 struct cfs_bandwidth *cfs_b =
2005 container_of(timer, struct cfs_bandwidth, slack_timer);
2006 do_sched_cfs_slack_timer(cfs_b);
2007
2008 return HRTIMER_NORESTART;
2009}
2010
2011static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
2012{
2013 struct cfs_bandwidth *cfs_b =
2014 container_of(timer, struct cfs_bandwidth, period_timer);
2015 ktime_t now;
2016 int overrun;
2017 int idle = 0;
2018
2019 for (;;) {
2020 now = hrtimer_cb_get_time(timer);
2021 overrun = hrtimer_forward(timer, now, cfs_b->period);
2022
2023 if (!overrun)
2024 break;
2025
2026 idle = do_sched_cfs_period_timer(cfs_b, overrun);
2027 }
2028
2029 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
2030}
2031
2032void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2033{
2034 raw_spin_lock_init(&cfs_b->lock);
2035 cfs_b->runtime = 0;
2036 cfs_b->quota = RUNTIME_INF;
2037 cfs_b->period = ns_to_ktime(default_cfs_period());
2038
2039 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
2040 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2041 cfs_b->period_timer.function = sched_cfs_period_timer;
2042 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2043 cfs_b->slack_timer.function = sched_cfs_slack_timer;
2044}
2045
2046static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2047{
2048 cfs_rq->runtime_enabled = 0;
2049 INIT_LIST_HEAD(&cfs_rq->throttled_list);
2050}
2051
2052/* requires cfs_b->lock, may release to reprogram timer */
2053void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2054{
2055 /*
2056 * The timer may be active because we're trying to set a new bandwidth
2057 * period or because we're racing with the tear-down path
2058 * (timer_active==0 becomes visible before the hrtimer call-back
2059 * terminates). In either case we ensure that it's re-programmed
2060 */
Ben Segall03d35a32013-10-16 11:16:22 -07002061 while (unlikely(hrtimer_active(&cfs_b->period_timer)) &&
2062 hrtimer_try_to_cancel(&cfs_b->period_timer) < 0) {
2063 /* bounce the lock to allow do_sched_cfs_period_timer to run */
Peter Zijlstra029632f2011-10-25 10:00:11 +02002064 raw_spin_unlock(&cfs_b->lock);
Ben Segall03d35a32013-10-16 11:16:22 -07002065 cpu_relax();
Peter Zijlstra029632f2011-10-25 10:00:11 +02002066 raw_spin_lock(&cfs_b->lock);
2067 /* if someone else restarted the timer then we're done */
2068 if (cfs_b->timer_active)
2069 return;
2070 }
2071
2072 cfs_b->timer_active = 1;
2073 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
2074}
2075
2076static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
2077{
2078 hrtimer_cancel(&cfs_b->period_timer);
2079 hrtimer_cancel(&cfs_b->slack_timer);
2080}
2081
Peter Boonstoppele38ca492012-08-09 15:34:47 -07002082static void unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002083{
2084 struct cfs_rq *cfs_rq;
2085
2086 for_each_leaf_cfs_rq(rq, cfs_rq) {
2087 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
2088
2089 if (!cfs_rq->runtime_enabled)
2090 continue;
2091
2092 /*
2093 * clock_task is not advancing so we just need to make sure
2094 * there's some valid quota amount
2095 */
2096 cfs_rq->runtime_remaining = cfs_b->quota;
2097 if (cfs_rq_throttled(cfs_rq))
2098 unthrottle_cfs_rq(cfs_rq);
2099 }
2100}
2101
2102#else /* CONFIG_CFS_BANDWIDTH */
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002103static __always_inline
2104void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002105static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
2106static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002107static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07002108
2109static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
2110{
2111 return 0;
2112}
Paul Turner64660c82011-07-21 09:43:36 -07002113
2114static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
2115{
2116 return 0;
2117}
2118
2119static inline int throttled_lb_pair(struct task_group *tg,
2120 int src_cpu, int dest_cpu)
2121{
2122 return 0;
2123}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002124
2125void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
2126
2127#ifdef CONFIG_FAIR_GROUP_SCHED
2128static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07002129#endif
2130
Peter Zijlstra029632f2011-10-25 10:00:11 +02002131static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2132{
2133 return NULL;
2134}
2135static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppele38ca492012-08-09 15:34:47 -07002136static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002137
2138#endif /* CONFIG_CFS_BANDWIDTH */
2139
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002140/**************************************************
2141 * CFS operations on tasks:
2142 */
2143
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002144#ifdef CONFIG_SCHED_HRTICK
2145static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
2146{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002147 struct sched_entity *se = &p->se;
Srivatsa Vaddagirie8c21d12013-05-13 15:11:55 -07002148 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002149
2150 WARN_ON(task_rq(p) != rq);
2151
Srivatsa Vaddagirie8c21d12013-05-13 15:11:55 -07002152 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002153 u64 slice = sched_slice(cfs_rq, se);
2154 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
2155 s64 delta = slice - ran;
2156
2157 if (delta < 0) {
2158 if (rq->curr == p)
2159 resched_task(p);
2160 return;
2161 }
2162
2163 /*
2164 * Don't schedule slices shorter than 10000ns, that just
2165 * doesn't make sense. Rely on vruntime for fairness.
2166 */
Peter Zijlstra31656512008-07-18 18:01:23 +02002167 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02002168 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002169
Peter Zijlstra31656512008-07-18 18:01:23 +02002170 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002171 }
2172}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002173
2174/*
2175 * called from enqueue/dequeue and updates the hrtick when the
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002176 * current task is from our class.
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002177 */
2178static void hrtick_update(struct rq *rq)
2179{
2180 struct task_struct *curr = rq->curr;
2181
Mike Galbraithb39e66e2011-11-22 15:20:07 +01002182 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002183 return;
2184
Srivatsa Vaddagirif12acce2013-04-18 11:42:22 -07002185 hrtick_start_fair(rq, curr);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002186}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302187#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002188static inline void
2189hrtick_start_fair(struct rq *rq, struct task_struct *p)
2190{
2191}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002192
2193static inline void hrtick_update(struct rq *rq)
2194{
2195}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002196#endif
2197
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002198/*
2199 * The enqueue_task method is called before nr_running is
2200 * increased. Here we update the fair scheduling stats and
2201 * then put the task into the rbtree:
2202 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00002203static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002204enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002205{
2206 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002207 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002208
2209 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002210 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002211 break;
2212 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002213 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002214
2215 /*
2216 * end evaluation on encountering a throttled cfs_rq
2217 *
2218 * note: in the case of encountering a throttled cfs_rq we will
2219 * post the final h_nr_running increment below.
2220 */
2221 if (cfs_rq_throttled(cfs_rq))
2222 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002223 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07002224
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002225 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002226 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002227
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002228 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002229 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002230 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002231
Paul Turner85dac902011-07-21 09:43:33 -07002232 if (cfs_rq_throttled(cfs_rq))
2233 break;
2234
Paul Turnerd6b55912010-11-15 15:47:09 -08002235 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002236 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002237 }
2238
Paul Turner85dac902011-07-21 09:43:33 -07002239 if (!se)
2240 inc_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002241 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002242}
2243
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002244static void set_next_buddy(struct sched_entity *se);
2245
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002246/*
2247 * The dequeue_task method is called before nr_running is
2248 * decreased. We remove the task from the rbtree and
2249 * update the fair scheduling stats:
2250 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002251static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002252{
2253 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002254 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002255 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002256
2257 for_each_sched_entity(se) {
2258 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002259 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07002260
2261 /*
2262 * end evaluation on encountering a throttled cfs_rq
2263 *
2264 * note: in the case of encountering a throttled cfs_rq we will
2265 * post the final h_nr_running decrement below.
2266 */
2267 if (cfs_rq_throttled(cfs_rq))
2268 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07002269 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002270
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002271 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002272 if (cfs_rq->load.weight) {
2273 /*
2274 * Bias pick_next to pick a task from this cfs_rq, as
2275 * p is sleeping when it is within its sched_slice.
2276 */
2277 if (task_sleep && parent_entity(se))
2278 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07002279
2280 /* avoid re-evaluating load for this entity */
2281 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002282 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002283 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002284 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002285 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002286
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002287 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08002288 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07002289 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002290
Paul Turner85dac902011-07-21 09:43:33 -07002291 if (cfs_rq_throttled(cfs_rq))
2292 break;
2293
Paul Turnerd6b55912010-11-15 15:47:09 -08002294 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08002295 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002296 }
2297
Paul Turner85dac902011-07-21 09:43:33 -07002298 if (!se)
2299 dec_nr_running(rq);
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02002300 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002301}
2302
Gregory Haskinse7693a32008-01-25 21:08:09 +01002303#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02002304/* Used instead of source_load when we know the type == 0 */
2305static unsigned long weighted_cpuload(const int cpu)
2306{
2307 return cpu_rq(cpu)->load.weight;
2308}
2309
2310/*
2311 * Return a low guess at the load of a migration-source cpu weighted
2312 * according to the scheduling class and "nice" value.
2313 *
2314 * We want to under-estimate the load of migration sources, to
2315 * balance conservatively.
2316 */
2317static unsigned long source_load(int cpu, int type)
2318{
2319 struct rq *rq = cpu_rq(cpu);
2320 unsigned long total = weighted_cpuload(cpu);
2321
2322 if (type == 0 || !sched_feat(LB_BIAS))
2323 return total;
2324
2325 return min(rq->cpu_load[type-1], total);
2326}
2327
2328/*
2329 * Return a high guess at the load of a migration-target cpu weighted
2330 * according to the scheduling class and "nice" value.
2331 */
2332static unsigned long target_load(int cpu, int type)
2333{
2334 struct rq *rq = cpu_rq(cpu);
2335 unsigned long total = weighted_cpuload(cpu);
2336
2337 if (type == 0 || !sched_feat(LB_BIAS))
2338 return total;
2339
2340 return max(rq->cpu_load[type-1], total);
2341}
2342
2343static unsigned long power_of(int cpu)
2344{
2345 return cpu_rq(cpu)->cpu_power;
2346}
2347
2348static unsigned long cpu_avg_load_per_task(int cpu)
2349{
2350 struct rq *rq = cpu_rq(cpu);
2351 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
2352
2353 if (nr_running)
2354 return rq->load.weight / nr_running;
2355
2356 return 0;
2357}
2358
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002359
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002360static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002361{
2362 struct sched_entity *se = &p->se;
2363 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002364 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002365
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002366#ifndef CONFIG_64BIT
2367 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002368
Peter Zijlstra3fe16982011-04-05 17:23:48 +02002369 do {
2370 min_vruntime_copy = cfs_rq->min_vruntime_copy;
2371 smp_rmb();
2372 min_vruntime = cfs_rq->min_vruntime;
2373 } while (min_vruntime != min_vruntime_copy);
2374#else
2375 min_vruntime = cfs_rq->min_vruntime;
2376#endif
2377
2378 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002379}
2380
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002381#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002382/*
2383 * effective_load() calculates the load change as seen from the root_task_group
2384 *
2385 * Adding load to a group doesn't make a group heavier, but can cause movement
2386 * of group shares between cpus. Assuming the shares were perfectly aligned one
2387 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002388 *
2389 * Calculate the effective load difference if @wl is added (subtracted) to @tg
2390 * on this @cpu and results in a total addition (subtraction) of @wg to the
2391 * total group weight.
2392 *
2393 * Given a runqueue weight distribution (rw_i) we can compute a shares
2394 * distribution (s_i) using:
2395 *
2396 * s_i = rw_i / \Sum rw_j (1)
2397 *
2398 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
2399 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
2400 * shares distribution (s_i):
2401 *
2402 * rw_i = { 2, 4, 1, 0 }
2403 * s_i = { 2/7, 4/7, 1/7, 0 }
2404 *
2405 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
2406 * task used to run on and the CPU the waker is running on), we need to
2407 * compute the effect of waking a task on either CPU and, in case of a sync
2408 * wakeup, compute the effect of the current task going to sleep.
2409 *
2410 * So for a change of @wl to the local @cpu with an overall group weight change
2411 * of @wl we can compute the new shares distribution (s'_i) using:
2412 *
2413 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
2414 *
2415 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
2416 * differences in waking a task to CPU 0. The additional task changes the
2417 * weight and shares distributions like:
2418 *
2419 * rw'_i = { 3, 4, 1, 0 }
2420 * s'_i = { 3/8, 4/8, 1/8, 0 }
2421 *
2422 * We can then compute the difference in effective weight by using:
2423 *
2424 * dw_i = S * (s'_i - s_i) (3)
2425 *
2426 * Where 'S' is the group weight as seen by its parent.
2427 *
2428 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
2429 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
2430 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02002431 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002432static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002433{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002434 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002435
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002436 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02002437 return wl;
2438
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002439 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002440 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002441
Paul Turner977dda72011-01-14 17:57:50 -08002442 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002443
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002444 /*
2445 * W = @wg + \Sum rw_j
2446 */
2447 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002448
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002449 /*
2450 * w = rw_i + @wl
2451 */
2452 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002453
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002454 /*
2455 * wl = S * s'_i; see (2)
2456 */
2457 if (W > 0 && w < W)
2458 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08002459 else
2460 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02002461
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002462 /*
2463 * Per the above, wl is the new se->load.weight value; since
2464 * those are clipped to [MIN_SHARES, ...) do so now. See
2465 * calc_cfs_shares().
2466 */
Paul Turner977dda72011-01-14 17:57:50 -08002467 if (wl < MIN_SHARES)
2468 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002469
2470 /*
2471 * wl = dw_i = S * (s'_i - s_i); see (3)
2472 */
Paul Turner977dda72011-01-14 17:57:50 -08002473 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002474
2475 /*
2476 * Recursively apply this logic to all parent groups to compute
2477 * the final effective load change on the root group. Since
2478 * only the @tg group gets extra weight, all parent groups can
2479 * only redistribute existing shares. @wl is the shift in shares
2480 * resulting from this level per the above.
2481 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002482 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002483 }
2484
2485 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002486}
2487#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002488
Peter Zijlstra83378262008-06-27 13:41:37 +02002489static inline unsigned long effective_load(struct task_group *tg, int cpu,
2490 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002491{
Peter Zijlstra83378262008-06-27 13:41:37 +02002492 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002493}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02002494
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02002495#endif
2496
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002497static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002498{
Paul Turnere37b6a72011-01-21 20:44:59 -08002499 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002500 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002501 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002502 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02002503 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002504 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002505
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002506 idx = sd->wake_idx;
2507 this_cpu = smp_processor_id();
2508 prev_cpu = task_cpu(p);
2509 load = source_load(prev_cpu, idx);
2510 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002511
2512 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002513 * If sync wakeup then subtract the (maximum possible)
2514 * effect of the currently running task from the load
2515 * of the current CPU:
2516 */
Peter Zijlstra83378262008-06-27 13:41:37 +02002517 if (sync) {
2518 tg = task_group(current);
2519 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002520
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002521 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02002522 load += effective_load(tg, prev_cpu, 0, -weight);
2523 }
2524
2525 tg = task_group(p);
2526 weight = p->se.load.weight;
2527
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002528 /*
2529 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002530 * due to the sync cause above having dropped this_load to 0, we'll
2531 * always have an imbalance, but there's really nothing you can do
2532 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02002533 *
2534 * Otherwise check if either cpus are near enough in load to allow this
2535 * task to be woken on this_cpu.
2536 */
Paul Turnere37b6a72011-01-21 20:44:59 -08002537 if (this_load > 0) {
2538 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002539
2540 this_eff_load = 100;
2541 this_eff_load *= power_of(prev_cpu);
2542 this_eff_load *= this_load +
2543 effective_load(tg, this_cpu, weight, weight);
2544
2545 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
2546 prev_eff_load *= power_of(this_cpu);
2547 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
2548
2549 balanced = this_eff_load <= prev_eff_load;
2550 } else
2551 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002552
2553 /*
2554 * If the currently running task will sleep within
2555 * a reasonable amount of time then attract this newly
2556 * woken task:
2557 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02002558 if (sync && balanced)
2559 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002560
Lucas De Marchi41acab82010-03-10 23:37:45 -03002561 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02002562 tl_per_task = cpu_avg_load_per_task(this_cpu);
2563
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002564 if (balanced ||
2565 (this_load <= load &&
2566 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002567 /*
2568 * This domain has SD_WAKE_AFFINE and
2569 * p is cache cold in this domain, and
2570 * there is no bad imbalance.
2571 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002572 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002573 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01002574
2575 return 1;
2576 }
2577 return 0;
2578}
2579
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002580/*
2581 * find_idlest_group finds and returns the least busy CPU group within the
2582 * domain.
2583 */
2584static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02002585find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002586 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01002587{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07002588 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002589 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002590 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002591
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002592 do {
2593 unsigned long load, avg_load;
2594 int local_group;
2595 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002596
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002597 /* Skip over this group if it has no CPUs allowed */
2598 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002599 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002600 continue;
2601
2602 local_group = cpumask_test_cpu(this_cpu,
2603 sched_group_cpus(group));
2604
2605 /* Tally up the load of all CPUs in the group */
2606 avg_load = 0;
2607
2608 for_each_cpu(i, sched_group_cpus(group)) {
2609 /* Bias balancing toward cpus of our domain */
2610 if (local_group)
2611 load = source_load(i, load_idx);
2612 else
2613 load = target_load(i, load_idx);
2614
2615 avg_load += load;
2616 }
2617
2618 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002619 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002620
2621 if (local_group) {
2622 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002623 } else if (avg_load < min_load) {
2624 min_load = avg_load;
2625 idlest = group;
2626 }
2627 } while (group = group->next, group != sd->groups);
2628
2629 if (!idlest || 100*this_load < imbalance*min_load)
2630 return NULL;
2631 return idlest;
2632}
2633
2634/*
2635 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2636 */
2637static int
2638find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2639{
2640 unsigned long load, min_load = ULONG_MAX;
2641 int idlest = -1;
2642 int i;
2643
2644 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002645 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002646 load = weighted_cpuload(i);
2647
2648 if (load < min_load || (load == min_load && i == this_cpu)) {
2649 min_load = load;
2650 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002651 }
2652 }
2653
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002654 return idlest;
2655}
Gregory Haskinse7693a32008-01-25 21:08:09 +01002656
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002657/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002658 * Try and locate an idle CPU in the sched_domain.
2659 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002660static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002661{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002662 struct sched_domain *sd;
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002663 struct sched_group *sg;
Mike Galbraith450e8d52013-01-28 12:19:25 +01002664 int i = task_cpu(p);
2665
2666 if (idle_cpu(target))
2667 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002668
2669 /*
Mike Galbraith450e8d52013-01-28 12:19:25 +01002670 * If the prevous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002671 */
Mike Galbraith450e8d52013-01-28 12:19:25 +01002672 if (i != target && cpus_share_cache(i, target) && idle_cpu(i))
2673 return i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002674
Steve Muckled448aba2012-10-24 15:00:20 -07002675 if (!sysctl_sched_wake_to_idle &&
2676 !(current->flags & PF_WAKE_UP_IDLE) &&
Steve Muckleff96cd22012-09-10 11:45:02 -07002677 !(p->flags & PF_WAKE_UP_IDLE))
2678 return target;
2679
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002680 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002681 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002682 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01002683 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08002684 for_each_lower_domain(sd) {
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002685 sg = sd->groups;
2686 do {
2687 if (!cpumask_intersects(sched_group_cpus(sg),
2688 tsk_cpus_allowed(p)))
2689 goto next;
2690
2691 for_each_cpu(i, sched_group_cpus(sg)) {
Mike Galbraith450e8d52013-01-28 12:19:25 +01002692 if (i == target || !idle_cpu(i))
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002693 goto next;
2694 }
2695
2696 target = cpumask_first_and(sched_group_cpus(sg),
2697 tsk_cpus_allowed(p));
2698 goto done;
2699next:
2700 sg = sg->next;
2701 } while (sg != sd->groups);
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002702 }
Peter Zijlstra4dcfe1022011-11-10 13:01:10 +01002703done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01002704 return target;
2705}
2706
2707/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002708 * sched_balance_self: balance the current task (running on cpu) in domains
2709 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2710 * SD_BALANCE_EXEC.
2711 *
2712 * Balance, ie. select the least loaded group.
2713 *
2714 * Returns the target CPU number, or the same CPU if no balancing is needed.
2715 *
2716 * preempt must be disabled.
2717 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002718static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002719select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002720{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002721 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002722 int cpu = smp_processor_id();
2723 int prev_cpu = task_cpu(p);
2724 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002725 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002726 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002727 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002728
Mike Galbraith76854c72011-11-22 15:18:24 +01002729 if (p->rt.nr_cpus_allowed == 1)
2730 return prev_cpu;
2731
Peter Zijlstra0763a662009-09-14 19:37:39 +02002732 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02002733 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002734 want_affine = 1;
2735 new_cpu = prev_cpu;
2736 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002737
Peter Zijlstradce840a2011-04-07 14:09:50 +02002738 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002739 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01002740 if (!(tmp->flags & SD_LOAD_BALANCE))
2741 continue;
2742
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002743 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02002744 * If power savings logic is enabled for a domain, see if we
2745 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002746 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02002747 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02002748 unsigned long power = 0;
2749 unsigned long nr_running = 0;
2750 unsigned long capacity;
2751 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002752
Peter Zijlstraae154be2009-09-10 14:40:57 +02002753 for_each_cpu(i, sched_domain_span(tmp)) {
2754 power += power_of(i);
2755 nr_running += cpu_rq(i)->cfs.nr_running;
2756 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002757
Nikhil Rao1399fa72011-05-18 10:09:39 -07002758 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002759
Peter Zijlstra59abf022009-09-16 08:28:30 +02002760 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2761 nr_running /= 2;
2762
2763 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002764 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002765 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002766
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002767 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002768 * If both cpu and prev_cpu are part of this domain,
2769 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01002770 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002771 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
2772 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
2773 affine_sd = tmp;
2774 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002775 }
2776
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002777 if (!want_sd && !want_affine)
2778 break;
2779
Peter Zijlstra0763a662009-09-14 19:37:39 +02002780 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002781 continue;
2782
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02002783 if (want_sd)
2784 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002785 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002786
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002787 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07002788 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02002789 prev_cpu = cpu;
2790
2791 new_cpu = select_idle_sibling(p, prev_cpu);
2792 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01002793 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02002794
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002795 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002796 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002797 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002798 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002799
Peter Zijlstra0763a662009-09-14 19:37:39 +02002800 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002801 sd = sd->child;
2802 continue;
2803 }
2804
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02002805 if (sd_flag & SD_BALANCE_WAKE)
2806 load_idx = sd->wake_idx;
2807
2808 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002809 if (!group) {
2810 sd = sd->child;
2811 continue;
2812 }
2813
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02002814 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002815 if (new_cpu == -1 || new_cpu == cpu) {
2816 /* Now try balancing at a lower domain level of cpu */
2817 sd = sd->child;
2818 continue;
2819 }
2820
2821 /* Now try balancing at a lower domain level of new_cpu */
2822 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002823 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002824 sd = NULL;
2825 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002826 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002827 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02002828 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02002829 sd = tmp;
2830 }
2831 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002832 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02002833unlock:
2834 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01002835
Peter Zijlstrac88d5912009-09-10 13:50:02 +02002836 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01002837}
2838#endif /* CONFIG_SMP */
2839
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002840static unsigned long
2841wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002842{
2843 unsigned long gran = sysctl_sched_wakeup_granularity;
2844
2845 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002846 * Since its curr running now, convert the gran from real-time
2847 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01002848 *
2849 * By using 'se' instead of 'curr' we penalize light tasks, so
2850 * they get preempted easier. That is, if 'se' < 'curr' then
2851 * the resulting gran will be larger, therefore penalizing the
2852 * lighter, if otoh 'se' > 'curr' then the resulting gran will
2853 * be smaller, again penalizing the lighter task.
2854 *
2855 * This is especially important for buddies when the leftmost
2856 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002857 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08002858 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02002859}
2860
2861/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02002862 * Should 'se' preempt 'curr'.
2863 *
2864 * |s1
2865 * |s2
2866 * |s3
2867 * g
2868 * |<--->|c
2869 *
2870 * w(c, s1) = -1
2871 * w(c, s2) = 0
2872 * w(c, s3) = 1
2873 *
2874 */
2875static int
2876wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2877{
2878 s64 gran, vdiff = curr->vruntime - se->vruntime;
2879
2880 if (vdiff <= 0)
2881 return -1;
2882
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01002883 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02002884 if (vdiff > gran)
2885 return 1;
2886
2887 return 0;
2888}
2889
Peter Zijlstra02479092008-11-04 21:25:10 +01002890static void set_last_buddy(struct sched_entity *se)
2891{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002892 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2893 return;
2894
2895 for_each_sched_entity(se)
2896 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002897}
2898
2899static void set_next_buddy(struct sched_entity *se)
2900{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002901 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
2902 return;
2903
2904 for_each_sched_entity(se)
2905 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01002906}
2907
Rik van Rielac53db52011-02-01 09:51:03 -05002908static void set_skip_buddy(struct sched_entity *se)
2909{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07002910 for_each_sched_entity(se)
2911 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05002912}
2913
Peter Zijlstra464b7522008-10-24 11:06:15 +02002914/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002915 * Preempt the current task with a newly woken task if needed:
2916 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02002917static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002918{
2919 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02002920 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002921 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002922 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002923 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01002924
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002925 if (unlikely(se == pse))
2926 return;
2927
Paul Turner5238cdd2011-07-21 09:43:37 -07002928 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01002929 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07002930 * unconditionally check_prempt_curr() after an enqueue (which may have
2931 * lead to a throttle). This both saves work and prevents false
2932 * next-buddy nomination below.
2933 */
2934 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
2935 return;
2936
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002937 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02002938 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002939 next_buddy_marked = 1;
2940 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02002941
Bharata B Raoaec0a512008-08-28 14:42:49 +05302942 /*
2943 * We can come here with TIF_NEED_RESCHED already set from new task
2944 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07002945 *
2946 * Note: this also catches the edge-case of curr being in a throttled
2947 * group (e.g. via set_curr_task), since update_curr() (in the
2948 * enqueue of curr) will have resulted in resched being set. This
2949 * prevents us from potentially nominating it as a false LAST_BUDDY
2950 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05302951 */
2952 if (test_tsk_need_resched(curr))
2953 return;
2954
Darren Harta2f5c9a2011-02-22 13:04:33 -08002955 /* Idle tasks are by definition preempted by non-idle tasks. */
2956 if (unlikely(curr->policy == SCHED_IDLE) &&
2957 likely(p->policy != SCHED_IDLE))
2958 goto preempt;
2959
Ingo Molnar91c234b2007-10-15 17:00:18 +02002960 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08002961 * Batch and idle tasks do not preempt non-idle tasks (their preemption
2962 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02002963 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01002964 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02002965 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002966
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002967 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07002968 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002969 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002970 if (wakeup_preempt_entity(se, pse) == 1) {
2971 /*
2972 * Bias pick_next to pick the sched entity that is
2973 * triggering this preemption.
2974 */
2975 if (!next_buddy_marked)
2976 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002977 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07002978 }
Jupyung Leea65ac742009-11-17 18:51:40 +09002979
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01002980 return;
2981
2982preempt:
2983 resched_task(curr);
2984 /*
2985 * Only set the backward buddy when the current task is still
2986 * on the rq. This can happen when a wakeup gets interleaved
2987 * with schedule on the ->pre_schedule() or idle_balance()
2988 * point, either of which can * drop the rq lock.
2989 *
2990 * Also, during early boot the idle thread is in the fair class,
2991 * for obvious reasons its a bad idea to schedule back to it.
2992 */
2993 if (unlikely(!se->on_rq || curr == rq->idle))
2994 return;
2995
2996 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
2997 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002998}
2999
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003000static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003001{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003002 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003003 struct cfs_rq *cfs_rq = &rq->cfs;
3004 struct sched_entity *se;
3005
Tim Blechmann36ace272009-11-24 11:55:45 +01003006 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003007 return NULL;
3008
3009 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02003010 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003011 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003012 cfs_rq = group_cfs_rq(se);
3013 } while (cfs_rq);
3014
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003015 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003016 if (hrtick_enabled(rq))
3017 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003018
3019 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003020}
3021
3022/*
3023 * Account for a descheduled task:
3024 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02003025static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003026{
3027 struct sched_entity *se = &prev->se;
3028 struct cfs_rq *cfs_rq;
3029
3030 for_each_sched_entity(se) {
3031 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02003032 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003033 }
3034}
3035
Rik van Rielac53db52011-02-01 09:51:03 -05003036/*
3037 * sched_yield() is very simple
3038 *
3039 * The magic of dealing with the ->skip buddy is in pick_next_entity.
3040 */
3041static void yield_task_fair(struct rq *rq)
3042{
3043 struct task_struct *curr = rq->curr;
3044 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
3045 struct sched_entity *se = &curr->se;
3046
3047 /*
3048 * Are we the only task in the tree?
3049 */
3050 if (unlikely(rq->nr_running == 1))
3051 return;
3052
3053 clear_buddies(cfs_rq, se);
3054
3055 if (curr->policy != SCHED_BATCH) {
3056 update_rq_clock(rq);
3057 /*
3058 * Update run-time statistics of the 'current'.
3059 */
3060 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01003061 /*
3062 * Tell update_rq_clock() that we've just updated,
3063 * so we don't do microscopic update in schedule()
3064 * and double the fastpath cost.
3065 */
3066 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05003067 }
3068
3069 set_skip_buddy(se);
3070}
3071
Mike Galbraithd95f4122011-02-01 09:50:51 -05003072static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
3073{
3074 struct sched_entity *se = &p->se;
3075
Paul Turner5238cdd2011-07-21 09:43:37 -07003076 /* throttled hierarchies are not runnable */
3077 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05003078 return false;
3079
3080 /* Tell the scheduler that we'd really like pse to run next. */
3081 set_next_buddy(se);
3082
Mike Galbraithd95f4122011-02-01 09:50:51 -05003083 yield_task_fair(rq);
3084
3085 return true;
3086}
3087
Peter Williams681f3e62007-10-24 18:23:51 +02003088#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003089/**************************************************
3090 * Fair scheduling class load-balancing methods:
3091 */
3092
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09003093static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3094
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003095#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01003096#define LBF_NEED_BREAK 0x02
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003097
3098struct lb_env {
3099 struct sched_domain *sd;
3100
3101 int src_cpu;
3102 struct rq *src_rq;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003103
3104 int dst_cpu;
3105 struct rq *dst_rq;
3106
3107 enum cpu_idle_type idle;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003108 long load_move;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003109 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003110
3111 unsigned int loop;
3112 unsigned int loop_break;
3113 unsigned int loop_max;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003114};
3115
Steve Muckle8f77c282013-03-11 16:33:42 -07003116static DEFINE_PER_CPU(bool, dbs_boost_needed);
3117
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003118/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003119 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003120 * Both runqueues must be locked.
3121 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003122static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003123{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003124 deactivate_task(env->src_rq, p, 0);
3125 set_task_cpu(p, env->dst_cpu);
3126 activate_task(env->dst_rq, p, 0);
3127 check_preempt_curr(env->dst_rq, p, 0);
Steve Muckle8f77c282013-03-11 16:33:42 -07003128 if (task_notify_on_migrate(p))
3129 per_cpu(dbs_boost_needed, env->dst_cpu) = true;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003130}
3131
3132/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02003133 * Is this task likely cache-hot:
3134 */
3135static int
3136task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
3137{
3138 s64 delta;
3139
3140 if (p->sched_class != &fair_sched_class)
3141 return 0;
3142
3143 if (unlikely(p->policy == SCHED_IDLE))
3144 return 0;
3145
3146 /*
3147 * Buddy candidates are cache hot:
3148 */
3149 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
3150 (&p->se == cfs_rq_of(&p->se)->next ||
3151 &p->se == cfs_rq_of(&p->se)->last))
3152 return 1;
3153
3154 if (sysctl_sched_migration_cost == -1)
3155 return 1;
3156 if (sysctl_sched_migration_cost == 0)
3157 return 0;
3158
3159 delta = now - p->se.exec_start;
3160
3161 return delta < (s64)sysctl_sched_migration_cost;
3162}
3163
3164/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003165 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3166 */
3167static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003168int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003169{
3170 int tsk_cache_hot = 0;
3171 /*
3172 * We do not migrate tasks that are:
3173 * 1) running (obviously), or
3174 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3175 * 3) are cache-hot on their current CPU.
3176 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003177 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003178 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003179 return 0;
3180 }
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003181 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003182
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003183 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003184 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003185 return 0;
3186 }
3187
3188 /*
3189 * Aggressive migration if:
3190 * 1) task is cache cold, or
3191 * 2) too many balance attempts have failed.
3192 */
3193
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003194 tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003195 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003196 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003197#ifdef CONFIG_SCHEDSTATS
3198 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003199 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03003200 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003201 }
3202#endif
3203 return 1;
3204 }
3205
3206 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03003207 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003208 return 0;
3209 }
3210 return 1;
3211}
3212
Peter Zijlstra897c3952009-12-17 17:45:42 +01003213/*
3214 * move_one_task tries to move exactly one task from busiest to this_rq, as
3215 * part of active balancing operations within "domain".
3216 * Returns 1 if successful and 0 otherwise.
3217 *
3218 * Called with both runqueues locked.
3219 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003220static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01003221{
3222 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003223
Peter Zijlstra367456c2012-02-20 21:49:09 +01003224 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
3225 if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
3226 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003227
Peter Zijlstra367456c2012-02-20 21:49:09 +01003228 if (!can_migrate_task(p, env))
3229 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003230
Peter Zijlstra367456c2012-02-20 21:49:09 +01003231 move_task(p, env);
3232 /*
3233 * Right now, this is only the second place move_task()
3234 * is called, so we can safely collect move_task()
3235 * stats here rather than inside move_task().
3236 */
3237 schedstat_inc(env->sd, lb_gained[env->idle]);
3238 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01003239 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01003240 return 0;
3241}
3242
Peter Zijlstra367456c2012-02-20 21:49:09 +01003243static unsigned long task_h_load(struct task_struct *p);
3244
Peter Zijlstraeb953082012-04-17 13:38:40 +02003245static const unsigned int sched_nr_migrate_break = 32;
3246
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003247/*
3248 * move_tasks tries to move up to load_move weighted load from busiest to
3249 * this_rq, as part of a balancing operation within domain "sd".
3250 * Returns 1 if successful and 0 otherwise.
3251 *
3252 * Called with both runqueues locked.
3253 */
3254static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003255{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003256 struct list_head *tasks = &env->src_rq->cfs_tasks;
3257 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01003258 unsigned long load;
3259 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003260
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003261 if (env->load_move <= 0)
3262 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003263
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003264 while (!list_empty(tasks)) {
3265 p = list_first_entry(tasks, struct task_struct, se.group_node);
3266
Peter Zijlstra367456c2012-02-20 21:49:09 +01003267 env->loop++;
3268 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003269 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003270 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003271
3272 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01003273 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02003274 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003275 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01003276 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02003277 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003278
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003279 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
Peter Zijlstra367456c2012-02-20 21:49:09 +01003280 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003281
Peter Zijlstra367456c2012-02-20 21:49:09 +01003282 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003283
Peter Zijlstraeb953082012-04-17 13:38:40 +02003284 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003285 goto next;
3286
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003287 if ((load / 2) > env->load_move)
Peter Zijlstra367456c2012-02-20 21:49:09 +01003288 goto next;
3289
3290 if (!can_migrate_task(p, env))
3291 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003292
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003293 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01003294 pulled++;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003295 env->load_move -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003296
3297#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01003298 /*
3299 * NEWIDLE balancing is a source of latency, so preemptible
3300 * kernels will stop after the first task is pulled to minimize
3301 * the critical section.
3302 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003303 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003304 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003305#endif
3306
Peter Zijlstraee00e662009-12-17 17:25:20 +01003307 /*
3308 * We only want to steal up to the prescribed amount of
3309 * weighted load.
3310 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003311 if (env->load_move <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01003312 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003313
Peter Zijlstra367456c2012-02-20 21:49:09 +01003314 continue;
3315next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003316 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003317 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003318
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003319 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01003320 * Right now, this is one of only two places move_task() is called,
3321 * so we can safely collect move_task() stats here rather than
3322 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003323 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01003324 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003325
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01003326 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003327}
3328
Peter Zijlstra230059de2009-12-17 17:47:12 +01003329#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003330/*
3331 * update tg->load_weight by folding this cpu's load_avg
3332 */
Paul Turner67e86252010-11-15 15:47:05 -08003333static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003334{
3335 struct cfs_rq *cfs_rq;
3336 unsigned long flags;
3337 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003338
3339 if (!tg->se[cpu])
3340 return 0;
3341
3342 rq = cpu_rq(cpu);
3343 cfs_rq = tg->cfs_rq[cpu];
3344
3345 raw_spin_lock_irqsave(&rq->lock, flags);
3346
3347 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08003348 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003349
3350 /*
3351 * We need to update shares after updating tg->load_weight in
3352 * order to adjust the weight of groups with long running tasks.
3353 */
Paul Turner6d5ab292011-01-21 20:45:01 -08003354 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003355
3356 raw_spin_unlock_irqrestore(&rq->lock, flags);
3357
3358 return 0;
3359}
3360
3361static void update_shares(int cpu)
3362{
3363 struct cfs_rq *cfs_rq;
3364 struct rq *rq = cpu_rq(cpu);
3365
3366 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003367 /*
3368 * Iterates the task_group tree in a bottom up fashion, see
3369 * list_add_leaf_cfs_rq() for details.
3370 */
Paul Turner64660c82011-07-21 09:43:36 -07003371 for_each_leaf_cfs_rq(rq, cfs_rq) {
3372 /* throttled entities do not contribute to load */
3373 if (throttled_hierarchy(cfs_rq))
3374 continue;
3375
Paul Turner67e86252010-11-15 15:47:05 -08003376 update_shares_cpu(cfs_rq->tg, cpu);
Paul Turner64660c82011-07-21 09:43:36 -07003377 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003378 rcu_read_unlock();
3379}
3380
Peter Zijlstra9763b672011-07-13 13:09:25 +02003381/*
3382 * Compute the cpu's hierarchical load factor for each task group.
3383 * This needs to be done in a top-down fashion because the load of a child
3384 * group is a fraction of its parents load.
3385 */
3386static int tg_load_down(struct task_group *tg, void *data)
3387{
3388 unsigned long load;
3389 long cpu = (long)data;
3390
3391 if (!tg->parent) {
3392 load = cpu_rq(cpu)->load.weight;
3393 } else {
3394 load = tg->parent->cfs_rq[cpu]->h_load;
3395 load *= tg->se[cpu]->load.weight;
3396 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
3397 }
3398
3399 tg->cfs_rq[cpu]->h_load = load;
3400
3401 return 0;
3402}
3403
3404static void update_h_load(long cpu)
3405{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003406 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003407 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstra367456c2012-02-20 21:49:09 +01003408 rcu_read_unlock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02003409}
3410
Peter Zijlstra367456c2012-02-20 21:49:09 +01003411static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003412{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003413 struct cfs_rq *cfs_rq = task_cfs_rq(p);
3414 unsigned long load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003415
Peter Zijlstra367456c2012-02-20 21:49:09 +01003416 load = p->se.load.weight;
3417 load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01003418
Peter Zijlstra367456c2012-02-20 21:49:09 +01003419 return load;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003420}
3421#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08003422static inline void update_shares(int cpu)
3423{
3424}
3425
Peter Zijlstra367456c2012-02-20 21:49:09 +01003426static inline void update_h_load(long cpu)
Peter Zijlstra230059de2009-12-17 17:47:12 +01003427{
Peter Zijlstra367456c2012-02-20 21:49:09 +01003428}
3429
3430static unsigned long task_h_load(struct task_struct *p)
3431{
3432 return p->se.load.weight;
Peter Zijlstra230059de2009-12-17 17:47:12 +01003433}
3434#endif
3435
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003436/********** Helpers for find_busiest_group ************************/
3437/*
3438 * sd_lb_stats - Structure to store the statistics of a sched_domain
3439 * during load balancing.
3440 */
3441struct sd_lb_stats {
3442 struct sched_group *busiest; /* Busiest group in this sd */
3443 struct sched_group *this; /* Local group in this sd */
3444 unsigned long total_load; /* Total load of all groups in sd */
3445 unsigned long total_pwr; /* Total power of all groups in sd */
3446 unsigned long avg_load; /* Average load across all groups in sd */
3447
3448 /** Statistics of this group */
3449 unsigned long this_load;
3450 unsigned long this_load_per_task;
3451 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07003452 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003453 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003454
3455 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003456 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003457 unsigned long max_load;
3458 unsigned long busiest_load_per_task;
3459 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003460 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07003461 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003462 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003463
3464 int group_imb; /* Is there imbalance in this sd */
3465#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3466 int power_savings_balance; /* Is powersave balance needed for this sd */
3467 struct sched_group *group_min; /* Least loaded group in sd */
3468 struct sched_group *group_leader; /* Group which relieves group_min */
3469 unsigned long min_load_per_task; /* load_per_task in group_min */
3470 unsigned long leader_nr_running; /* Nr running of group_leader */
3471 unsigned long min_nr_running; /* Nr running of group_min */
3472#endif
3473};
3474
3475/*
3476 * sg_lb_stats - stats of a sched_group required for load_balancing
3477 */
3478struct sg_lb_stats {
3479 unsigned long avg_load; /*Avg load across the CPUs of the group */
3480 unsigned long group_load; /* Total load over the CPUs of the group */
3481 unsigned long sum_nr_running; /* Nr tasks running in the group */
3482 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3483 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003484 unsigned long idle_cpus;
3485 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003486 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07003487 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003488};
3489
3490/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003491 * get_sd_load_idx - Obtain the load index for a given sched domain.
3492 * @sd: The sched_domain whose load_idx is to be obtained.
3493 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3494 */
3495static inline int get_sd_load_idx(struct sched_domain *sd,
3496 enum cpu_idle_type idle)
3497{
3498 int load_idx;
3499
3500 switch (idle) {
3501 case CPU_NOT_IDLE:
3502 load_idx = sd->busy_idx;
3503 break;
3504
3505 case CPU_NEWLY_IDLE:
3506 load_idx = sd->newidle_idx;
3507 break;
3508 default:
3509 load_idx = sd->idle_idx;
3510 break;
3511 }
3512
3513 return load_idx;
3514}
3515
3516
3517#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3518/**
3519 * init_sd_power_savings_stats - Initialize power savings statistics for
3520 * the given sched_domain, during load balancing.
3521 *
3522 * @sd: Sched domain whose power-savings statistics are to be initialized.
3523 * @sds: Variable containing the statistics for sd.
3524 * @idle: Idle status of the CPU at which we're performing load-balancing.
3525 */
3526static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3527 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3528{
3529 /*
3530 * Busy processors will not participate in power savings
3531 * balance.
3532 */
3533 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3534 sds->power_savings_balance = 0;
3535 else {
3536 sds->power_savings_balance = 1;
3537 sds->min_nr_running = ULONG_MAX;
3538 sds->leader_nr_running = 0;
3539 }
3540}
3541
3542/**
3543 * update_sd_power_savings_stats - Update the power saving stats for a
3544 * sched_domain while performing load balancing.
3545 *
3546 * @group: sched_group belonging to the sched_domain under consideration.
3547 * @sds: Variable containing the statistics of the sched_domain
3548 * @local_group: Does group contain the CPU for which we're performing
3549 * load balancing ?
3550 * @sgs: Variable containing the statistics of the group.
3551 */
3552static inline void update_sd_power_savings_stats(struct sched_group *group,
3553 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3554{
3555
3556 if (!sds->power_savings_balance)
3557 return;
3558
3559 /*
3560 * If the local group is idle or completely loaded
3561 * no need to do power savings balance at this domain
3562 */
3563 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3564 !sds->this_nr_running))
3565 sds->power_savings_balance = 0;
3566
3567 /*
3568 * If a group is already running at full capacity or idle,
3569 * don't include that group in power savings calculations
3570 */
3571 if (!sds->power_savings_balance ||
3572 sgs->sum_nr_running >= sgs->group_capacity ||
3573 !sgs->sum_nr_running)
3574 return;
3575
3576 /*
3577 * Calculate the group which has the least non-idle load.
3578 * This is the group from where we need to pick up the load
3579 * for saving power
3580 */
3581 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3582 (sgs->sum_nr_running == sds->min_nr_running &&
3583 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3584 sds->group_min = group;
3585 sds->min_nr_running = sgs->sum_nr_running;
3586 sds->min_load_per_task = sgs->sum_weighted_load /
3587 sgs->sum_nr_running;
3588 }
3589
3590 /*
3591 * Calculate the group which is almost near its
3592 * capacity but still has some space to pick up some load
3593 * from other group and save more power
3594 */
3595 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3596 return;
3597
3598 if (sgs->sum_nr_running > sds->leader_nr_running ||
3599 (sgs->sum_nr_running == sds->leader_nr_running &&
3600 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3601 sds->group_leader = group;
3602 sds->leader_nr_running = sgs->sum_nr_running;
3603 }
3604}
3605
3606/**
3607 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3608 * @sds: Variable containing the statistics of the sched_domain
3609 * under consideration.
3610 * @this_cpu: Cpu at which we're currently performing load-balancing.
3611 * @imbalance: Variable to store the imbalance.
3612 *
3613 * Description:
3614 * Check if we have potential to perform some power-savings balance.
3615 * If yes, set the busiest group to be the least loaded group in the
3616 * sched_domain, so that it's CPUs can be put to idle.
3617 *
3618 * Returns 1 if there is potential to perform power-savings balance.
3619 * Else returns 0.
3620 */
3621static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3622 int this_cpu, unsigned long *imbalance)
3623{
3624 if (!sds->power_savings_balance)
3625 return 0;
3626
3627 if (sds->this != sds->group_leader ||
3628 sds->group_leader == sds->group_min)
3629 return 0;
3630
3631 *imbalance = sds->min_load_per_task;
3632 sds->busiest = sds->group_min;
3633
3634 return 1;
3635
3636}
3637#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3638static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3639 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3640{
3641 return;
3642}
3643
3644static inline void update_sd_power_savings_stats(struct sched_group *group,
3645 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3646{
3647 return;
3648}
3649
3650static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3651 int this_cpu, unsigned long *imbalance)
3652{
3653 return 0;
3654}
3655#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3656
3657
3658unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3659{
Nikhil Rao1399fa72011-05-18 10:09:39 -07003660 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003661}
3662
3663unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3664{
3665 return default_scale_freq_power(sd, cpu);
3666}
3667
3668unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3669{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003670 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003671 unsigned long smt_gain = sd->smt_gain;
3672
3673 smt_gain /= weight;
3674
3675 return smt_gain;
3676}
3677
3678unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3679{
3680 return default_scale_smt_power(sd, cpu);
3681}
3682
3683unsigned long scale_rt_power(int cpu)
3684{
3685 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrac44229e2012-05-22 14:04:28 +02003686 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003687
Peter Zijlstrac44229e2012-05-22 14:04:28 +02003688 /*
3689 * Since we're reading these variables without serialization make sure
3690 * we read them once before doing sanity checks on them.
3691 */
3692 age_stamp = ACCESS_ONCE(rq->age_stamp);
3693 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003694
Peter Zijlstrac44229e2012-05-22 14:04:28 +02003695 total = sched_avg_period() + (rq->clock - age_stamp);
3696
3697 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003698 /* Ensures that power won't end up being negative */
3699 available = 0;
3700 } else {
Peter Zijlstrac44229e2012-05-22 14:04:28 +02003701 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07003702 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003703
Nikhil Rao1399fa72011-05-18 10:09:39 -07003704 if (unlikely((s64)total < SCHED_POWER_SCALE))
3705 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003706
Nikhil Rao1399fa72011-05-18 10:09:39 -07003707 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003708
3709 return div_u64(available, total);
3710}
3711
3712static void update_cpu_power(struct sched_domain *sd, int cpu)
3713{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02003714 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07003715 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003716 struct sched_group *sdg = sd->groups;
3717
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003718 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3719 if (sched_feat(ARCH_POWER))
3720 power *= arch_scale_smt_power(sd, cpu);
3721 else
3722 power *= default_scale_smt_power(sd, cpu);
3723
Nikhil Rao1399fa72011-05-18 10:09:39 -07003724 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003725 }
3726
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003727 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003728
3729 if (sched_feat(ARCH_POWER))
3730 power *= arch_scale_freq_power(sd, cpu);
3731 else
3732 power *= default_scale_freq_power(sd, cpu);
3733
Nikhil Rao1399fa72011-05-18 10:09:39 -07003734 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003735
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003736 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003737 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003738
3739 if (!power)
3740 power = 1;
3741
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02003742 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003743 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003744}
3745
Peter Zijlstra029632f2011-10-25 10:00:11 +02003746void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003747{
3748 struct sched_domain *child = sd->child;
3749 struct sched_group *group, *sdg = sd->groups;
3750 unsigned long power;
Vincent Guittot4ec44122011-12-12 20:21:08 +01003751 unsigned long interval;
3752
3753 interval = msecs_to_jiffies(sd->balance_interval);
3754 interval = clamp(interval, 1UL, max_load_balance_interval);
3755 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003756
3757 if (!child) {
3758 update_cpu_power(sd, cpu);
3759 return;
3760 }
3761
3762 power = 0;
3763
3764 group = child->groups;
3765 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003766 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003767 group = group->next;
3768 } while (group != child->groups);
3769
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003770 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003771}
3772
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003773/*
3774 * Try and fix up capacity for tiny siblings, this is needed when
3775 * things like SD_ASYM_PACKING need f_b_g to select another sibling
3776 * which on its own isn't powerful enough.
3777 *
3778 * See update_sd_pick_busiest() and check_asym_packing().
3779 */
3780static inline int
3781fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3782{
3783 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07003784 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003785 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02003786 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003787 return 0;
3788
3789 /*
3790 * If ~90% of the cpu_power is still there, we're good.
3791 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003792 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003793 return 1;
3794
3795 return 0;
3796}
3797
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003798/**
3799 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3800 * @sd: The sched_domain whose statistics are to be updated.
3801 * @group: sched_group whose statistics are to be updated.
3802 * @this_cpu: Cpu for which load balance is currently performed.
3803 * @idle: Idle status of this_cpu
3804 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003805 * @local_group: Does group contain this_cpu.
3806 * @cpus: Set of cpus considered for load balancing.
3807 * @balance: Should we balance.
3808 * @sgs: variable to hold the statistics for this group.
3809 */
3810static inline void update_sg_lb_stats(struct sched_domain *sd,
3811 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003812 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003813 int local_group, const struct cpumask *cpus,
3814 int *balance, struct sg_lb_stats *sgs)
3815{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003816 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003817 int i;
3818 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003819 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003820
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06003821 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003822 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003823
3824 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003825 max_cpu_load = 0;
3826 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003827 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003828
3829 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3830 struct rq *rq = cpu_rq(i);
3831
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003832 /* Bias balancing toward cpus of our domain */
3833 if (local_group) {
3834 if (idle_cpu(i) && !first_idle_cpu) {
3835 first_idle_cpu = 1;
3836 balance_cpu = i;
3837 }
3838
3839 load = target_load(i, load_idx);
3840 } else {
3841 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003842 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003843 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07003844 max_nr_running = rq->nr_running;
3845 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003846 if (min_cpu_load > load)
3847 min_cpu_load = load;
3848 }
3849
3850 sgs->group_load += load;
3851 sgs->sum_nr_running += rq->nr_running;
3852 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003853 if (idle_cpu(i))
3854 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003855 }
3856
3857 /*
3858 * First idle cpu or the first cpu(busiest) in this sched group
3859 * is eligible for doing load balancing at this and above
3860 * domains. In the newly idle case, we will allow all the cpu's
3861 * to do the newly idle load balance.
3862 */
Vincent Guittot4ec44122011-12-12 20:21:08 +01003863 if (local_group) {
3864 if (idle != CPU_NEWLY_IDLE) {
3865 if (balance_cpu != this_cpu) {
3866 *balance = 0;
3867 return;
3868 }
3869 update_group_power(sd, this_cpu);
3870 } else if (time_after_eq(jiffies, group->sgp->next_update))
3871 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003872 }
3873
3874 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003875 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003876
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003877 /*
3878 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01003879 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003880 *
3881 * APZ: with cgroup the avg task weight can vary wildly and
3882 * might not be a suitable number - should we keep a
3883 * normalized nr_running number somewhere that negates
3884 * the hierarchy?
3885 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003886 if (sgs->sum_nr_running)
3887 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003888
Peter Zijlstra866ab432011-02-21 18:56:47 +01003889 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003890 sgs->group_imb = 1;
3891
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003892 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07003893 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003894 if (!sgs->group_capacity)
3895 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003896 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07003897
3898 if (sgs->group_capacity > sgs->sum_nr_running)
3899 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003900}
3901
3902/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10003903 * update_sd_pick_busiest - return 1 on busiest group
3904 * @sd: sched_domain whose statistics are to be checked
3905 * @sds: sched_domain statistics
3906 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10003907 * @sgs: sched_group statistics
3908 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10003909 *
3910 * Determine if @sg is a busier group than the previously selected
3911 * busiest group.
3912 */
3913static bool update_sd_pick_busiest(struct sched_domain *sd,
3914 struct sd_lb_stats *sds,
3915 struct sched_group *sg,
3916 struct sg_lb_stats *sgs,
3917 int this_cpu)
3918{
3919 if (sgs->avg_load <= sds->max_load)
3920 return false;
3921
3922 if (sgs->sum_nr_running > sgs->group_capacity)
3923 return true;
3924
3925 if (sgs->group_imb)
3926 return true;
3927
3928 /*
3929 * ASYM_PACKING needs to move all the work to the lowest
3930 * numbered CPUs in the group, therefore mark all groups
3931 * higher than ourself as busy.
3932 */
3933 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3934 this_cpu < group_first_cpu(sg)) {
3935 if (!sds->busiest)
3936 return true;
3937
3938 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
3939 return true;
3940 }
3941
3942 return false;
3943}
3944
3945/**
Hui Kang461819a2011-10-11 23:00:59 -04003946 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003947 * @sd: sched_domain whose statistics are to be updated.
3948 * @this_cpu: Cpu for which load balance is currently performed.
3949 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003950 * @cpus: Set of cpus considered for load balancing.
3951 * @balance: Should we balance.
3952 * @sds: variable to hold the statistics for this sched_domain.
3953 */
3954static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003955 enum cpu_idle_type idle, const struct cpumask *cpus,
3956 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003957{
3958 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003959 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003960 struct sg_lb_stats sgs;
3961 int load_idx, prefer_sibling = 0;
3962
3963 if (child && child->flags & SD_PREFER_SIBLING)
3964 prefer_sibling = 1;
3965
3966 init_sd_power_savings_stats(sd, sds, idle);
3967 load_idx = get_sd_load_idx(sd, idle);
3968
3969 do {
3970 int local_group;
3971
Michael Neuling532cb4c2010-06-08 14:57:02 +10003972 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003973 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003974 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003975 local_group, cpus, balance, &sgs);
3976
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003977 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003978 return;
3979
3980 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003981 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003982
3983 /*
3984 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10003985 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07003986 * and move all the excess tasks away. We lower the capacity
3987 * of a group only if the local group has the capacity to fit
3988 * these excess tasks, i.e. nr_running < group_capacity. The
3989 * extra check prevents the case where you always pull from the
3990 * heaviest group when it is already under-utilized (possible
3991 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003992 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07003993 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003994 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3995
3996 if (local_group) {
3997 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10003998 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003999 sds->this_nr_running = sgs.sum_nr_running;
4000 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004001 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004002 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004003 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004004 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10004005 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004006 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004007 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004008 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004009 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07004010 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004011 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004012 sds->group_imb = sgs.group_imb;
4013 }
4014
Michael Neuling532cb4c2010-06-08 14:57:02 +10004015 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
4016 sg = sg->next;
4017 } while (sg != sd->groups);
4018}
4019
Michael Neuling532cb4c2010-06-08 14:57:02 +10004020/**
4021 * check_asym_packing - Check to see if the group is packed into the
4022 * sched doman.
4023 *
4024 * This is primarily intended to used at the sibling level. Some
4025 * cores like POWER7 prefer to use lower numbered SMT threads. In the
4026 * case of POWER7, it can move to lower SMT modes only when higher
4027 * threads are idle. When in lower SMT modes, the threads will
4028 * perform better since they share less core resources. Hence when we
4029 * have idle threads, we want them to be the higher ones.
4030 *
4031 * This packing function is run on idle threads. It checks to see if
4032 * the busiest CPU in this domain (core in the P7 case) has a higher
4033 * CPU number than the packing function is being run on. Here we are
4034 * assuming lower CPU number will be equivalent to lower a SMT thread
4035 * number.
4036 *
Michael Neulingb6b12292010-06-10 12:06:21 +10004037 * Returns 1 when packing is required and a task should be moved to
4038 * this CPU. The amount of the imbalance is returned in *imbalance.
4039 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10004040 * @sd: The sched_domain whose packing is to be checked.
4041 * @sds: Statistics of the sched_domain which is to be packed
4042 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4043 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10004044 */
4045static int check_asym_packing(struct sched_domain *sd,
4046 struct sd_lb_stats *sds,
4047 int this_cpu, unsigned long *imbalance)
4048{
4049 int busiest_cpu;
4050
4051 if (!(sd->flags & SD_ASYM_PACKING))
4052 return 0;
4053
4054 if (!sds->busiest)
4055 return 0;
4056
4057 busiest_cpu = group_first_cpu(sds->busiest);
4058 if (this_cpu > busiest_cpu)
4059 return 0;
4060
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004061 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07004062 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10004063 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004064}
4065
4066/**
4067 * fix_small_imbalance - Calculate the minor imbalance that exists
4068 * amongst the groups of a sched_domain, during
4069 * load balancing.
4070 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4071 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4072 * @imbalance: Variable to store the imbalance.
4073 */
4074static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4075 int this_cpu, unsigned long *imbalance)
4076{
4077 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4078 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004079 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004080
4081 if (sds->this_nr_running) {
4082 sds->this_load_per_task /= sds->this_nr_running;
4083 if (sds->busiest_load_per_task >
4084 sds->this_load_per_task)
4085 imbn = 1;
4086 } else
4087 sds->this_load_per_task =
4088 cpu_avg_load_per_task(this_cpu);
4089
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004090 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07004091 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004092 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004093
4094 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4095 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004096 *imbalance = sds->busiest_load_per_task;
4097 return;
4098 }
4099
4100 /*
4101 * OK, we don't have enough imbalance to justify moving tasks,
4102 * however we may be able to increase total CPU power used by
4103 * moving them.
4104 */
4105
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004106 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004107 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004108 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004109 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004110 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004111
4112 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004113 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004114 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004115 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004116 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004117 min(sds->busiest_load_per_task, sds->max_load - tmp);
4118
4119 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004120 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07004121 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004122 tmp = (sds->max_load * sds->busiest->sgp->power) /
4123 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004124 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07004125 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004126 sds->this->sgp->power;
4127 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004128 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004129 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004130
4131 /* Move if we gain throughput */
4132 if (pwr_move > pwr_now)
4133 *imbalance = sds->busiest_load_per_task;
4134}
4135
4136/**
4137 * calculate_imbalance - Calculate the amount of imbalance present within the
4138 * groups of a given sched_domain during load balance.
4139 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4140 * @this_cpu: Cpu for which currently load balance is being performed.
4141 * @imbalance: The variable to store the imbalance.
4142 */
4143static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4144 unsigned long *imbalance)
4145{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004146 unsigned long max_pull, load_above_capacity = ~0UL;
4147
4148 sds->busiest_load_per_task /= sds->busiest_nr_running;
4149 if (sds->group_imb) {
4150 sds->busiest_load_per_task =
4151 min(sds->busiest_load_per_task, sds->avg_load);
4152 }
4153
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004154 /*
4155 * In the presence of smp nice balancing, certain scenarios can have
4156 * max load less than avg load(as we skip the groups at or below
4157 * its cpu_power, while calculating max_load..)
4158 */
4159 if (sds->max_load < sds->avg_load) {
4160 *imbalance = 0;
4161 return fix_small_imbalance(sds, this_cpu, imbalance);
4162 }
4163
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004164 if (!sds->group_imb) {
4165 /*
4166 * Don't want to pull so many tasks that a group would go idle.
4167 */
4168 load_above_capacity = (sds->busiest_nr_running -
4169 sds->busiest_group_capacity);
4170
Nikhil Rao1399fa72011-05-18 10:09:39 -07004171 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004172
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004173 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08004174 }
4175
4176 /*
4177 * We're trying to get all the cpus to the average_load, so we don't
4178 * want to push ourselves above the average load, nor do we wish to
4179 * reduce the max loaded cpu below the average load. At the same time,
4180 * we also don't want to reduce the group load below the group capacity
4181 * (so that we can implement power-savings policies etc). Thus we look
4182 * for the minimum possible imbalance.
4183 * Be careful of negative numbers as they'll appear as very large values
4184 * with unsigned longs.
4185 */
4186 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004187
4188 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004189 *imbalance = min(max_pull * sds->busiest->sgp->power,
4190 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07004191 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004192
4193 /*
4194 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004195 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004196 * a think about bumping its value to force at least one task to be
4197 * moved
4198 */
4199 if (*imbalance < sds->busiest_load_per_task)
4200 return fix_small_imbalance(sds, this_cpu, imbalance);
4201
4202}
Nikhil Raofab47622010-10-15 13:12:29 -07004203
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004204/******* find_busiest_group() helpers end here *********************/
4205
4206/**
4207 * find_busiest_group - Returns the busiest group within the sched_domain
4208 * if there is an imbalance. If there isn't an imbalance, and
4209 * the user has opted for power-savings, it returns a group whose
4210 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
4211 * such a group exists.
4212 *
4213 * Also calculates the amount of weighted load which should be moved
4214 * to restore balance.
4215 *
4216 * @sd: The sched_domain whose busiest group is to be returned.
4217 * @this_cpu: The cpu for which load balancing is currently being performed.
4218 * @imbalance: Variable which stores amount of weighted load which should
4219 * be moved to restore balance/put a group to idle.
4220 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004221 * @cpus: The set of CPUs under consideration for load-balancing.
4222 * @balance: Pointer to a variable indicating if this_cpu
4223 * is the appropriate cpu to perform load balancing at this_level.
4224 *
4225 * Returns: - the busiest group if imbalance exists.
4226 * - If no imbalance and user has opted for power-savings balance,
4227 * return the least loaded group whose CPUs can be
4228 * put to idle by rebalancing its tasks onto our group.
4229 */
4230static struct sched_group *
4231find_busiest_group(struct sched_domain *sd, int this_cpu,
4232 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004233 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004234{
4235 struct sd_lb_stats sds;
4236
4237 memset(&sds, 0, sizeof(sds));
4238
4239 /*
4240 * Compute the various statistics relavent for load balancing at
4241 * this level.
4242 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004243 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004244
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004245 /*
4246 * this_cpu is not the appropriate cpu to perform load balancing at
4247 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004248 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01004249 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004250 goto ret;
4251
Michael Neuling532cb4c2010-06-08 14:57:02 +10004252 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
4253 check_asym_packing(sd, &sds, this_cpu, imbalance))
4254 return sds.busiest;
4255
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004256 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004257 if (!sds.busiest || sds.busiest_nr_running == 0)
4258 goto out_balanced;
4259
Nikhil Rao1399fa72011-05-18 10:09:39 -07004260 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07004261
Peter Zijlstra866ab432011-02-21 18:56:47 +01004262 /*
4263 * If the busiest group is imbalanced the below checks don't
4264 * work because they assumes all things are equal, which typically
4265 * isn't true due to cpus_allowed constraints and the like.
4266 */
4267 if (sds.group_imb)
4268 goto force_balance;
4269
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004270 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07004271 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4272 !sds.busiest_has_capacity)
4273 goto force_balance;
4274
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004275 /*
4276 * If the local group is more busy than the selected busiest group
4277 * don't try and pull any tasks.
4278 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004279 if (sds.this_load >= sds.max_load)
4280 goto out_balanced;
4281
Peter Zijlstracc57aa82011-02-21 18:55:32 +01004282 /*
4283 * Don't pull any tasks if this group is already above the domain
4284 * average load.
4285 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004286 if (sds.this_load >= sds.avg_load)
4287 goto out_balanced;
4288
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004289 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004290 /*
4291 * This cpu is idle. If the busiest group load doesn't
4292 * have more tasks than the number of available cpu's and
4293 * there is no imbalance between this and busiest group
4294 * wrt to idle cpu's, it is balanced.
4295 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004296 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004297 sds.busiest_nr_running <= sds.busiest_group_weight)
4298 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01004299 } else {
4300 /*
4301 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4302 * imbalance_pct to be conservative.
4303 */
4304 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4305 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07004306 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004307
Nikhil Raofab47622010-10-15 13:12:29 -07004308force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004309 /* Looks like there is an imbalance. Compute it */
4310 calculate_imbalance(&sds, this_cpu, imbalance);
4311 return sds.busiest;
4312
4313out_balanced:
4314 /*
4315 * There is no obvious imbalance. But check if we can do some balancing
4316 * to save power.
4317 */
4318 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4319 return sds.busiest;
4320ret:
4321 *imbalance = 0;
4322 return NULL;
4323}
4324
4325/*
4326 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4327 */
4328static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004329find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4330 enum cpu_idle_type idle, unsigned long imbalance,
4331 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004332{
4333 struct rq *busiest = NULL, *rq;
4334 unsigned long max_load = 0;
4335 int i;
4336
4337 for_each_cpu(i, sched_group_cpus(group)) {
4338 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07004339 unsigned long capacity = DIV_ROUND_CLOSEST(power,
4340 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004341 unsigned long wl;
4342
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004343 if (!capacity)
4344 capacity = fix_small_capacity(sd, group);
4345
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004346 if (!cpumask_test_cpu(i, cpus))
4347 continue;
4348
4349 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004350 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004351
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004352 /*
4353 * When comparing with imbalance, use weighted_cpuload()
4354 * which is not scaled with the cpu power.
4355 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004356 if (capacity && rq->nr_running == 1 && wl > imbalance)
4357 continue;
4358
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004359 /*
4360 * For the load comparisons with the other cpu's, consider
4361 * the weighted_cpuload() scaled with the cpu power, so that
4362 * the load can be moved away from the cpu that is potentially
4363 * running at a lower capacity.
4364 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07004365 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01004366
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004367 if (wl > max_load) {
4368 max_load = wl;
4369 busiest = rq;
4370 }
4371 }
4372
4373 return busiest;
4374}
4375
4376/*
4377 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4378 * so long as it is large enough.
4379 */
4380#define MAX_PINNED_INTERVAL 512
4381
4382/* Working cpumask for load_balance and load_balance_newidle. */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004383DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004384
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004385static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10004386 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004387{
4388 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10004389
4390 /*
4391 * ASYM_PACKING needs to force migrate tasks from busy but
4392 * higher numbered CPUs in order to pack all tasks in the
4393 * lowest numbered CPUs.
4394 */
4395 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
4396 return 1;
4397
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004398 /*
4399 * The only task running in a non-idle cpu can be moved to this
4400 * cpu in an attempt to completely freeup the other CPU
4401 * package.
4402 *
4403 * The package power saving logic comes from
4404 * find_busiest_group(). If there are no imbalance, then
4405 * f_b_g() will return NULL. However when sched_mc={1,2} then
4406 * f_b_g() will select a group from which a running task may be
4407 * pulled to this cpu in order to make the other package idle.
4408 * If there is no opportunity to make a package idle and if
4409 * there are no imbalance, then f_b_g() will return NULL and no
4410 * action will be taken in load_balance_newidle().
4411 *
4412 * Under normal task pull operation due to imbalance, there
4413 * will be more than one task in the source run queue and
4414 * move_tasks() will succeed. ld_moved will be true and this
4415 * active balance code will not be triggered.
4416 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01004417 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4418 return 0;
4419 }
4420
4421 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
4422}
4423
Tejun Heo969c7922010-05-06 18:49:21 +02004424static int active_load_balance_cpu_stop(void *data);
4425
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004426/*
4427 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4428 * tasks if there is an imbalance.
4429 */
4430static int load_balance(int this_cpu, struct rq *this_rq,
4431 struct sched_domain *sd, enum cpu_idle_type idle,
4432 int *balance)
4433{
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004434 int ld_moved, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004435 struct sched_group *group;
4436 unsigned long imbalance;
Steve Muckleeaf00192013-11-19 14:16:53 -08004437 struct rq *busiest = NULL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004438 unsigned long flags;
4439 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4440
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004441 struct lb_env env = {
4442 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004443 .dst_cpu = this_cpu,
4444 .dst_rq = this_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004445 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02004446 .loop_break = sched_nr_migrate_break,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004447 };
4448
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004449 cpumask_copy(cpus, cpu_active_mask);
4450
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004451 schedstat_inc(sd, lb_count[idle]);
4452
4453redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004454 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004455 cpus, balance);
4456
4457 if (*balance == 0)
4458 goto out_balanced;
4459
4460 if (!group) {
4461 schedstat_inc(sd, lb_nobusyg[idle]);
4462 goto out_balanced;
4463 }
4464
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10004465 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004466 if (!busiest) {
4467 schedstat_inc(sd, lb_nobusyq[idle]);
4468 goto out_balanced;
4469 }
4470
4471 BUG_ON(busiest == this_rq);
4472
4473 schedstat_add(sd, lb_imbalance[idle], imbalance);
4474
4475 ld_moved = 0;
4476 if (busiest->nr_running > 1) {
4477 /*
4478 * Attempt to move tasks. If find_busiest_group has found
4479 * an imbalance but busiest->nr_running <= 1, the group is
4480 * still unbalanced. ld_moved simply stays zero, so it is
4481 * correctly treated as an imbalance.
4482 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004483 env.flags |= LBF_ALL_PINNED;
Peter Zijlstraeb953082012-04-17 13:38:40 +02004484 env.load_move = imbalance;
4485 env.src_cpu = busiest->cpu;
4486 env.src_rq = busiest;
4487 env.loop_max = min_t(unsigned long, sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004488
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004489more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004490 local_irq_save(flags);
4491 double_rq_lock(this_rq, busiest);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004492 if (!env.loop)
4493 update_h_load(env.src_cpu);
4494 ld_moved += move_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004495 double_rq_unlock(this_rq, busiest);
4496 local_irq_restore(flags);
4497
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004498 if (env.flags & LBF_NEED_BREAK) {
4499 env.flags &= ~LBF_NEED_BREAK;
4500 goto more_balance;
4501 }
4502
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004503 /*
4504 * some other cpu did the load balance for us.
4505 */
4506 if (ld_moved && this_cpu != smp_processor_id())
4507 resched_cpu(this_cpu);
4508
4509 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004510 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004511 cpumask_clear_cpu(cpu_of(busiest), cpus);
4512 if (!cpumask_empty(cpus))
4513 goto redo;
4514 goto out_balanced;
4515 }
4516 }
4517
4518 if (!ld_moved) {
4519 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07004520 /*
4521 * Increment the failure counter only on periodic balance.
4522 * We do not want newidle balance, which can be very
4523 * frequent, pollute the failure counter causing
4524 * excessive cache_hot migrations and active balances.
4525 */
4526 if (idle != CPU_NEWLY_IDLE)
4527 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004528
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004529 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004530 raw_spin_lock_irqsave(&busiest->lock, flags);
4531
Tejun Heo969c7922010-05-06 18:49:21 +02004532 /* don't kick the active_load_balance_cpu_stop,
4533 * if the curr task on busiest cpu can't be
4534 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004535 */
4536 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004537 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004538 raw_spin_unlock_irqrestore(&busiest->lock,
4539 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004540 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004541 goto out_one_pinned;
4542 }
4543
Tejun Heo969c7922010-05-06 18:49:21 +02004544 /*
4545 * ->active_balance synchronizes accesses to
4546 * ->active_balance_work. Once set, it's cleared
4547 * only after active load balance is finished.
4548 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004549 if (!busiest->active_balance) {
4550 busiest->active_balance = 1;
4551 busiest->push_cpu = this_cpu;
4552 active_balance = 1;
4553 }
4554 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02004555
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004556 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02004557 stop_one_cpu_nowait(cpu_of(busiest),
4558 active_load_balance_cpu_stop, busiest,
4559 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004560
4561 /*
4562 * We've kicked active balancing, reset the failure
4563 * counter.
4564 */
4565 sd->nr_balance_failed = sd->cache_nice_tries+1;
4566 }
Steve Muckle8f77c282013-03-11 16:33:42 -07004567 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004568 sd->nr_balance_failed = 0;
Steve Muckle8f77c282013-03-11 16:33:42 -07004569 if (per_cpu(dbs_boost_needed, this_cpu)) {
4570 per_cpu(dbs_boost_needed, this_cpu) = false;
4571 atomic_notifier_call_chain(&migration_notifier_head,
4572 this_cpu,
4573 (void *)cpu_of(busiest));
4574 }
4575 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004576 if (likely(!active_balance)) {
4577 /* We were unbalanced, so reset the balancing interval */
4578 sd->balance_interval = sd->min_interval;
4579 } else {
4580 /*
4581 * If we've begun active balancing, start to back off. This
4582 * case may not be covered by the all_pinned logic if there
4583 * is only 1 task on the busy runqueue (because we don't call
4584 * move_tasks).
4585 */
4586 if (sd->balance_interval < sd->max_interval)
4587 sd->balance_interval *= 2;
4588 }
4589
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004590 goto out;
4591
4592out_balanced:
4593 schedstat_inc(sd, lb_balanced[idle]);
4594
4595 sd->nr_balance_failed = 0;
4596
4597out_one_pinned:
4598 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004599 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02004600 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004601 (sd->balance_interval < sd->max_interval))
4602 sd->balance_interval *= 2;
4603
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08004604 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004605out:
Steve Muckleeaf00192013-11-19 14:16:53 -08004606 trace_sched_load_balance(this_cpu, idle, *balance,
4607 group ? group->cpumask[0] : 0,
4608 busiest ? busiest->nr_running : 0, imbalance,
4609 env.flags, ld_moved, sd->balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004610 return ld_moved;
4611}
4612
4613/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004614 * idle_balance is called by schedule() if this_cpu is about to become
4615 * idle. Attempts to pull tasks from other CPUs.
4616 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004617void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004618{
4619 struct sched_domain *sd;
4620 int pulled_task = 0;
4621 unsigned long next_balance = jiffies + HZ;
4622
4623 this_rq->idle_stamp = this_rq->clock;
4624
4625 if (this_rq->avg_idle < sysctl_sched_migration_cost)
4626 return;
4627
Peter Zijlstraf492e122009-12-23 15:29:42 +01004628 /*
4629 * Drop the rq->lock, but keep IRQ/preempt disabled.
4630 */
4631 raw_spin_unlock(&this_rq->lock);
4632
Paul Turnerc66eaf62010-11-15 15:47:07 -08004633 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02004634 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004635 for_each_domain(this_cpu, sd) {
4636 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01004637 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004638
4639 if (!(sd->flags & SD_LOAD_BALANCE))
4640 continue;
4641
Peter Zijlstraf492e122009-12-23 15:29:42 +01004642 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004643 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01004644 pulled_task = load_balance(this_cpu, this_rq,
4645 sd, CPU_NEWLY_IDLE, &balance);
4646 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004647
4648 interval = msecs_to_jiffies(sd->balance_interval);
4649 if (time_after(next_balance, sd->last_balance + interval))
4650 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004651 if (pulled_task) {
4652 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004653 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08004654 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004655 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004656 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01004657
4658 raw_spin_lock(&this_rq->lock);
4659
Srivatsa Vaddagirice1a0412013-03-07 12:14:53 -08004660 if (!pulled_task || time_after(jiffies, this_rq->next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004661 /*
4662 * We are going idle. next_balance may be set based on
4663 * a busy processor. So reset next_balance.
4664 */
4665 this_rq->next_balance = next_balance;
4666 }
4667}
4668
4669/*
Tejun Heo969c7922010-05-06 18:49:21 +02004670 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
4671 * running tasks off the busiest CPU onto idle CPUs. It requires at
4672 * least 1 task to be running on each physical CPU where possible, and
4673 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004674 */
Tejun Heo969c7922010-05-06 18:49:21 +02004675static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004676{
Tejun Heo969c7922010-05-06 18:49:21 +02004677 struct rq *busiest_rq = data;
4678 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004679 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02004680 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004681 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02004682
4683 raw_spin_lock_irq(&busiest_rq->lock);
4684
4685 /* make sure the requested cpu hasn't gone down in the meantime */
4686 if (unlikely(busiest_cpu != smp_processor_id() ||
4687 !busiest_rq->active_balance))
4688 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004689
4690 /* Is there any task to move? */
4691 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02004692 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004693
4694 /*
4695 * This condition is "impossible", if it occurs
4696 * we need to fix it. Originally reported by
4697 * Bjorn Helgaas on a 128-cpu setup.
4698 */
4699 BUG_ON(busiest_rq == target_rq);
4700
4701 /* move a task from busiest_rq to target_rq */
4702 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004703
4704 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02004705 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004706 for_each_domain(target_cpu, sd) {
4707 if ((sd->flags & SD_LOAD_BALANCE) &&
4708 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4709 break;
4710 }
4711
4712 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004713 struct lb_env env = {
4714 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004715 .dst_cpu = target_cpu,
4716 .dst_rq = target_rq,
4717 .src_cpu = busiest_rq->cpu,
4718 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004719 .idle = CPU_IDLE,
4720 };
4721
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004722 schedstat_inc(sd, alb_count);
4723
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004724 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004725 schedstat_inc(sd, alb_pushed);
4726 else
4727 schedstat_inc(sd, alb_failed);
4728 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004729 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004730 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02004731out_unlock:
4732 busiest_rq->active_balance = 0;
4733 raw_spin_unlock_irq(&busiest_rq->lock);
Steve Muckle8f77c282013-03-11 16:33:42 -07004734 if (per_cpu(dbs_boost_needed, target_cpu)) {
4735 per_cpu(dbs_boost_needed, target_cpu) = false;
4736 atomic_notifier_call_chain(&migration_notifier_head,
4737 target_cpu,
4738 (void *)cpu_of(busiest_rq));
4739 }
Tejun Heo969c7922010-05-06 18:49:21 +02004740 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004741}
4742
4743#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004744/*
4745 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004746 * - When one of the busy CPUs notice that there may be an idle rebalancing
4747 * needed, they will kick the idle load balancer, which then does idle
4748 * load balancing for all the idle CPUs.
4749 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004750static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004751 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004752 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004753 unsigned long next_balance; /* in jiffy units */
4754} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004755
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004756#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4757/**
4758 * lowest_flag_domain - Return lowest sched_domain containing flag.
4759 * @cpu: The cpu whose lowest level of sched domain is to
4760 * be returned.
4761 * @flag: The flag to check for the lowest sched_domain
4762 * for the given cpu.
4763 *
4764 * Returns the lowest sched_domain of a cpu which contains the given flag.
4765 */
4766static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4767{
4768 struct sched_domain *sd;
4769
4770 for_each_domain(cpu, sd)
Hillf Danton08354712011-06-16 21:55:19 -04004771 if (sd->flags & flag)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004772 break;
4773
4774 return sd;
4775}
4776
4777/**
4778 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4779 * @cpu: The cpu whose domains we're iterating over.
4780 * @sd: variable holding the value of the power_savings_sd
4781 * for cpu.
4782 * @flag: The flag to filter the sched_domains to be iterated.
4783 *
4784 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4785 * set, starting from the lowest sched_domain to the highest.
4786 */
4787#define for_each_flag_domain(cpu, sd, flag) \
4788 for (sd = lowest_flag_domain(cpu, flag); \
4789 (sd && (sd->flags & flag)); sd = sd->parent)
4790
4791/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004792 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4793 * @cpu: The cpu which is nominating a new idle_load_balancer.
4794 *
4795 * Returns: Returns the id of the idle load balancer if it exists,
4796 * Else, returns >= nr_cpu_ids.
4797 *
4798 * This algorithm picks the idle load balancer such that it belongs to a
4799 * semi-idle powersavings sched_domain. The idea is to try and avoid
4800 * completely idle packages/cores just for the purpose of idle load balancing
4801 * when there are other idle cpu's which are better suited for that job.
4802 */
4803static int find_new_ilb(int cpu)
4804{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004805 int ilb = cpumask_first(nohz.idle_cpus_mask);
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004806 struct sched_group *ilbg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004807 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004808
4809 /*
4810 * Have idle load balancer selection from semi-idle packages only
4811 * when power-aware load balancing is enabled
4812 */
4813 if (!(sched_smt_power_savings || sched_mc_power_savings))
4814 goto out_done;
4815
4816 /*
4817 * Optimize for the case when we have no idle CPUs or only one
4818 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4819 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004820 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004821 goto out_done;
4822
Peter Zijlstradce840a2011-04-07 14:09:50 +02004823 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004824 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004825 ilbg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004826
4827 do {
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004828 if (ilbg->group_weight !=
4829 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4830 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4831 sched_group_cpus(ilbg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02004832 goto unlock;
4833 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004834
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004835 ilbg = ilbg->next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004836
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004837 } while (ilbg != sd->groups);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004838 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004839unlock:
4840 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004841
4842out_done:
Suresh Siddha786d6dc2011-12-01 17:07:35 -08004843 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4844 return ilb;
4845
4846 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004847}
4848#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4849static inline int find_new_ilb(int call_cpu)
4850{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004851 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004852}
4853#endif
4854
4855/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004856 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
4857 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
4858 * CPU (if there is one).
4859 */
4860static void nohz_balancer_kick(int cpu)
4861{
4862 int ilb_cpu;
4863
4864 nohz.next_balance++;
4865
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004866 ilb_cpu = find_new_ilb(cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004867
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004868 if (ilb_cpu >= nr_cpu_ids)
4869 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004870
Suresh Siddhacd490c52011-12-06 11:26:34 -08004871 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08004872 return;
4873 /*
4874 * Use smp_send_reschedule() instead of resched_cpu().
4875 * This way we generate a sched IPI on the target cpu which
4876 * is idle. And the softirq performing nohz idle load balance
4877 * will be run before returning from the IPI.
4878 */
4879 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004880 return;
4881}
4882
Suresh Siddha71325962012-01-19 18:28:57 -08004883static inline void clear_nohz_tick_stopped(int cpu)
4884{
4885 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
4886 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
4887 atomic_dec(&nohz.nr_cpus);
4888 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
4889 }
4890}
4891
Suresh Siddha69e1e812011-12-01 17:07:33 -08004892static inline void set_cpu_sd_state_busy(void)
4893{
4894 struct sched_domain *sd;
4895 int cpu = smp_processor_id();
4896
4897 if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4898 return;
4899 clear_bit(NOHZ_IDLE, nohz_flags(cpu));
4900
4901 rcu_read_lock();
4902 for_each_domain(cpu, sd)
4903 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
4904 rcu_read_unlock();
4905}
4906
4907void set_cpu_sd_state_idle(void)
4908{
4909 struct sched_domain *sd;
4910 int cpu = smp_processor_id();
4911
4912 if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
4913 return;
4914 set_bit(NOHZ_IDLE, nohz_flags(cpu));
4915
4916 rcu_read_lock();
4917 for_each_domain(cpu, sd)
4918 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
4919 rcu_read_unlock();
4920}
4921
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004922/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004923 * This routine will record that this cpu is going idle with tick stopped.
4924 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004925 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004926void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004927{
4928 int cpu = smp_processor_id();
4929
Suresh Siddha71325962012-01-19 18:28:57 -08004930 /*
4931 * If this cpu is going down, then nothing needs to be done.
4932 */
4933 if (!cpu_active(cpu))
4934 return;
4935
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004936 if (stop_tick) {
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004937 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004938 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004939
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004940 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08004941 atomic_inc(&nohz.nr_cpus);
Suresh Siddha1c792db2011-12-01 17:07:32 -08004942 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004943 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004944 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004945}
Suresh Siddha71325962012-01-19 18:28:57 -08004946
4947static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
4948 unsigned long action, void *hcpu)
4949{
4950 switch (action & ~CPU_TASKS_FROZEN) {
4951 case CPU_DYING:
4952 clear_nohz_tick_stopped(smp_processor_id());
4953 return NOTIFY_OK;
4954 default:
4955 return NOTIFY_DONE;
4956 }
4957}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004958#endif
4959
4960static DEFINE_SPINLOCK(balancing);
4961
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004962/*
4963 * Scale the max load_balance interval with the number of CPUs in the system.
4964 * This trades load-balance latency on larger machines for less cross talk.
4965 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004966void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02004967{
4968 max_load_balance_interval = HZ*num_online_cpus()/10;
4969}
4970
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004971/*
4972 * It checks each scheduling domain to see if it is due to be balanced,
4973 * and initiates a balancing operation if so.
4974 *
4975 * Balancing parameters are set up in arch_init_sched_domains.
4976 */
4977static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4978{
4979 int balance = 1;
4980 struct rq *rq = cpu_rq(cpu);
4981 unsigned long interval;
4982 struct sched_domain *sd;
4983 /* Earliest time when we have to do rebalance again */
4984 unsigned long next_balance = jiffies + 60*HZ;
4985 int update_next_balance = 0;
4986 int need_serialize;
4987
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004988 update_shares(cpu);
4989
Peter Zijlstradce840a2011-04-07 14:09:50 +02004990 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004991 for_each_domain(cpu, sd) {
4992 if (!(sd->flags & SD_LOAD_BALANCE))
4993 continue;
4994
4995 interval = sd->balance_interval;
4996 if (idle != CPU_IDLE)
4997 interval *= sd->busy_factor;
4998
4999 /* scale ms to jiffies */
5000 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005001 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005002
5003 need_serialize = sd->flags & SD_SERIALIZE;
5004
5005 if (need_serialize) {
5006 if (!spin_trylock(&balancing))
5007 goto out;
5008 }
5009
5010 if (time_after_eq(jiffies, sd->last_balance + interval)) {
5011 if (load_balance(cpu, rq, sd, idle, &balance)) {
5012 /*
5013 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005014 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005015 */
5016 idle = CPU_NOT_IDLE;
5017 }
5018 sd->last_balance = jiffies;
5019 }
5020 if (need_serialize)
5021 spin_unlock(&balancing);
5022out:
5023 if (time_after(next_balance, sd->last_balance + interval)) {
5024 next_balance = sd->last_balance + interval;
5025 update_next_balance = 1;
5026 }
5027
5028 /*
5029 * Stop the load balance at this level. There is another
5030 * CPU in our sched group which is doing load balancing more
5031 * actively.
5032 */
5033 if (!balance)
5034 break;
5035 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005036 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005037
5038 /*
5039 * next_balance will be updated only when there is a need.
5040 * When the cpu is attached to null domain for ex, it will not be
5041 * updated.
5042 */
5043 if (likely(update_next_balance))
5044 rq->next_balance = next_balance;
5045}
5046
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005047#ifdef CONFIG_NO_HZ
5048/*
5049 * In CONFIG_NO_HZ case, the idle balance kickee will do the
5050 * rebalancing for all the cpus for whom scheduler ticks are stopped.
5051 */
5052static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5053{
5054 struct rq *this_rq = cpu_rq(this_cpu);
5055 struct rq *rq;
5056 int balance_cpu;
5057
Suresh Siddha1c792db2011-12-01 17:07:32 -08005058 if (idle != CPU_IDLE ||
5059 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
5060 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005061
5062 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08005063 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005064 continue;
5065
5066 /*
5067 * If this cpu gets work to do, stop the load balancing
5068 * work being done for other cpus. Next load
5069 * balancing owner will pick it up.
5070 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08005071 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005072 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005073
5074 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02005075 update_rq_clock(this_rq);
Peter Zijlstrae2d51f22012-05-11 17:31:26 +02005076 update_idle_cpu_load(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005077 raw_spin_unlock_irq(&this_rq->lock);
5078
5079 rebalance_domains(balance_cpu, CPU_IDLE);
5080
5081 rq = cpu_rq(balance_cpu);
5082 if (time_after(this_rq->next_balance, rq->next_balance))
5083 this_rq->next_balance = rq->next_balance;
5084 }
5085 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005086end:
5087 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005088}
5089
5090/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005091 * Current heuristic for kicking the idle load balancer in the presence
5092 * of an idle cpu is the system.
5093 * - This rq has more than one task.
5094 * - At any scheduler domain level, this cpu's scheduler group has multiple
5095 * busy cpu's exceeding the group's power.
5096 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
5097 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005098 */
5099static inline int nohz_kick_needed(struct rq *rq, int cpu)
5100{
5101 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005102 struct sched_domain *sd;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005103
Suresh Siddha1c792db2011-12-01 17:07:32 -08005104 if (unlikely(idle_cpu(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005105 return 0;
5106
Suresh Siddha1c792db2011-12-01 17:07:32 -08005107 /*
5108 * We may be recently in ticked or tickless idle mode. At the first
5109 * busy tick after returning from idle, we will update the busy stats.
5110 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08005111 set_cpu_sd_state_busy();
Suresh Siddha71325962012-01-19 18:28:57 -08005112 clear_nohz_tick_stopped(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005113
5114 /*
5115 * None are in tickless mode and hence no need for NOHZ idle load
5116 * balancing.
5117 */
5118 if (likely(!atomic_read(&nohz.nr_cpus)))
5119 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08005120
5121 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005122 return 0;
5123
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005124 if (rq->nr_running >= 2)
5125 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005126
Peter Zijlstra067491b2011-12-07 14:32:08 +01005127 rcu_read_lock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005128 for_each_domain(cpu, sd) {
5129 struct sched_group *sg = sd->groups;
5130 struct sched_group_power *sgp = sg->sgp;
5131 int nr_busy = atomic_read(&sgp->nr_busy_cpus);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005132
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005133 if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01005134 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005135
5136 if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
5137 && (cpumask_first_and(nohz.idle_cpus_mask,
5138 sched_domain_span(sd)) < cpu))
Peter Zijlstra067491b2011-12-07 14:32:08 +01005139 goto need_kick_unlock;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005140
5141 if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
5142 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005143 }
Peter Zijlstra067491b2011-12-07 14:32:08 +01005144 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005145 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01005146
5147need_kick_unlock:
5148 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08005149need_kick:
5150 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005151}
5152#else
5153static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
5154#endif
5155
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005156/*
5157 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005158 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005159 */
5160static void run_rebalance_domains(struct softirq_action *h)
5161{
5162 int this_cpu = smp_processor_id();
5163 struct rq *this_rq = cpu_rq(this_cpu);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07005164 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005165 CPU_IDLE : CPU_NOT_IDLE;
5166
5167 rebalance_domains(this_cpu, idle);
5168
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005169 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005170 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005171 * balancing on behalf of the other idle cpus whose ticks are
5172 * stopped.
5173 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005174 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005175}
5176
5177static inline int on_null_domain(int cpu)
5178{
Paul E. McKenney90a65012010-02-28 08:32:18 -08005179 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005180}
5181
5182/*
5183 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005184 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005185void trigger_load_balance(struct rq *rq, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005186{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005187 /* Don't need to rebalance while attached to NULL domain */
5188 if (time_after_eq(jiffies, rq->next_balance) &&
5189 likely(!on_null_domain(cpu)))
5190 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005191#ifdef CONFIG_NO_HZ
Suresh Siddha1c792db2011-12-01 17:07:32 -08005192 if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07005193 nohz_balancer_kick(cpu);
5194#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005195}
5196
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005197static void rq_online_fair(struct rq *rq)
5198{
5199 update_sysctl();
5200}
5201
5202static void rq_offline_fair(struct rq *rq)
5203{
5204 update_sysctl();
Peter Boonstoppele38ca492012-08-09 15:34:47 -07005205
5206 /* Ensure any throttled groups are reachable by pick_next_task */
5207 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005208}
5209
Dhaval Giani55e12e52008-06-24 23:39:43 +05305210#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02005211
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005212/*
5213 * scheduler tick hitting a task of our scheduling class:
5214 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005215static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005216{
5217 struct cfs_rq *cfs_rq;
5218 struct sched_entity *se = &curr->se;
5219
5220 for_each_sched_entity(se) {
5221 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005222 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005223 }
5224}
5225
5226/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005227 * called on fork with the child task as argument from the parent's context
5228 * - child not yet on the tasklist
5229 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005230 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005231static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005232{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005233 struct cfs_rq *cfs_rq;
5234 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02005235 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005236 struct rq *rq = this_rq();
5237 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005238
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005239 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005240
Peter Zijlstra861d0342010-08-19 13:31:43 +02005241 update_rq_clock(rq);
5242
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09005243 cfs_rq = task_cfs_rq(current);
5244 curr = cfs_rq->curr;
5245
Daisuke Nishimura628753322013-09-10 18:16:36 +09005246 /*
5247 * Not only the cpu but also the task_group of the parent might have
5248 * been changed after parent->se.parent,cfs_rq were copied to
5249 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
5250 * of child point to valid ones.
5251 */
5252 rcu_read_lock();
5253 __set_task_cpu(p, this_cpu);
5254 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005255
Ting Yang7109c442007-08-28 12:53:24 +02005256 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005257
Mike Galbraithb5d9d732009-09-08 11:12:28 +02005258 if (curr)
5259 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02005260 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005261
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005262 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02005263 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02005264 * Upon rescheduling, sched_class::put_prev_task() will place
5265 * 'current' within the tree based on its new key value.
5266 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005267 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05305268 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02005269 }
5270
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005271 se->vruntime -= cfs_rq->min_vruntime;
5272
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005273 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005274}
5275
Steven Rostedtcb469842008-01-25 21:08:22 +01005276/*
5277 * Priority of the task has changed. Check to see if we preempt
5278 * the current task.
5279 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005280static void
5281prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01005282{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005283 if (!p->se.on_rq)
5284 return;
5285
Steven Rostedtcb469842008-01-25 21:08:22 +01005286 /*
5287 * Reschedule if we are currently running on this runqueue and
5288 * our priority decreased, or if we are not currently running on
5289 * this runqueue and our priority is higher than the current's
5290 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005291 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01005292 if (p->prio > oldprio)
5293 resched_task(rq->curr);
5294 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005295 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005296}
5297
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005298static void switched_from_fair(struct rq *rq, struct task_struct *p)
5299{
5300 struct sched_entity *se = &p->se;
5301 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5302
5303 /*
George McCollister6ba4d1d2014-02-18 17:56:51 -06005304 * Ensure the task's vruntime is normalized, so that when it's
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005305 * switched back to the fair class the enqueue_entity(.flags=0) will
5306 * do the right thing.
5307 *
George McCollister6ba4d1d2014-02-18 17:56:51 -06005308 * If it's on_rq, then the dequeue_entity(.flags=0) will already
5309 * have normalized the vruntime, if it's !on_rq, then only when
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005310 * the task is sleeping will it still have non-normalized vruntime.
5311 */
George McCollister6ba4d1d2014-02-18 17:56:51 -06005312 if (!p->on_rq && p->state != TASK_RUNNING) {
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005313 /*
5314 * Fix up our vruntime so that the current sleep doesn't
5315 * cause 'unlimited' sleep bonus.
5316 */
5317 place_entity(cfs_rq, se, 0);
5318 se->vruntime -= cfs_rq->min_vruntime;
5319 }
5320}
5321
Steven Rostedtcb469842008-01-25 21:08:22 +01005322/*
5323 * We switched to the sched_fair class.
5324 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005325static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005326{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005327 if (!p->se.on_rq)
5328 return;
5329
Steven Rostedtcb469842008-01-25 21:08:22 +01005330 /*
5331 * We were most likely switched from sched_rt, so
5332 * kick off the schedule if running, otherwise just see
5333 * if we can still preempt the current task.
5334 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005335 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01005336 resched_task(rq->curr);
5337 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02005338 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005339}
5340
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005341/* Account for a task changing its policy or group.
5342 *
5343 * This routine is mostly called to set cfs_rq->curr field when a task
5344 * migrates between groups/classes.
5345 */
5346static void set_curr_task_fair(struct rq *rq)
5347{
5348 struct sched_entity *se = &rq->curr->se;
5349
Paul Turnerec12cb72011-07-21 09:43:30 -07005350 for_each_sched_entity(se) {
5351 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5352
5353 set_next_entity(cfs_rq, se);
5354 /* ensure bandwidth has been allocated on our new cfs_rq */
5355 account_cfs_rq_runtime(cfs_rq, 0);
5356 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005357}
5358
Peter Zijlstra029632f2011-10-25 10:00:11 +02005359void init_cfs_rq(struct cfs_rq *cfs_rq)
5360{
5361 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005362 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
5363#ifndef CONFIG_64BIT
5364 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
5365#endif
5366}
5367
Peter Zijlstra810b3812008-02-29 15:21:01 -05005368#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005369static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05005370{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005371 /*
5372 * If the task was not on the rq at the time of this cgroup movement
5373 * it must have been asleep, sleeping tasks keep their ->vruntime
5374 * absolute on their old rq until wakeup (needed for the fair sleeper
5375 * bonus in place_entity()).
5376 *
5377 * If it was on the rq, we've just 'preempted' it, which does convert
5378 * ->vruntime to a relative base.
5379 *
5380 * Make sure both cases convert their relative position when migrating
5381 * to another cgroup's rq. This does somewhat interfere with the
5382 * fair sleeper stuff for the first placement, but who cares.
5383 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005384 /*
5385 * When !on_rq, vruntime of the task has usually NOT been normalized.
5386 * But there are some cases where it has already been normalized:
5387 *
5388 * - Moving a forked child which is waiting for being woken up by
5389 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005390 * - Moving a task which has been woken up by try_to_wake_up() and
5391 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005392 *
5393 * To prevent boost or penalty in the new cfs_rq caused by delta
5394 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
5395 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09005396 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09005397 on_rq = 1;
5398
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005399 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005400 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
5401 set_task_rq(p, task_cpu(p));
5402 if (!on_rq)
5403 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005404}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005405
5406void free_fair_sched_group(struct task_group *tg)
5407{
5408 int i;
5409
5410 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
5411
5412 for_each_possible_cpu(i) {
5413 if (tg->cfs_rq)
5414 kfree(tg->cfs_rq[i]);
5415 if (tg->se)
5416 kfree(tg->se[i]);
5417 }
5418
5419 kfree(tg->cfs_rq);
5420 kfree(tg->se);
5421}
5422
5423int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5424{
5425 struct cfs_rq *cfs_rq;
5426 struct sched_entity *se;
5427 int i;
5428
5429 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
5430 if (!tg->cfs_rq)
5431 goto err;
5432 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
5433 if (!tg->se)
5434 goto err;
5435
5436 tg->shares = NICE_0_LOAD;
5437
5438 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
5439
5440 for_each_possible_cpu(i) {
5441 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
5442 GFP_KERNEL, cpu_to_node(i));
5443 if (!cfs_rq)
5444 goto err;
5445
5446 se = kzalloc_node(sizeof(struct sched_entity),
5447 GFP_KERNEL, cpu_to_node(i));
5448 if (!se)
5449 goto err_free_rq;
5450
5451 init_cfs_rq(cfs_rq);
5452 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
5453 }
5454
5455 return 1;
5456
5457err_free_rq:
5458 kfree(cfs_rq);
5459err:
5460 return 0;
5461}
5462
5463void unregister_fair_sched_group(struct task_group *tg, int cpu)
5464{
5465 struct rq *rq = cpu_rq(cpu);
5466 unsigned long flags;
5467
5468 /*
5469 * Only empty task groups can be destroyed; so we can speculatively
5470 * check on_list without danger of it being re-added.
5471 */
5472 if (!tg->cfs_rq[cpu]->on_list)
5473 return;
5474
5475 raw_spin_lock_irqsave(&rq->lock, flags);
5476 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
5477 raw_spin_unlock_irqrestore(&rq->lock, flags);
5478}
5479
5480void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
5481 struct sched_entity *se, int cpu,
5482 struct sched_entity *parent)
5483{
5484 struct rq *rq = cpu_rq(cpu);
5485
5486 cfs_rq->tg = tg;
5487 cfs_rq->rq = rq;
5488#ifdef CONFIG_SMP
5489 /* allow initial update_cfs_load() to truncate */
5490 cfs_rq->load_stamp = 1;
Peter Zijlstra810b3812008-02-29 15:21:01 -05005491#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005492 init_cfs_rq_runtime(cfs_rq);
5493
5494 tg->cfs_rq[cpu] = cfs_rq;
5495 tg->se[cpu] = se;
5496
5497 /* se could be NULL for root_task_group */
5498 if (!se)
5499 return;
5500
5501 if (!parent)
5502 se->cfs_rq = &rq->cfs;
5503 else
5504 se->cfs_rq = parent->my_q;
5505
5506 se->my_q = cfs_rq;
Paul Turner9af6b692013-10-16 11:16:27 -07005507 /* guarantee group entities always have weight */
5508 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005509 se->parent = parent;
5510}
5511
5512static DEFINE_MUTEX(shares_mutex);
5513
5514int sched_group_set_shares(struct task_group *tg, unsigned long shares)
5515{
5516 int i;
5517 unsigned long flags;
5518
5519 /*
5520 * We can't change the weight of the root cgroup.
5521 */
5522 if (!tg->se[0])
5523 return -EINVAL;
5524
5525 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
5526
5527 mutex_lock(&shares_mutex);
5528 if (tg->shares == shares)
5529 goto done;
5530
5531 tg->shares = shares;
5532 for_each_possible_cpu(i) {
5533 struct rq *rq = cpu_rq(i);
5534 struct sched_entity *se;
5535
5536 se = tg->se[i];
5537 /* Propagate contribution to hierarchy */
5538 raw_spin_lock_irqsave(&rq->lock, flags);
5539 for_each_sched_entity(se)
5540 update_cfs_shares(group_cfs_rq(se));
5541 raw_spin_unlock_irqrestore(&rq->lock, flags);
5542 }
5543
5544done:
5545 mutex_unlock(&shares_mutex);
5546 return 0;
5547}
5548#else /* CONFIG_FAIR_GROUP_SCHED */
5549
5550void free_fair_sched_group(struct task_group *tg) { }
5551
5552int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
5553{
5554 return 1;
5555}
5556
5557void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
5558
5559#endif /* CONFIG_FAIR_GROUP_SCHED */
5560
Peter Zijlstra810b3812008-02-29 15:21:01 -05005561
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07005562static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00005563{
5564 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00005565 unsigned int rr_interval = 0;
5566
5567 /*
5568 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
5569 * idle runqueue:
5570 */
Peter Williams0d721ce2009-09-21 01:31:53 +00005571 if (rq->cfs.load.weight)
Zhu Yanhai1836cd12013-01-08 12:56:52 +08005572 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00005573
5574 return rr_interval;
5575}
5576
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005577/*
5578 * All the scheduling class methods:
5579 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02005580const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005581 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005582 .enqueue_task = enqueue_task_fair,
5583 .dequeue_task = dequeue_task_fair,
5584 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05005585 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005586
Ingo Molnar2e09bf52007-10-15 17:00:05 +02005587 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005588
5589 .pick_next_task = pick_next_task_fair,
5590 .put_prev_task = put_prev_task_fair,
5591
Peter Williams681f3e62007-10-24 18:23:51 +02005592#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08005593 .select_task_rq = select_task_rq_fair,
5594
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005595 .rq_online = rq_online_fair,
5596 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005597
5598 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02005599#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005600
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005601 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005602 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01005603 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005604
5605 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005606 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01005607 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005608
Peter Williams0d721ce2009-09-21 01:31:53 +00005609 .get_rr_interval = get_rr_interval_fair,
5610
Peter Zijlstra810b3812008-02-29 15:21:01 -05005611#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02005612 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05005613#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005614};
5615
5616#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02005617void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005618{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005619 struct cfs_rq *cfs_rq;
5620
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005621 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02005622 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02005623 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01005624 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005625}
5626#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02005627
5628__init void init_sched_fair_class(void)
5629{
5630#ifdef CONFIG_SMP
5631 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
5632
5633#ifdef CONFIG_NO_HZ
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08005634 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005635 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08005636 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005637#endif
5638#endif /* SMP */
5639
5640}