blob: 0588c0b933a8d4b2a2ff74ff7b4abbc2a4caed0c [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>
Arjan van de Ven97455122008-01-25 21:08:34 +010026
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020027/*
Peter Zijlstra21805082007-08-25 18:41:53 +020028 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090029 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020030 *
Peter Zijlstra21805082007-08-25 18:41:53 +020031 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020032 * 'timeslice length' - timeslices in CFS are of variable length
33 * and have no persistent notion like in traditional, time-slice
34 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020035 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020036 * (to see the precise effective timeslice length of your workload,
37 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020038 */
Mike Galbraith21406922010-03-11 17:17:15 +010039unsigned int sysctl_sched_latency = 6000000ULL;
40unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020041
42/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010043 * The initial- and re-scaling of tunables is configurable
44 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
45 *
46 * Options are:
47 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
48 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
49 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
50 */
51enum sched_tunable_scaling sysctl_sched_tunable_scaling
52 = SCHED_TUNABLESCALING_LOG;
53
54/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010055 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090056 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010057 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020058unsigned int sysctl_sched_min_granularity = 750000ULL;
59unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010060
61/*
62 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
63 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020064static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010065
66/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020067 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020068 * parent will (try to) run first.
69 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020070unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020071
72/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020073 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020074 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020075 *
76 * This option delays the preemption effects of decoupled workloads
77 * and reduces their over-scheduling. Synchronous workloads will still
78 * have immediate wakeup/sleep latencies.
79 */
Mike Galbraith172e0822009-09-09 15:41:37 +020080unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010081unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082
Ingo Molnarda84d962007-10-15 17:00:18 +020083const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
84
Paul Turnera7a4f8a2010-11-15 15:47:06 -080085/*
86 * The exponential sliding window over which load is averaged for shares
87 * distribution.
88 * (default: 10msec)
89 */
90unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
91
Peter Zijlstraa4c2f002008-10-17 19:27:03 +020092static const struct sched_class fair_sched_class;
93
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020094/**************************************************************
95 * CFS operations on generic schedulable entities:
96 */
97
98#ifdef CONFIG_FAIR_GROUP_SCHED
99
100/* cpu runqueue to which this cfs_rq is attached */
101static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
102{
103 return cfs_rq->rq;
104}
105
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200106/* An entity is a task if it doesn't "own" a runqueue */
107#define entity_is_task(se) (!se->my_q)
108
Peter Zijlstra8f488942009-07-24 12:25:30 +0200109static inline struct task_struct *task_of(struct sched_entity *se)
110{
111#ifdef CONFIG_SCHED_DEBUG
112 WARN_ON_ONCE(!entity_is_task(se));
113#endif
114 return container_of(se, struct task_struct, se);
115}
116
Peter Zijlstrab7581492008-04-19 19:45:00 +0200117/* Walk up scheduling entities hierarchy */
118#define for_each_sched_entity(se) \
119 for (; se; se = se->parent)
120
121static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
122{
123 return p->se.cfs_rq;
124}
125
126/* runqueue on which this entity is (to be) queued */
127static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
128{
129 return se->cfs_rq;
130}
131
132/* runqueue "owned" by this group */
133static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
134{
135 return grp->my_q;
136}
137
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800138static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
139{
140 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800141 /*
142 * Ensure we either appear before our parent (if already
143 * enqueued) or force our parent to appear after us when it is
144 * enqueued. The fact that we always enqueue bottom-up
145 * reduces this to two cases.
146 */
147 if (cfs_rq->tg->parent &&
148 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
149 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800150 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800151 } else {
152 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
153 &rq_of(cfs_rq)->leaf_cfs_rq_list);
154 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800155
156 cfs_rq->on_list = 1;
157 }
158}
159
160static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
161{
162 if (cfs_rq->on_list) {
163 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
164 cfs_rq->on_list = 0;
165 }
166}
167
Peter Zijlstrab7581492008-04-19 19:45:00 +0200168/* Iterate thr' all leaf cfs_rq's on a runqueue */
169#define for_each_leaf_cfs_rq(rq, cfs_rq) \
170 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
171
172/* Do the two (enqueued) entities belong to the same group ? */
173static inline int
174is_same_group(struct sched_entity *se, struct sched_entity *pse)
175{
176 if (se->cfs_rq == pse->cfs_rq)
177 return 1;
178
179 return 0;
180}
181
182static inline struct sched_entity *parent_entity(struct sched_entity *se)
183{
184 return se->parent;
185}
186
Peter Zijlstra464b7522008-10-24 11:06:15 +0200187/* return depth at which a sched entity is present in the hierarchy */
188static inline int depth_se(struct sched_entity *se)
189{
190 int depth = 0;
191
192 for_each_sched_entity(se)
193 depth++;
194
195 return depth;
196}
197
198static void
199find_matching_se(struct sched_entity **se, struct sched_entity **pse)
200{
201 int se_depth, pse_depth;
202
203 /*
204 * preemption test can be made between sibling entities who are in the
205 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
206 * both tasks until we find their ancestors who are siblings of common
207 * parent.
208 */
209
210 /* First walk up until both entities are at same depth */
211 se_depth = depth_se(*se);
212 pse_depth = depth_se(*pse);
213
214 while (se_depth > pse_depth) {
215 se_depth--;
216 *se = parent_entity(*se);
217 }
218
219 while (pse_depth > se_depth) {
220 pse_depth--;
221 *pse = parent_entity(*pse);
222 }
223
224 while (!is_same_group(*se, *pse)) {
225 *se = parent_entity(*se);
226 *pse = parent_entity(*pse);
227 }
228}
229
Peter Zijlstra8f488942009-07-24 12:25:30 +0200230#else /* !CONFIG_FAIR_GROUP_SCHED */
231
232static inline struct task_struct *task_of(struct sched_entity *se)
233{
234 return container_of(se, struct task_struct, se);
235}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200236
237static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
238{
239 return container_of(cfs_rq, struct rq, cfs);
240}
241
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200242#define entity_is_task(se) 1
243
Peter Zijlstrab7581492008-04-19 19:45:00 +0200244#define for_each_sched_entity(se) \
245 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200246
Peter Zijlstrab7581492008-04-19 19:45:00 +0200247static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200248{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200249 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200250}
251
Peter Zijlstrab7581492008-04-19 19:45:00 +0200252static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
253{
254 struct task_struct *p = task_of(se);
255 struct rq *rq = task_rq(p);
256
257 return &rq->cfs;
258}
259
260/* runqueue "owned" by this group */
261static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
262{
263 return NULL;
264}
265
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800266static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
267{
268}
269
270static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
271{
272}
273
Peter Zijlstrab7581492008-04-19 19:45:00 +0200274#define for_each_leaf_cfs_rq(rq, cfs_rq) \
275 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
276
277static inline int
278is_same_group(struct sched_entity *se, struct sched_entity *pse)
279{
280 return 1;
281}
282
283static inline struct sched_entity *parent_entity(struct sched_entity *se)
284{
285 return NULL;
286}
287
Peter Zijlstra464b7522008-10-24 11:06:15 +0200288static inline void
289find_matching_se(struct sched_entity **se, struct sched_entity **pse)
290{
291}
292
Peter Zijlstrab7581492008-04-19 19:45:00 +0200293#endif /* CONFIG_FAIR_GROUP_SCHED */
294
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200295
296/**************************************************************
297 * Scheduling class tree data structure manipulation methods:
298 */
299
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200300static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200301{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200302 s64 delta = (s64)(vruntime - min_vruntime);
303 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200304 min_vruntime = vruntime;
305
306 return min_vruntime;
307}
308
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200309static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200310{
311 s64 delta = (s64)(vruntime - min_vruntime);
312 if (delta < 0)
313 min_vruntime = vruntime;
314
315 return min_vruntime;
316}
317
Fabio Checconi54fdc582009-07-16 12:32:27 +0200318static inline int entity_before(struct sched_entity *a,
319 struct sched_entity *b)
320{
321 return (s64)(a->vruntime - b->vruntime) < 0;
322}
323
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200324static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra90146232007-10-15 17:00:05 +0200325{
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200326 return se->vruntime - cfs_rq->min_vruntime;
Peter Zijlstra90146232007-10-15 17:00:05 +0200327}
328
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200329static void update_min_vruntime(struct cfs_rq *cfs_rq)
330{
331 u64 vruntime = cfs_rq->min_vruntime;
332
333 if (cfs_rq->curr)
334 vruntime = cfs_rq->curr->vruntime;
335
336 if (cfs_rq->rb_leftmost) {
337 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
338 struct sched_entity,
339 run_node);
340
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100341 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200342 vruntime = se->vruntime;
343 else
344 vruntime = min_vruntime(vruntime, se->vruntime);
345 }
346
347 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200348#ifndef CONFIG_64BIT
349 smp_wmb();
350 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
351#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200352}
353
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200354/*
355 * Enqueue an entity into the rb-tree:
356 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200357static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200358{
359 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
360 struct rb_node *parent = NULL;
361 struct sched_entity *entry;
Peter Zijlstra90146232007-10-15 17:00:05 +0200362 s64 key = entity_key(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200363 int leftmost = 1;
364
365 /*
366 * Find the right place in the rbtree:
367 */
368 while (*link) {
369 parent = *link;
370 entry = rb_entry(parent, struct sched_entity, run_node);
371 /*
372 * We dont care about collisions. Nodes with
373 * the same key stay together.
374 */
Peter Zijlstra90146232007-10-15 17:00:05 +0200375 if (key < entity_key(cfs_rq, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200376 link = &parent->rb_left;
377 } else {
378 link = &parent->rb_right;
379 leftmost = 0;
380 }
381 }
382
383 /*
384 * Maintain a cache of leftmost tree entries (it is frequently
385 * used):
386 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200387 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200388 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200389
390 rb_link_node(&se->run_node, parent, link);
391 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200392}
393
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200394static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200395{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100396 if (cfs_rq->rb_leftmost == &se->run_node) {
397 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100398
399 next_node = rb_next(&se->run_node);
400 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100401 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200402
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200403 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200404}
405
Rik van Rielac53db52011-02-01 09:51:03 -0500406static struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200407{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100408 struct rb_node *left = cfs_rq->rb_leftmost;
409
410 if (!left)
411 return NULL;
412
413 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200414}
415
Rik van Rielac53db52011-02-01 09:51:03 -0500416static struct sched_entity *__pick_next_entity(struct sched_entity *se)
417{
418 struct rb_node *next = rb_next(&se->run_node);
419
420 if (!next)
421 return NULL;
422
423 return rb_entry(next, struct sched_entity, run_node);
424}
425
426#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100427static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200428{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100429 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200430
Balbir Singh70eee742008-02-22 13:25:53 +0530431 if (!last)
432 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100433
434 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200435}
436
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200437/**************************************************************
438 * Scheduling class statistics methods:
439 */
440
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100441int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700442 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100443 loff_t *ppos)
444{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700445 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100446 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100447
448 if (ret || !write)
449 return ret;
450
451 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
452 sysctl_sched_min_granularity);
453
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100454#define WRT_SYSCTL(name) \
455 (normalized_sysctl_##name = sysctl_##name / (factor))
456 WRT_SYSCTL(sched_min_granularity);
457 WRT_SYSCTL(sched_latency);
458 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100459#undef WRT_SYSCTL
460
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100461 return 0;
462}
463#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200464
465/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200466 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200467 */
468static inline unsigned long
469calc_delta_fair(unsigned long delta, struct sched_entity *se)
470{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200471 if (unlikely(se->load.weight != NICE_0_LOAD))
472 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200473
474 return delta;
475}
476
477/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200478 * The idea is to set a period in which each task runs once.
479 *
480 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
481 * this period because otherwise the slices get too small.
482 *
483 * p = (nr <= nl) ? l : l*nr/nl
484 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200485static u64 __sched_period(unsigned long nr_running)
486{
487 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100488 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200489
490 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100491 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200492 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200493 }
494
495 return period;
496}
497
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200498/*
499 * We calculate the wall-time slice from the period by taking a part
500 * proportional to the weight.
501 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200502 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200503 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200504static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200505{
Mike Galbraith0a582442009-01-02 12:16:42 +0100506 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200507
Mike Galbraith0a582442009-01-02 12:16:42 +0100508 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100509 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200510 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100511
512 cfs_rq = cfs_rq_of(se);
513 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200514
Mike Galbraith0a582442009-01-02 12:16:42 +0100515 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200516 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100517
518 update_load_add(&lw, se->load.weight);
519 load = &lw;
520 }
521 slice = calc_delta_mine(slice, se->load.weight, load);
522 }
523 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200524}
525
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200526/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200527 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200528 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200529 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200530 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200531static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200532{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200533 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200534}
535
Paul Turnerd6b55912010-11-15 15:47:09 -0800536static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update);
Paul Turner6d5ab292011-01-21 20:45:01 -0800537static void update_cfs_shares(struct cfs_rq *cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800538
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200539/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200540 * Update the current task's runtime statistics. Skip current tasks that
541 * are not in our scheduling class.
542 */
543static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200544__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
545 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200546{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200547 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200548
Lucas De Marchi41acab82010-03-10 23:37:45 -0300549 schedstat_set(curr->statistics.exec_max,
550 max((u64)delta_exec, curr->statistics.exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200551
552 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200553 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200554 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100555
Ingo Molnare9acbff2007-10-15 17:00:04 +0200556 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200557 update_min_vruntime(cfs_rq);
Paul Turner3b3d1902010-11-15 15:47:08 -0800558
Peter Zijlstra70caf8a2010-11-20 00:53:51 +0100559#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
Paul Turner3b3d1902010-11-15 15:47:08 -0800560 cfs_rq->load_unacc_exec_time += delta_exec;
Paul Turner3b3d1902010-11-15 15:47:08 -0800561#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562}
563
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200564static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200565{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200566 struct sched_entity *curr = cfs_rq->curr;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700567 u64 now = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200568 unsigned long delta_exec;
569
570 if (unlikely(!curr))
571 return;
572
573 /*
574 * Get the amount of time the current task was running
575 * since the last time we changed load (this cannot
576 * overflow on 32 bits):
577 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200578 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100579 if (!delta_exec)
580 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200581
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200582 __update_curr(cfs_rq, curr, delta_exec);
583 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100584
585 if (entity_is_task(curr)) {
586 struct task_struct *curtask = task_of(curr);
587
Ingo Molnarf977bb42009-09-13 18:15:54 +0200588 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100589 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700590 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100591 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200592}
593
594static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200595update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200596{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300597 schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200598}
599
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200600/*
601 * Task is being enqueued - update stats:
602 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200603static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200604{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200605 /*
606 * Are we enqueueing a waiting task? (for current tasks
607 * a dequeue/enqueue event is a NOP)
608 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200609 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200610 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200611}
612
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200613static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200614update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200615{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300616 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
617 rq_of(cfs_rq)->clock - se->statistics.wait_start));
618 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
619 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
620 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200621#ifdef CONFIG_SCHEDSTATS
622 if (entity_is_task(se)) {
623 trace_sched_stat_wait(task_of(se),
Lucas De Marchi41acab82010-03-10 23:37:45 -0300624 rq_of(cfs_rq)->clock - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200625 }
626#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300627 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200628}
629
630static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200631update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200632{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200633 /*
634 * Mark the end of the wait period if dequeueing a
635 * waiting task:
636 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200637 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200638 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200639}
640
641/*
642 * We are picking a new current task - update its stats:
643 */
644static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200645update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200646{
647 /*
648 * We are starting a new run period:
649 */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700650 se->exec_start = rq_of(cfs_rq)->clock_task;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200651}
652
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200653/**************************************************
654 * Scheduling class queueing methods:
655 */
656
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200657#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
658static void
659add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
660{
661 cfs_rq->task_weight += weight;
662}
663#else
664static inline void
665add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
666{
667}
668#endif
669
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200670static void
671account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
672{
673 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200674 if (!parent_entity(se))
675 inc_cpu_load(rq_of(cfs_rq), se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530676 if (entity_is_task(se)) {
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200677 add_cfs_task_weight(cfs_rq, se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530678 list_add(&se->group_node, &cfs_rq->tasks);
679 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200680 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200681}
682
683static void
684account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
685{
686 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200687 if (!parent_entity(se))
688 dec_cpu_load(rq_of(cfs_rq), se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530689 if (entity_is_task(se)) {
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200690 add_cfs_task_weight(cfs_rq, -se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530691 list_del_init(&se->group_node);
692 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200693 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200694}
695
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800696#ifdef CONFIG_FAIR_GROUP_SCHED
697# ifdef CONFIG_SMP
Paul Turnerd6b55912010-11-15 15:47:09 -0800698static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq,
699 int global_update)
700{
701 struct task_group *tg = cfs_rq->tg;
702 long load_avg;
703
704 load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1);
705 load_avg -= cfs_rq->load_contribution;
706
707 if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) {
708 atomic_add(load_avg, &tg->load_weight);
709 cfs_rq->load_contribution += load_avg;
710 }
711}
712
713static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800714{
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800715 u64 period = sysctl_sched_shares_window;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800716 u64 now, delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800717 unsigned long load = cfs_rq->load.weight;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800718
Paul Turnerb815f192011-01-21 20:45:00 -0800719 if (cfs_rq->tg == &root_task_group)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800720 return;
721
Paul Turner05ca62c2011-01-21 20:45:02 -0800722 now = rq_of(cfs_rq)->clock_task;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800723 delta = now - cfs_rq->load_stamp;
724
Paul Turnere33078b2010-11-15 15:47:04 -0800725 /* truncate load history at 4 idle periods */
726 if (cfs_rq->load_stamp > cfs_rq->load_last &&
727 now - cfs_rq->load_last > 4 * period) {
728 cfs_rq->load_period = 0;
729 cfs_rq->load_avg = 0;
Paul Turnerf07333b2011-01-21 20:45:03 -0800730 delta = period - 1;
Paul Turnere33078b2010-11-15 15:47:04 -0800731 }
732
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800733 cfs_rq->load_stamp = now;
Paul Turner3b3d1902010-11-15 15:47:08 -0800734 cfs_rq->load_unacc_exec_time = 0;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800735 cfs_rq->load_period += delta;
Paul Turnere33078b2010-11-15 15:47:04 -0800736 if (load) {
737 cfs_rq->load_last = now;
738 cfs_rq->load_avg += delta * load;
739 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800740
Paul Turnerd6b55912010-11-15 15:47:09 -0800741 /* consider updating load contribution on each fold or truncate */
742 if (global_update || cfs_rq->load_period > period
743 || !cfs_rq->load_period)
744 update_cfs_rq_load_contribution(cfs_rq, global_update);
745
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800746 while (cfs_rq->load_period > period) {
747 /*
748 * Inline assembly required to prevent the compiler
749 * optimising this loop into a divmod call.
750 * See __iter_div_u64_rem() for another example of this.
751 */
752 asm("" : "+rm" (cfs_rq->load_period));
753 cfs_rq->load_period /= 2;
754 cfs_rq->load_avg /= 2;
755 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800756
Paul Turnere33078b2010-11-15 15:47:04 -0800757 if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg)
758 list_del_leaf_cfs_rq(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800759}
760
Paul Turner6d5ab292011-01-21 20:45:01 -0800761static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800762{
763 long load_weight, load, shares;
764
Paul Turner6d5ab292011-01-21 20:45:01 -0800765 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800766
767 load_weight = atomic_read(&tg->load_weight);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800768 load_weight += load;
Paul Turner6d5ab292011-01-21 20:45:01 -0800769 load_weight -= cfs_rq->load_contribution;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800770
771 shares = (tg->shares * load);
772 if (load_weight)
773 shares /= load_weight;
774
775 if (shares < MIN_SHARES)
776 shares = MIN_SHARES;
777 if (shares > tg->shares)
778 shares = tg->shares;
779
780 return shares;
781}
782
783static void update_entity_shares_tick(struct cfs_rq *cfs_rq)
784{
785 if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) {
786 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -0800787 update_cfs_shares(cfs_rq);
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800788 }
789}
790# else /* CONFIG_SMP */
791static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
792{
793}
794
Paul Turner6d5ab292011-01-21 20:45:01 -0800795static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800796{
797 return tg->shares;
798}
799
800static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
801{
802}
803# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800804static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
805 unsigned long weight)
806{
Paul Turner19e5eeb2010-12-15 19:10:18 -0800807 if (se->on_rq) {
808 /* commit outstanding execution time */
809 if (cfs_rq->curr == se)
810 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800811 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -0800812 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800813
814 update_load_set(&se->load, weight);
815
816 if (se->on_rq)
817 account_entity_enqueue(cfs_rq, se);
818}
819
Paul Turner6d5ab292011-01-21 20:45:01 -0800820static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800821{
822 struct task_group *tg;
823 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800824 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800825
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800826 tg = cfs_rq->tg;
827 se = tg->se[cpu_of(rq_of(cfs_rq))];
828 if (!se)
829 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +0800830#ifndef CONFIG_SMP
831 if (likely(se->load.weight == tg->shares))
832 return;
833#endif
Paul Turner6d5ab292011-01-21 20:45:01 -0800834 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800835
836 reweight_entity(cfs_rq_of(se), se, shares);
837}
838#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerd6b55912010-11-15 15:47:09 -0800839static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800840{
841}
842
Paul Turner6d5ab292011-01-21 20:45:01 -0800843static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800844{
845}
Paul Turner43365bd2010-12-15 19:10:17 -0800846
847static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq)
848{
849}
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800850#endif /* CONFIG_FAIR_GROUP_SCHED */
851
Ingo Molnar2396af62007-08-09 11:16:48 +0200852static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200853{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200854#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +0200855 struct task_struct *tsk = NULL;
856
857 if (entity_is_task(se))
858 tsk = task_of(se);
859
Lucas De Marchi41acab82010-03-10 23:37:45 -0300860 if (se->statistics.sleep_start) {
861 u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200862
863 if ((s64)delta < 0)
864 delta = 0;
865
Lucas De Marchi41acab82010-03-10 23:37:45 -0300866 if (unlikely(delta > se->statistics.sleep_max))
867 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200868
Lucas De Marchi41acab82010-03-10 23:37:45 -0300869 se->statistics.sleep_start = 0;
870 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +0100871
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200872 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +0200873 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200874 trace_sched_stat_sleep(tsk, delta);
875 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200876 }
Lucas De Marchi41acab82010-03-10 23:37:45 -0300877 if (se->statistics.block_start) {
878 u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200879
880 if ((s64)delta < 0)
881 delta = 0;
882
Lucas De Marchi41acab82010-03-10 23:37:45 -0300883 if (unlikely(delta > se->statistics.block_max))
884 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200885
Lucas De Marchi41acab82010-03-10 23:37:45 -0300886 se->statistics.block_start = 0;
887 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +0200888
Peter Zijlstrae4143142009-07-23 20:13:26 +0200889 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -0700890 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -0300891 se->statistics.iowait_sum += delta;
892 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200893 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -0700894 }
895
Peter Zijlstrae4143142009-07-23 20:13:26 +0200896 /*
897 * Blocking time is in units of nanosecs, so shift by
898 * 20 to get a milliseconds-range estimation of the
899 * amount of time that the task spent sleeping:
900 */
901 if (unlikely(prof_on == SLEEP_PROFILING)) {
902 profile_hits(SLEEP_PROFILING,
903 (void *)get_wchan(tsk),
904 delta >> 20);
905 }
906 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +0200907 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200908 }
909#endif
910}
911
Peter Zijlstraddc97292007-10-15 17:00:10 +0200912static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
913{
914#ifdef CONFIG_SCHED_DEBUG
915 s64 d = se->vruntime - cfs_rq->min_vruntime;
916
917 if (d < 0)
918 d = -d;
919
920 if (d > 3*sysctl_sched_latency)
921 schedstat_inc(cfs_rq, nr_spread_over);
922#endif
923}
924
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200925static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200926place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
927{
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200928 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200929
Peter Zijlstra2cb86002007-11-09 22:39:37 +0100930 /*
931 * The 'current' period is already promised to the current tasks,
932 * however the extra weight of the new task will slow them down a
933 * little, place the new task so that it fits in the slot that
934 * stays open at the end.
935 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200936 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200937 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200938
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200939 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +0100940 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200941 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200942
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200943 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200944 * Halve their sleep time's effect, to allow
945 * for a gentler effect of sleepers:
946 */
947 if (sched_feat(GENTLE_FAIR_SLEEPERS))
948 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +0200949
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200950 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200951 }
952
Mike Galbraithb5d9d732009-09-08 11:12:28 +0200953 /* ensure we never gain time by being placed backwards. */
954 vruntime = max_vruntime(se->vruntime, vruntime);
955
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200956 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200957}
958
959static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100960enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200961{
962 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100963 * Update the normalized vruntime before updating min_vruntime
964 * through callig update_curr().
965 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100966 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100967 se->vruntime += cfs_rq->min_vruntime;
968
969 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +0200970 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200971 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200972 update_curr(cfs_rq);
Paul Turnerd6b55912010-11-15 15:47:09 -0800973 update_cfs_load(cfs_rq, 0);
Peter Zijlstraa9922412008-05-05 23:56:17 +0200974 account_entity_enqueue(cfs_rq, se);
Paul Turner6d5ab292011-01-21 20:45:01 -0800975 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200976
Peter Zijlstra88ec22d2009-12-16 18:04:41 +0100977 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200978 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +0200979 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200980 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200981
Ingo Molnard2417e52007-08-09 11:16:47 +0200982 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +0200983 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200984 if (se != cfs_rq->curr)
985 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800986 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800987
988 if (cfs_rq->nr_running == 1)
989 list_add_leaf_cfs_rq(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200990}
991
Rik van Riel2c13c9192011-02-01 09:48:37 -0500992static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +0100993{
Rik van Riel2c13c9192011-02-01 09:48:37 -0500994 for_each_sched_entity(se) {
995 struct cfs_rq *cfs_rq = cfs_rq_of(se);
996 if (cfs_rq->last == se)
997 cfs_rq->last = NULL;
998 else
999 break;
1000 }
1001}
Peter Zijlstra2002c692008-11-11 11:52:33 +01001002
Rik van Riel2c13c9192011-02-01 09:48:37 -05001003static void __clear_buddies_next(struct sched_entity *se)
1004{
1005 for_each_sched_entity(se) {
1006 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1007 if (cfs_rq->next == se)
1008 cfs_rq->next = NULL;
1009 else
1010 break;
1011 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01001012}
1013
Rik van Rielac53db52011-02-01 09:51:03 -05001014static void __clear_buddies_skip(struct sched_entity *se)
1015{
1016 for_each_sched_entity(se) {
1017 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1018 if (cfs_rq->skip == se)
1019 cfs_rq->skip = NULL;
1020 else
1021 break;
1022 }
1023}
1024
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001025static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
1026{
Rik van Riel2c13c9192011-02-01 09:48:37 -05001027 if (cfs_rq->last == se)
1028 __clear_buddies_last(se);
1029
1030 if (cfs_rq->next == se)
1031 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05001032
1033 if (cfs_rq->skip == se)
1034 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01001035}
1036
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001037static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001038dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001039{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001040 /*
1041 * Update run-time statistics of the 'current'.
1042 */
1043 update_curr(cfs_rq);
1044
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02001045 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001046 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001047#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001048 if (entity_is_task(se)) {
1049 struct task_struct *tsk = task_of(se);
1050
1051 if (tsk->state & TASK_INTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001052 se->statistics.sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001053 if (tsk->state & TASK_UNINTERRUPTIBLE)
Lucas De Marchi41acab82010-03-10 23:37:45 -03001054 se->statistics.block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001055 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02001056#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02001057 }
1058
Peter Zijlstra2002c692008-11-11 11:52:33 +01001059 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001060
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001061 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001062 __dequeue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001063 se->on_rq = 0;
Paul Turnerd6b55912010-11-15 15:47:09 -08001064 update_cfs_load(cfs_rq, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001065 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001066
1067 /*
1068 * Normalize the entity after updating the min_vruntime because the
1069 * update can refer to the ->curr item and we need to reflect this
1070 * movement in our normalized position.
1071 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001072 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001073 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07001074
1075 update_min_vruntime(cfs_rq);
1076 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001077}
1078
1079/*
1080 * Preempt the current task with a newly woken task if needed:
1081 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02001082static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001083check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001084{
Peter Zijlstra11697832007-09-05 14:32:49 +02001085 unsigned long ideal_runtime, delta_exec;
1086
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02001087 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02001088 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001089 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001090 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001091 /*
1092 * The current task ran long enough, ensure it doesn't get
1093 * re-elected due to buddy favours.
1094 */
1095 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001096 return;
1097 }
1098
1099 /*
1100 * Ensure that a task that missed wakeup preemption by a
1101 * narrow margin doesn't have to wait for a full slice.
1102 * This also mitigates buddy induced latencies under load.
1103 */
1104 if (!sched_feat(WAKEUP_PREEMPT))
1105 return;
1106
1107 if (delta_exec < sysctl_sched_min_granularity)
1108 return;
1109
1110 if (cfs_rq->nr_running > 1) {
Rik van Rielac53db52011-02-01 09:51:03 -05001111 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001112 s64 delta = curr->vruntime - se->vruntime;
1113
Mike Galbraithd7d82942011-01-05 05:41:17 +01001114 if (delta < 0)
1115 return;
1116
Mike Galbraithf685cea2009-10-23 23:09:22 +02001117 if (delta > ideal_runtime)
1118 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01001119 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001120}
1121
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001122static void
Ingo Molnar8494f412007-08-09 11:16:48 +02001123set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001124{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001125 /* 'current' is not kept within the tree. */
1126 if (se->on_rq) {
1127 /*
1128 * Any task has to be enqueued before it get to execute on
1129 * a CPU. So account for the time it spent waiting on the
1130 * runqueue.
1131 */
1132 update_stats_wait_end(cfs_rq, se);
1133 __dequeue_entity(cfs_rq, se);
1134 }
1135
Ingo Molnar79303e92007-08-09 11:16:47 +02001136 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001137 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001138#ifdef CONFIG_SCHEDSTATS
1139 /*
1140 * Track our maximum slice length, if the CPU's load is at
1141 * least twice that of our own weight (i.e. dont track it
1142 * when there are only lesser-weight tasks around):
1143 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001144 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03001145 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02001146 se->sum_exec_runtime - se->prev_sum_exec_runtime);
1147 }
1148#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02001149 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001150}
1151
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02001152static int
1153wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
1154
Rik van Rielac53db52011-02-01 09:51:03 -05001155/*
1156 * Pick the next process, keeping these things in mind, in this order:
1157 * 1) keep things fair between processes/task groups
1158 * 2) pick the "next" process, since someone really wants that to run
1159 * 3) pick the "last" process, for cache locality
1160 * 4) do not run the "skip" process, if something else is available
1161 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001162static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001163{
Rik van Rielac53db52011-02-01 09:51:03 -05001164 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001165 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001166
Rik van Rielac53db52011-02-01 09:51:03 -05001167 /*
1168 * Avoid running the skip buddy, if running something else can
1169 * be done without getting too unfair.
1170 */
1171 if (cfs_rq->skip == se) {
1172 struct sched_entity *second = __pick_next_entity(se);
1173 if (second && wakeup_preempt_entity(second, left) < 1)
1174 se = second;
1175 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001176
Mike Galbraithf685cea2009-10-23 23:09:22 +02001177 /*
1178 * Prefer last buddy, try to return the CPU to a preempted task.
1179 */
1180 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
1181 se = cfs_rq->last;
1182
Rik van Rielac53db52011-02-01 09:51:03 -05001183 /*
1184 * Someone really wants this to run. If it's not unfair, run it.
1185 */
1186 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
1187 se = cfs_rq->next;
1188
Mike Galbraithf685cea2009-10-23 23:09:22 +02001189 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01001190
1191 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001192}
1193
Ingo Molnarab6cde22007-08-09 11:16:48 +02001194static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001195{
1196 /*
1197 * If still on the runqueue then deactivate_task()
1198 * was not called and update_curr() has to be done:
1199 */
1200 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02001201 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001202
Peter Zijlstraddc97292007-10-15 17:00:10 +02001203 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001204 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02001205 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001206 /* Put 'current' back into the tree. */
1207 __enqueue_entity(cfs_rq, prev);
1208 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02001209 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001210}
1211
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001212static void
1213entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001214{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001215 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001216 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001217 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001218 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001219
Paul Turner43365bd2010-12-15 19:10:17 -08001220 /*
1221 * Update share accounting for long-running entities.
1222 */
1223 update_entity_shares_tick(cfs_rq);
1224
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001225#ifdef CONFIG_SCHED_HRTICK
1226 /*
1227 * queued ticks are scheduled to match the slice, so don't bother
1228 * validating it and just reschedule.
1229 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07001230 if (queued) {
1231 resched_task(rq_of(cfs_rq)->curr);
1232 return;
1233 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001234 /*
1235 * don't let the period tick interfere with the hrtick preemption
1236 */
1237 if (!sched_feat(DOUBLE_TICK) &&
1238 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
1239 return;
1240#endif
1241
Peter Zijlstrace6c1312007-10-15 17:00:14 +02001242 if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001243 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001244}
1245
1246/**************************************************
1247 * CFS operations on tasks:
1248 */
1249
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001250#ifdef CONFIG_SCHED_HRTICK
1251static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
1252{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001253 struct sched_entity *se = &p->se;
1254 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1255
1256 WARN_ON(task_rq(p) != rq);
1257
1258 if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
1259 u64 slice = sched_slice(cfs_rq, se);
1260 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
1261 s64 delta = slice - ran;
1262
1263 if (delta < 0) {
1264 if (rq->curr == p)
1265 resched_task(p);
1266 return;
1267 }
1268
1269 /*
1270 * Don't schedule slices shorter than 10000ns, that just
1271 * doesn't make sense. Rely on vruntime for fairness.
1272 */
Peter Zijlstra31656512008-07-18 18:01:23 +02001273 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02001274 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001275
Peter Zijlstra31656512008-07-18 18:01:23 +02001276 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001277 }
1278}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001279
1280/*
1281 * called from enqueue/dequeue and updates the hrtick when the
1282 * current task is from our class and nr_running is low enough
1283 * to matter.
1284 */
1285static void hrtick_update(struct rq *rq)
1286{
1287 struct task_struct *curr = rq->curr;
1288
1289 if (curr->sched_class != &fair_sched_class)
1290 return;
1291
1292 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
1293 hrtick_start_fair(rq, curr);
1294}
Dhaval Giani55e12e52008-06-24 23:39:43 +05301295#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001296static inline void
1297hrtick_start_fair(struct rq *rq, struct task_struct *p)
1298{
1299}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001300
1301static inline void hrtick_update(struct rq *rq)
1302{
1303}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001304#endif
1305
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001306/*
1307 * The enqueue_task method is called before nr_running is
1308 * increased. Here we update the fair scheduling stats and
1309 * then put the task into the rbtree:
1310 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001311static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001312enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001313{
1314 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001315 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001316
1317 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001318 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001319 break;
1320 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001321 enqueue_entity(cfs_rq, se, flags);
1322 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001323 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001324
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001325 for_each_sched_entity(se) {
1326 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1327
Paul Turnerd6b55912010-11-15 15:47:09 -08001328 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08001329 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001330 }
1331
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001332 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001333}
1334
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001335static void set_next_buddy(struct sched_entity *se);
1336
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001337/*
1338 * The dequeue_task method is called before nr_running is
1339 * decreased. We remove the task from the rbtree and
1340 * update the fair scheduling stats:
1341 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001342static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001343{
1344 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001345 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001346 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001347
1348 for_each_sched_entity(se) {
1349 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001350 dequeue_entity(cfs_rq, se, flags);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001351
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001352 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001353 if (cfs_rq->load.weight) {
1354 /*
1355 * Bias pick_next to pick a task from this cfs_rq, as
1356 * p is sleeping when it is within its sched_slice.
1357 */
1358 if (task_sleep && parent_entity(se))
1359 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07001360
1361 /* avoid re-evaluating load for this entity */
1362 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001363 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001364 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001365 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001366 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001367
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001368 for_each_sched_entity(se) {
1369 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1370
Paul Turnerd6b55912010-11-15 15:47:09 -08001371 update_cfs_load(cfs_rq, 0);
Paul Turner6d5ab292011-01-21 20:45:01 -08001372 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001373 }
1374
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001375 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001376}
1377
Gregory Haskinse7693a32008-01-25 21:08:09 +01001378#ifdef CONFIG_SMP
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001379
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001380static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001381{
1382 struct sched_entity *se = &p->se;
1383 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02001384 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001385
Peter Zijlstra3fe16982011-04-05 17:23:48 +02001386#ifndef CONFIG_64BIT
1387 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001388
Peter Zijlstra3fe16982011-04-05 17:23:48 +02001389 do {
1390 min_vruntime_copy = cfs_rq->min_vruntime_copy;
1391 smp_rmb();
1392 min_vruntime = cfs_rq->min_vruntime;
1393 } while (min_vruntime != min_vruntime_copy);
1394#else
1395 min_vruntime = cfs_rq->min_vruntime;
1396#endif
1397
1398 se->vruntime -= min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01001399}
1400
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001401#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02001402/*
1403 * effective_load() calculates the load change as seen from the root_task_group
1404 *
1405 * Adding load to a group doesn't make a group heavier, but can cause movement
1406 * of group shares between cpus. Assuming the shares were perfectly aligned one
1407 * can calculate the shift in shares.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02001408 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001409static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001410{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001411 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02001412
1413 if (!tg->parent)
1414 return wl;
1415
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001416 for_each_sched_entity(se) {
Paul Turner977dda72011-01-14 17:57:50 -08001417 long lw, w;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001418
Paul Turner977dda72011-01-14 17:57:50 -08001419 tg = se->my_q->tg;
1420 w = se->my_q->load.weight;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001421
Paul Turner977dda72011-01-14 17:57:50 -08001422 /* use this cpu's instantaneous contribution */
1423 lw = atomic_read(&tg->load_weight);
1424 lw -= se->my_q->load_contribution;
1425 lw += w + wg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001426
Paul Turner977dda72011-01-14 17:57:50 -08001427 wl += w;
Peter Zijlstra940959e2008-09-23 15:33:42 +02001428
Paul Turner977dda72011-01-14 17:57:50 -08001429 if (lw > 0 && wl < lw)
1430 wl = (wl * tg->shares) / lw;
1431 else
1432 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02001433
Paul Turner977dda72011-01-14 17:57:50 -08001434 /* zero point is MIN_SHARES */
1435 if (wl < MIN_SHARES)
1436 wl = MIN_SHARES;
1437 wl -= se->load.weight;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001438 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001439 }
1440
1441 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001442}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001443
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001444#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001445
Peter Zijlstra83378262008-06-27 13:41:37 +02001446static inline unsigned long effective_load(struct task_group *tg, int cpu,
1447 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001448{
Peter Zijlstra83378262008-06-27 13:41:37 +02001449 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001450}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001451
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001452#endif
1453
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001454static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001455{
Paul Turnere37b6a72011-01-21 20:44:59 -08001456 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001457 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001458 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001459 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02001460 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001461 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001462
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001463 idx = sd->wake_idx;
1464 this_cpu = smp_processor_id();
1465 prev_cpu = task_cpu(p);
1466 load = source_load(prev_cpu, idx);
1467 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001468
1469 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001470 * If sync wakeup then subtract the (maximum possible)
1471 * effect of the currently running task from the load
1472 * of the current CPU:
1473 */
Peter Zijlstra83378262008-06-27 13:41:37 +02001474 if (sync) {
1475 tg = task_group(current);
1476 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001477
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001478 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02001479 load += effective_load(tg, prev_cpu, 0, -weight);
1480 }
1481
1482 tg = task_group(p);
1483 weight = p->se.load.weight;
1484
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02001485 /*
1486 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001487 * due to the sync cause above having dropped this_load to 0, we'll
1488 * always have an imbalance, but there's really nothing you can do
1489 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02001490 *
1491 * Otherwise check if either cpus are near enough in load to allow this
1492 * task to be woken on this_cpu.
1493 */
Paul Turnere37b6a72011-01-21 20:44:59 -08001494 if (this_load > 0) {
1495 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001496
1497 this_eff_load = 100;
1498 this_eff_load *= power_of(prev_cpu);
1499 this_eff_load *= this_load +
1500 effective_load(tg, this_cpu, weight, weight);
1501
1502 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
1503 prev_eff_load *= power_of(this_cpu);
1504 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
1505
1506 balanced = this_eff_load <= prev_eff_load;
1507 } else
1508 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001509
1510 /*
1511 * If the currently running task will sleep within
1512 * a reasonable amount of time then attract this newly
1513 * woken task:
1514 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02001515 if (sync && balanced)
1516 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001517
Lucas De Marchi41acab82010-03-10 23:37:45 -03001518 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001519 tl_per_task = cpu_avg_load_per_task(this_cpu);
1520
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001521 if (balanced ||
1522 (this_load <= load &&
1523 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001524 /*
1525 * This domain has SD_WAKE_AFFINE and
1526 * p is cache cold in this domain, and
1527 * there is no bad imbalance.
1528 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001529 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03001530 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001531
1532 return 1;
1533 }
1534 return 0;
1535}
1536
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001537/*
1538 * find_idlest_group finds and returns the least busy CPU group within the
1539 * domain.
1540 */
1541static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02001542find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001543 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001544{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001545 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001546 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001547 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001548
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001549 do {
1550 unsigned long load, avg_load;
1551 int local_group;
1552 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001553
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001554 /* Skip over this group if it has no CPUs allowed */
1555 if (!cpumask_intersects(sched_group_cpus(group),
1556 &p->cpus_allowed))
1557 continue;
1558
1559 local_group = cpumask_test_cpu(this_cpu,
1560 sched_group_cpus(group));
1561
1562 /* Tally up the load of all CPUs in the group */
1563 avg_load = 0;
1564
1565 for_each_cpu(i, sched_group_cpus(group)) {
1566 /* Bias balancing toward cpus of our domain */
1567 if (local_group)
1568 load = source_load(i, load_idx);
1569 else
1570 load = target_load(i, load_idx);
1571
1572 avg_load += load;
1573 }
1574
1575 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02001576 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001577
1578 if (local_group) {
1579 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001580 } else if (avg_load < min_load) {
1581 min_load = avg_load;
1582 idlest = group;
1583 }
1584 } while (group = group->next, group != sd->groups);
1585
1586 if (!idlest || 100*this_load < imbalance*min_load)
1587 return NULL;
1588 return idlest;
1589}
1590
1591/*
1592 * find_idlest_cpu - find the idlest cpu among the cpus in group.
1593 */
1594static int
1595find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
1596{
1597 unsigned long load, min_load = ULONG_MAX;
1598 int idlest = -1;
1599 int i;
1600
1601 /* Traverse only the allowed CPUs */
1602 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
1603 load = weighted_cpuload(i);
1604
1605 if (load < min_load || (load == min_load && i == this_cpu)) {
1606 min_load = load;
1607 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001608 }
1609 }
1610
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001611 return idlest;
1612}
Gregory Haskinse7693a32008-01-25 21:08:09 +01001613
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001614/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001615 * Try and locate an idle CPU in the sched_domain.
1616 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001617static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001618{
1619 int cpu = smp_processor_id();
1620 int prev_cpu = task_cpu(p);
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001621 struct sched_domain *sd;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001622 int i;
1623
1624 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001625 * If the task is going to be woken-up on this cpu and if it is
1626 * already idle, then it is the right target.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001627 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001628 if (target == cpu && idle_cpu(cpu))
1629 return cpu;
1630
1631 /*
1632 * If the task is going to be woken-up on the cpu where it previously
1633 * ran and if it is currently idle, then it the right target.
1634 */
1635 if (target == prev_cpu && idle_cpu(prev_cpu))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001636 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001637
1638 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001639 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001640 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02001641 rcu_read_lock();
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001642 for_each_domain(target, sd) {
1643 if (!(sd->flags & SD_SHARE_PKG_RESOURCES))
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001644 break;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001645
1646 for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
1647 if (idle_cpu(i)) {
1648 target = i;
1649 break;
1650 }
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001651 }
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001652
1653 /*
1654 * Lets stop looking for an idle sibling when we reached
1655 * the domain that spans the current cpu and prev_cpu.
1656 */
1657 if (cpumask_test_cpu(cpu, sched_domain_span(sd)) &&
1658 cpumask_test_cpu(prev_cpu, sched_domain_span(sd)))
1659 break;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001660 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02001661 rcu_read_unlock();
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001662
1663 return target;
1664}
1665
1666/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001667 * sched_balance_self: balance the current task (running on cpu) in domains
1668 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1669 * SD_BALANCE_EXEC.
1670 *
1671 * Balance, ie. select the least loaded group.
1672 *
1673 * Returns the target CPU number, or the same CPU if no balancing is needed.
1674 *
1675 * preempt must be disabled.
1676 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01001677static int
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001678select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001679{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001680 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001681 int cpu = smp_processor_id();
1682 int prev_cpu = task_cpu(p);
1683 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001684 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001685 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001686 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001687
Peter Zijlstra0763a662009-09-14 19:37:39 +02001688 if (sd_flag & SD_BALANCE_WAKE) {
Mike Galbraithbeac4c72010-03-11 17:17:20 +01001689 if (cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001690 want_affine = 1;
1691 new_cpu = prev_cpu;
1692 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001693
Peter Zijlstradce840a2011-04-07 14:09:50 +02001694 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001695 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01001696 if (!(tmp->flags & SD_LOAD_BALANCE))
1697 continue;
1698
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001699 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02001700 * If power savings logic is enabled for a domain, see if we
1701 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001702 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02001703 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02001704 unsigned long power = 0;
1705 unsigned long nr_running = 0;
1706 unsigned long capacity;
1707 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001708
Peter Zijlstraae154be2009-09-10 14:40:57 +02001709 for_each_cpu(i, sched_domain_span(tmp)) {
1710 power += power_of(i);
1711 nr_running += cpu_rq(i)->cfs.nr_running;
1712 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001713
Nikhil Rao1399fa72011-05-18 10:09:39 -07001714 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001715
Peter Zijlstra59abf022009-09-16 08:28:30 +02001716 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1717 nr_running /= 2;
1718
1719 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001720 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001721 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001722
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001723 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001724 * If both cpu and prev_cpu are part of this domain,
1725 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001726 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001727 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
1728 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
1729 affine_sd = tmp;
1730 want_affine = 0;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001731 }
1732
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001733 if (!want_sd && !want_affine)
1734 break;
1735
Peter Zijlstra0763a662009-09-14 19:37:39 +02001736 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001737 continue;
1738
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001739 if (want_sd)
1740 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001741 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001742
Mike Galbraith8b911ac2010-03-11 17:17:16 +01001743 if (affine_sd) {
Suresh Siddha99bd5e22010-03-31 16:47:45 -07001744 if (cpu == prev_cpu || wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02001745 prev_cpu = cpu;
1746
1747 new_cpu = select_idle_sibling(p, prev_cpu);
1748 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01001749 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02001750
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001751 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001752 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001753 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001754 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001755
Peter Zijlstra0763a662009-09-14 19:37:39 +02001756 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001757 sd = sd->child;
1758 continue;
1759 }
1760
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001761 if (sd_flag & SD_BALANCE_WAKE)
1762 load_idx = sd->wake_idx;
1763
1764 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001765 if (!group) {
1766 sd = sd->child;
1767 continue;
1768 }
1769
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02001770 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001771 if (new_cpu == -1 || new_cpu == cpu) {
1772 /* Now try balancing at a lower domain level of cpu */
1773 sd = sd->child;
1774 continue;
1775 }
1776
1777 /* Now try balancing at a lower domain level of new_cpu */
1778 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02001779 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001780 sd = NULL;
1781 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02001782 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001783 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02001784 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001785 sd = tmp;
1786 }
1787 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001788 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02001789unlock:
1790 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01001791
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001792 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001793}
1794#endif /* CONFIG_SMP */
1795
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001796static unsigned long
1797wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001798{
1799 unsigned long gran = sysctl_sched_wakeup_granularity;
1800
1801 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001802 * Since its curr running now, convert the gran from real-time
1803 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01001804 *
1805 * By using 'se' instead of 'curr' we penalize light tasks, so
1806 * they get preempted easier. That is, if 'se' < 'curr' then
1807 * the resulting gran will be larger, therefore penalizing the
1808 * lighter, if otoh 'se' > 'curr' then the resulting gran will
1809 * be smaller, again penalizing the lighter task.
1810 *
1811 * This is especially important for buddies when the leftmost
1812 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001813 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08001814 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001815}
1816
1817/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02001818 * Should 'se' preempt 'curr'.
1819 *
1820 * |s1
1821 * |s2
1822 * |s3
1823 * g
1824 * |<--->|c
1825 *
1826 * w(c, s1) = -1
1827 * w(c, s2) = 0
1828 * w(c, s3) = 1
1829 *
1830 */
1831static int
1832wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
1833{
1834 s64 gran, vdiff = curr->vruntime - se->vruntime;
1835
1836 if (vdiff <= 0)
1837 return -1;
1838
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001839 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02001840 if (vdiff > gran)
1841 return 1;
1842
1843 return 0;
1844}
1845
Peter Zijlstra02479092008-11-04 21:25:10 +01001846static void set_last_buddy(struct sched_entity *se)
1847{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07001848 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
1849 return;
1850
1851 for_each_sched_entity(se)
1852 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01001853}
1854
1855static void set_next_buddy(struct sched_entity *se)
1856{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07001857 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
1858 return;
1859
1860 for_each_sched_entity(se)
1861 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01001862}
1863
Rik van Rielac53db52011-02-01 09:51:03 -05001864static void set_skip_buddy(struct sched_entity *se)
1865{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07001866 for_each_sched_entity(se)
1867 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05001868}
1869
Peter Zijlstra464b7522008-10-24 11:06:15 +02001870/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001871 * Preempt the current task with a newly woken task if needed:
1872 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02001873static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001874{
1875 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02001876 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01001877 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02001878 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001879 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01001880
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001881 if (unlikely(se == pse))
1882 return;
1883
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001884 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02001885 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001886 next_buddy_marked = 1;
1887 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02001888
Bharata B Raoaec0a512008-08-28 14:42:49 +05301889 /*
1890 * We can come here with TIF_NEED_RESCHED already set from new task
1891 * wake up path.
1892 */
1893 if (test_tsk_need_resched(curr))
1894 return;
1895
Darren Harta2f5c9a2011-02-22 13:04:33 -08001896 /* Idle tasks are by definition preempted by non-idle tasks. */
1897 if (unlikely(curr->policy == SCHED_IDLE) &&
1898 likely(p->policy != SCHED_IDLE))
1899 goto preempt;
1900
Ingo Molnar91c234b2007-10-15 17:00:18 +02001901 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08001902 * Batch and idle tasks do not preempt non-idle tasks (their preemption
1903 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02001904 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001905 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02001906 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001907
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001908
Peter Zijlstraad4b78b2009-09-16 12:31:31 +02001909 if (!sched_feat(WAKEUP_PREEMPT))
1910 return;
1911
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001912 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07001913 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001914 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001915 if (wakeup_preempt_entity(se, pse) == 1) {
1916 /*
1917 * Bias pick_next to pick the sched entity that is
1918 * triggering this preemption.
1919 */
1920 if (!next_buddy_marked)
1921 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001922 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07001923 }
Jupyung Leea65ac742009-11-17 18:51:40 +09001924
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001925 return;
1926
1927preempt:
1928 resched_task(curr);
1929 /*
1930 * Only set the backward buddy when the current task is still
1931 * on the rq. This can happen when a wakeup gets interleaved
1932 * with schedule on the ->pre_schedule() or idle_balance()
1933 * point, either of which can * drop the rq lock.
1934 *
1935 * Also, during early boot the idle thread is in the fair class,
1936 * for obvious reasons its a bad idea to schedule back to it.
1937 */
1938 if (unlikely(!se->on_rq || curr == rq->idle))
1939 return;
1940
1941 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
1942 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001943}
1944
Ingo Molnarfb8d4722007-08-09 11:16:48 +02001945static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001946{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001947 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001948 struct cfs_rq *cfs_rq = &rq->cfs;
1949 struct sched_entity *se;
1950
Tim Blechmann36ace272009-11-24 11:55:45 +01001951 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001952 return NULL;
1953
1954 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02001955 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001956 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001957 cfs_rq = group_cfs_rq(se);
1958 } while (cfs_rq);
1959
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001960 p = task_of(se);
1961 hrtick_start_fair(rq, p);
1962
1963 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001964}
1965
1966/*
1967 * Account for a descheduled task:
1968 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02001969static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001970{
1971 struct sched_entity *se = &prev->se;
1972 struct cfs_rq *cfs_rq;
1973
1974 for_each_sched_entity(se) {
1975 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02001976 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001977 }
1978}
1979
Rik van Rielac53db52011-02-01 09:51:03 -05001980/*
1981 * sched_yield() is very simple
1982 *
1983 * The magic of dealing with the ->skip buddy is in pick_next_entity.
1984 */
1985static void yield_task_fair(struct rq *rq)
1986{
1987 struct task_struct *curr = rq->curr;
1988 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
1989 struct sched_entity *se = &curr->se;
1990
1991 /*
1992 * Are we the only task in the tree?
1993 */
1994 if (unlikely(rq->nr_running == 1))
1995 return;
1996
1997 clear_buddies(cfs_rq, se);
1998
1999 if (curr->policy != SCHED_BATCH) {
2000 update_rq_clock(rq);
2001 /*
2002 * Update run-time statistics of the 'current'.
2003 */
2004 update_curr(cfs_rq);
2005 }
2006
2007 set_skip_buddy(se);
2008}
2009
Mike Galbraithd95f4122011-02-01 09:50:51 -05002010static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
2011{
2012 struct sched_entity *se = &p->se;
2013
2014 if (!se->on_rq)
2015 return false;
2016
2017 /* Tell the scheduler that we'd really like pse to run next. */
2018 set_next_buddy(se);
2019
Mike Galbraithd95f4122011-02-01 09:50:51 -05002020 yield_task_fair(rq);
2021
2022 return true;
2023}
2024
Peter Williams681f3e62007-10-24 18:23:51 +02002025#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002026/**************************************************
2027 * Fair scheduling class load-balancing methods:
2028 */
2029
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002030/*
2031 * pull_task - move a task from a remote runqueue to the local runqueue.
2032 * Both runqueues must be locked.
2033 */
2034static void pull_task(struct rq *src_rq, struct task_struct *p,
2035 struct rq *this_rq, int this_cpu)
2036{
2037 deactivate_task(src_rq, p, 0);
2038 set_task_cpu(p, this_cpu);
2039 activate_task(this_rq, p, 0);
2040 check_preempt_curr(this_rq, p, 0);
2041}
2042
2043/*
2044 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2045 */
2046static
2047int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
2048 struct sched_domain *sd, enum cpu_idle_type idle,
2049 int *all_pinned)
2050{
2051 int tsk_cache_hot = 0;
2052 /*
2053 * We do not migrate tasks that are:
2054 * 1) running (obviously), or
2055 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2056 * 3) are cache-hot on their current CPU.
2057 */
2058 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002059 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002060 return 0;
2061 }
2062 *all_pinned = 0;
2063
2064 if (task_running(rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002065 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002066 return 0;
2067 }
2068
2069 /*
2070 * Aggressive migration if:
2071 * 1) task is cache cold, or
2072 * 2) too many balance attempts have failed.
2073 */
2074
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07002075 tsk_cache_hot = task_hot(p, rq->clock_task, sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002076 if (!tsk_cache_hot ||
2077 sd->nr_balance_failed > sd->cache_nice_tries) {
2078#ifdef CONFIG_SCHEDSTATS
2079 if (tsk_cache_hot) {
2080 schedstat_inc(sd, lb_hot_gained[idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03002081 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002082 }
2083#endif
2084 return 1;
2085 }
2086
2087 if (tsk_cache_hot) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002088 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002089 return 0;
2090 }
2091 return 1;
2092}
2093
Peter Zijlstra897c3952009-12-17 17:45:42 +01002094/*
2095 * move_one_task tries to move exactly one task from busiest to this_rq, as
2096 * part of active balancing operations within "domain".
2097 * Returns 1 if successful and 0 otherwise.
2098 *
2099 * Called with both runqueues locked.
2100 */
2101static int
2102move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2103 struct sched_domain *sd, enum cpu_idle_type idle)
2104{
2105 struct task_struct *p, *n;
2106 struct cfs_rq *cfs_rq;
2107 int pinned = 0;
2108
2109 for_each_leaf_cfs_rq(busiest, cfs_rq) {
2110 list_for_each_entry_safe(p, n, &cfs_rq->tasks, se.group_node) {
2111
2112 if (!can_migrate_task(p, busiest, this_cpu,
2113 sd, idle, &pinned))
2114 continue;
2115
2116 pull_task(busiest, p, this_rq, this_cpu);
2117 /*
2118 * Right now, this is only the second place pull_task()
2119 * is called, so we can safely collect pull_task()
2120 * stats here rather than inside pull_task().
2121 */
2122 schedstat_inc(sd, lb_gained[idle]);
2123 return 1;
2124 }
2125 }
2126
2127 return 0;
2128}
2129
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002130static unsigned long
2131balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2132 unsigned long max_load_move, struct sched_domain *sd,
2133 enum cpu_idle_type idle, int *all_pinned,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002134 struct cfs_rq *busiest_cfs_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002135{
Ken Chenb30aef12011-04-08 12:20:16 -07002136 int loops = 0, pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002137 long rem_load_move = max_load_move;
Peter Zijlstraee00e662009-12-17 17:25:20 +01002138 struct task_struct *p, *n;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002139
2140 if (max_load_move == 0)
2141 goto out;
2142
Peter Zijlstraee00e662009-12-17 17:25:20 +01002143 list_for_each_entry_safe(p, n, &busiest_cfs_rq->tasks, se.group_node) {
2144 if (loops++ > sysctl_sched_nr_migrate)
2145 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002146
Peter Zijlstraee00e662009-12-17 17:25:20 +01002147 if ((p->se.load.weight >> 1) > rem_load_move ||
Ken Chenb30aef12011-04-08 12:20:16 -07002148 !can_migrate_task(p, busiest, this_cpu, sd, idle,
2149 all_pinned))
Peter Zijlstraee00e662009-12-17 17:25:20 +01002150 continue;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002151
Peter Zijlstraee00e662009-12-17 17:25:20 +01002152 pull_task(busiest, p, this_rq, this_cpu);
2153 pulled++;
2154 rem_load_move -= p->se.load.weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002155
2156#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01002157 /*
2158 * NEWIDLE balancing is a source of latency, so preemptible
2159 * kernels will stop after the first task is pulled to minimize
2160 * the critical section.
2161 */
2162 if (idle == CPU_NEWLY_IDLE)
2163 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002164#endif
2165
Peter Zijlstraee00e662009-12-17 17:25:20 +01002166 /*
2167 * We only want to steal up to the prescribed amount of
2168 * weighted load.
2169 */
2170 if (rem_load_move <= 0)
2171 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002172 }
2173out:
2174 /*
2175 * Right now, this is one of only two places pull_task() is called,
2176 * so we can safely collect pull_task() stats here rather than
2177 * inside pull_task().
2178 */
2179 schedstat_add(sd, lb_gained[idle], pulled);
2180
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002181 return max_load_move - rem_load_move;
2182}
2183
Peter Zijlstra230059de2009-12-17 17:47:12 +01002184#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002185/*
2186 * update tg->load_weight by folding this cpu's load_avg
2187 */
Paul Turner67e86252010-11-15 15:47:05 -08002188static int update_shares_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002189{
2190 struct cfs_rq *cfs_rq;
2191 unsigned long flags;
2192 struct rq *rq;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002193
2194 if (!tg->se[cpu])
2195 return 0;
2196
2197 rq = cpu_rq(cpu);
2198 cfs_rq = tg->cfs_rq[cpu];
2199
2200 raw_spin_lock_irqsave(&rq->lock, flags);
2201
2202 update_rq_clock(rq);
Paul Turnerd6b55912010-11-15 15:47:09 -08002203 update_cfs_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002204
2205 /*
2206 * We need to update shares after updating tg->load_weight in
2207 * order to adjust the weight of groups with long running tasks.
2208 */
Paul Turner6d5ab292011-01-21 20:45:01 -08002209 update_cfs_shares(cfs_rq);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002210
2211 raw_spin_unlock_irqrestore(&rq->lock, flags);
2212
2213 return 0;
2214}
2215
2216static void update_shares(int cpu)
2217{
2218 struct cfs_rq *cfs_rq;
2219 struct rq *rq = cpu_rq(cpu);
2220
2221 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02002222 /*
2223 * Iterates the task_group tree in a bottom up fashion, see
2224 * list_add_leaf_cfs_rq() for details.
2225 */
Paul Turner67e86252010-11-15 15:47:05 -08002226 for_each_leaf_cfs_rq(rq, cfs_rq)
2227 update_shares_cpu(cfs_rq->tg, cpu);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002228 rcu_read_unlock();
2229}
2230
Peter Zijlstra9763b672011-07-13 13:09:25 +02002231/*
2232 * Compute the cpu's hierarchical load factor for each task group.
2233 * This needs to be done in a top-down fashion because the load of a child
2234 * group is a fraction of its parents load.
2235 */
2236static int tg_load_down(struct task_group *tg, void *data)
2237{
2238 unsigned long load;
2239 long cpu = (long)data;
2240
2241 if (!tg->parent) {
2242 load = cpu_rq(cpu)->load.weight;
2243 } else {
2244 load = tg->parent->cfs_rq[cpu]->h_load;
2245 load *= tg->se[cpu]->load.weight;
2246 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
2247 }
2248
2249 tg->cfs_rq[cpu]->h_load = load;
2250
2251 return 0;
2252}
2253
2254static void update_h_load(long cpu)
2255{
2256 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
2257}
2258
Peter Zijlstra230059de2009-12-17 17:47:12 +01002259static unsigned long
2260load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
2261 unsigned long max_load_move,
2262 struct sched_domain *sd, enum cpu_idle_type idle,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002263 int *all_pinned)
Peter Zijlstra230059de2009-12-17 17:47:12 +01002264{
2265 long rem_load_move = max_load_move;
Peter Zijlstra9763b672011-07-13 13:09:25 +02002266 struct cfs_rq *busiest_cfs_rq;
Peter Zijlstra230059de2009-12-17 17:47:12 +01002267
2268 rcu_read_lock();
Peter Zijlstra9763b672011-07-13 13:09:25 +02002269 update_h_load(cpu_of(busiest));
Peter Zijlstra230059de2009-12-17 17:47:12 +01002270
Peter Zijlstra9763b672011-07-13 13:09:25 +02002271 for_each_leaf_cfs_rq(busiest, busiest_cfs_rq) {
Peter Zijlstra230059de2009-12-17 17:47:12 +01002272 unsigned long busiest_h_load = busiest_cfs_rq->h_load;
2273 unsigned long busiest_weight = busiest_cfs_rq->load.weight;
2274 u64 rem_load, moved_load;
2275
2276 /*
2277 * empty group
2278 */
2279 if (!busiest_cfs_rq->task_weight)
2280 continue;
2281
2282 rem_load = (u64)rem_load_move * busiest_weight;
2283 rem_load = div_u64(rem_load, busiest_h_load + 1);
2284
2285 moved_load = balance_tasks(this_rq, this_cpu, busiest,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002286 rem_load, sd, idle, all_pinned,
Peter Zijlstra230059de2009-12-17 17:47:12 +01002287 busiest_cfs_rq);
2288
2289 if (!moved_load)
2290 continue;
2291
2292 moved_load *= busiest_h_load;
2293 moved_load = div_u64(moved_load, busiest_weight + 1);
2294
2295 rem_load_move -= moved_load;
2296 if (rem_load_move < 0)
2297 break;
2298 }
2299 rcu_read_unlock();
2300
2301 return max_load_move - rem_load_move;
2302}
2303#else
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08002304static inline void update_shares(int cpu)
2305{
2306}
2307
Peter Zijlstra230059de2009-12-17 17:47:12 +01002308static unsigned long
2309load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
2310 unsigned long max_load_move,
2311 struct sched_domain *sd, enum cpu_idle_type idle,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002312 int *all_pinned)
Peter Zijlstra230059de2009-12-17 17:47:12 +01002313{
2314 return balance_tasks(this_rq, this_cpu, busiest,
2315 max_load_move, sd, idle, all_pinned,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002316 &busiest->cfs);
Peter Zijlstra230059de2009-12-17 17:47:12 +01002317}
2318#endif
2319
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002320/*
2321 * move_tasks tries to move up to max_load_move weighted load from busiest to
2322 * this_rq, as part of a balancing operation within domain "sd".
2323 * Returns 1 if successful and 0 otherwise.
2324 *
2325 * Called with both runqueues locked.
2326 */
2327static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2328 unsigned long max_load_move,
2329 struct sched_domain *sd, enum cpu_idle_type idle,
2330 int *all_pinned)
2331{
Peter Zijlstra3d45fd82009-12-17 17:12:46 +01002332 unsigned long total_load_moved = 0, load_moved;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002333
2334 do {
Peter Zijlstra3d45fd82009-12-17 17:12:46 +01002335 load_moved = load_balance_fair(this_rq, this_cpu, busiest,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002336 max_load_move - total_load_moved,
Vladimir Davydov931aeed2011-05-03 22:31:07 +04002337 sd, idle, all_pinned);
Peter Zijlstra3d45fd82009-12-17 17:12:46 +01002338
2339 total_load_moved += load_moved;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002340
2341#ifdef CONFIG_PREEMPT
2342 /*
2343 * NEWIDLE balancing is a source of latency, so preemptible
2344 * kernels will stop after the first task is pulled to minimize
2345 * the critical section.
2346 */
2347 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
2348 break;
Peter Zijlstrabaa8c112009-12-17 18:10:09 +01002349
2350 if (raw_spin_is_contended(&this_rq->lock) ||
2351 raw_spin_is_contended(&busiest->lock))
2352 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002353#endif
Peter Zijlstra3d45fd82009-12-17 17:12:46 +01002354 } while (load_moved && max_load_move > total_load_moved);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002355
2356 return total_load_moved > 0;
2357}
2358
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002359/********** Helpers for find_busiest_group ************************/
2360/*
2361 * sd_lb_stats - Structure to store the statistics of a sched_domain
2362 * during load balancing.
2363 */
2364struct sd_lb_stats {
2365 struct sched_group *busiest; /* Busiest group in this sd */
2366 struct sched_group *this; /* Local group in this sd */
2367 unsigned long total_load; /* Total load of all groups in sd */
2368 unsigned long total_pwr; /* Total power of all groups in sd */
2369 unsigned long avg_load; /* Average load across all groups in sd */
2370
2371 /** Statistics of this group */
2372 unsigned long this_load;
2373 unsigned long this_load_per_task;
2374 unsigned long this_nr_running;
Nikhil Raofab47622010-10-15 13:12:29 -07002375 unsigned long this_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002376 unsigned int this_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002377
2378 /* Statistics of the busiest group */
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002379 unsigned int busiest_idle_cpus;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002380 unsigned long max_load;
2381 unsigned long busiest_load_per_task;
2382 unsigned long busiest_nr_running;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08002383 unsigned long busiest_group_capacity;
Nikhil Raofab47622010-10-15 13:12:29 -07002384 unsigned long busiest_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002385 unsigned int busiest_group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002386
2387 int group_imb; /* Is there imbalance in this sd */
2388#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2389 int power_savings_balance; /* Is powersave balance needed for this sd */
2390 struct sched_group *group_min; /* Least loaded group in sd */
2391 struct sched_group *group_leader; /* Group which relieves group_min */
2392 unsigned long min_load_per_task; /* load_per_task in group_min */
2393 unsigned long leader_nr_running; /* Nr running of group_leader */
2394 unsigned long min_nr_running; /* Nr running of group_min */
2395#endif
2396};
2397
2398/*
2399 * sg_lb_stats - stats of a sched_group required for load_balancing
2400 */
2401struct sg_lb_stats {
2402 unsigned long avg_load; /*Avg load across the CPUs of the group */
2403 unsigned long group_load; /* Total load over the CPUs of the group */
2404 unsigned long sum_nr_running; /* Nr tasks running in the group */
2405 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
2406 unsigned long group_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002407 unsigned long idle_cpus;
2408 unsigned long group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002409 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07002410 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002411};
2412
2413/**
2414 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
2415 * @group: The group whose first cpu is to be returned.
2416 */
2417static inline unsigned int group_first_cpu(struct sched_group *group)
2418{
2419 return cpumask_first(sched_group_cpus(group));
2420}
2421
2422/**
2423 * get_sd_load_idx - Obtain the load index for a given sched domain.
2424 * @sd: The sched_domain whose load_idx is to be obtained.
2425 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
2426 */
2427static inline int get_sd_load_idx(struct sched_domain *sd,
2428 enum cpu_idle_type idle)
2429{
2430 int load_idx;
2431
2432 switch (idle) {
2433 case CPU_NOT_IDLE:
2434 load_idx = sd->busy_idx;
2435 break;
2436
2437 case CPU_NEWLY_IDLE:
2438 load_idx = sd->newidle_idx;
2439 break;
2440 default:
2441 load_idx = sd->idle_idx;
2442 break;
2443 }
2444
2445 return load_idx;
2446}
2447
2448
2449#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2450/**
2451 * init_sd_power_savings_stats - Initialize power savings statistics for
2452 * the given sched_domain, during load balancing.
2453 *
2454 * @sd: Sched domain whose power-savings statistics are to be initialized.
2455 * @sds: Variable containing the statistics for sd.
2456 * @idle: Idle status of the CPU at which we're performing load-balancing.
2457 */
2458static inline void init_sd_power_savings_stats(struct sched_domain *sd,
2459 struct sd_lb_stats *sds, enum cpu_idle_type idle)
2460{
2461 /*
2462 * Busy processors will not participate in power savings
2463 * balance.
2464 */
2465 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
2466 sds->power_savings_balance = 0;
2467 else {
2468 sds->power_savings_balance = 1;
2469 sds->min_nr_running = ULONG_MAX;
2470 sds->leader_nr_running = 0;
2471 }
2472}
2473
2474/**
2475 * update_sd_power_savings_stats - Update the power saving stats for a
2476 * sched_domain while performing load balancing.
2477 *
2478 * @group: sched_group belonging to the sched_domain under consideration.
2479 * @sds: Variable containing the statistics of the sched_domain
2480 * @local_group: Does group contain the CPU for which we're performing
2481 * load balancing ?
2482 * @sgs: Variable containing the statistics of the group.
2483 */
2484static inline void update_sd_power_savings_stats(struct sched_group *group,
2485 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
2486{
2487
2488 if (!sds->power_savings_balance)
2489 return;
2490
2491 /*
2492 * If the local group is idle or completely loaded
2493 * no need to do power savings balance at this domain
2494 */
2495 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
2496 !sds->this_nr_running))
2497 sds->power_savings_balance = 0;
2498
2499 /*
2500 * If a group is already running at full capacity or idle,
2501 * don't include that group in power savings calculations
2502 */
2503 if (!sds->power_savings_balance ||
2504 sgs->sum_nr_running >= sgs->group_capacity ||
2505 !sgs->sum_nr_running)
2506 return;
2507
2508 /*
2509 * Calculate the group which has the least non-idle load.
2510 * This is the group from where we need to pick up the load
2511 * for saving power
2512 */
2513 if ((sgs->sum_nr_running < sds->min_nr_running) ||
2514 (sgs->sum_nr_running == sds->min_nr_running &&
2515 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
2516 sds->group_min = group;
2517 sds->min_nr_running = sgs->sum_nr_running;
2518 sds->min_load_per_task = sgs->sum_weighted_load /
2519 sgs->sum_nr_running;
2520 }
2521
2522 /*
2523 * Calculate the group which is almost near its
2524 * capacity but still has some space to pick up some load
2525 * from other group and save more power
2526 */
2527 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
2528 return;
2529
2530 if (sgs->sum_nr_running > sds->leader_nr_running ||
2531 (sgs->sum_nr_running == sds->leader_nr_running &&
2532 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
2533 sds->group_leader = group;
2534 sds->leader_nr_running = sgs->sum_nr_running;
2535 }
2536}
2537
2538/**
2539 * check_power_save_busiest_group - see if there is potential for some power-savings balance
2540 * @sds: Variable containing the statistics of the sched_domain
2541 * under consideration.
2542 * @this_cpu: Cpu at which we're currently performing load-balancing.
2543 * @imbalance: Variable to store the imbalance.
2544 *
2545 * Description:
2546 * Check if we have potential to perform some power-savings balance.
2547 * If yes, set the busiest group to be the least loaded group in the
2548 * sched_domain, so that it's CPUs can be put to idle.
2549 *
2550 * Returns 1 if there is potential to perform power-savings balance.
2551 * Else returns 0.
2552 */
2553static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
2554 int this_cpu, unsigned long *imbalance)
2555{
2556 if (!sds->power_savings_balance)
2557 return 0;
2558
2559 if (sds->this != sds->group_leader ||
2560 sds->group_leader == sds->group_min)
2561 return 0;
2562
2563 *imbalance = sds->min_load_per_task;
2564 sds->busiest = sds->group_min;
2565
2566 return 1;
2567
2568}
2569#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
2570static inline void init_sd_power_savings_stats(struct sched_domain *sd,
2571 struct sd_lb_stats *sds, enum cpu_idle_type idle)
2572{
2573 return;
2574}
2575
2576static inline void update_sd_power_savings_stats(struct sched_group *group,
2577 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
2578{
2579 return;
2580}
2581
2582static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
2583 int this_cpu, unsigned long *imbalance)
2584{
2585 return 0;
2586}
2587#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
2588
2589
2590unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
2591{
Nikhil Rao1399fa72011-05-18 10:09:39 -07002592 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002593}
2594
2595unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
2596{
2597 return default_scale_freq_power(sd, cpu);
2598}
2599
2600unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
2601{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002602 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002603 unsigned long smt_gain = sd->smt_gain;
2604
2605 smt_gain /= weight;
2606
2607 return smt_gain;
2608}
2609
2610unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
2611{
2612 return default_scale_smt_power(sd, cpu);
2613}
2614
2615unsigned long scale_rt_power(int cpu)
2616{
2617 struct rq *rq = cpu_rq(cpu);
2618 u64 total, available;
2619
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002620 total = sched_avg_period() + (rq->clock - rq->age_stamp);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07002621
2622 if (unlikely(total < rq->rt_avg)) {
2623 /* Ensures that power won't end up being negative */
2624 available = 0;
2625 } else {
2626 available = total - rq->rt_avg;
2627 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002628
Nikhil Rao1399fa72011-05-18 10:09:39 -07002629 if (unlikely((s64)total < SCHED_POWER_SCALE))
2630 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002631
Nikhil Rao1399fa72011-05-18 10:09:39 -07002632 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002633
2634 return div_u64(available, total);
2635}
2636
2637static void update_cpu_power(struct sched_domain *sd, int cpu)
2638{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02002639 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07002640 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002641 struct sched_group *sdg = sd->groups;
2642
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002643 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
2644 if (sched_feat(ARCH_POWER))
2645 power *= arch_scale_smt_power(sd, cpu);
2646 else
2647 power *= default_scale_smt_power(sd, cpu);
2648
Nikhil Rao1399fa72011-05-18 10:09:39 -07002649 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002650 }
2651
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002652 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002653
2654 if (sched_feat(ARCH_POWER))
2655 power *= arch_scale_freq_power(sd, cpu);
2656 else
2657 power *= default_scale_freq_power(sd, cpu);
2658
Nikhil Rao1399fa72011-05-18 10:09:39 -07002659 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002660
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002661 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07002662 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002663
2664 if (!power)
2665 power = 1;
2666
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02002667 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002668 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002669}
2670
2671static void update_group_power(struct sched_domain *sd, int cpu)
2672{
2673 struct sched_domain *child = sd->child;
2674 struct sched_group *group, *sdg = sd->groups;
2675 unsigned long power;
2676
2677 if (!child) {
2678 update_cpu_power(sd, cpu);
2679 return;
2680 }
2681
2682 power = 0;
2683
2684 group = child->groups;
2685 do {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002686 power += group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002687 group = group->next;
2688 } while (group != child->groups);
2689
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002690 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002691}
2692
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002693/*
2694 * Try and fix up capacity for tiny siblings, this is needed when
2695 * things like SD_ASYM_PACKING need f_b_g to select another sibling
2696 * which on its own isn't powerful enough.
2697 *
2698 * See update_sd_pick_busiest() and check_asym_packing().
2699 */
2700static inline int
2701fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
2702{
2703 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07002704 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002705 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02002706 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002707 return 0;
2708
2709 /*
2710 * If ~90% of the cpu_power is still there, we're good.
2711 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002712 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002713 return 1;
2714
2715 return 0;
2716}
2717
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002718/**
2719 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
2720 * @sd: The sched_domain whose statistics are to be updated.
2721 * @group: sched_group whose statistics are to be updated.
2722 * @this_cpu: Cpu for which load balance is currently performed.
2723 * @idle: Idle status of this_cpu
2724 * @load_idx: Load index of sched_domain of this_cpu for load calc.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002725 * @local_group: Does group contain this_cpu.
2726 * @cpus: Set of cpus considered for load balancing.
2727 * @balance: Should we balance.
2728 * @sgs: variable to hold the statistics for this group.
2729 */
2730static inline void update_sg_lb_stats(struct sched_domain *sd,
2731 struct sched_group *group, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08002732 enum cpu_idle_type idle, int load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002733 int local_group, const struct cpumask *cpus,
2734 int *balance, struct sg_lb_stats *sgs)
2735{
Nikhil Rao2582f0e2010-10-13 12:09:36 -07002736 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002737 int i;
2738 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08002739 unsigned long avg_load_per_task = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002740
Gautham R Shenoy871e35b2010-01-20 14:02:44 -06002741 if (local_group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002742 balance_cpu = group_first_cpu(group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002743
2744 /* Tally up the load of all CPUs in the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002745 max_cpu_load = 0;
2746 min_cpu_load = ~0UL;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07002747 max_nr_running = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002748
2749 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
2750 struct rq *rq = cpu_rq(i);
2751
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002752 /* Bias balancing toward cpus of our domain */
2753 if (local_group) {
2754 if (idle_cpu(i) && !first_idle_cpu) {
2755 first_idle_cpu = 1;
2756 balance_cpu = i;
2757 }
2758
2759 load = target_load(i, load_idx);
2760 } else {
2761 load = source_load(i, load_idx);
Nikhil Rao2582f0e2010-10-13 12:09:36 -07002762 if (load > max_cpu_load) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002763 max_cpu_load = load;
Nikhil Rao2582f0e2010-10-13 12:09:36 -07002764 max_nr_running = rq->nr_running;
2765 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002766 if (min_cpu_load > load)
2767 min_cpu_load = load;
2768 }
2769
2770 sgs->group_load += load;
2771 sgs->sum_nr_running += rq->nr_running;
2772 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002773 if (idle_cpu(i))
2774 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002775 }
2776
2777 /*
2778 * First idle cpu or the first cpu(busiest) in this sched group
2779 * is eligible for doing load balancing at this and above
2780 * domains. In the newly idle case, we will allow all the cpu's
2781 * to do the newly idle load balance.
2782 */
Peter Zijlstrabbc8cb52010-07-09 15:15:43 +02002783 if (idle != CPU_NEWLY_IDLE && local_group) {
2784 if (balance_cpu != this_cpu) {
2785 *balance = 0;
2786 return;
2787 }
2788 update_group_power(sd, this_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002789 }
2790
2791 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002792 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002793
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002794 /*
2795 * Consider the group unbalanced when the imbalance is larger
Peter Zijlstra866ab432011-02-21 18:56:47 +01002796 * than the average weight of a task.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002797 *
2798 * APZ: with cgroup the avg task weight can vary wildly and
2799 * might not be a suitable number - should we keep a
2800 * normalized nr_running number somewhere that negates
2801 * the hierarchy?
2802 */
Suresh Siddhadd5feea2010-02-23 16:13:52 -08002803 if (sgs->sum_nr_running)
2804 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002805
Peter Zijlstra866ab432011-02-21 18:56:47 +01002806 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002807 sgs->group_imb = 1;
2808
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002809 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07002810 SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10002811 if (!sgs->group_capacity)
2812 sgs->group_capacity = fix_small_capacity(sd, group);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002813 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07002814
2815 if (sgs->group_capacity > sgs->sum_nr_running)
2816 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002817}
2818
2819/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10002820 * update_sd_pick_busiest - return 1 on busiest group
2821 * @sd: sched_domain whose statistics are to be checked
2822 * @sds: sched_domain statistics
2823 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10002824 * @sgs: sched_group statistics
2825 * @this_cpu: the current cpu
Michael Neuling532cb4c2010-06-08 14:57:02 +10002826 *
2827 * Determine if @sg is a busier group than the previously selected
2828 * busiest group.
2829 */
2830static bool update_sd_pick_busiest(struct sched_domain *sd,
2831 struct sd_lb_stats *sds,
2832 struct sched_group *sg,
2833 struct sg_lb_stats *sgs,
2834 int this_cpu)
2835{
2836 if (sgs->avg_load <= sds->max_load)
2837 return false;
2838
2839 if (sgs->sum_nr_running > sgs->group_capacity)
2840 return true;
2841
2842 if (sgs->group_imb)
2843 return true;
2844
2845 /*
2846 * ASYM_PACKING needs to move all the work to the lowest
2847 * numbered CPUs in the group, therefore mark all groups
2848 * higher than ourself as busy.
2849 */
2850 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
2851 this_cpu < group_first_cpu(sg)) {
2852 if (!sds->busiest)
2853 return true;
2854
2855 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
2856 return true;
2857 }
2858
2859 return false;
2860}
2861
2862/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002863 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
2864 * @sd: sched_domain whose statistics are to be updated.
2865 * @this_cpu: Cpu for which load balance is currently performed.
2866 * @idle: Idle status of this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002867 * @cpus: Set of cpus considered for load balancing.
2868 * @balance: Should we balance.
2869 * @sds: variable to hold the statistics for this sched_domain.
2870 */
2871static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08002872 enum cpu_idle_type idle, const struct cpumask *cpus,
2873 int *balance, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002874{
2875 struct sched_domain *child = sd->child;
Michael Neuling532cb4c2010-06-08 14:57:02 +10002876 struct sched_group *sg = sd->groups;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002877 struct sg_lb_stats sgs;
2878 int load_idx, prefer_sibling = 0;
2879
2880 if (child && child->flags & SD_PREFER_SIBLING)
2881 prefer_sibling = 1;
2882
2883 init_sd_power_savings_stats(sd, sds, idle);
2884 load_idx = get_sd_load_idx(sd, idle);
2885
2886 do {
2887 int local_group;
2888
Michael Neuling532cb4c2010-06-08 14:57:02 +10002889 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002890 memset(&sgs, 0, sizeof(sgs));
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08002891 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002892 local_group, cpus, balance, &sgs);
2893
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01002894 if (local_group && !(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002895 return;
2896
2897 sds->total_load += sgs.group_load;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002898 sds->total_pwr += sg->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002899
2900 /*
2901 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10002902 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07002903 * and move all the excess tasks away. We lower the capacity
2904 * of a group only if the local group has the capacity to fit
2905 * these excess tasks, i.e. nr_running < group_capacity. The
2906 * extra check prevents the case where you always pull from the
2907 * heaviest group when it is already under-utilized (possible
2908 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002909 */
Nikhil Rao75dd3212010-10-15 13:12:30 -07002910 if (prefer_sibling && !local_group && sds->this_has_capacity)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002911 sgs.group_capacity = min(sgs.group_capacity, 1UL);
2912
2913 if (local_group) {
2914 sds->this_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10002915 sds->this = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002916 sds->this_nr_running = sgs.sum_nr_running;
2917 sds->this_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07002918 sds->this_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002919 sds->this_idle_cpus = sgs.idle_cpus;
Michael Neuling532cb4c2010-06-08 14:57:02 +10002920 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002921 sds->max_load = sgs.avg_load;
Michael Neuling532cb4c2010-06-08 14:57:02 +10002922 sds->busiest = sg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002923 sds->busiest_nr_running = sgs.sum_nr_running;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002924 sds->busiest_idle_cpus = sgs.idle_cpus;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08002925 sds->busiest_group_capacity = sgs.group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002926 sds->busiest_load_per_task = sgs.sum_weighted_load;
Nikhil Raofab47622010-10-15 13:12:29 -07002927 sds->busiest_has_capacity = sgs.group_has_capacity;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07002928 sds->busiest_group_weight = sgs.group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002929 sds->group_imb = sgs.group_imb;
2930 }
2931
Michael Neuling532cb4c2010-06-08 14:57:02 +10002932 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
2933 sg = sg->next;
2934 } while (sg != sd->groups);
2935}
2936
Michael Neuling2ec57d42010-06-29 12:02:01 +10002937int __weak arch_sd_sibling_asym_packing(void)
Michael Neuling532cb4c2010-06-08 14:57:02 +10002938{
2939 return 0*SD_ASYM_PACKING;
2940}
2941
2942/**
2943 * check_asym_packing - Check to see if the group is packed into the
2944 * sched doman.
2945 *
2946 * This is primarily intended to used at the sibling level. Some
2947 * cores like POWER7 prefer to use lower numbered SMT threads. In the
2948 * case of POWER7, it can move to lower SMT modes only when higher
2949 * threads are idle. When in lower SMT modes, the threads will
2950 * perform better since they share less core resources. Hence when we
2951 * have idle threads, we want them to be the higher ones.
2952 *
2953 * This packing function is run on idle threads. It checks to see if
2954 * the busiest CPU in this domain (core in the P7 case) has a higher
2955 * CPU number than the packing function is being run on. Here we are
2956 * assuming lower CPU number will be equivalent to lower a SMT thread
2957 * number.
2958 *
Michael Neulingb6b12292010-06-10 12:06:21 +10002959 * Returns 1 when packing is required and a task should be moved to
2960 * this CPU. The amount of the imbalance is returned in *imbalance.
2961 *
Michael Neuling532cb4c2010-06-08 14:57:02 +10002962 * @sd: The sched_domain whose packing is to be checked.
2963 * @sds: Statistics of the sched_domain which is to be packed
2964 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
2965 * @imbalance: returns amount of imbalanced due to packing.
Michael Neuling532cb4c2010-06-08 14:57:02 +10002966 */
2967static int check_asym_packing(struct sched_domain *sd,
2968 struct sd_lb_stats *sds,
2969 int this_cpu, unsigned long *imbalance)
2970{
2971 int busiest_cpu;
2972
2973 if (!(sd->flags & SD_ASYM_PACKING))
2974 return 0;
2975
2976 if (!sds->busiest)
2977 return 0;
2978
2979 busiest_cpu = group_first_cpu(sds->busiest);
2980 if (this_cpu > busiest_cpu)
2981 return 0;
2982
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02002983 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power,
Nikhil Rao1399fa72011-05-18 10:09:39 -07002984 SCHED_POWER_SCALE);
Michael Neuling532cb4c2010-06-08 14:57:02 +10002985 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002986}
2987
2988/**
2989 * fix_small_imbalance - Calculate the minor imbalance that exists
2990 * amongst the groups of a sched_domain, during
2991 * load balancing.
2992 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
2993 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
2994 * @imbalance: Variable to store the imbalance.
2995 */
2996static inline void fix_small_imbalance(struct sd_lb_stats *sds,
2997 int this_cpu, unsigned long *imbalance)
2998{
2999 unsigned long tmp, pwr_now = 0, pwr_move = 0;
3000 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003001 unsigned long scaled_busy_load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003002
3003 if (sds->this_nr_running) {
3004 sds->this_load_per_task /= sds->this_nr_running;
3005 if (sds->busiest_load_per_task >
3006 sds->this_load_per_task)
3007 imbn = 1;
3008 } else
3009 sds->this_load_per_task =
3010 cpu_avg_load_per_task(this_cpu);
3011
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003012 scaled_busy_load_per_task = sds->busiest_load_per_task
Nikhil Rao1399fa72011-05-18 10:09:39 -07003013 * SCHED_POWER_SCALE;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003014 scaled_busy_load_per_task /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003015
3016 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
3017 (scaled_busy_load_per_task * imbn)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003018 *imbalance = sds->busiest_load_per_task;
3019 return;
3020 }
3021
3022 /*
3023 * OK, we don't have enough imbalance to justify moving tasks,
3024 * however we may be able to increase total CPU power used by
3025 * moving them.
3026 */
3027
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003028 pwr_now += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003029 min(sds->busiest_load_per_task, sds->max_load);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003030 pwr_now += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003031 min(sds->this_load_per_task, sds->this_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003032 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003033
3034 /* Amount of load we'd subtract */
Nikhil Rao1399fa72011-05-18 10:09:39 -07003035 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003036 sds->busiest->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003037 if (sds->max_load > tmp)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003038 pwr_move += sds->busiest->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003039 min(sds->busiest_load_per_task, sds->max_load - tmp);
3040
3041 /* Amount of load we'd add */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003042 if (sds->max_load * sds->busiest->sgp->power <
Nikhil Rao1399fa72011-05-18 10:09:39 -07003043 sds->busiest_load_per_task * SCHED_POWER_SCALE)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003044 tmp = (sds->max_load * sds->busiest->sgp->power) /
3045 sds->this->sgp->power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003046 else
Nikhil Rao1399fa72011-05-18 10:09:39 -07003047 tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003048 sds->this->sgp->power;
3049 pwr_move += sds->this->sgp->power *
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003050 min(sds->this_load_per_task, sds->this_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003051 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003052
3053 /* Move if we gain throughput */
3054 if (pwr_move > pwr_now)
3055 *imbalance = sds->busiest_load_per_task;
3056}
3057
3058/**
3059 * calculate_imbalance - Calculate the amount of imbalance present within the
3060 * groups of a given sched_domain during load balance.
3061 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
3062 * @this_cpu: Cpu for which currently load balance is being performed.
3063 * @imbalance: The variable to store the imbalance.
3064 */
3065static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
3066 unsigned long *imbalance)
3067{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003068 unsigned long max_pull, load_above_capacity = ~0UL;
3069
3070 sds->busiest_load_per_task /= sds->busiest_nr_running;
3071 if (sds->group_imb) {
3072 sds->busiest_load_per_task =
3073 min(sds->busiest_load_per_task, sds->avg_load);
3074 }
3075
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003076 /*
3077 * In the presence of smp nice balancing, certain scenarios can have
3078 * max load less than avg load(as we skip the groups at or below
3079 * its cpu_power, while calculating max_load..)
3080 */
3081 if (sds->max_load < sds->avg_load) {
3082 *imbalance = 0;
3083 return fix_small_imbalance(sds, this_cpu, imbalance);
3084 }
3085
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003086 if (!sds->group_imb) {
3087 /*
3088 * Don't want to pull so many tasks that a group would go idle.
3089 */
3090 load_above_capacity = (sds->busiest_nr_running -
3091 sds->busiest_group_capacity);
3092
Nikhil Rao1399fa72011-05-18 10:09:39 -07003093 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003094
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003095 load_above_capacity /= sds->busiest->sgp->power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08003096 }
3097
3098 /*
3099 * We're trying to get all the cpus to the average_load, so we don't
3100 * want to push ourselves above the average load, nor do we wish to
3101 * reduce the max loaded cpu below the average load. At the same time,
3102 * we also don't want to reduce the group load below the group capacity
3103 * (so that we can implement power-savings policies etc). Thus we look
3104 * for the minimum possible imbalance.
3105 * Be careful of negative numbers as they'll appear as very large values
3106 * with unsigned longs.
3107 */
3108 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003109
3110 /* How much load to actually move to equalise the imbalance */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02003111 *imbalance = min(max_pull * sds->busiest->sgp->power,
3112 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
Nikhil Rao1399fa72011-05-18 10:09:39 -07003113 / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003114
3115 /*
3116 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003117 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003118 * a think about bumping its value to force at least one task to be
3119 * moved
3120 */
3121 if (*imbalance < sds->busiest_load_per_task)
3122 return fix_small_imbalance(sds, this_cpu, imbalance);
3123
3124}
Nikhil Raofab47622010-10-15 13:12:29 -07003125
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003126/******* find_busiest_group() helpers end here *********************/
3127
3128/**
3129 * find_busiest_group - Returns the busiest group within the sched_domain
3130 * if there is an imbalance. If there isn't an imbalance, and
3131 * the user has opted for power-savings, it returns a group whose
3132 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
3133 * such a group exists.
3134 *
3135 * Also calculates the amount of weighted load which should be moved
3136 * to restore balance.
3137 *
3138 * @sd: The sched_domain whose busiest group is to be returned.
3139 * @this_cpu: The cpu for which load balancing is currently being performed.
3140 * @imbalance: Variable which stores amount of weighted load which should
3141 * be moved to restore balance/put a group to idle.
3142 * @idle: The idle status of this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003143 * @cpus: The set of CPUs under consideration for load-balancing.
3144 * @balance: Pointer to a variable indicating if this_cpu
3145 * is the appropriate cpu to perform load balancing at this_level.
3146 *
3147 * Returns: - the busiest group if imbalance exists.
3148 * - If no imbalance and user has opted for power-savings balance,
3149 * return the least loaded group whose CPUs can be
3150 * put to idle by rebalancing its tasks onto our group.
3151 */
3152static struct sched_group *
3153find_busiest_group(struct sched_domain *sd, int this_cpu,
3154 unsigned long *imbalance, enum cpu_idle_type idle,
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003155 const struct cpumask *cpus, int *balance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003156{
3157 struct sd_lb_stats sds;
3158
3159 memset(&sds, 0, sizeof(sds));
3160
3161 /*
3162 * Compute the various statistics relavent for load balancing at
3163 * this level.
3164 */
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003165 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003166
Peter Zijlstracc57aa82011-02-21 18:55:32 +01003167 /*
3168 * this_cpu is not the appropriate cpu to perform load balancing at
3169 * this level.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003170 */
Peter Zijlstra8f190fb2009-12-24 14:18:21 +01003171 if (!(*balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003172 goto ret;
3173
Michael Neuling532cb4c2010-06-08 14:57:02 +10003174 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) &&
3175 check_asym_packing(sd, &sds, this_cpu, imbalance))
3176 return sds.busiest;
3177
Peter Zijlstracc57aa82011-02-21 18:55:32 +01003178 /* There is no busy sibling group to pull tasks from */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003179 if (!sds.busiest || sds.busiest_nr_running == 0)
3180 goto out_balanced;
3181
Nikhil Rao1399fa72011-05-18 10:09:39 -07003182 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07003183
Peter Zijlstra866ab432011-02-21 18:56:47 +01003184 /*
3185 * If the busiest group is imbalanced the below checks don't
3186 * work because they assumes all things are equal, which typically
3187 * isn't true due to cpus_allowed constraints and the like.
3188 */
3189 if (sds.group_imb)
3190 goto force_balance;
3191
Peter Zijlstracc57aa82011-02-21 18:55:32 +01003192 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nikhil Raofab47622010-10-15 13:12:29 -07003193 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
3194 !sds.busiest_has_capacity)
3195 goto force_balance;
3196
Peter Zijlstracc57aa82011-02-21 18:55:32 +01003197 /*
3198 * If the local group is more busy than the selected busiest group
3199 * don't try and pull any tasks.
3200 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003201 if (sds.this_load >= sds.max_load)
3202 goto out_balanced;
3203
Peter Zijlstracc57aa82011-02-21 18:55:32 +01003204 /*
3205 * Don't pull any tasks if this group is already above the domain
3206 * average load.
3207 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003208 if (sds.this_load >= sds.avg_load)
3209 goto out_balanced;
3210
Peter Zijlstrac186faf2011-02-21 18:52:53 +01003211 if (idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003212 /*
3213 * This cpu is idle. If the busiest group load doesn't
3214 * have more tasks than the number of available cpu's and
3215 * there is no imbalance between this and busiest group
3216 * wrt to idle cpu's, it is balanced.
3217 */
Peter Zijlstrac186faf2011-02-21 18:52:53 +01003218 if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003219 sds.busiest_nr_running <= sds.busiest_group_weight)
3220 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01003221 } else {
3222 /*
3223 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
3224 * imbalance_pct to be conservative.
3225 */
3226 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
3227 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07003228 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003229
Nikhil Raofab47622010-10-15 13:12:29 -07003230force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003231 /* Looks like there is an imbalance. Compute it */
3232 calculate_imbalance(&sds, this_cpu, imbalance);
3233 return sds.busiest;
3234
3235out_balanced:
3236 /*
3237 * There is no obvious imbalance. But check if we can do some balancing
3238 * to save power.
3239 */
3240 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
3241 return sds.busiest;
3242ret:
3243 *imbalance = 0;
3244 return NULL;
3245}
3246
3247/*
3248 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3249 */
3250static struct rq *
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003251find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
3252 enum cpu_idle_type idle, unsigned long imbalance,
3253 const struct cpumask *cpus)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003254{
3255 struct rq *busiest = NULL, *rq;
3256 unsigned long max_load = 0;
3257 int i;
3258
3259 for_each_cpu(i, sched_group_cpus(group)) {
3260 unsigned long power = power_of(i);
Nikhil Rao1399fa72011-05-18 10:09:39 -07003261 unsigned long capacity = DIV_ROUND_CLOSEST(power,
3262 SCHED_POWER_SCALE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003263 unsigned long wl;
3264
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003265 if (!capacity)
3266 capacity = fix_small_capacity(sd, group);
3267
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003268 if (!cpumask_test_cpu(i, cpus))
3269 continue;
3270
3271 rq = cpu_rq(i);
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01003272 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003273
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01003274 /*
3275 * When comparing with imbalance, use weighted_cpuload()
3276 * which is not scaled with the cpu power.
3277 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003278 if (capacity && rq->nr_running == 1 && wl > imbalance)
3279 continue;
3280
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01003281 /*
3282 * For the load comparisons with the other cpu's, consider
3283 * the weighted_cpuload() scaled with the cpu power, so that
3284 * the load can be moved away from the cpu that is potentially
3285 * running at a lower capacity.
3286 */
Nikhil Rao1399fa72011-05-18 10:09:39 -07003287 wl = (wl * SCHED_POWER_SCALE) / power;
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01003288
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003289 if (wl > max_load) {
3290 max_load = wl;
3291 busiest = rq;
3292 }
3293 }
3294
3295 return busiest;
3296}
3297
3298/*
3299 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3300 * so long as it is large enough.
3301 */
3302#define MAX_PINNED_INTERVAL 512
3303
3304/* Working cpumask for load_balance and load_balance_newidle. */
3305static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
3306
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003307static int need_active_balance(struct sched_domain *sd, int idle,
Michael Neuling532cb4c2010-06-08 14:57:02 +10003308 int busiest_cpu, int this_cpu)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01003309{
3310 if (idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10003311
3312 /*
3313 * ASYM_PACKING needs to force migrate tasks from busy but
3314 * higher numbered CPUs in order to pack all tasks in the
3315 * lowest numbered CPUs.
3316 */
3317 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu)
3318 return 1;
3319
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01003320 /*
3321 * The only task running in a non-idle cpu can be moved to this
3322 * cpu in an attempt to completely freeup the other CPU
3323 * package.
3324 *
3325 * The package power saving logic comes from
3326 * find_busiest_group(). If there are no imbalance, then
3327 * f_b_g() will return NULL. However when sched_mc={1,2} then
3328 * f_b_g() will select a group from which a running task may be
3329 * pulled to this cpu in order to make the other package idle.
3330 * If there is no opportunity to make a package idle and if
3331 * there are no imbalance, then f_b_g() will return NULL and no
3332 * action will be taken in load_balance_newidle().
3333 *
3334 * Under normal task pull operation due to imbalance, there
3335 * will be more than one task in the source run queue and
3336 * move_tasks() will succeed. ld_moved will be true and this
3337 * active balance code will not be triggered.
3338 */
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01003339 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
3340 return 0;
3341 }
3342
3343 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
3344}
3345
Tejun Heo969c7922010-05-06 18:49:21 +02003346static int active_load_balance_cpu_stop(void *data);
3347
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003348/*
3349 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3350 * tasks if there is an imbalance.
3351 */
3352static int load_balance(int this_cpu, struct rq *this_rq,
3353 struct sched_domain *sd, enum cpu_idle_type idle,
3354 int *balance)
3355{
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003356 int ld_moved, all_pinned = 0, active_balance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003357 struct sched_group *group;
3358 unsigned long imbalance;
3359 struct rq *busiest;
3360 unsigned long flags;
3361 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
3362
3363 cpumask_copy(cpus, cpu_active_mask);
3364
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003365 schedstat_inc(sd, lb_count[idle]);
3366
3367redo:
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003368 group = find_busiest_group(sd, this_cpu, &imbalance, idle,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003369 cpus, balance);
3370
3371 if (*balance == 0)
3372 goto out_balanced;
3373
3374 if (!group) {
3375 schedstat_inc(sd, lb_nobusyg[idle]);
3376 goto out_balanced;
3377 }
3378
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10003379 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003380 if (!busiest) {
3381 schedstat_inc(sd, lb_nobusyq[idle]);
3382 goto out_balanced;
3383 }
3384
3385 BUG_ON(busiest == this_rq);
3386
3387 schedstat_add(sd, lb_imbalance[idle], imbalance);
3388
3389 ld_moved = 0;
3390 if (busiest->nr_running > 1) {
3391 /*
3392 * Attempt to move tasks. If find_busiest_group has found
3393 * an imbalance but busiest->nr_running <= 1, the group is
3394 * still unbalanced. ld_moved simply stays zero, so it is
3395 * correctly treated as an imbalance.
3396 */
Ken Chenb30aef12011-04-08 12:20:16 -07003397 all_pinned = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003398 local_irq_save(flags);
3399 double_rq_lock(this_rq, busiest);
3400 ld_moved = move_tasks(this_rq, this_cpu, busiest,
3401 imbalance, sd, idle, &all_pinned);
3402 double_rq_unlock(this_rq, busiest);
3403 local_irq_restore(flags);
3404
3405 /*
3406 * some other cpu did the load balance for us.
3407 */
3408 if (ld_moved && this_cpu != smp_processor_id())
3409 resched_cpu(this_cpu);
3410
3411 /* All tasks on this runqueue were pinned by CPU affinity */
3412 if (unlikely(all_pinned)) {
3413 cpumask_clear_cpu(cpu_of(busiest), cpus);
3414 if (!cpumask_empty(cpus))
3415 goto redo;
3416 goto out_balanced;
3417 }
3418 }
3419
3420 if (!ld_moved) {
3421 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07003422 /*
3423 * Increment the failure counter only on periodic balance.
3424 * We do not want newidle balance, which can be very
3425 * frequent, pollute the failure counter causing
3426 * excessive cache_hot migrations and active balances.
3427 */
3428 if (idle != CPU_NEWLY_IDLE)
3429 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003430
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003431 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003432 raw_spin_lock_irqsave(&busiest->lock, flags);
3433
Tejun Heo969c7922010-05-06 18:49:21 +02003434 /* don't kick the active_load_balance_cpu_stop,
3435 * if the curr task on busiest cpu can't be
3436 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003437 */
3438 if (!cpumask_test_cpu(this_cpu,
3439 &busiest->curr->cpus_allowed)) {
3440 raw_spin_unlock_irqrestore(&busiest->lock,
3441 flags);
3442 all_pinned = 1;
3443 goto out_one_pinned;
3444 }
3445
Tejun Heo969c7922010-05-06 18:49:21 +02003446 /*
3447 * ->active_balance synchronizes accesses to
3448 * ->active_balance_work. Once set, it's cleared
3449 * only after active load balance is finished.
3450 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003451 if (!busiest->active_balance) {
3452 busiest->active_balance = 1;
3453 busiest->push_cpu = this_cpu;
3454 active_balance = 1;
3455 }
3456 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02003457
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003458 if (active_balance)
Tejun Heo969c7922010-05-06 18:49:21 +02003459 stop_one_cpu_nowait(cpu_of(busiest),
3460 active_load_balance_cpu_stop, busiest,
3461 &busiest->active_balance_work);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003462
3463 /*
3464 * We've kicked active balancing, reset the failure
3465 * counter.
3466 */
3467 sd->nr_balance_failed = sd->cache_nice_tries+1;
3468 }
3469 } else
3470 sd->nr_balance_failed = 0;
3471
3472 if (likely(!active_balance)) {
3473 /* We were unbalanced, so reset the balancing interval */
3474 sd->balance_interval = sd->min_interval;
3475 } else {
3476 /*
3477 * If we've begun active balancing, start to back off. This
3478 * case may not be covered by the all_pinned logic if there
3479 * is only 1 task on the busy runqueue (because we don't call
3480 * move_tasks).
3481 */
3482 if (sd->balance_interval < sd->max_interval)
3483 sd->balance_interval *= 2;
3484 }
3485
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003486 goto out;
3487
3488out_balanced:
3489 schedstat_inc(sd, lb_balanced[idle]);
3490
3491 sd->nr_balance_failed = 0;
3492
3493out_one_pinned:
3494 /* tune up the balancing interval */
3495 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3496 (sd->balance_interval < sd->max_interval))
3497 sd->balance_interval *= 2;
3498
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08003499 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003500out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003501 return ld_moved;
3502}
3503
3504/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003505 * idle_balance is called by schedule() if this_cpu is about to become
3506 * idle. Attempts to pull tasks from other CPUs.
3507 */
3508static void idle_balance(int this_cpu, struct rq *this_rq)
3509{
3510 struct sched_domain *sd;
3511 int pulled_task = 0;
3512 unsigned long next_balance = jiffies + HZ;
3513
3514 this_rq->idle_stamp = this_rq->clock;
3515
3516 if (this_rq->avg_idle < sysctl_sched_migration_cost)
3517 return;
3518
Peter Zijlstraf492e122009-12-23 15:29:42 +01003519 /*
3520 * Drop the rq->lock, but keep IRQ/preempt disabled.
3521 */
3522 raw_spin_unlock(&this_rq->lock);
3523
Paul Turnerc66eaf62010-11-15 15:47:07 -08003524 update_shares(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02003525 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003526 for_each_domain(this_cpu, sd) {
3527 unsigned long interval;
Peter Zijlstraf492e122009-12-23 15:29:42 +01003528 int balance = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003529
3530 if (!(sd->flags & SD_LOAD_BALANCE))
3531 continue;
3532
Peter Zijlstraf492e122009-12-23 15:29:42 +01003533 if (sd->flags & SD_BALANCE_NEWIDLE) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003534 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01003535 pulled_task = load_balance(this_cpu, this_rq,
3536 sd, CPU_NEWLY_IDLE, &balance);
3537 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003538
3539 interval = msecs_to_jiffies(sd->balance_interval);
3540 if (time_after(next_balance, sd->last_balance + interval))
3541 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08003542 if (pulled_task) {
3543 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003544 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08003545 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003546 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003547 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01003548
3549 raw_spin_lock(&this_rq->lock);
3550
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003551 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
3552 /*
3553 * We are going idle. next_balance may be set based on
3554 * a busy processor. So reset next_balance.
3555 */
3556 this_rq->next_balance = next_balance;
3557 }
3558}
3559
3560/*
Tejun Heo969c7922010-05-06 18:49:21 +02003561 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
3562 * running tasks off the busiest CPU onto idle CPUs. It requires at
3563 * least 1 task to be running on each physical CPU where possible, and
3564 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003565 */
Tejun Heo969c7922010-05-06 18:49:21 +02003566static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003567{
Tejun Heo969c7922010-05-06 18:49:21 +02003568 struct rq *busiest_rq = data;
3569 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003570 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02003571 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003572 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02003573
3574 raw_spin_lock_irq(&busiest_rq->lock);
3575
3576 /* make sure the requested cpu hasn't gone down in the meantime */
3577 if (unlikely(busiest_cpu != smp_processor_id() ||
3578 !busiest_rq->active_balance))
3579 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003580
3581 /* Is there any task to move? */
3582 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02003583 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003584
3585 /*
3586 * This condition is "impossible", if it occurs
3587 * we need to fix it. Originally reported by
3588 * Bjorn Helgaas on a 128-cpu setup.
3589 */
3590 BUG_ON(busiest_rq == target_rq);
3591
3592 /* move a task from busiest_rq to target_rq */
3593 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003594
3595 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02003596 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003597 for_each_domain(target_cpu, sd) {
3598 if ((sd->flags & SD_LOAD_BALANCE) &&
3599 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
3600 break;
3601 }
3602
3603 if (likely(sd)) {
3604 schedstat_inc(sd, alb_count);
3605
3606 if (move_one_task(target_rq, target_cpu, busiest_rq,
3607 sd, CPU_IDLE))
3608 schedstat_inc(sd, alb_pushed);
3609 else
3610 schedstat_inc(sd, alb_failed);
3611 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003612 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003613 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02003614out_unlock:
3615 busiest_rq->active_balance = 0;
3616 raw_spin_unlock_irq(&busiest_rq->lock);
3617 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003618}
3619
3620#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003621
3622static DEFINE_PER_CPU(struct call_single_data, remote_sched_softirq_cb);
3623
3624static void trigger_sched_softirq(void *data)
3625{
3626 raise_softirq_irqoff(SCHED_SOFTIRQ);
3627}
3628
3629static inline void init_sched_softirq_csd(struct call_single_data *csd)
3630{
3631 csd->func = trigger_sched_softirq;
3632 csd->info = NULL;
3633 csd->flags = 0;
3634 csd->priv = 0;
3635}
3636
3637/*
3638 * idle load balancing details
3639 * - One of the idle CPUs nominates itself as idle load_balancer, while
3640 * entering idle.
3641 * - This idle load balancer CPU will also go into tickless mode when
3642 * it is idle, just like all other idle CPUs
3643 * - When one of the busy CPUs notice that there may be an idle rebalancing
3644 * needed, they will kick the idle load balancer, which then does idle
3645 * load balancing for all the idle CPUs.
3646 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003647static struct {
3648 atomic_t load_balancer;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003649 atomic_t first_pick_cpu;
3650 atomic_t second_pick_cpu;
3651 cpumask_var_t idle_cpus_mask;
3652 cpumask_var_t grp_idle_mask;
3653 unsigned long next_balance; /* in jiffy units */
3654} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003655
3656int get_nohz_load_balancer(void)
3657{
3658 return atomic_read(&nohz.load_balancer);
3659}
3660
3661#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3662/**
3663 * lowest_flag_domain - Return lowest sched_domain containing flag.
3664 * @cpu: The cpu whose lowest level of sched domain is to
3665 * be returned.
3666 * @flag: The flag to check for the lowest sched_domain
3667 * for the given cpu.
3668 *
3669 * Returns the lowest sched_domain of a cpu which contains the given flag.
3670 */
3671static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
3672{
3673 struct sched_domain *sd;
3674
3675 for_each_domain(cpu, sd)
3676 if (sd && (sd->flags & flag))
3677 break;
3678
3679 return sd;
3680}
3681
3682/**
3683 * for_each_flag_domain - Iterates over sched_domains containing the flag.
3684 * @cpu: The cpu whose domains we're iterating over.
3685 * @sd: variable holding the value of the power_savings_sd
3686 * for cpu.
3687 * @flag: The flag to filter the sched_domains to be iterated.
3688 *
3689 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
3690 * set, starting from the lowest sched_domain to the highest.
3691 */
3692#define for_each_flag_domain(cpu, sd, flag) \
3693 for (sd = lowest_flag_domain(cpu, flag); \
3694 (sd && (sd->flags & flag)); sd = sd->parent)
3695
3696/**
3697 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
3698 * @ilb_group: group to be checked for semi-idleness
3699 *
3700 * Returns: 1 if the group is semi-idle. 0 otherwise.
3701 *
3702 * We define a sched_group to be semi idle if it has atleast one idle-CPU
3703 * and atleast one non-idle CPU. This helper function checks if the given
3704 * sched_group is semi-idle or not.
3705 */
3706static inline int is_semi_idle_group(struct sched_group *ilb_group)
3707{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003708 cpumask_and(nohz.grp_idle_mask, nohz.idle_cpus_mask,
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003709 sched_group_cpus(ilb_group));
3710
3711 /*
3712 * A sched_group is semi-idle when it has atleast one busy cpu
3713 * and atleast one idle cpu.
3714 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003715 if (cpumask_empty(nohz.grp_idle_mask))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003716 return 0;
3717
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003718 if (cpumask_equal(nohz.grp_idle_mask, sched_group_cpus(ilb_group)))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003719 return 0;
3720
3721 return 1;
3722}
3723/**
3724 * find_new_ilb - Finds the optimum idle load balancer for nomination.
3725 * @cpu: The cpu which is nominating a new idle_load_balancer.
3726 *
3727 * Returns: Returns the id of the idle load balancer if it exists,
3728 * Else, returns >= nr_cpu_ids.
3729 *
3730 * This algorithm picks the idle load balancer such that it belongs to a
3731 * semi-idle powersavings sched_domain. The idea is to try and avoid
3732 * completely idle packages/cores just for the purpose of idle load balancing
3733 * when there are other idle cpu's which are better suited for that job.
3734 */
3735static int find_new_ilb(int cpu)
3736{
3737 struct sched_domain *sd;
3738 struct sched_group *ilb_group;
Peter Zijlstradce840a2011-04-07 14:09:50 +02003739 int ilb = nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003740
3741 /*
3742 * Have idle load balancer selection from semi-idle packages only
3743 * when power-aware load balancing is enabled
3744 */
3745 if (!(sched_smt_power_savings || sched_mc_power_savings))
3746 goto out_done;
3747
3748 /*
3749 * Optimize for the case when we have no idle CPUs or only one
3750 * idle CPU. Don't walk the sched_domain hierarchy in such cases
3751 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003752 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003753 goto out_done;
3754
Peter Zijlstradce840a2011-04-07 14:09:50 +02003755 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003756 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
3757 ilb_group = sd->groups;
3758
3759 do {
Peter Zijlstradce840a2011-04-07 14:09:50 +02003760 if (is_semi_idle_group(ilb_group)) {
3761 ilb = cpumask_first(nohz.grp_idle_mask);
3762 goto unlock;
3763 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003764
3765 ilb_group = ilb_group->next;
3766
3767 } while (ilb_group != sd->groups);
3768 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003769unlock:
3770 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003771
3772out_done:
Peter Zijlstradce840a2011-04-07 14:09:50 +02003773 return ilb;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003774}
3775#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
3776static inline int find_new_ilb(int call_cpu)
3777{
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003778 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003779}
3780#endif
3781
3782/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003783 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
3784 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
3785 * CPU (if there is one).
3786 */
3787static void nohz_balancer_kick(int cpu)
3788{
3789 int ilb_cpu;
3790
3791 nohz.next_balance++;
3792
3793 ilb_cpu = get_nohz_load_balancer();
3794
3795 if (ilb_cpu >= nr_cpu_ids) {
3796 ilb_cpu = cpumask_first(nohz.idle_cpus_mask);
3797 if (ilb_cpu >= nr_cpu_ids)
3798 return;
3799 }
3800
3801 if (!cpu_rq(ilb_cpu)->nohz_balance_kick) {
3802 struct call_single_data *cp;
3803
3804 cpu_rq(ilb_cpu)->nohz_balance_kick = 1;
3805 cp = &per_cpu(remote_sched_softirq_cb, cpu);
3806 __smp_call_function_single(ilb_cpu, cp, 0);
3807 }
3808 return;
3809}
3810
3811/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003812 * This routine will try to nominate the ilb (idle load balancing)
3813 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003814 * load balancing on behalf of all those cpus.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003815 *
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003816 * When the ilb owner becomes busy, we will not have new ilb owner until some
3817 * idle CPU wakes up and goes back to idle or some busy CPU tries to kick
3818 * idle load balancing by kicking one of the idle CPUs.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003819 *
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003820 * Ticks are stopped for the ilb owner as well, with busy CPU kicking this
3821 * ilb owner CPU in future (when there is a need for idle load balancing on
3822 * behalf of all idle CPUs).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003823 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003824void select_nohz_load_balancer(int stop_tick)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003825{
3826 int cpu = smp_processor_id();
3827
3828 if (stop_tick) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003829 if (!cpu_active(cpu)) {
3830 if (atomic_read(&nohz.load_balancer) != cpu)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003831 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003832
3833 /*
3834 * If we are going offline and still the leader,
3835 * give up!
3836 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003837 if (atomic_cmpxchg(&nohz.load_balancer, cpu,
3838 nr_cpu_ids) != cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003839 BUG();
3840
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003841 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003842 }
3843
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003844 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003845
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003846 if (atomic_read(&nohz.first_pick_cpu) == cpu)
3847 atomic_cmpxchg(&nohz.first_pick_cpu, cpu, nr_cpu_ids);
3848 if (atomic_read(&nohz.second_pick_cpu) == cpu)
3849 atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003850
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003851 if (atomic_read(&nohz.load_balancer) >= nr_cpu_ids) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003852 int new_ilb;
3853
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003854 /* make me the ilb owner */
3855 if (atomic_cmpxchg(&nohz.load_balancer, nr_cpu_ids,
3856 cpu) != nr_cpu_ids)
3857 return;
3858
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003859 /*
3860 * Check to see if there is a more power-efficient
3861 * ilb.
3862 */
3863 new_ilb = find_new_ilb(cpu);
3864 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003865 atomic_set(&nohz.load_balancer, nr_cpu_ids);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003866 resched_cpu(new_ilb);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003867 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003868 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003869 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003870 }
3871 } else {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003872 if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask))
3873 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003874
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003875 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003876
3877 if (atomic_read(&nohz.load_balancer) == cpu)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003878 if (atomic_cmpxchg(&nohz.load_balancer, cpu,
3879 nr_cpu_ids) != cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003880 BUG();
3881 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003882 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003883}
3884#endif
3885
3886static DEFINE_SPINLOCK(balancing);
3887
Peter Zijlstra49c022e2011-04-05 10:14:25 +02003888static unsigned long __read_mostly max_load_balance_interval = HZ/10;
3889
3890/*
3891 * Scale the max load_balance interval with the number of CPUs in the system.
3892 * This trades load-balance latency on larger machines for less cross talk.
3893 */
3894static void update_max_interval(void)
3895{
3896 max_load_balance_interval = HZ*num_online_cpus()/10;
3897}
3898
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003899/*
3900 * It checks each scheduling domain to see if it is due to be balanced,
3901 * and initiates a balancing operation if so.
3902 *
3903 * Balancing parameters are set up in arch_init_sched_domains.
3904 */
3905static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3906{
3907 int balance = 1;
3908 struct rq *rq = cpu_rq(cpu);
3909 unsigned long interval;
3910 struct sched_domain *sd;
3911 /* Earliest time when we have to do rebalance again */
3912 unsigned long next_balance = jiffies + 60*HZ;
3913 int update_next_balance = 0;
3914 int need_serialize;
3915
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003916 update_shares(cpu);
3917
Peter Zijlstradce840a2011-04-07 14:09:50 +02003918 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003919 for_each_domain(cpu, sd) {
3920 if (!(sd->flags & SD_LOAD_BALANCE))
3921 continue;
3922
3923 interval = sd->balance_interval;
3924 if (idle != CPU_IDLE)
3925 interval *= sd->busy_factor;
3926
3927 /* scale ms to jiffies */
3928 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02003929 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003930
3931 need_serialize = sd->flags & SD_SERIALIZE;
3932
3933 if (need_serialize) {
3934 if (!spin_trylock(&balancing))
3935 goto out;
3936 }
3937
3938 if (time_after_eq(jiffies, sd->last_balance + interval)) {
3939 if (load_balance(cpu, rq, sd, idle, &balance)) {
3940 /*
3941 * We've pulled tasks over so either we're no
Peter Zijlstrac186faf2011-02-21 18:52:53 +01003942 * longer idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003943 */
3944 idle = CPU_NOT_IDLE;
3945 }
3946 sd->last_balance = jiffies;
3947 }
3948 if (need_serialize)
3949 spin_unlock(&balancing);
3950out:
3951 if (time_after(next_balance, sd->last_balance + interval)) {
3952 next_balance = sd->last_balance + interval;
3953 update_next_balance = 1;
3954 }
3955
3956 /*
3957 * Stop the load balance at this level. There is another
3958 * CPU in our sched group which is doing load balancing more
3959 * actively.
3960 */
3961 if (!balance)
3962 break;
3963 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02003964 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01003965
3966 /*
3967 * next_balance will be updated only when there is a need.
3968 * When the cpu is attached to null domain for ex, it will not be
3969 * updated.
3970 */
3971 if (likely(update_next_balance))
3972 rq->next_balance = next_balance;
3973}
3974
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07003975#ifdef CONFIG_NO_HZ
3976/*
3977 * In CONFIG_NO_HZ case, the idle balance kickee will do the
3978 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3979 */
3980static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
3981{
3982 struct rq *this_rq = cpu_rq(this_cpu);
3983 struct rq *rq;
3984 int balance_cpu;
3985
3986 if (idle != CPU_IDLE || !this_rq->nohz_balance_kick)
3987 return;
3988
3989 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
3990 if (balance_cpu == this_cpu)
3991 continue;
3992
3993 /*
3994 * If this cpu gets work to do, stop the load balancing
3995 * work being done for other cpus. Next load
3996 * balancing owner will pick it up.
3997 */
3998 if (need_resched()) {
3999 this_rq->nohz_balance_kick = 0;
4000 break;
4001 }
4002
4003 raw_spin_lock_irq(&this_rq->lock);
Suresh Siddha5343bdb2010-07-09 15:19:54 +02004004 update_rq_clock(this_rq);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004005 update_cpu_load(this_rq);
4006 raw_spin_unlock_irq(&this_rq->lock);
4007
4008 rebalance_domains(balance_cpu, CPU_IDLE);
4009
4010 rq = cpu_rq(balance_cpu);
4011 if (time_after(this_rq->next_balance, rq->next_balance))
4012 this_rq->next_balance = rq->next_balance;
4013 }
4014 nohz.next_balance = this_rq->next_balance;
4015 this_rq->nohz_balance_kick = 0;
4016}
4017
4018/*
4019 * Current heuristic for kicking the idle load balancer
4020 * - first_pick_cpu is the one of the busy CPUs. It will kick
4021 * idle load balancer when it has more than one process active. This
4022 * eliminates the need for idle load balancing altogether when we have
4023 * only one running process in the system (common case).
4024 * - If there are more than one busy CPU, idle load balancer may have
4025 * to run for active_load_balance to happen (i.e., two busy CPUs are
4026 * SMT or core siblings and can run better if they move to different
4027 * physical CPUs). So, second_pick_cpu is the second of the busy CPUs
4028 * which will kick idle load balancer as soon as it has any load.
4029 */
4030static inline int nohz_kick_needed(struct rq *rq, int cpu)
4031{
4032 unsigned long now = jiffies;
4033 int ret;
4034 int first_pick_cpu, second_pick_cpu;
4035
4036 if (time_before(now, nohz.next_balance))
4037 return 0;
4038
Suresh Siddhaf6c3f162010-09-13 11:02:21 -07004039 if (rq->idle_at_tick)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004040 return 0;
4041
4042 first_pick_cpu = atomic_read(&nohz.first_pick_cpu);
4043 second_pick_cpu = atomic_read(&nohz.second_pick_cpu);
4044
4045 if (first_pick_cpu < nr_cpu_ids && first_pick_cpu != cpu &&
4046 second_pick_cpu < nr_cpu_ids && second_pick_cpu != cpu)
4047 return 0;
4048
4049 ret = atomic_cmpxchg(&nohz.first_pick_cpu, nr_cpu_ids, cpu);
4050 if (ret == nr_cpu_ids || ret == cpu) {
4051 atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids);
4052 if (rq->nr_running > 1)
4053 return 1;
4054 } else {
4055 ret = atomic_cmpxchg(&nohz.second_pick_cpu, nr_cpu_ids, cpu);
4056 if (ret == nr_cpu_ids || ret == cpu) {
4057 if (rq->nr_running)
4058 return 1;
4059 }
4060 }
4061 return 0;
4062}
4063#else
4064static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
4065#endif
4066
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004067/*
4068 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004069 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004070 */
4071static void run_rebalance_domains(struct softirq_action *h)
4072{
4073 int this_cpu = smp_processor_id();
4074 struct rq *this_rq = cpu_rq(this_cpu);
4075 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4076 CPU_IDLE : CPU_NOT_IDLE;
4077
4078 rebalance_domains(this_cpu, idle);
4079
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004080 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004081 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004082 * balancing on behalf of the other idle cpus whose ticks are
4083 * stopped.
4084 */
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004085 nohz_idle_balance(this_cpu, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004086}
4087
4088static inline int on_null_domain(int cpu)
4089{
Paul E. McKenney90a65012010-02-28 08:32:18 -08004090 return !rcu_dereference_sched(cpu_rq(cpu)->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004091}
4092
4093/*
4094 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004095 */
4096static inline void trigger_load_balance(struct rq *rq, int cpu)
4097{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004098 /* Don't need to rebalance while attached to NULL domain */
4099 if (time_after_eq(jiffies, rq->next_balance) &&
4100 likely(!on_null_domain(cpu)))
4101 raise_softirq(SCHED_SOFTIRQ);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07004102#ifdef CONFIG_NO_HZ
4103 else if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
4104 nohz_balancer_kick(cpu);
4105#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004106}
4107
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01004108static void rq_online_fair(struct rq *rq)
4109{
4110 update_sysctl();
4111}
4112
4113static void rq_offline_fair(struct rq *rq)
4114{
4115 update_sysctl();
4116}
4117
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004118#else /* CONFIG_SMP */
4119
4120/*
4121 * on UP we do not need to balance between CPUs:
4122 */
4123static inline void idle_balance(int cpu, struct rq *rq)
4124{
4125}
4126
Dhaval Giani55e12e52008-06-24 23:39:43 +05304127#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02004128
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004129/*
4130 * scheduler tick hitting a task of our scheduling class:
4131 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004132static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004133{
4134 struct cfs_rq *cfs_rq;
4135 struct sched_entity *se = &curr->se;
4136
4137 for_each_sched_entity(se) {
4138 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004139 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004140 }
4141}
4142
4143/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004144 * called on fork with the child task as argument from the parent's context
4145 * - child not yet on the tasklist
4146 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004147 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004148static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004149{
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004150 struct cfs_rq *cfs_rq = task_cfs_rq(current);
Ingo Molnar429d43b2007-10-15 17:00:03 +02004151 struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02004152 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004153 struct rq *rq = this_rq();
4154 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004155
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004156 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004157
Peter Zijlstra861d0342010-08-19 13:31:43 +02004158 update_rq_clock(rq);
4159
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07004160 if (unlikely(task_cpu(p) != this_cpu)) {
4161 rcu_read_lock();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004162 __set_task_cpu(p, this_cpu);
Paul E. McKenneyb0a0f662010-10-06 17:32:51 -07004163 rcu_read_unlock();
4164 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004165
Ting Yang7109c442007-08-28 12:53:24 +02004166 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004167
Mike Galbraithb5d9d732009-09-08 11:12:28 +02004168 if (curr)
4169 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004170 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02004171
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004172 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02004173 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02004174 * Upon rescheduling, sched_class::put_prev_task() will place
4175 * 'current' within the tree based on its new key value.
4176 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02004177 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05304178 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02004179 }
4180
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004181 se->vruntime -= cfs_rq->min_vruntime;
4182
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004183 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004184}
4185
Steven Rostedtcb469842008-01-25 21:08:22 +01004186/*
4187 * Priority of the task has changed. Check to see if we preempt
4188 * the current task.
4189 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004190static void
4191prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01004192{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004193 if (!p->se.on_rq)
4194 return;
4195
Steven Rostedtcb469842008-01-25 21:08:22 +01004196 /*
4197 * Reschedule if we are currently running on this runqueue and
4198 * our priority decreased, or if we are not currently running on
4199 * this runqueue and our priority is higher than the current's
4200 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004201 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01004202 if (p->prio > oldprio)
4203 resched_task(rq->curr);
4204 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02004205 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004206}
4207
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004208static void switched_from_fair(struct rq *rq, struct task_struct *p)
4209{
4210 struct sched_entity *se = &p->se;
4211 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4212
4213 /*
4214 * Ensure the task's vruntime is normalized, so that when its
4215 * switched back to the fair class the enqueue_entity(.flags=0) will
4216 * do the right thing.
4217 *
4218 * If it was on_rq, then the dequeue_entity(.flags=0) will already
4219 * have normalized the vruntime, if it was !on_rq, then only when
4220 * the task is sleeping will it still have non-normalized vruntime.
4221 */
4222 if (!se->on_rq && p->state != TASK_RUNNING) {
4223 /*
4224 * Fix up our vruntime so that the current sleep doesn't
4225 * cause 'unlimited' sleep bonus.
4226 */
4227 place_entity(cfs_rq, se, 0);
4228 se->vruntime -= cfs_rq->min_vruntime;
4229 }
4230}
4231
Steven Rostedtcb469842008-01-25 21:08:22 +01004232/*
4233 * We switched to the sched_fair class.
4234 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004235static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01004236{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004237 if (!p->se.on_rq)
4238 return;
4239
Steven Rostedtcb469842008-01-25 21:08:22 +01004240 /*
4241 * We were most likely switched from sched_rt, so
4242 * kick off the schedule if running, otherwise just see
4243 * if we can still preempt the current task.
4244 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004245 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01004246 resched_task(rq->curr);
4247 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02004248 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004249}
4250
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004251/* Account for a task changing its policy or group.
4252 *
4253 * This routine is mostly called to set cfs_rq->curr field when a task
4254 * migrates between groups/classes.
4255 */
4256static void set_curr_task_fair(struct rq *rq)
4257{
4258 struct sched_entity *se = &rq->curr->se;
4259
4260 for_each_sched_entity(se)
4261 set_next_entity(cfs_rq_of(se), se);
4262}
4263
Peter Zijlstra810b3812008-02-29 15:21:01 -05004264#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02004265static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05004266{
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02004267 /*
4268 * If the task was not on the rq at the time of this cgroup movement
4269 * it must have been asleep, sleeping tasks keep their ->vruntime
4270 * absolute on their old rq until wakeup (needed for the fair sleeper
4271 * bonus in place_entity()).
4272 *
4273 * If it was on the rq, we've just 'preempted' it, which does convert
4274 * ->vruntime to a relative base.
4275 *
4276 * Make sure both cases convert their relative position when migrating
4277 * to another cgroup's rq. This does somewhat interfere with the
4278 * fair sleeper stuff for the first placement, but who cares.
4279 */
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004280 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02004281 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
4282 set_task_rq(p, task_cpu(p));
4283 if (!on_rq)
4284 p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime;
Peter Zijlstra810b3812008-02-29 15:21:01 -05004285}
4286#endif
4287
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07004288static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00004289{
4290 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00004291 unsigned int rr_interval = 0;
4292
4293 /*
4294 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
4295 * idle runqueue:
4296 */
Peter Williams0d721ce2009-09-21 01:31:53 +00004297 if (rq->cfs.load.weight)
4298 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00004299
4300 return rr_interval;
4301}
4302
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004303/*
4304 * All the scheduling class methods:
4305 */
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004306static const struct sched_class fair_sched_class = {
4307 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004308 .enqueue_task = enqueue_task_fair,
4309 .dequeue_task = dequeue_task_fair,
4310 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05004311 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004312
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004313 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004314
4315 .pick_next_task = pick_next_task_fair,
4316 .put_prev_task = put_prev_task_fair,
4317
Peter Williams681f3e62007-10-24 18:23:51 +02004318#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08004319 .select_task_rq = select_task_rq_fair,
4320
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01004321 .rq_online = rq_online_fair,
4322 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004323
4324 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02004325#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004326
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004327 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004328 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01004329 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01004330
4331 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004332 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01004333 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05004334
Peter Williams0d721ce2009-09-21 01:31:53 +00004335 .get_rr_interval = get_rr_interval_fair,
4336
Peter Zijlstra810b3812008-02-29 15:21:01 -05004337#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02004338 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05004339#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004340};
4341
4342#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02004343static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004344{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004345 struct cfs_rq *cfs_rq;
4346
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01004347 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02004348 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02004349 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01004350 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004351}
4352#endif