blob: efe6457ac5c8345d0be01c001d9fe5c3bd39c6a5 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/slab.h>
27#include <linux/profile.h>
28#include <linux/interrupt.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020029#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000030#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020031#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020032
33#include <trace/events/sched.h>
34
35#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010036
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037/*
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090039 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020040 *
Peter Zijlstra21805082007-08-25 18:41:53 +020041 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020042 * 'timeslice length' - timeslices in CFS are of variable length
43 * and have no persistent notion like in traditional, time-slice
44 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020046 * (to see the precise effective timeslice length of your workload,
47 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 */
Mike Galbraith21406922010-03-11 17:17:15 +010049unsigned int sysctl_sched_latency = 6000000ULL;
50unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020051
52/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010053 * The initial- and re-scaling of tunables is configurable
54 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
55 *
56 * Options are:
57 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
58 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
59 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
60 */
61enum sched_tunable_scaling sysctl_sched_tunable_scaling
62 = SCHED_TUNABLESCALING_LOG;
63
64/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010065 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090066 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020068unsigned int sysctl_sched_min_granularity = 750000ULL;
69unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010070
71/*
72 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
73 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020074static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010075
76/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020078 * parent will (try to) run first.
79 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020080unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020081
82/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020083 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020084 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020085 *
86 * This option delays the preemption effects of decoupled workloads
87 * and reduces their over-scheduling. Synchronous workloads will still
88 * have immediate wakeup/sleep latencies.
89 */
Mike Galbraith172e0822009-09-09 15:41:37 +020090unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010091unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020092
Ingo Molnarda84d962007-10-15 17:00:18 +020093const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
94
Paul Turnera7a4f8a2010-11-15 15:47:06 -080095/*
96 * The exponential sliding window over which load is averaged for shares
97 * distribution.
98 * (default: 10msec)
99 */
100unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
101
Paul Turnerec12cb72011-07-21 09:43:30 -0700102#ifdef CONFIG_CFS_BANDWIDTH
103/*
104 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
105 * each time a cfs_rq requests quota.
106 *
107 * Note: in the case that the slice exceeds the runtime remaining (either due
108 * to consumption or the quota being specified to be smaller than the slice)
109 * we will always only issue the remaining available time.
110 *
111 * default: 5 msec, units: microseconds
112 */
113unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
114#endif
115
Paul Gortmaker85276322013-04-19 15:10:50 -0400116static inline void update_load_add(struct load_weight *lw, unsigned long inc)
117{
118 lw->weight += inc;
119 lw->inv_weight = 0;
120}
121
122static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
123{
124 lw->weight -= dec;
125 lw->inv_weight = 0;
126}
127
128static inline void update_load_set(struct load_weight *lw, unsigned long w)
129{
130 lw->weight = w;
131 lw->inv_weight = 0;
132}
133
Peter Zijlstra029632f2011-10-25 10:00:11 +0200134/*
135 * Increase the granularity value when there are more CPUs,
136 * because with more CPUs the 'effective latency' as visible
137 * to users decreases. But the relationship is not linear,
138 * so pick a second-best guess by going with the log2 of the
139 * number of CPUs.
140 *
141 * This idea comes from the SD scheduler of Con Kolivas:
142 */
143static int get_update_sysctl_factor(void)
144{
145 unsigned int cpus = min_t(int, num_online_cpus(), 8);
146 unsigned int factor;
147
148 switch (sysctl_sched_tunable_scaling) {
149 case SCHED_TUNABLESCALING_NONE:
150 factor = 1;
151 break;
152 case SCHED_TUNABLESCALING_LINEAR:
153 factor = cpus;
154 break;
155 case SCHED_TUNABLESCALING_LOG:
156 default:
157 factor = 1 + ilog2(cpus);
158 break;
159 }
160
161 return factor;
162}
163
164static void update_sysctl(void)
165{
166 unsigned int factor = get_update_sysctl_factor();
167
168#define SET_SYSCTL(name) \
169 (sysctl_##name = (factor) * normalized_sysctl_##name)
170 SET_SYSCTL(sched_min_granularity);
171 SET_SYSCTL(sched_latency);
172 SET_SYSCTL(sched_wakeup_granularity);
173#undef SET_SYSCTL
174}
175
176void sched_init_granularity(void)
177{
178 update_sysctl();
179}
180
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100181#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200182#define WMULT_SHIFT 32
183
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100184static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200185{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100186 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200187
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100188 if (likely(lw->inv_weight))
189 return;
190
191 w = scale_load_down(lw->weight);
192
193 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
194 lw->inv_weight = 1;
195 else if (unlikely(!w))
196 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200197 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100198 lw->inv_weight = WMULT_CONST / w;
199}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200200
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100201/*
202 * delta_exec * weight / lw.weight
203 * OR
204 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
205 *
206 * Either weight := NICE_0_LOAD and lw \e prio_to_wmult[], in which case
207 * we're guaranteed shift stays positive because inv_weight is guaranteed to
208 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
209 *
210 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
211 * weight/lw.weight <= 1, and therefore our shift will also be positive.
212 */
213static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
214{
215 u64 fact = scale_load_down(weight);
216 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200217
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100218 __update_inv_weight(lw);
219
220 if (unlikely(fact >> 32)) {
221 while (fact >> 32) {
222 fact >>= 1;
223 shift--;
224 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200225 }
226
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100227 /* hint to use a 32x32->64 mul */
228 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200229
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100230 while (fact >> 32) {
231 fact >>= 1;
232 shift--;
233 }
234
235 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200236}
237
238
239const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200240
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200241/**************************************************************
242 * CFS operations on generic schedulable entities:
243 */
244
245#ifdef CONFIG_FAIR_GROUP_SCHED
246
247/* cpu runqueue to which this cfs_rq is attached */
248static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
249{
250 return cfs_rq->rq;
251}
252
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200253/* An entity is a task if it doesn't "own" a runqueue */
254#define entity_is_task(se) (!se->my_q)
255
Peter Zijlstra8f488942009-07-24 12:25:30 +0200256static inline struct task_struct *task_of(struct sched_entity *se)
257{
258#ifdef CONFIG_SCHED_DEBUG
259 WARN_ON_ONCE(!entity_is_task(se));
260#endif
261 return container_of(se, struct task_struct, se);
262}
263
Peter Zijlstrab7581492008-04-19 19:45:00 +0200264/* Walk up scheduling entities hierarchy */
265#define for_each_sched_entity(se) \
266 for (; se; se = se->parent)
267
268static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
269{
270 return p->se.cfs_rq;
271}
272
273/* runqueue on which this entity is (to be) queued */
274static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
275{
276 return se->cfs_rq;
277}
278
279/* runqueue "owned" by this group */
280static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
281{
282 return grp->my_q;
283}
284
Paul Turneraff3e492012-10-04 13:18:30 +0200285static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
286 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200287
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800288static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
289{
290 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800291 /*
292 * Ensure we either appear before our parent (if already
293 * enqueued) or force our parent to appear after us when it is
294 * enqueued. The fact that we always enqueue bottom-up
295 * reduces this to two cases.
296 */
297 if (cfs_rq->tg->parent &&
298 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
299 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800300 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800301 } else {
302 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
303 &rq_of(cfs_rq)->leaf_cfs_rq_list);
304 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800305
306 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200307 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200308 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800309 }
310}
311
312static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
313{
314 if (cfs_rq->on_list) {
315 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
316 cfs_rq->on_list = 0;
317 }
318}
319
Peter Zijlstrab7581492008-04-19 19:45:00 +0200320/* Iterate thr' all leaf cfs_rq's on a runqueue */
321#define for_each_leaf_cfs_rq(rq, cfs_rq) \
322 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
323
324/* Do the two (enqueued) entities belong to the same group ? */
325static inline int
326is_same_group(struct sched_entity *se, struct sched_entity *pse)
327{
328 if (se->cfs_rq == pse->cfs_rq)
329 return 1;
330
331 return 0;
332}
333
334static inline struct sched_entity *parent_entity(struct sched_entity *se)
335{
336 return se->parent;
337}
338
Peter Zijlstra464b7522008-10-24 11:06:15 +0200339/* return depth at which a sched entity is present in the hierarchy */
340static inline int depth_se(struct sched_entity *se)
341{
342 int depth = 0;
343
344 for_each_sched_entity(se)
345 depth++;
346
347 return depth;
348}
349
350static void
351find_matching_se(struct sched_entity **se, struct sched_entity **pse)
352{
353 int se_depth, pse_depth;
354
355 /*
356 * preemption test can be made between sibling entities who are in the
357 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
358 * both tasks until we find their ancestors who are siblings of common
359 * parent.
360 */
361
362 /* First walk up until both entities are at same depth */
363 se_depth = depth_se(*se);
364 pse_depth = depth_se(*pse);
365
366 while (se_depth > pse_depth) {
367 se_depth--;
368 *se = parent_entity(*se);
369 }
370
371 while (pse_depth > se_depth) {
372 pse_depth--;
373 *pse = parent_entity(*pse);
374 }
375
376 while (!is_same_group(*se, *pse)) {
377 *se = parent_entity(*se);
378 *pse = parent_entity(*pse);
379 }
380}
381
Peter Zijlstra8f488942009-07-24 12:25:30 +0200382#else /* !CONFIG_FAIR_GROUP_SCHED */
383
384static inline struct task_struct *task_of(struct sched_entity *se)
385{
386 return container_of(se, struct task_struct, se);
387}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200388
389static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
390{
391 return container_of(cfs_rq, struct rq, cfs);
392}
393
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200394#define entity_is_task(se) 1
395
Peter Zijlstrab7581492008-04-19 19:45:00 +0200396#define for_each_sched_entity(se) \
397 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200398
Peter Zijlstrab7581492008-04-19 19:45:00 +0200399static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200400{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200401 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200402}
403
Peter Zijlstrab7581492008-04-19 19:45:00 +0200404static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
405{
406 struct task_struct *p = task_of(se);
407 struct rq *rq = task_rq(p);
408
409 return &rq->cfs;
410}
411
412/* runqueue "owned" by this group */
413static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
414{
415 return NULL;
416}
417
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800418static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
419{
420}
421
422static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
423{
424}
425
Peter Zijlstrab7581492008-04-19 19:45:00 +0200426#define for_each_leaf_cfs_rq(rq, cfs_rq) \
427 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
428
429static inline int
430is_same_group(struct sched_entity *se, struct sched_entity *pse)
431{
432 return 1;
433}
434
435static inline struct sched_entity *parent_entity(struct sched_entity *se)
436{
437 return NULL;
438}
439
Peter Zijlstra464b7522008-10-24 11:06:15 +0200440static inline void
441find_matching_se(struct sched_entity **se, struct sched_entity **pse)
442{
443}
444
Peter Zijlstrab7581492008-04-19 19:45:00 +0200445#endif /* CONFIG_FAIR_GROUP_SCHED */
446
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700447static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100448void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200449
450/**************************************************************
451 * Scheduling class tree data structure manipulation methods:
452 */
453
Andrei Epure1bf08232013-03-12 21:12:24 +0200454static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200455{
Andrei Epure1bf08232013-03-12 21:12:24 +0200456 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200457 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200458 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200459
Andrei Epure1bf08232013-03-12 21:12:24 +0200460 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200461}
462
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200463static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200464{
465 s64 delta = (s64)(vruntime - min_vruntime);
466 if (delta < 0)
467 min_vruntime = vruntime;
468
469 return min_vruntime;
470}
471
Fabio Checconi54fdc582009-07-16 12:32:27 +0200472static inline int entity_before(struct sched_entity *a,
473 struct sched_entity *b)
474{
475 return (s64)(a->vruntime - b->vruntime) < 0;
476}
477
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200478static void update_min_vruntime(struct cfs_rq *cfs_rq)
479{
480 u64 vruntime = cfs_rq->min_vruntime;
481
482 if (cfs_rq->curr)
483 vruntime = cfs_rq->curr->vruntime;
484
485 if (cfs_rq->rb_leftmost) {
486 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
487 struct sched_entity,
488 run_node);
489
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100490 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200491 vruntime = se->vruntime;
492 else
493 vruntime = min_vruntime(vruntime, se->vruntime);
494 }
495
Andrei Epure1bf08232013-03-12 21:12:24 +0200496 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200497 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200498#ifndef CONFIG_64BIT
499 smp_wmb();
500 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
501#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200502}
503
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200504/*
505 * Enqueue an entity into the rb-tree:
506 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200507static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200508{
509 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
510 struct rb_node *parent = NULL;
511 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200512 int leftmost = 1;
513
514 /*
515 * Find the right place in the rbtree:
516 */
517 while (*link) {
518 parent = *link;
519 entry = rb_entry(parent, struct sched_entity, run_node);
520 /*
521 * We dont care about collisions. Nodes with
522 * the same key stay together.
523 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200524 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200525 link = &parent->rb_left;
526 } else {
527 link = &parent->rb_right;
528 leftmost = 0;
529 }
530 }
531
532 /*
533 * Maintain a cache of leftmost tree entries (it is frequently
534 * used):
535 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200536 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200537 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200538
539 rb_link_node(&se->run_node, parent, link);
540 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200541}
542
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200543static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200544{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100545 if (cfs_rq->rb_leftmost == &se->run_node) {
546 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100547
548 next_node = rb_next(&se->run_node);
549 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100550 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200551
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200552 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200553}
554
Peter Zijlstra029632f2011-10-25 10:00:11 +0200555struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200556{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100557 struct rb_node *left = cfs_rq->rb_leftmost;
558
559 if (!left)
560 return NULL;
561
562 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200563}
564
Rik van Rielac53db52011-02-01 09:51:03 -0500565static struct sched_entity *__pick_next_entity(struct sched_entity *se)
566{
567 struct rb_node *next = rb_next(&se->run_node);
568
569 if (!next)
570 return NULL;
571
572 return rb_entry(next, struct sched_entity, run_node);
573}
574
575#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200576struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200577{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100578 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200579
Balbir Singh70eee742008-02-22 13:25:53 +0530580 if (!last)
581 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100582
583 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200584}
585
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200586/**************************************************************
587 * Scheduling class statistics methods:
588 */
589
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100590int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700591 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100592 loff_t *ppos)
593{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700594 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100595 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100596
597 if (ret || !write)
598 return ret;
599
600 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
601 sysctl_sched_min_granularity);
602
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100603#define WRT_SYSCTL(name) \
604 (normalized_sysctl_##name = sysctl_##name / (factor))
605 WRT_SYSCTL(sched_min_granularity);
606 WRT_SYSCTL(sched_latency);
607 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100608#undef WRT_SYSCTL
609
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100610 return 0;
611}
612#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200613
614/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200615 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200616 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100617static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200618{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200619 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100620 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200621
622 return delta;
623}
624
625/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200626 * The idea is to set a period in which each task runs once.
627 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200628 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200629 * this period because otherwise the slices get too small.
630 *
631 * p = (nr <= nl) ? l : l*nr/nl
632 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200633static u64 __sched_period(unsigned long nr_running)
634{
635 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100636 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200637
638 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100639 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200640 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200641 }
642
643 return period;
644}
645
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200646/*
647 * We calculate the wall-time slice from the period by taking a part
648 * proportional to the weight.
649 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200650 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200652static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200653{
Mike Galbraith0a582442009-01-02 12:16:42 +0100654 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200655
Mike Galbraith0a582442009-01-02 12:16:42 +0100656 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100657 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200658 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100659
660 cfs_rq = cfs_rq_of(se);
661 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200662
Mike Galbraith0a582442009-01-02 12:16:42 +0100663 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200664 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100665
666 update_load_add(&lw, se->load.weight);
667 load = &lw;
668 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100669 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100670 }
671 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200672}
673
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200674/*
Andrei Epure660cc002013-03-11 12:03:20 +0200675 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200676 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200677 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200678 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200679static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200680{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200681 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200682}
683
Alex Shia75cdaa2013-06-20 10:18:47 +0800684#ifdef CONFIG_SMP
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100685static unsigned long task_h_load(struct task_struct *p);
686
Alex Shia75cdaa2013-06-20 10:18:47 +0800687static inline void __update_task_entity_contrib(struct sched_entity *se);
688
689/* Give new task start runnable values to heavy its load in infant time */
690void init_task_runnable_average(struct task_struct *p)
691{
692 u32 slice;
693
694 p->se.avg.decay_count = 0;
695 slice = sched_slice(task_cfs_rq(p), &p->se) >> 10;
696 p->se.avg.runnable_avg_sum = slice;
697 p->se.avg.runnable_avg_period = slice;
698 __update_task_entity_contrib(&p->se);
699}
700#else
701void init_task_runnable_average(struct task_struct *p)
702{
703}
704#endif
705
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200706/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100707 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200708 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200709static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200710{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200711 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200712 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100713 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200714
715 if (unlikely(!curr))
716 return;
717
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100718 delta_exec = now - curr->exec_start;
719 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100720 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200721
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200722 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100723
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100724 schedstat_set(curr->statistics.exec_max,
725 max(delta_exec, curr->statistics.exec_max));
726
727 curr->sum_exec_runtime += delta_exec;
728 schedstat_add(cfs_rq, exec_clock, delta_exec);
729
730 curr->vruntime += calc_delta_fair(delta_exec, curr);
731 update_min_vruntime(cfs_rq);
732
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100733 if (entity_is_task(curr)) {
734 struct task_struct *curtask = task_of(curr);
735
Ingo Molnarf977bb42009-09-13 18:15:54 +0200736 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100737 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700738 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100739 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700740
741 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200742}
743
744static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200745update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200746{
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200747 schedstat_set(se->statistics.wait_start, rq_clock(rq_of(cfs_rq)));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200748}
749
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200750/*
751 * Task is being enqueued - update stats:
752 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200753static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200754{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200755 /*
756 * Are we enqueueing a waiting task? (for current tasks
757 * a dequeue/enqueue event is a NOP)
758 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200759 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200760 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200761}
762
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200763static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200764update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200765{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300766 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200767 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start));
Lucas De Marchi41acab82010-03-10 23:37:45 -0300768 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
769 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200770 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200771#ifdef CONFIG_SCHEDSTATS
772 if (entity_is_task(se)) {
773 trace_sched_stat_wait(task_of(se),
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200774 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200775 }
776#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300777 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200778}
779
780static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200781update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200782{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200783 /*
784 * Mark the end of the wait period if dequeueing a
785 * waiting task:
786 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200787 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200788 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200789}
790
791/*
792 * We are picking a new current task - update its stats:
793 */
794static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200795update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200796{
797 /*
798 * We are starting a new run period:
799 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200800 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200801}
802
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200803/**************************************************
804 * Scheduling class queueing methods:
805 */
806
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200807#ifdef CONFIG_NUMA_BALANCING
808/*
Mel Gorman598f0ec2013-10-07 11:28:55 +0100809 * Approximate time to scan a full NUMA task in ms. The task scan period is
810 * calculated based on the tasks virtual memory size and
811 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200812 */
Mel Gorman598f0ec2013-10-07 11:28:55 +0100813unsigned int sysctl_numa_balancing_scan_period_min = 1000;
814unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200815
816/* Portion of address space to scan in MB */
817unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200818
Peter Zijlstra4b96a292012-10-25 14:16:47 +0200819/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
820unsigned int sysctl_numa_balancing_scan_delay = 1000;
821
Rik van Rielde1c9ce2013-10-07 11:29:39 +0100822/*
823 * After skipping a page migration on a shared page, skip N more numa page
824 * migrations unconditionally. This reduces the number of NUMA migrations
825 * in shared memory workloads, and has the effect of pulling tasks towards
826 * where their memory lives, over pulling the memory towards the task.
827 */
828unsigned int sysctl_numa_balancing_migrate_deferred = 16;
829
Mel Gorman598f0ec2013-10-07 11:28:55 +0100830static unsigned int task_nr_scan_windows(struct task_struct *p)
831{
832 unsigned long rss = 0;
833 unsigned long nr_scan_pages;
834
835 /*
836 * Calculations based on RSS as non-present and empty pages are skipped
837 * by the PTE scanner and NUMA hinting faults should be trapped based
838 * on resident pages
839 */
840 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
841 rss = get_mm_rss(p->mm);
842 if (!rss)
843 rss = nr_scan_pages;
844
845 rss = round_up(rss, nr_scan_pages);
846 return rss / nr_scan_pages;
847}
848
849/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
850#define MAX_SCAN_WINDOW 2560
851
852static unsigned int task_scan_min(struct task_struct *p)
853{
854 unsigned int scan, floor;
855 unsigned int windows = 1;
856
857 if (sysctl_numa_balancing_scan_size < MAX_SCAN_WINDOW)
858 windows = MAX_SCAN_WINDOW / sysctl_numa_balancing_scan_size;
859 floor = 1000 / windows;
860
861 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
862 return max_t(unsigned int, floor, scan);
863}
864
865static unsigned int task_scan_max(struct task_struct *p)
866{
867 unsigned int smin = task_scan_min(p);
868 unsigned int smax;
869
870 /* Watch for min being lower than max due to floor calculations */
871 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
872 return max(smin, smax);
873}
874
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100875static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
876{
877 rq->nr_numa_running += (p->numa_preferred_nid != -1);
878 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
879}
880
881static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
882{
883 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
884 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
885}
886
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100887struct numa_group {
888 atomic_t refcount;
889
890 spinlock_t lock; /* nr_tasks, tasks */
891 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +0100892 pid_t gid;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100893 struct list_head task_list;
894
895 struct rcu_head rcu;
Mel Gorman989348b2013-10-07 11:29:40 +0100896 unsigned long total_faults;
897 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100898};
899
Mel Gormane29cf082013-10-07 11:29:22 +0100900pid_t task_numa_group_id(struct task_struct *p)
901{
902 return p->numa_group ? p->numa_group->gid : 0;
903}
904
Mel Gormanac8e8952013-10-07 11:29:03 +0100905static inline int task_faults_idx(int nid, int priv)
906{
907 return 2 * nid + priv;
908}
909
910static inline unsigned long task_faults(struct task_struct *p, int nid)
911{
912 if (!p->numa_faults)
913 return 0;
914
915 return p->numa_faults[task_faults_idx(nid, 0)] +
916 p->numa_faults[task_faults_idx(nid, 1)];
917}
918
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100919static inline unsigned long group_faults(struct task_struct *p, int nid)
920{
921 if (!p->numa_group)
922 return 0;
923
Wanpeng Li82897b42013-12-12 15:23:25 +0800924 return p->numa_group->faults[task_faults_idx(nid, 0)] +
925 p->numa_group->faults[task_faults_idx(nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100926}
927
928/*
929 * These return the fraction of accesses done by a particular task, or
930 * task group, on a particular numa node. The group weight is given a
931 * larger multiplier, in order to group tasks together that are almost
932 * evenly spread out between numa nodes.
933 */
934static inline unsigned long task_weight(struct task_struct *p, int nid)
935{
936 unsigned long total_faults;
937
938 if (!p->numa_faults)
939 return 0;
940
941 total_faults = p->total_numa_faults;
942
943 if (!total_faults)
944 return 0;
945
946 return 1000 * task_faults(p, nid) / total_faults;
947}
948
949static inline unsigned long group_weight(struct task_struct *p, int nid)
950{
Mel Gorman989348b2013-10-07 11:29:40 +0100951 if (!p->numa_group || !p->numa_group->total_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100952 return 0;
953
Mel Gorman989348b2013-10-07 11:29:40 +0100954 return 1000 * group_faults(p, nid) / p->numa_group->total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100955}
956
Mel Gormane6628d52013-10-07 11:29:02 +0100957static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +0100958static unsigned long source_load(int cpu, int type);
959static unsigned long target_load(int cpu, int type);
960static unsigned long power_of(int cpu);
961static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +0100962
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100963/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +0100964struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100965 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +0100966 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100967
968 /* Total compute capacity of CPUs on a node */
969 unsigned long power;
970
971 /* Approximate capacity in terms of runnable tasks on a node */
972 unsigned long capacity;
973 int has_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +0100974};
Mel Gormane6628d52013-10-07 11:29:02 +0100975
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100976/*
977 * XXX borrowed from update_sg_lb_stats
978 */
979static void update_numa_stats(struct numa_stats *ns, int nid)
980{
Peter Zijlstra5eca82a2013-11-06 18:47:57 +0100981 int cpu, cpus = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100982
983 memset(ns, 0, sizeof(*ns));
984 for_each_cpu(cpu, cpumask_of_node(nid)) {
985 struct rq *rq = cpu_rq(cpu);
986
987 ns->nr_running += rq->nr_running;
988 ns->load += weighted_cpuload(cpu);
989 ns->power += power_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +0100990
991 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100992 }
993
Peter Zijlstra5eca82a2013-11-06 18:47:57 +0100994 /*
995 * If we raced with hotplug and there are no CPUs left in our mask
996 * the @ns structure is NULL'ed and task_numa_compare() will
997 * not find this node attractive.
998 *
999 * We'll either bail at !has_capacity, or we'll detect a huge imbalance
1000 * and bail there.
1001 */
1002 if (!cpus)
1003 return;
1004
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001005 ns->load = (ns->load * SCHED_POWER_SCALE) / ns->power;
1006 ns->capacity = DIV_ROUND_CLOSEST(ns->power, SCHED_POWER_SCALE);
1007 ns->has_capacity = (ns->nr_running < ns->capacity);
1008}
1009
Mel Gorman58d081b2013-10-07 11:29:10 +01001010struct task_numa_env {
1011 struct task_struct *p;
1012
1013 int src_cpu, src_nid;
1014 int dst_cpu, dst_nid;
1015
1016 struct numa_stats src_stats, dst_stats;
1017
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001018 int imbalance_pct;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001019
1020 struct task_struct *best_task;
1021 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001022 int best_cpu;
1023};
1024
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001025static void task_numa_assign(struct task_numa_env *env,
1026 struct task_struct *p, long imp)
1027{
1028 if (env->best_task)
1029 put_task_struct(env->best_task);
1030 if (p)
1031 get_task_struct(p);
1032
1033 env->best_task = p;
1034 env->best_imp = imp;
1035 env->best_cpu = env->dst_cpu;
1036}
1037
1038/*
1039 * This checks if the overall compute and NUMA accesses of the system would
1040 * be improved if the source tasks was migrated to the target dst_cpu taking
1041 * into account that it might be best if task running on the dst_cpu should
1042 * be exchanged with the source task
1043 */
Rik van Riel887c2902013-10-07 11:29:31 +01001044static void task_numa_compare(struct task_numa_env *env,
1045 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001046{
1047 struct rq *src_rq = cpu_rq(env->src_cpu);
1048 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1049 struct task_struct *cur;
1050 long dst_load, src_load;
1051 long load;
Rik van Riel887c2902013-10-07 11:29:31 +01001052 long imp = (groupimp > 0) ? groupimp : taskimp;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001053
1054 rcu_read_lock();
1055 cur = ACCESS_ONCE(dst_rq->curr);
1056 if (cur->pid == 0) /* idle */
1057 cur = NULL;
1058
1059 /*
1060 * "imp" is the fault differential for the source task between the
1061 * source and destination node. Calculate the total differential for
1062 * the source task and potential destination task. The more negative
1063 * the value is, the more rmeote accesses that would be expected to
1064 * be incurred if the tasks were swapped.
1065 */
1066 if (cur) {
1067 /* Skip this swap candidate if cannot move to the source cpu */
1068 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1069 goto unlock;
1070
Rik van Riel887c2902013-10-07 11:29:31 +01001071 /*
1072 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa52013-10-07 11:29:32 +01001073 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001074 */
Rik van Rielca28aa52013-10-07 11:29:32 +01001075 if (cur->numa_group == env->p->numa_group) {
Rik van Riel887c2902013-10-07 11:29:31 +01001076 imp = taskimp + task_weight(cur, env->src_nid) -
1077 task_weight(cur, env->dst_nid);
Rik van Rielca28aa52013-10-07 11:29:32 +01001078 /*
1079 * Add some hysteresis to prevent swapping the
1080 * tasks within a group over tiny differences.
1081 */
1082 if (cur->numa_group)
1083 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001084 } else {
Rik van Rielca28aa52013-10-07 11:29:32 +01001085 /*
1086 * Compare the group weights. If a task is all by
1087 * itself (not part of a group), use the task weight
1088 * instead.
1089 */
1090 if (env->p->numa_group)
1091 imp = groupimp;
1092 else
1093 imp = taskimp;
1094
1095 if (cur->numa_group)
1096 imp += group_weight(cur, env->src_nid) -
1097 group_weight(cur, env->dst_nid);
1098 else
1099 imp += task_weight(cur, env->src_nid) -
1100 task_weight(cur, env->dst_nid);
Rik van Riel887c2902013-10-07 11:29:31 +01001101 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001102 }
1103
1104 if (imp < env->best_imp)
1105 goto unlock;
1106
1107 if (!cur) {
1108 /* Is there capacity at our destination? */
1109 if (env->src_stats.has_capacity &&
1110 !env->dst_stats.has_capacity)
1111 goto unlock;
1112
1113 goto balance;
1114 }
1115
1116 /* Balance doesn't matter much if we're running a task per cpu */
1117 if (src_rq->nr_running == 1 && dst_rq->nr_running == 1)
1118 goto assign;
1119
1120 /*
1121 * In the overloaded case, try and keep the load balanced.
1122 */
1123balance:
1124 dst_load = env->dst_stats.load;
1125 src_load = env->src_stats.load;
1126
1127 /* XXX missing power terms */
1128 load = task_h_load(env->p);
1129 dst_load += load;
1130 src_load -= load;
1131
1132 if (cur) {
1133 load = task_h_load(cur);
1134 dst_load -= load;
1135 src_load += load;
1136 }
1137
1138 /* make src_load the smaller */
1139 if (dst_load < src_load)
1140 swap(dst_load, src_load);
1141
1142 if (src_load * env->imbalance_pct < dst_load * 100)
1143 goto unlock;
1144
1145assign:
1146 task_numa_assign(env, cur, imp);
1147unlock:
1148 rcu_read_unlock();
1149}
1150
Rik van Riel887c2902013-10-07 11:29:31 +01001151static void task_numa_find_cpu(struct task_numa_env *env,
1152 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001153{
1154 int cpu;
1155
1156 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1157 /* Skip this CPU if the source task cannot migrate */
1158 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1159 continue;
1160
1161 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001162 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001163 }
1164}
1165
Mel Gorman58d081b2013-10-07 11:29:10 +01001166static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001167{
Mel Gorman58d081b2013-10-07 11:29:10 +01001168 struct task_numa_env env = {
1169 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001170
Mel Gorman58d081b2013-10-07 11:29:10 +01001171 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001172 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001173
1174 .imbalance_pct = 112,
1175
1176 .best_task = NULL,
1177 .best_imp = 0,
1178 .best_cpu = -1
Mel Gorman58d081b2013-10-07 11:29:10 +01001179 };
1180 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001181 unsigned long taskweight, groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001182 int nid, ret;
Rik van Riel887c2902013-10-07 11:29:31 +01001183 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001184
Mel Gorman58d081b2013-10-07 11:29:10 +01001185 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001186 * Pick the lowest SD_NUMA domain, as that would have the smallest
1187 * imbalance and would be the first to start moving tasks about.
1188 *
1189 * And we want to avoid any moving of tasks about, as that would create
1190 * random movement of tasks -- counter the numa conditions we're trying
1191 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001192 */
Mel Gormane6628d52013-10-07 11:29:02 +01001193 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001194 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001195 if (sd)
1196 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001197 rcu_read_unlock();
1198
Rik van Riel46a73e82013-11-11 19:29:25 -05001199 /*
1200 * Cpusets can break the scheduler domain tree into smaller
1201 * balance domains, some of which do not cross NUMA boundaries.
1202 * Tasks that are "trapped" in such domains cannot be migrated
1203 * elsewhere, so there is no point in (re)trying.
1204 */
1205 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001206 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001207 return -EINVAL;
1208 }
1209
Rik van Riel887c2902013-10-07 11:29:31 +01001210 taskweight = task_weight(p, env.src_nid);
1211 groupweight = group_weight(p, env.src_nid);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001212 update_numa_stats(&env.src_stats, env.src_nid);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001213 env.dst_nid = p->numa_preferred_nid;
Rik van Riel887c2902013-10-07 11:29:31 +01001214 taskimp = task_weight(p, env.dst_nid) - taskweight;
1215 groupimp = group_weight(p, env.dst_nid) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001216 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001217
Rik van Riele1dda8a2013-10-07 11:29:19 +01001218 /* If the preferred nid has capacity, try to use it. */
1219 if (env.dst_stats.has_capacity)
Rik van Riel887c2902013-10-07 11:29:31 +01001220 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001221
1222 /* No space available on the preferred nid. Look elsewhere. */
1223 if (env.best_cpu == -1) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001224 for_each_online_node(nid) {
1225 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001226 continue;
1227
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001228 /* Only consider nodes where both task and groups benefit */
Rik van Riel887c2902013-10-07 11:29:31 +01001229 taskimp = task_weight(p, nid) - taskweight;
1230 groupimp = group_weight(p, nid) - groupweight;
1231 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001232 continue;
1233
1234 env.dst_nid = nid;
1235 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel887c2902013-10-07 11:29:31 +01001236 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001237 }
1238 }
1239
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001240 /* No better CPU than the current one was found. */
1241 if (env.best_cpu == -1)
1242 return -EAGAIN;
1243
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001244 sched_setnuma(p, env.dst_nid);
1245
Rik van Riel04bb2f92013-10-07 11:29:36 +01001246 /*
1247 * Reset the scan period if the task is being rescheduled on an
1248 * alternative node to recheck if the tasks is now properly placed.
1249 */
1250 p->numa_scan_period = task_scan_min(p);
1251
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001252 if (env.best_task == NULL) {
1253 int ret = migrate_task_to(p, env.best_cpu);
1254 return ret;
1255 }
1256
1257 ret = migrate_swap(p, env.best_task);
1258 put_task_struct(env.best_task);
1259 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001260}
1261
Mel Gorman6b9a7462013-10-07 11:29:11 +01001262/* Attempt to migrate a task to a CPU on the preferred node. */
1263static void numa_migrate_preferred(struct task_struct *p)
1264{
Rik van Riel2739d3e2013-10-07 11:29:41 +01001265 /* This task has no NUMA fault statistics yet */
1266 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
1267 return;
1268
1269 /* Periodically retry migrating the task to the preferred node */
1270 p->numa_migrate_retry = jiffies + HZ;
1271
Mel Gorman6b9a7462013-10-07 11:29:11 +01001272 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001273 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001274 return;
1275
Mel Gorman6b9a7462013-10-07 11:29:11 +01001276 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001277 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001278}
1279
Rik van Riel04bb2f92013-10-07 11:29:36 +01001280/*
1281 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1282 * increments. The more local the fault statistics are, the higher the scan
1283 * period will be for the next scan window. If local/remote ratio is below
1284 * NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) the
1285 * scan period will decrease
1286 */
1287#define NUMA_PERIOD_SLOTS 10
1288#define NUMA_PERIOD_THRESHOLD 3
1289
1290/*
1291 * Increase the scan period (slow down scanning) if the majority of
1292 * our memory is already on our local node, or if the majority of
1293 * the page accesses are shared with other processes.
1294 * Otherwise, decrease the scan period.
1295 */
1296static void update_task_scan_period(struct task_struct *p,
1297 unsigned long shared, unsigned long private)
1298{
1299 unsigned int period_slot;
1300 int ratio;
1301 int diff;
1302
1303 unsigned long remote = p->numa_faults_locality[0];
1304 unsigned long local = p->numa_faults_locality[1];
1305
1306 /*
1307 * If there were no record hinting faults then either the task is
1308 * completely idle or all activity is areas that are not of interest
1309 * to automatic numa balancing. Scan slower
1310 */
1311 if (local + shared == 0) {
1312 p->numa_scan_period = min(p->numa_scan_period_max,
1313 p->numa_scan_period << 1);
1314
1315 p->mm->numa_next_scan = jiffies +
1316 msecs_to_jiffies(p->numa_scan_period);
1317
1318 return;
1319 }
1320
1321 /*
1322 * Prepare to scale scan period relative to the current period.
1323 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1324 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1325 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1326 */
1327 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1328 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1329 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1330 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1331 if (!slot)
1332 slot = 1;
1333 diff = slot * period_slot;
1334 } else {
1335 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1336
1337 /*
1338 * Scale scan rate increases based on sharing. There is an
1339 * inverse relationship between the degree of sharing and
1340 * the adjustment made to the scanning period. Broadly
1341 * speaking the intent is that there is little point
1342 * scanning faster if shared accesses dominate as it may
1343 * simply bounce migrations uselessly
1344 */
Rik van Riel04bb2f92013-10-07 11:29:36 +01001345 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared));
1346 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1347 }
1348
1349 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1350 task_scan_min(p), task_scan_max(p));
1351 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1352}
1353
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001354static void task_numa_placement(struct task_struct *p)
1355{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001356 int seq, nid, max_nid = -1, max_group_nid = -1;
1357 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001358 unsigned long fault_types[2] = { 0, 0 };
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001359 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001360
Hugh Dickins2832bc12012-12-19 17:42:16 -08001361 seq = ACCESS_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001362 if (p->numa_scan_seq == seq)
1363 return;
1364 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001365 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001366
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001367 /* If the task is part of a group prevent parallel updates to group stats */
1368 if (p->numa_group) {
1369 group_lock = &p->numa_group->lock;
1370 spin_lock(group_lock);
1371 }
1372
Mel Gorman688b7582013-10-07 11:28:58 +01001373 /* Find the node with the highest number of faults */
1374 for_each_online_node(nid) {
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001375 unsigned long faults = 0, group_faults = 0;
Mel Gormanac8e8952013-10-07 11:29:03 +01001376 int priv, i;
Mel Gorman745d6142013-10-07 11:28:59 +01001377
Mel Gormanac8e8952013-10-07 11:29:03 +01001378 for (priv = 0; priv < 2; priv++) {
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001379 long diff;
1380
Mel Gormanac8e8952013-10-07 11:29:03 +01001381 i = task_faults_idx(nid, priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001382 diff = -p->numa_faults[i];
Mel Gorman745d6142013-10-07 11:28:59 +01001383
Mel Gormanac8e8952013-10-07 11:29:03 +01001384 /* Decay existing window, copy faults since last scan */
1385 p->numa_faults[i] >>= 1;
1386 p->numa_faults[i] += p->numa_faults_buffer[i];
Rik van Riel04bb2f92013-10-07 11:29:36 +01001387 fault_types[priv] += p->numa_faults_buffer[i];
Mel Gormanac8e8952013-10-07 11:29:03 +01001388 p->numa_faults_buffer[i] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001389
1390 faults += p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001391 diff += p->numa_faults[i];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001392 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001393 if (p->numa_group) {
1394 /* safe because we can only change our own group */
Mel Gorman989348b2013-10-07 11:29:40 +01001395 p->numa_group->faults[i] += diff;
1396 p->numa_group->total_faults += diff;
1397 group_faults += p->numa_group->faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001398 }
Mel Gormanac8e8952013-10-07 11:29:03 +01001399 }
1400
Mel Gorman688b7582013-10-07 11:28:58 +01001401 if (faults > max_faults) {
1402 max_faults = faults;
1403 max_nid = nid;
1404 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001405
1406 if (group_faults > max_group_faults) {
1407 max_group_faults = group_faults;
1408 max_group_nid = nid;
1409 }
1410 }
1411
Rik van Riel04bb2f92013-10-07 11:29:36 +01001412 update_task_scan_period(p, fault_types[0], fault_types[1]);
1413
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001414 if (p->numa_group) {
1415 /*
1416 * If the preferred task and group nids are different,
1417 * iterate over the nodes again to find the best place.
1418 */
1419 if (max_nid != max_group_nid) {
1420 unsigned long weight, max_weight = 0;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001421
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001422 for_each_online_node(nid) {
1423 weight = task_weight(p, nid) + group_weight(p, nid);
1424 if (weight > max_weight) {
1425 max_weight = weight;
1426 max_nid = nid;
1427 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001428 }
1429 }
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001430
1431 spin_unlock(group_lock);
Mel Gorman688b7582013-10-07 11:28:58 +01001432 }
1433
Mel Gorman6b9a7462013-10-07 11:29:11 +01001434 /* Preferred node as the node with the most faults */
Mel Gorman3a7053b2013-10-07 11:29:00 +01001435 if (max_faults && max_nid != p->numa_preferred_nid) {
Mel Gormane6628d52013-10-07 11:29:02 +01001436 /* Update the preferred nid and migrate task if possible */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001437 sched_setnuma(p, max_nid);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001438 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01001439 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001440}
1441
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001442static inline int get_numa_group(struct numa_group *grp)
1443{
1444 return atomic_inc_not_zero(&grp->refcount);
1445}
1446
1447static inline void put_numa_group(struct numa_group *grp)
1448{
1449 if (atomic_dec_and_test(&grp->refcount))
1450 kfree_rcu(grp, rcu);
1451}
1452
Mel Gorman3e6a9412013-10-07 11:29:35 +01001453static void task_numa_group(struct task_struct *p, int cpupid, int flags,
1454 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001455{
1456 struct numa_group *grp, *my_grp;
1457 struct task_struct *tsk;
1458 bool join = false;
1459 int cpu = cpupid_to_cpu(cpupid);
1460 int i;
1461
1462 if (unlikely(!p->numa_group)) {
1463 unsigned int size = sizeof(struct numa_group) +
Mel Gorman989348b2013-10-07 11:29:40 +01001464 2*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001465
1466 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1467 if (!grp)
1468 return;
1469
1470 atomic_set(&grp->refcount, 1);
1471 spin_lock_init(&grp->lock);
1472 INIT_LIST_HEAD(&grp->task_list);
Mel Gormane29cf082013-10-07 11:29:22 +01001473 grp->gid = p->pid;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001474
1475 for (i = 0; i < 2*nr_node_ids; i++)
Mel Gorman989348b2013-10-07 11:29:40 +01001476 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001477
Mel Gorman989348b2013-10-07 11:29:40 +01001478 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001479
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001480 list_add(&p->numa_entry, &grp->task_list);
1481 grp->nr_tasks++;
1482 rcu_assign_pointer(p->numa_group, grp);
1483 }
1484
1485 rcu_read_lock();
1486 tsk = ACCESS_ONCE(cpu_rq(cpu)->curr);
1487
1488 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02001489 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001490
1491 grp = rcu_dereference(tsk->numa_group);
1492 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001493 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001494
1495 my_grp = p->numa_group;
1496 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001497 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001498
1499 /*
1500 * Only join the other group if its bigger; if we're the bigger group,
1501 * the other task will join us.
1502 */
1503 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02001504 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001505
1506 /*
1507 * Tie-break on the grp address.
1508 */
1509 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001510 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001511
Rik van Rieldabe1d92013-10-07 11:29:34 +01001512 /* Always join threads in the same process. */
1513 if (tsk->mm == current->mm)
1514 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001515
Rik van Rieldabe1d92013-10-07 11:29:34 +01001516 /* Simple filter to avoid false positives due to PID collisions */
1517 if (flags & TNF_SHARED)
1518 join = true;
1519
Mel Gorman3e6a9412013-10-07 11:29:35 +01001520 /* Update priv based on whether false sharing was detected */
1521 *priv = !join;
1522
Rik van Rieldabe1d92013-10-07 11:29:34 +01001523 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02001524 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001525
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001526 rcu_read_unlock();
1527
1528 if (!join)
1529 return;
1530
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001531 double_lock(&my_grp->lock, &grp->lock);
1532
Mel Gorman989348b2013-10-07 11:29:40 +01001533 for (i = 0; i < 2*nr_node_ids; i++) {
1534 my_grp->faults[i] -= p->numa_faults[i];
1535 grp->faults[i] += p->numa_faults[i];
1536 }
1537 my_grp->total_faults -= p->total_numa_faults;
1538 grp->total_faults += p->total_numa_faults;
1539
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001540 list_move(&p->numa_entry, &grp->task_list);
1541 my_grp->nr_tasks--;
1542 grp->nr_tasks++;
1543
1544 spin_unlock(&my_grp->lock);
1545 spin_unlock(&grp->lock);
1546
1547 rcu_assign_pointer(p->numa_group, grp);
1548
1549 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02001550 return;
1551
1552no_join:
1553 rcu_read_unlock();
1554 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001555}
1556
1557void task_numa_free(struct task_struct *p)
1558{
1559 struct numa_group *grp = p->numa_group;
1560 int i;
Rik van Riel82727012013-10-07 11:29:28 +01001561 void *numa_faults = p->numa_faults;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001562
1563 if (grp) {
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001564 spin_lock(&grp->lock);
Mel Gorman989348b2013-10-07 11:29:40 +01001565 for (i = 0; i < 2*nr_node_ids; i++)
1566 grp->faults[i] -= p->numa_faults[i];
1567 grp->total_faults -= p->total_numa_faults;
1568
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001569 list_del(&p->numa_entry);
1570 grp->nr_tasks--;
1571 spin_unlock(&grp->lock);
1572 rcu_assign_pointer(p->numa_group, NULL);
1573 put_numa_group(grp);
1574 }
1575
Rik van Riel82727012013-10-07 11:29:28 +01001576 p->numa_faults = NULL;
1577 p->numa_faults_buffer = NULL;
1578 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001579}
1580
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001581/*
1582 * Got a PROT_NONE fault for a page on @node.
1583 */
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001584void task_numa_fault(int last_cpupid, int node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001585{
1586 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001587 bool migrated = flags & TNF_MIGRATED;
Mel Gormanac8e8952013-10-07 11:29:03 +01001588 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001589
Dave Kleikamp10e84b92013-07-31 13:53:35 -07001590 if (!numabalancing_enabled)
Mel Gorman1a687c22012-11-22 11:16:36 +00001591 return;
1592
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01001593 /* for example, ksmd faulting in a user's mm */
1594 if (!p->mm)
1595 return;
1596
Rik van Riel82727012013-10-07 11:29:28 +01001597 /* Do not worry about placement if exiting */
1598 if (p->state == TASK_DEAD)
1599 return;
1600
Mel Gormanf809ca92013-10-07 11:28:57 +01001601 /* Allocate buffer to track faults on a per-node basis */
1602 if (unlikely(!p->numa_faults)) {
Mel Gormanac8e8952013-10-07 11:29:03 +01001603 int size = sizeof(*p->numa_faults) * 2 * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01001604
Mel Gorman745d6142013-10-07 11:28:59 +01001605 /* numa_faults and numa_faults_buffer share the allocation */
1606 p->numa_faults = kzalloc(size * 2, GFP_KERNEL|__GFP_NOWARN);
Mel Gormanf809ca92013-10-07 11:28:57 +01001607 if (!p->numa_faults)
1608 return;
Mel Gorman745d6142013-10-07 11:28:59 +01001609
1610 BUG_ON(p->numa_faults_buffer);
Mel Gormanac8e8952013-10-07 11:29:03 +01001611 p->numa_faults_buffer = p->numa_faults + (2 * nr_node_ids);
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001612 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001613 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01001614 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001615
Mel Gormanfb003b82012-11-15 09:01:14 +00001616 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001617 * First accesses are treated as private, otherwise consider accesses
1618 * to be private if the accessing pid has not changed
1619 */
1620 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
1621 priv = 1;
1622 } else {
1623 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001624 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01001625 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001626 }
1627
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001628 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01001629
Rik van Riel2739d3e2013-10-07 11:29:41 +01001630 /*
1631 * Retry task to preferred node migration periodically, in case it
1632 * case it previously failed, or the scheduler moved us.
1633 */
1634 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01001635 numa_migrate_preferred(p);
1636
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001637 if (migrated)
1638 p->numa_pages_migrated += pages;
1639
Mel Gormanac8e8952013-10-07 11:29:03 +01001640 p->numa_faults_buffer[task_faults_idx(node, priv)] += pages;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001641 p->numa_faults_locality[!!(flags & TNF_FAULT_LOCAL)] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001642}
1643
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001644static void reset_ptenuma_scan(struct task_struct *p)
1645{
1646 ACCESS_ONCE(p->mm->numa_scan_seq)++;
1647 p->mm->numa_scan_offset = 0;
1648}
1649
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001650/*
1651 * The expensive part of numa migration is done from task_work context.
1652 * Triggered from task_tick_numa().
1653 */
1654void task_numa_work(struct callback_head *work)
1655{
1656 unsigned long migrate, next_scan, now = jiffies;
1657 struct task_struct *p = current;
1658 struct mm_struct *mm = p->mm;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001659 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00001660 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001661 unsigned long nr_pte_updates = 0;
Mel Gorman9f406042012-11-14 18:34:32 +00001662 long pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001663
1664 WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
1665
1666 work->next = work; /* protect against double add */
1667 /*
1668 * Who cares about NUMA placement when they're dying.
1669 *
1670 * NOTE: make sure not to dereference p->mm before this check,
1671 * exit_task_work() happens _after_ exit_mm() so we could be called
1672 * without p->mm even though we still had it when we enqueued this
1673 * work.
1674 */
1675 if (p->flags & PF_EXITING)
1676 return;
1677
Mel Gorman930aa172013-10-07 11:29:37 +01001678 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01001679 mm->numa_next_scan = now +
1680 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00001681 }
1682
1683 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001684 * Enforce maximal scan/migration frequency..
1685 */
1686 migrate = mm->numa_next_scan;
1687 if (time_before(now, migrate))
1688 return;
1689
Mel Gorman598f0ec2013-10-07 11:28:55 +01001690 if (p->numa_scan_period == 0) {
1691 p->numa_scan_period_max = task_scan_max(p);
1692 p->numa_scan_period = task_scan_min(p);
1693 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001694
Mel Gormanfb003b82012-11-15 09:01:14 +00001695 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001696 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
1697 return;
1698
Mel Gormane14808b2012-11-19 10:59:15 +00001699 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01001700 * Delay this task enough that another task of this mm will likely win
1701 * the next time around.
1702 */
1703 p->node_stamp += 2 * TICK_NSEC;
1704
Mel Gorman9f406042012-11-14 18:34:32 +00001705 start = mm->numa_scan_offset;
1706 pages = sysctl_numa_balancing_scan_size;
1707 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
1708 if (!pages)
1709 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001710
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001711 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00001712 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001713 if (!vma) {
1714 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00001715 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001716 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001717 }
Mel Gorman9f406042012-11-14 18:34:32 +00001718 for (; vma; vma = vma->vm_next) {
Mel Gormanfc3147242013-10-07 11:29:09 +01001719 if (!vma_migratable(vma) || !vma_policy_mof(p, vma))
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001720 continue;
1721
Mel Gorman4591ce4f2013-10-07 11:29:13 +01001722 /*
1723 * Shared library pages mapped by multiple processes are not
1724 * migrated as it is expected they are cache replicated. Avoid
1725 * hinting faults in read-only file-backed mappings or the vdso
1726 * as migrating the pages will be of marginal benefit.
1727 */
1728 if (!vma->vm_mm ||
1729 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
1730 continue;
1731
Mel Gorman3c67f472013-12-18 17:08:40 -08001732 /*
1733 * Skip inaccessible VMAs to avoid any confusion between
1734 * PROT_NONE and NUMA hinting ptes
1735 */
1736 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
1737 continue;
1738
Mel Gorman9f406042012-11-14 18:34:32 +00001739 do {
1740 start = max(start, vma->vm_start);
1741 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
1742 end = min(end, vma->vm_end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001743 nr_pte_updates += change_prot_numa(vma, start, end);
1744
1745 /*
1746 * Scan sysctl_numa_balancing_scan_size but ensure that
1747 * at least one PTE is updated so that unused virtual
1748 * address space is quickly skipped.
1749 */
1750 if (nr_pte_updates)
1751 pages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001752
Mel Gorman9f406042012-11-14 18:34:32 +00001753 start = end;
1754 if (pages <= 0)
1755 goto out;
1756 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001757 }
1758
Mel Gorman9f406042012-11-14 18:34:32 +00001759out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001760 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001761 * It is possible to reach the end of the VMA list but the last few
1762 * VMAs are not guaranteed to the vma_migratable. If they are not, we
1763 * would find the !migratable VMA on the next scan but not reset the
1764 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001765 */
1766 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00001767 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001768 else
1769 reset_ptenuma_scan(p);
1770 up_read(&mm->mmap_sem);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001771}
1772
1773/*
1774 * Drive the periodic memory faults..
1775 */
1776void task_tick_numa(struct rq *rq, struct task_struct *curr)
1777{
1778 struct callback_head *work = &curr->numa_work;
1779 u64 period, now;
1780
1781 /*
1782 * We don't care about NUMA placement if we don't have memory.
1783 */
1784 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
1785 return;
1786
1787 /*
1788 * Using runtime rather than walltime has the dual advantage that
1789 * we (mostly) drive the selection from busy threads and that the
1790 * task needs to have done some actual work before we bother with
1791 * NUMA placement.
1792 */
1793 now = curr->se.sum_exec_runtime;
1794 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
1795
1796 if (now - curr->node_stamp > period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001797 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01001798 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01001799 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001800
1801 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
1802 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
1803 task_work_add(curr, work, true);
1804 }
1805 }
1806}
1807#else
1808static void task_tick_numa(struct rq *rq, struct task_struct *curr)
1809{
1810}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001811
1812static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1813{
1814}
1815
1816static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1817{
1818}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001819#endif /* CONFIG_NUMA_BALANCING */
1820
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001821static void
1822account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
1823{
1824 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001825 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02001826 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01001827#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001828 if (entity_is_task(se)) {
1829 struct rq *rq = rq_of(cfs_rq);
1830
1831 account_numa_enqueue(rq, task_of(se));
1832 list_add(&se->group_node, &rq->cfs_tasks);
1833 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01001834#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001835 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001836}
1837
1838static void
1839account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
1840{
1841 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001842 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02001843 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001844 if (entity_is_task(se)) {
1845 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05301846 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001847 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001848 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001849}
1850
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001851#ifdef CONFIG_FAIR_GROUP_SCHED
1852# ifdef CONFIG_SMP
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001853static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
1854{
1855 long tg_weight;
1856
1857 /*
1858 * Use this CPU's actual weight instead of the last load_contribution
1859 * to gain a more accurate current total weight. See
1860 * update_cfs_rq_load_contribution().
1861 */
Alex Shibf5b9862013-06-20 10:18:54 +08001862 tg_weight = atomic_long_read(&tg->load_avg);
Paul Turner82958362012-10-04 13:18:31 +02001863 tg_weight -= cfs_rq->tg_load_contrib;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001864 tg_weight += cfs_rq->load.weight;
1865
1866 return tg_weight;
1867}
1868
Paul Turner6d5ab292011-01-21 20:45:01 -08001869static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001870{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001871 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001872
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001873 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -08001874 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001875
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001876 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02001877 if (tg_weight)
1878 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001879
1880 if (shares < MIN_SHARES)
1881 shares = MIN_SHARES;
1882 if (shares > tg->shares)
1883 shares = tg->shares;
1884
1885 return shares;
1886}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001887# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08001888static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001889{
1890 return tg->shares;
1891}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001892# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001893static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
1894 unsigned long weight)
1895{
Paul Turner19e5eeb2010-12-15 19:10:18 -08001896 if (se->on_rq) {
1897 /* commit outstanding execution time */
1898 if (cfs_rq->curr == se)
1899 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001900 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08001901 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001902
1903 update_load_set(&se->load, weight);
1904
1905 if (se->on_rq)
1906 account_entity_enqueue(cfs_rq, se);
1907}
1908
Paul Turner82958362012-10-04 13:18:31 +02001909static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
1910
Paul Turner6d5ab292011-01-21 20:45:01 -08001911static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001912{
1913 struct task_group *tg;
1914 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001915 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001916
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001917 tg = cfs_rq->tg;
1918 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07001919 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001920 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08001921#ifndef CONFIG_SMP
1922 if (likely(se->load.weight == tg->shares))
1923 return;
1924#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08001925 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001926
1927 reweight_entity(cfs_rq_of(se), se, shares);
1928}
1929#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -08001930static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001931{
1932}
1933#endif /* CONFIG_FAIR_GROUP_SCHED */
1934
Alex Shi141965c2013-06-26 13:05:39 +08001935#ifdef CONFIG_SMP
Paul Turner9d85f212012-10-04 13:18:29 +02001936/*
Paul Turner5b51f2f2012-10-04 13:18:32 +02001937 * We choose a half-life close to 1 scheduling period.
1938 * Note: The tables below are dependent on this value.
1939 */
1940#define LOAD_AVG_PERIOD 32
1941#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
1942#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */
1943
1944/* Precomputed fixed inverse multiplies for multiplication by y^n */
1945static const u32 runnable_avg_yN_inv[] = {
1946 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
1947 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
1948 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
1949 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
1950 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
1951 0x85aac367, 0x82cd8698,
1952};
1953
1954/*
1955 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
1956 * over-estimates when re-combining.
1957 */
1958static const u32 runnable_avg_yN_sum[] = {
1959 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
1960 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
1961 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
1962};
1963
1964/*
Paul Turner9d85f212012-10-04 13:18:29 +02001965 * Approximate:
1966 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
1967 */
1968static __always_inline u64 decay_load(u64 val, u64 n)
1969{
Paul Turner5b51f2f2012-10-04 13:18:32 +02001970 unsigned int local_n;
1971
1972 if (!n)
1973 return val;
1974 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
1975 return 0;
1976
1977 /* after bounds checking we can collapse to 32-bit */
1978 local_n = n;
1979
1980 /*
1981 * As y^PERIOD = 1/2, we can combine
1982 * y^n = 1/2^(n/PERIOD) * k^(n%PERIOD)
1983 * With a look-up table which covers k^n (n<PERIOD)
1984 *
1985 * To achieve constant time decay_load.
1986 */
1987 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
1988 val >>= local_n / LOAD_AVG_PERIOD;
1989 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02001990 }
1991
Paul Turner5b51f2f2012-10-04 13:18:32 +02001992 val *= runnable_avg_yN_inv[local_n];
1993 /* We don't use SRR here since we always want to round down. */
1994 return val >> 32;
1995}
1996
1997/*
1998 * For updates fully spanning n periods, the contribution to runnable
1999 * average will be: \Sum 1024*y^n
2000 *
2001 * We can compute this reasonably efficiently by combining:
2002 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2003 */
2004static u32 __compute_runnable_contrib(u64 n)
2005{
2006 u32 contrib = 0;
2007
2008 if (likely(n <= LOAD_AVG_PERIOD))
2009 return runnable_avg_yN_sum[n];
2010 else if (unlikely(n >= LOAD_AVG_MAX_N))
2011 return LOAD_AVG_MAX;
2012
2013 /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */
2014 do {
2015 contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */
2016 contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD];
2017
2018 n -= LOAD_AVG_PERIOD;
2019 } while (n > LOAD_AVG_PERIOD);
2020
2021 contrib = decay_load(contrib, n);
2022 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002023}
2024
2025/*
2026 * We can represent the historical contribution to runnable average as the
2027 * coefficients of a geometric series. To do this we sub-divide our runnable
2028 * history into segments of approximately 1ms (1024us); label the segment that
2029 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2030 *
2031 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2032 * p0 p1 p2
2033 * (now) (~1ms ago) (~2ms ago)
2034 *
2035 * Let u_i denote the fraction of p_i that the entity was runnable.
2036 *
2037 * We then designate the fractions u_i as our co-efficients, yielding the
2038 * following representation of historical load:
2039 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2040 *
2041 * We choose y based on the with of a reasonably scheduling period, fixing:
2042 * y^32 = 0.5
2043 *
2044 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2045 * approximately half as much as the contribution to load within the last ms
2046 * (u_0).
2047 *
2048 * When a period "rolls over" and we have new u_0`, multiplying the previous
2049 * sum again by y is sufficient to update:
2050 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2051 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2052 */
2053static __always_inline int __update_entity_runnable_avg(u64 now,
2054 struct sched_avg *sa,
2055 int runnable)
2056{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002057 u64 delta, periods;
2058 u32 runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002059 int delta_w, decayed = 0;
2060
2061 delta = now - sa->last_runnable_update;
2062 /*
2063 * This should only happen when time goes backwards, which it
2064 * unfortunately does during sched clock init when we swap over to TSC.
2065 */
2066 if ((s64)delta < 0) {
2067 sa->last_runnable_update = now;
2068 return 0;
2069 }
2070
2071 /*
2072 * Use 1024ns as the unit of measurement since it's a reasonable
2073 * approximation of 1us and fast to compute.
2074 */
2075 delta >>= 10;
2076 if (!delta)
2077 return 0;
2078 sa->last_runnable_update = now;
2079
2080 /* delta_w is the amount already accumulated against our next period */
2081 delta_w = sa->runnable_avg_period % 1024;
2082 if (delta + delta_w >= 1024) {
2083 /* period roll-over */
2084 decayed = 1;
2085
2086 /*
2087 * Now that we know we're crossing a period boundary, figure
2088 * out how much from delta we need to complete the current
2089 * period and accrue it.
2090 */
2091 delta_w = 1024 - delta_w;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002092 if (runnable)
2093 sa->runnable_avg_sum += delta_w;
2094 sa->runnable_avg_period += delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002095
Paul Turner5b51f2f2012-10-04 13:18:32 +02002096 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002097
Paul Turner5b51f2f2012-10-04 13:18:32 +02002098 /* Figure out how many additional periods this update spans */
2099 periods = delta / 1024;
2100 delta %= 1024;
2101
2102 sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum,
2103 periods + 1);
2104 sa->runnable_avg_period = decay_load(sa->runnable_avg_period,
2105 periods + 1);
2106
2107 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
2108 runnable_contrib = __compute_runnable_contrib(periods);
2109 if (runnable)
2110 sa->runnable_avg_sum += runnable_contrib;
2111 sa->runnable_avg_period += runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002112 }
2113
2114 /* Remainder of delta accrued against u_0` */
2115 if (runnable)
2116 sa->runnable_avg_sum += delta;
2117 sa->runnable_avg_period += delta;
2118
2119 return decayed;
2120}
2121
Paul Turner9ee474f2012-10-04 13:18:30 +02002122/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02002123static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02002124{
2125 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2126 u64 decays = atomic64_read(&cfs_rq->decay_counter);
2127
2128 decays -= se->avg.decay_count;
2129 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02002130 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02002131
2132 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
2133 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02002134
2135 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02002136}
2137
Paul Turnerc566e8e2012-10-04 13:18:30 +02002138#ifdef CONFIG_FAIR_GROUP_SCHED
2139static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
2140 int force_update)
2141{
2142 struct task_group *tg = cfs_rq->tg;
Alex Shibf5b9862013-06-20 10:18:54 +08002143 long tg_contrib;
Paul Turnerc566e8e2012-10-04 13:18:30 +02002144
2145 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
2146 tg_contrib -= cfs_rq->tg_load_contrib;
2147
Alex Shibf5b9862013-06-20 10:18:54 +08002148 if (force_update || abs(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
2149 atomic_long_add(tg_contrib, &tg->load_avg);
Paul Turnerc566e8e2012-10-04 13:18:30 +02002150 cfs_rq->tg_load_contrib += tg_contrib;
2151 }
2152}
Paul Turner8165e142012-10-04 13:18:31 +02002153
Paul Turnerbb17f652012-10-04 13:18:31 +02002154/*
2155 * Aggregate cfs_rq runnable averages into an equivalent task_group
2156 * representation for computing load contributions.
2157 */
2158static inline void __update_tg_runnable_avg(struct sched_avg *sa,
2159 struct cfs_rq *cfs_rq)
2160{
2161 struct task_group *tg = cfs_rq->tg;
2162 long contrib;
2163
2164 /* The fraction of a cpu used by this cfs_rq */
Michal Nazarewicz85b088e2013-11-10 20:42:01 +01002165 contrib = div_u64((u64)sa->runnable_avg_sum << NICE_0_SHIFT,
Paul Turnerbb17f652012-10-04 13:18:31 +02002166 sa->runnable_avg_period + 1);
2167 contrib -= cfs_rq->tg_runnable_contrib;
2168
2169 if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
2170 atomic_add(contrib, &tg->runnable_avg);
2171 cfs_rq->tg_runnable_contrib += contrib;
2172 }
2173}
2174
Paul Turner8165e142012-10-04 13:18:31 +02002175static inline void __update_group_entity_contrib(struct sched_entity *se)
2176{
2177 struct cfs_rq *cfs_rq = group_cfs_rq(se);
2178 struct task_group *tg = cfs_rq->tg;
Paul Turnerbb17f652012-10-04 13:18:31 +02002179 int runnable_avg;
2180
Paul Turner8165e142012-10-04 13:18:31 +02002181 u64 contrib;
2182
2183 contrib = cfs_rq->tg_load_contrib * tg->shares;
Alex Shibf5b9862013-06-20 10:18:54 +08002184 se->avg.load_avg_contrib = div_u64(contrib,
2185 atomic_long_read(&tg->load_avg) + 1);
Paul Turnerbb17f652012-10-04 13:18:31 +02002186
2187 /*
2188 * For group entities we need to compute a correction term in the case
2189 * that they are consuming <1 cpu so that we would contribute the same
2190 * load as a task of equal weight.
2191 *
2192 * Explicitly co-ordinating this measurement would be expensive, but
2193 * fortunately the sum of each cpus contribution forms a usable
2194 * lower-bound on the true value.
2195 *
2196 * Consider the aggregate of 2 contributions. Either they are disjoint
2197 * (and the sum represents true value) or they are disjoint and we are
2198 * understating by the aggregate of their overlap.
2199 *
2200 * Extending this to N cpus, for a given overlap, the maximum amount we
2201 * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
2202 * cpus that overlap for this interval and w_i is the interval width.
2203 *
2204 * On a small machine; the first term is well-bounded which bounds the
2205 * total error since w_i is a subset of the period. Whereas on a
2206 * larger machine, while this first term can be larger, if w_i is the
2207 * of consequential size guaranteed to see n_i*w_i quickly converge to
2208 * our upper bound of 1-cpu.
2209 */
2210 runnable_avg = atomic_read(&tg->runnable_avg);
2211 if (runnable_avg < NICE_0_LOAD) {
2212 se->avg.load_avg_contrib *= runnable_avg;
2213 se->avg.load_avg_contrib >>= NICE_0_SHIFT;
2214 }
Paul Turner8165e142012-10-04 13:18:31 +02002215}
Paul Turnerc566e8e2012-10-04 13:18:30 +02002216#else
2217static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
2218 int force_update) {}
Paul Turnerbb17f652012-10-04 13:18:31 +02002219static inline void __update_tg_runnable_avg(struct sched_avg *sa,
2220 struct cfs_rq *cfs_rq) {}
Paul Turner8165e142012-10-04 13:18:31 +02002221static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Paul Turnerc566e8e2012-10-04 13:18:30 +02002222#endif
2223
Paul Turner8165e142012-10-04 13:18:31 +02002224static inline void __update_task_entity_contrib(struct sched_entity *se)
2225{
2226 u32 contrib;
2227
2228 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
2229 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
2230 contrib /= (se->avg.runnable_avg_period + 1);
2231 se->avg.load_avg_contrib = scale_load(contrib);
2232}
2233
Paul Turner2dac7542012-10-04 13:18:30 +02002234/* Compute the current contribution to load_avg by se, return any delta */
2235static long __update_entity_load_avg_contrib(struct sched_entity *se)
2236{
2237 long old_contrib = se->avg.load_avg_contrib;
2238
Paul Turner8165e142012-10-04 13:18:31 +02002239 if (entity_is_task(se)) {
2240 __update_task_entity_contrib(se);
2241 } else {
Paul Turnerbb17f652012-10-04 13:18:31 +02002242 __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
Paul Turner8165e142012-10-04 13:18:31 +02002243 __update_group_entity_contrib(se);
2244 }
Paul Turner2dac7542012-10-04 13:18:30 +02002245
2246 return se->avg.load_avg_contrib - old_contrib;
2247}
2248
Paul Turner9ee474f2012-10-04 13:18:30 +02002249static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
2250 long load_contrib)
2251{
2252 if (likely(load_contrib < cfs_rq->blocked_load_avg))
2253 cfs_rq->blocked_load_avg -= load_contrib;
2254 else
2255 cfs_rq->blocked_load_avg = 0;
2256}
2257
Paul Turnerf1b17282012-10-04 13:18:31 +02002258static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
2259
Paul Turner9d85f212012-10-04 13:18:29 +02002260/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02002261static inline void update_entity_load_avg(struct sched_entity *se,
2262 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002263{
Paul Turner2dac7542012-10-04 13:18:30 +02002264 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2265 long contrib_delta;
Paul Turnerf1b17282012-10-04 13:18:31 +02002266 u64 now;
Paul Turner2dac7542012-10-04 13:18:30 +02002267
Paul Turnerf1b17282012-10-04 13:18:31 +02002268 /*
2269 * For a group entity we need to use their owned cfs_rq_clock_task() in
2270 * case they are the parent of a throttled hierarchy.
2271 */
2272 if (entity_is_task(se))
2273 now = cfs_rq_clock_task(cfs_rq);
2274 else
2275 now = cfs_rq_clock_task(group_cfs_rq(se));
2276
2277 if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
Paul Turner2dac7542012-10-04 13:18:30 +02002278 return;
2279
2280 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02002281
2282 if (!update_cfs_rq)
2283 return;
2284
Paul Turner2dac7542012-10-04 13:18:30 +02002285 if (se->on_rq)
2286 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02002287 else
2288 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
2289}
2290
2291/*
2292 * Decay the load contributed by all blocked children and account this so that
2293 * their contribution may appropriately discounted when they wake up.
2294 */
Paul Turneraff3e492012-10-04 13:18:30 +02002295static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02002296{
Paul Turnerf1b17282012-10-04 13:18:31 +02002297 u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
Paul Turner9ee474f2012-10-04 13:18:30 +02002298 u64 decays;
2299
2300 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02002301 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02002302 return;
2303
Alex Shi25099402013-06-20 10:18:55 +08002304 if (atomic_long_read(&cfs_rq->removed_load)) {
2305 unsigned long removed_load;
2306 removed_load = atomic_long_xchg(&cfs_rq->removed_load, 0);
Paul Turneraff3e492012-10-04 13:18:30 +02002307 subtract_blocked_load_contrib(cfs_rq, removed_load);
2308 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002309
Paul Turneraff3e492012-10-04 13:18:30 +02002310 if (decays) {
2311 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
2312 decays);
2313 atomic64_add(decays, &cfs_rq->decay_counter);
2314 cfs_rq->last_decay = now;
2315 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02002316
2317 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02002318}
Ben Segall18bf2802012-10-04 12:51:20 +02002319
2320static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
2321{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002322 __update_entity_runnable_avg(rq_clock_task(rq), &rq->avg, runnable);
Paul Turnerbb17f652012-10-04 13:18:31 +02002323 __update_tg_runnable_avg(&rq->avg, &rq->cfs);
Ben Segall18bf2802012-10-04 12:51:20 +02002324}
Paul Turner2dac7542012-10-04 13:18:30 +02002325
2326/* Add the load generated by se into cfs_rq's child load-average */
2327static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002328 struct sched_entity *se,
2329 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02002330{
Paul Turneraff3e492012-10-04 13:18:30 +02002331 /*
2332 * We track migrations using entity decay_count <= 0, on a wake-up
2333 * migration we use a negative decay count to track the remote decays
2334 * accumulated while sleeping.
Alex Shia75cdaa2013-06-20 10:18:47 +08002335 *
2336 * Newly forked tasks are enqueued with se->avg.decay_count == 0, they
2337 * are seen by enqueue_entity_load_avg() as a migration with an already
2338 * constructed load_avg_contrib.
Paul Turneraff3e492012-10-04 13:18:30 +02002339 */
2340 if (unlikely(se->avg.decay_count <= 0)) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002341 se->avg.last_runnable_update = rq_clock_task(rq_of(cfs_rq));
Paul Turneraff3e492012-10-04 13:18:30 +02002342 if (se->avg.decay_count) {
2343 /*
2344 * In a wake-up migration we have to approximate the
2345 * time sleeping. This is because we can't synchronize
2346 * clock_task between the two cpus, and it is not
2347 * guaranteed to be read-safe. Instead, we can
2348 * approximate this using our carried decays, which are
2349 * explicitly atomically readable.
2350 */
2351 se->avg.last_runnable_update -= (-se->avg.decay_count)
2352 << 20;
2353 update_entity_load_avg(se, 0);
2354 /* Indicate that we're now synchronized and on-rq */
2355 se->avg.decay_count = 0;
2356 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002357 wakeup = 0;
2358 } else {
Vincent Guittot93906752014-01-22 08:45:34 +01002359 __synchronize_entity_decay(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02002360 }
2361
Paul Turneraff3e492012-10-04 13:18:30 +02002362 /* migrated tasks did not contribute to our blocked load */
2363 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02002364 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02002365 update_entity_load_avg(se, 0);
2366 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002367
Paul Turner2dac7542012-10-04 13:18:30 +02002368 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02002369 /* we force update consideration on load-balancer moves */
2370 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02002371}
2372
Paul Turner9ee474f2012-10-04 13:18:30 +02002373/*
2374 * Remove se's load from this cfs_rq child load-average, if the entity is
2375 * transitioning to a blocked state we track its projected decay using
2376 * blocked_load_avg.
2377 */
Paul Turner2dac7542012-10-04 13:18:30 +02002378static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002379 struct sched_entity *se,
2380 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02002381{
Paul Turner9ee474f2012-10-04 13:18:30 +02002382 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02002383 /* we force update consideration on load-balancer moves */
2384 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02002385
Paul Turner2dac7542012-10-04 13:18:30 +02002386 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02002387 if (sleep) {
2388 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
2389 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
2390 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02002391}
Vincent Guittot642dbc32013-04-18 18:34:26 +02002392
2393/*
2394 * Update the rq's load with the elapsed running time before entering
2395 * idle. if the last scheduled task is not a CFS task, idle_enter will
2396 * be the only way to update the runnable statistic.
2397 */
2398void idle_enter_fair(struct rq *this_rq)
2399{
2400 update_rq_runnable_avg(this_rq, 1);
2401}
2402
2403/*
2404 * Update the rq's load with the elapsed idle time before a task is
2405 * scheduled. if the newly scheduled task is not a CFS task, idle_exit will
2406 * be the only way to update the runnable statistic.
2407 */
2408void idle_exit_fair(struct rq *this_rq)
2409{
2410 update_rq_runnable_avg(this_rq, 0);
2411}
2412
Paul Turner9d85f212012-10-04 13:18:29 +02002413#else
Paul Turner9ee474f2012-10-04 13:18:30 +02002414static inline void update_entity_load_avg(struct sched_entity *se,
2415 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02002416static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02002417static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002418 struct sched_entity *se,
2419 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02002420static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002421 struct sched_entity *se,
2422 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02002423static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
2424 int force_update) {}
Paul Turner9d85f212012-10-04 13:18:29 +02002425#endif
2426
Ingo Molnar2396af62007-08-09 11:16:48 +02002427static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002428{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002429#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02002430 struct task_struct *tsk = NULL;
2431
2432 if (entity_is_task(se))
2433 tsk = task_of(se);
2434
Lucas De Marchi41acab82010-03-10 23:37:45 -03002435 if (se->statistics.sleep_start) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002436 u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002437
2438 if ((s64)delta < 0)
2439 delta = 0;
2440
Lucas De Marchi41acab82010-03-10 23:37:45 -03002441 if (unlikely(delta > se->statistics.sleep_max))
2442 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002443
Peter Zijlstra8c79a042012-01-30 14:51:37 +01002444 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03002445 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01002446
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002447 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02002448 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002449 trace_sched_stat_sleep(tsk, delta);
2450 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002451 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03002452 if (se->statistics.block_start) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002453 u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002454
2455 if ((s64)delta < 0)
2456 delta = 0;
2457
Lucas De Marchi41acab82010-03-10 23:37:45 -03002458 if (unlikely(delta > se->statistics.block_max))
2459 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002460
Peter Zijlstra8c79a042012-01-30 14:51:37 +01002461 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03002462 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02002463
Peter Zijlstrae4143142009-07-23 20:13:26 +02002464 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07002465 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002466 se->statistics.iowait_sum += delta;
2467 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002468 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07002469 }
2470
Andrew Vaginb781a602011-11-28 12:03:35 +03002471 trace_sched_stat_blocked(tsk, delta);
2472
Peter Zijlstrae4143142009-07-23 20:13:26 +02002473 /*
2474 * Blocking time is in units of nanosecs, so shift by
2475 * 20 to get a milliseconds-range estimation of the
2476 * amount of time that the task spent sleeping:
2477 */
2478 if (unlikely(prof_on == SLEEP_PROFILING)) {
2479 profile_hits(SLEEP_PROFILING,
2480 (void *)get_wchan(tsk),
2481 delta >> 20);
2482 }
2483 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02002484 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002485 }
2486#endif
2487}
2488
Peter Zijlstraddc97292007-10-15 17:00:10 +02002489static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
2490{
2491#ifdef CONFIG_SCHED_DEBUG
2492 s64 d = se->vruntime - cfs_rq->min_vruntime;
2493
2494 if (d < 0)
2495 d = -d;
2496
2497 if (d > 3*sysctl_sched_latency)
2498 schedstat_inc(cfs_rq, nr_spread_over);
2499#endif
2500}
2501
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002502static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002503place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
2504{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02002505 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02002506
Peter Zijlstra2cb86002007-11-09 22:39:37 +01002507 /*
2508 * The 'current' period is already promised to the current tasks,
2509 * however the extra weight of the new task will slow them down a
2510 * little, place the new task so that it fits in the slot that
2511 * stays open at the end.
2512 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02002513 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02002514 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002515
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002516 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01002517 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002518 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02002519
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002520 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002521 * Halve their sleep time's effect, to allow
2522 * for a gentler effect of sleepers:
2523 */
2524 if (sched_feat(GENTLE_FAIR_SLEEPERS))
2525 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02002526
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002527 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002528 }
2529
Mike Galbraithb5d9d732009-09-08 11:12:28 +02002530 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05302531 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002532}
2533
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002534static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
2535
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002536static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002537enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002538{
2539 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002540 * Update the normalized vruntime before updating min_vruntime
Kamalesh Babulal0fc576d2013-06-27 11:24:18 +05302541 * through calling update_curr().
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002542 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002543 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002544 se->vruntime += cfs_rq->min_vruntime;
2545
2546 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002547 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002548 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02002549 update_curr(cfs_rq);
Paul Turnerf269ae02012-10-04 13:18:31 +02002550 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002551 account_entity_enqueue(cfs_rq, se);
2552 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002553
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002554 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002555 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02002556 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02002557 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002558
Ingo Molnard2417e52007-08-09 11:16:47 +02002559 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02002560 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002561 if (se != cfs_rq->curr)
2562 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002563 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08002564
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002565 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08002566 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002567 check_enqueue_throttle(cfs_rq);
2568 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002569}
2570
Rik van Riel2c13c9192011-02-01 09:48:37 -05002571static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01002572{
Rik van Riel2c13c9192011-02-01 09:48:37 -05002573 for_each_sched_entity(se) {
2574 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2575 if (cfs_rq->last == se)
2576 cfs_rq->last = NULL;
2577 else
2578 break;
2579 }
2580}
Peter Zijlstra2002c692008-11-11 11:52:33 +01002581
Rik van Riel2c13c9192011-02-01 09:48:37 -05002582static void __clear_buddies_next(struct sched_entity *se)
2583{
2584 for_each_sched_entity(se) {
2585 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2586 if (cfs_rq->next == se)
2587 cfs_rq->next = NULL;
2588 else
2589 break;
2590 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01002591}
2592
Rik van Rielac53db52011-02-01 09:51:03 -05002593static void __clear_buddies_skip(struct sched_entity *se)
2594{
2595 for_each_sched_entity(se) {
2596 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2597 if (cfs_rq->skip == se)
2598 cfs_rq->skip = NULL;
2599 else
2600 break;
2601 }
2602}
2603
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01002604static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
2605{
Rik van Riel2c13c9192011-02-01 09:48:37 -05002606 if (cfs_rq->last == se)
2607 __clear_buddies_last(se);
2608
2609 if (cfs_rq->next == se)
2610 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05002611
2612 if (cfs_rq->skip == se)
2613 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01002614}
2615
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002616static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07002617
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002618static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002619dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002620{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002621 /*
2622 * Update run-time statistics of the 'current'.
2623 */
2624 update_curr(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002625 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002626
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02002627 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002628 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02002629#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002630 if (entity_is_task(se)) {
2631 struct task_struct *tsk = task_of(se);
2632
2633 if (tsk->state & TASK_INTERRUPTIBLE)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002634 se->statistics.sleep_start = rq_clock(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002635 if (tsk->state & TASK_UNINTERRUPTIBLE)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002636 se->statistics.block_start = rq_clock(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002637 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02002638#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02002639 }
2640
Peter Zijlstra2002c692008-11-11 11:52:33 +01002641 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01002642
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002643 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002644 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002645 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002646 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002647
2648 /*
2649 * Normalize the entity after updating the min_vruntime because the
2650 * update can refer to the ->curr item and we need to reflect this
2651 * movement in our normalized position.
2652 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002653 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002654 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07002655
Paul Turnerd8b49862011-07-21 09:43:41 -07002656 /* return excess runtime on last dequeue */
2657 return_cfs_rq_runtime(cfs_rq);
2658
Peter Zijlstra1e876232011-05-17 16:21:10 -07002659 update_min_vruntime(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002660 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002661}
2662
2663/*
2664 * Preempt the current task with a newly woken task if needed:
2665 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02002666static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02002667check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002668{
Peter Zijlstra11697832007-09-05 14:32:49 +02002669 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002670 struct sched_entity *se;
2671 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02002672
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02002673 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02002674 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01002675 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002676 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01002677 /*
2678 * The current task ran long enough, ensure it doesn't get
2679 * re-elected due to buddy favours.
2680 */
2681 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002682 return;
2683 }
2684
2685 /*
2686 * Ensure that a task that missed wakeup preemption by a
2687 * narrow margin doesn't have to wait for a full slice.
2688 * This also mitigates buddy induced latencies under load.
2689 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002690 if (delta_exec < sysctl_sched_min_granularity)
2691 return;
2692
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002693 se = __pick_first_entity(cfs_rq);
2694 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02002695
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002696 if (delta < 0)
2697 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01002698
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002699 if (delta > ideal_runtime)
2700 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002701}
2702
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002703static void
Ingo Molnar8494f412007-08-09 11:16:48 +02002704set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002705{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002706 /* 'current' is not kept within the tree. */
2707 if (se->on_rq) {
2708 /*
2709 * Any task has to be enqueued before it get to execute on
2710 * a CPU. So account for the time it spent waiting on the
2711 * runqueue.
2712 */
2713 update_stats_wait_end(cfs_rq, se);
2714 __dequeue_entity(cfs_rq, se);
2715 }
2716
Ingo Molnar79303e92007-08-09 11:16:47 +02002717 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02002718 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002719#ifdef CONFIG_SCHEDSTATS
2720 /*
2721 * Track our maximum slice length, if the CPU's load is at
2722 * least twice that of our own weight (i.e. dont track it
2723 * when there are only lesser-weight tasks around):
2724 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002725 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002726 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02002727 se->sum_exec_runtime - se->prev_sum_exec_runtime);
2728 }
2729#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02002730 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002731}
2732
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02002733static int
2734wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
2735
Rik van Rielac53db52011-02-01 09:51:03 -05002736/*
2737 * Pick the next process, keeping these things in mind, in this order:
2738 * 1) keep things fair between processes/task groups
2739 * 2) pick the "next" process, since someone really wants that to run
2740 * 3) pick the "last" process, for cache locality
2741 * 4) do not run the "skip" process, if something else is available
2742 */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01002743static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01002744{
Rik van Rielac53db52011-02-01 09:51:03 -05002745 struct sched_entity *se = __pick_first_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002746 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +01002747
Rik van Rielac53db52011-02-01 09:51:03 -05002748 /*
2749 * Avoid running the skip buddy, if running something else can
2750 * be done without getting too unfair.
2751 */
2752 if (cfs_rq->skip == se) {
2753 struct sched_entity *second = __pick_next_entity(se);
2754 if (second && wakeup_preempt_entity(second, left) < 1)
2755 se = second;
2756 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01002757
Mike Galbraithf685cea2009-10-23 23:09:22 +02002758 /*
2759 * Prefer last buddy, try to return the CPU to a preempted task.
2760 */
2761 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
2762 se = cfs_rq->last;
2763
Rik van Rielac53db52011-02-01 09:51:03 -05002764 /*
2765 * Someone really wants this to run. If it's not unfair, run it.
2766 */
2767 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
2768 se = cfs_rq->next;
2769
Mike Galbraithf685cea2009-10-23 23:09:22 +02002770 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01002771
2772 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01002773}
2774
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002775static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
2776
Ingo Molnarab6cde22007-08-09 11:16:48 +02002777static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002778{
2779 /*
2780 * If still on the runqueue then deactivate_task()
2781 * was not called and update_curr() has to be done:
2782 */
2783 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02002784 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002785
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002786 /* throttle cfs_rqs exceeding runtime */
2787 check_cfs_rq_runtime(cfs_rq);
2788
Peter Zijlstraddc97292007-10-15 17:00:10 +02002789 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002790 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02002791 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002792 /* Put 'current' back into the tree. */
2793 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02002794 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02002795 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002796 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02002797 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002798}
2799
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002800static void
2801entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002802{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002803 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002804 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002805 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002806 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002807
Paul Turner43365bd2010-12-15 19:10:17 -08002808 /*
Paul Turner9d85f212012-10-04 13:18:29 +02002809 * Ensure that runnable average is periodically updated.
2810 */
Paul Turner9ee474f2012-10-04 13:18:30 +02002811 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02002812 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstrabf0bd942013-07-26 23:48:42 +02002813 update_cfs_shares(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02002814
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002815#ifdef CONFIG_SCHED_HRTICK
2816 /*
2817 * queued ticks are scheduled to match the slice, so don't bother
2818 * validating it and just reschedule.
2819 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07002820 if (queued) {
2821 resched_task(rq_of(cfs_rq)->curr);
2822 return;
2823 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002824 /*
2825 * don't let the period tick interfere with the hrtick preemption
2826 */
2827 if (!sched_feat(DOUBLE_TICK) &&
2828 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
2829 return;
2830#endif
2831
Yong Zhang2c2efae2011-07-29 16:20:33 +08002832 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02002833 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002834}
2835
Paul Turnerab84d312011-07-21 09:43:28 -07002836
2837/**************************************************
2838 * CFS bandwidth control machinery
2839 */
2840
2841#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02002842
2843#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01002844static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02002845
2846static inline bool cfs_bandwidth_used(void)
2847{
Ingo Molnarc5905af2012-02-24 08:31:31 +01002848 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002849}
2850
Ben Segall1ee14e62013-10-16 11:16:12 -07002851void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02002852{
Ben Segall1ee14e62013-10-16 11:16:12 -07002853 static_key_slow_inc(&__cfs_bandwidth_used);
2854}
2855
2856void cfs_bandwidth_usage_dec(void)
2857{
2858 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002859}
2860#else /* HAVE_JUMP_LABEL */
2861static bool cfs_bandwidth_used(void)
2862{
2863 return true;
2864}
2865
Ben Segall1ee14e62013-10-16 11:16:12 -07002866void cfs_bandwidth_usage_inc(void) {}
2867void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02002868#endif /* HAVE_JUMP_LABEL */
2869
Paul Turnerab84d312011-07-21 09:43:28 -07002870/*
2871 * default period for cfs group bandwidth.
2872 * default: 0.1s, units: nanoseconds
2873 */
2874static inline u64 default_cfs_period(void)
2875{
2876 return 100000000ULL;
2877}
Paul Turnerec12cb72011-07-21 09:43:30 -07002878
2879static inline u64 sched_cfs_bandwidth_slice(void)
2880{
2881 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
2882}
2883
Paul Turnera9cf55b2011-07-21 09:43:32 -07002884/*
2885 * Replenish runtime according to assigned quota and update expiration time.
2886 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
2887 * additional synchronization around rq->lock.
2888 *
2889 * requires cfs_b->lock
2890 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02002891void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07002892{
2893 u64 now;
2894
2895 if (cfs_b->quota == RUNTIME_INF)
2896 return;
2897
2898 now = sched_clock_cpu(smp_processor_id());
2899 cfs_b->runtime = cfs_b->quota;
2900 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
2901}
2902
Peter Zijlstra029632f2011-10-25 10:00:11 +02002903static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
2904{
2905 return &tg->cfs_bandwidth;
2906}
2907
Paul Turnerf1b17282012-10-04 13:18:31 +02002908/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
2909static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
2910{
2911 if (unlikely(cfs_rq->throttle_count))
2912 return cfs_rq->throttled_clock_task;
2913
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002914 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02002915}
2916
Paul Turner85dac902011-07-21 09:43:33 -07002917/* returns 0 on failure to allocate runtime */
2918static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07002919{
2920 struct task_group *tg = cfs_rq->tg;
2921 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07002922 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07002923
2924 /* note: this is a positive sum as runtime_remaining <= 0 */
2925 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
2926
2927 raw_spin_lock(&cfs_b->lock);
2928 if (cfs_b->quota == RUNTIME_INF)
2929 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07002930 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07002931 /*
2932 * If the bandwidth pool has become inactive, then at least one
2933 * period must have elapsed since the last consumption.
2934 * Refresh the global state and ensure bandwidth timer becomes
2935 * active.
2936 */
2937 if (!cfs_b->timer_active) {
2938 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07002939 __start_cfs_bandwidth(cfs_b);
Paul Turnera9cf55b2011-07-21 09:43:32 -07002940 }
Paul Turner58088ad2011-07-21 09:43:31 -07002941
2942 if (cfs_b->runtime > 0) {
2943 amount = min(cfs_b->runtime, min_amount);
2944 cfs_b->runtime -= amount;
2945 cfs_b->idle = 0;
2946 }
Paul Turnerec12cb72011-07-21 09:43:30 -07002947 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07002948 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07002949 raw_spin_unlock(&cfs_b->lock);
2950
2951 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07002952 /*
2953 * we may have advanced our local expiration to account for allowed
2954 * spread between our sched_clock and the one on which runtime was
2955 * issued.
2956 */
2957 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
2958 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07002959
2960 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07002961}
2962
2963/*
2964 * Note: This depends on the synchronization provided by sched_clock and the
2965 * fact that rq->clock snapshots this value.
2966 */
2967static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
2968{
2969 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07002970
2971 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002972 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07002973 return;
2974
2975 if (cfs_rq->runtime_remaining < 0)
2976 return;
2977
2978 /*
2979 * If the local deadline has passed we have to consider the
2980 * possibility that our sched_clock is 'fast' and the global deadline
2981 * has not truly expired.
2982 *
2983 * Fortunately we can check determine whether this the case by checking
2984 * whether the global deadline has advanced.
2985 */
2986
2987 if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
2988 /* extend local deadline, drift is bounded above by 2 ticks */
2989 cfs_rq->runtime_expires += TICK_NSEC;
2990 } else {
2991 /* global deadline is ahead, expiration has passed */
2992 cfs_rq->runtime_remaining = 0;
2993 }
Paul Turnerec12cb72011-07-21 09:43:30 -07002994}
2995
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01002996static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07002997{
Paul Turnera9cf55b2011-07-21 09:43:32 -07002998 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07002999 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003000 expire_cfs_rq_runtime(cfs_rq);
3001
3002 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07003003 return;
3004
Paul Turner85dac902011-07-21 09:43:33 -07003005 /*
3006 * if we're unable to extend our runtime we resched so that the active
3007 * hierarchy can be throttled
3008 */
3009 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
3010 resched_task(rq_of(cfs_rq)->curr);
Paul Turnerec12cb72011-07-21 09:43:30 -07003011}
3012
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003013static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003014void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07003015{
Paul Turner56f570e2011-11-07 20:26:33 -08003016 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07003017 return;
3018
3019 __account_cfs_rq_runtime(cfs_rq, delta_exec);
3020}
3021
Paul Turner85dac902011-07-21 09:43:33 -07003022static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
3023{
Paul Turner56f570e2011-11-07 20:26:33 -08003024 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07003025}
3026
Paul Turner64660c82011-07-21 09:43:36 -07003027/* check whether cfs_rq, or any parent, is throttled */
3028static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
3029{
Paul Turner56f570e2011-11-07 20:26:33 -08003030 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07003031}
3032
3033/*
3034 * Ensure that neither of the group entities corresponding to src_cpu or
3035 * dest_cpu are members of a throttled hierarchy when performing group
3036 * load-balance operations.
3037 */
3038static inline int throttled_lb_pair(struct task_group *tg,
3039 int src_cpu, int dest_cpu)
3040{
3041 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
3042
3043 src_cfs_rq = tg->cfs_rq[src_cpu];
3044 dest_cfs_rq = tg->cfs_rq[dest_cpu];
3045
3046 return throttled_hierarchy(src_cfs_rq) ||
3047 throttled_hierarchy(dest_cfs_rq);
3048}
3049
3050/* updated child weight may affect parent so we have to do this bottom up */
3051static int tg_unthrottle_up(struct task_group *tg, void *data)
3052{
3053 struct rq *rq = data;
3054 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3055
3056 cfs_rq->throttle_count--;
3057#ifdef CONFIG_SMP
3058 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02003059 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003060 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02003061 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07003062 }
3063#endif
3064
3065 return 0;
3066}
3067
3068static int tg_throttle_down(struct task_group *tg, void *data)
3069{
3070 struct rq *rq = data;
3071 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3072
Paul Turner82958362012-10-04 13:18:31 +02003073 /* group is entering throttled state, stop time */
3074 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003075 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07003076 cfs_rq->throttle_count++;
3077
3078 return 0;
3079}
3080
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003081static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07003082{
3083 struct rq *rq = rq_of(cfs_rq);
3084 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3085 struct sched_entity *se;
3086 long task_delta, dequeue = 1;
3087
3088 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
3089
Paul Turnerf1b17282012-10-04 13:18:31 +02003090 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07003091 rcu_read_lock();
3092 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
3093 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07003094
3095 task_delta = cfs_rq->h_nr_running;
3096 for_each_sched_entity(se) {
3097 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
3098 /* throttled entity or throttle-on-deactivate */
3099 if (!se->on_rq)
3100 break;
3101
3102 if (dequeue)
3103 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
3104 qcfs_rq->h_nr_running -= task_delta;
3105
3106 if (qcfs_rq->load.weight)
3107 dequeue = 0;
3108 }
3109
3110 if (!se)
3111 rq->nr_running -= task_delta;
3112
3113 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003114 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07003115 raw_spin_lock(&cfs_b->lock);
3116 list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Ben Segallf9f9ffc2013-10-16 11:16:32 -07003117 if (!cfs_b->timer_active)
3118 __start_cfs_bandwidth(cfs_b);
Paul Turner85dac902011-07-21 09:43:33 -07003119 raw_spin_unlock(&cfs_b->lock);
3120}
3121
Peter Zijlstra029632f2011-10-25 10:00:11 +02003122void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07003123{
3124 struct rq *rq = rq_of(cfs_rq);
3125 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3126 struct sched_entity *se;
3127 int enqueue = 1;
3128 long task_delta;
3129
Michael Wang22b958d2013-06-04 14:23:39 +08003130 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07003131
3132 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02003133
3134 update_rq_clock(rq);
3135
Paul Turner671fd9d2011-07-21 09:43:34 -07003136 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003137 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07003138 list_del_rcu(&cfs_rq->throttled_list);
3139 raw_spin_unlock(&cfs_b->lock);
3140
Paul Turner64660c82011-07-21 09:43:36 -07003141 /* update hierarchical throttle state */
3142 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
3143
Paul Turner671fd9d2011-07-21 09:43:34 -07003144 if (!cfs_rq->load.weight)
3145 return;
3146
3147 task_delta = cfs_rq->h_nr_running;
3148 for_each_sched_entity(se) {
3149 if (se->on_rq)
3150 enqueue = 0;
3151
3152 cfs_rq = cfs_rq_of(se);
3153 if (enqueue)
3154 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
3155 cfs_rq->h_nr_running += task_delta;
3156
3157 if (cfs_rq_throttled(cfs_rq))
3158 break;
3159 }
3160
3161 if (!se)
3162 rq->nr_running += task_delta;
3163
3164 /* determine whether we need to wake up potentially idle cpu */
3165 if (rq->curr == rq->idle && rq->cfs.nr_running)
3166 resched_task(rq->curr);
3167}
3168
3169static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
3170 u64 remaining, u64 expires)
3171{
3172 struct cfs_rq *cfs_rq;
3173 u64 runtime = remaining;
3174
3175 rcu_read_lock();
3176 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
3177 throttled_list) {
3178 struct rq *rq = rq_of(cfs_rq);
3179
3180 raw_spin_lock(&rq->lock);
3181 if (!cfs_rq_throttled(cfs_rq))
3182 goto next;
3183
3184 runtime = -cfs_rq->runtime_remaining + 1;
3185 if (runtime > remaining)
3186 runtime = remaining;
3187 remaining -= runtime;
3188
3189 cfs_rq->runtime_remaining += runtime;
3190 cfs_rq->runtime_expires = expires;
3191
3192 /* we check whether we're throttled above */
3193 if (cfs_rq->runtime_remaining > 0)
3194 unthrottle_cfs_rq(cfs_rq);
3195
3196next:
3197 raw_spin_unlock(&rq->lock);
3198
3199 if (!remaining)
3200 break;
3201 }
3202 rcu_read_unlock();
3203
3204 return remaining;
3205}
3206
Paul Turner58088ad2011-07-21 09:43:31 -07003207/*
3208 * Responsible for refilling a task_group's bandwidth and unthrottling its
3209 * cfs_rqs as appropriate. If there has been no activity within the last
3210 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
3211 * used to track this state.
3212 */
3213static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
3214{
Paul Turner671fd9d2011-07-21 09:43:34 -07003215 u64 runtime, runtime_expires;
3216 int idle = 1, throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07003217
3218 raw_spin_lock(&cfs_b->lock);
3219 /* no need to continue the timer with no bandwidth constraint */
3220 if (cfs_b->quota == RUNTIME_INF)
3221 goto out_unlock;
3222
Paul Turner671fd9d2011-07-21 09:43:34 -07003223 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
3224 /* idle depends on !throttled (for the case of a large deficit) */
3225 idle = cfs_b->idle && !throttled;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07003226 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07003227
Paul Turnera9cf55b2011-07-21 09:43:32 -07003228 /* if we're going inactive then everything else can be deferred */
3229 if (idle)
3230 goto out_unlock;
3231
Ben Segall927b54f2013-10-16 11:16:22 -07003232 /*
3233 * if we have relooped after returning idle once, we need to update our
3234 * status as actually running, so that other cpus doing
3235 * __start_cfs_bandwidth will stop trying to cancel us.
3236 */
3237 cfs_b->timer_active = 1;
3238
Paul Turnera9cf55b2011-07-21 09:43:32 -07003239 __refill_cfs_bandwidth_runtime(cfs_b);
3240
Paul Turner671fd9d2011-07-21 09:43:34 -07003241 if (!throttled) {
3242 /* mark as potentially idle for the upcoming period */
3243 cfs_b->idle = 1;
3244 goto out_unlock;
3245 }
Paul Turner58088ad2011-07-21 09:43:31 -07003246
Nikhil Raoe8da1b12011-07-21 09:43:40 -07003247 /* account preceding periods in which throttling occurred */
3248 cfs_b->nr_throttled += overrun;
3249
Paul Turner671fd9d2011-07-21 09:43:34 -07003250 /*
3251 * There are throttled entities so we must first use the new bandwidth
3252 * to unthrottle them before making it generally available. This
3253 * ensures that all existing debts will be paid before a new cfs_rq is
3254 * allowed to run.
3255 */
3256 runtime = cfs_b->runtime;
3257 runtime_expires = cfs_b->runtime_expires;
3258 cfs_b->runtime = 0;
3259
3260 /*
3261 * This check is repeated as we are holding onto the new bandwidth
3262 * while we unthrottle. This can potentially race with an unthrottled
3263 * group trying to acquire new bandwidth from the global pool.
3264 */
3265 while (throttled && runtime > 0) {
3266 raw_spin_unlock(&cfs_b->lock);
3267 /* we can't nest cfs_b->lock while distributing bandwidth */
3268 runtime = distribute_cfs_runtime(cfs_b, runtime,
3269 runtime_expires);
3270 raw_spin_lock(&cfs_b->lock);
3271
3272 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
3273 }
3274
3275 /* return (any) remaining runtime */
3276 cfs_b->runtime = runtime;
3277 /*
3278 * While we are ensured activity in the period following an
3279 * unthrottle, this also covers the case in which the new bandwidth is
3280 * insufficient to cover the existing bandwidth deficit. (Forcing the
3281 * timer to remain active while there are any throttled entities.)
3282 */
3283 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07003284out_unlock:
3285 if (idle)
3286 cfs_b->timer_active = 0;
3287 raw_spin_unlock(&cfs_b->lock);
3288
3289 return idle;
3290}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003291
Paul Turnerd8b49862011-07-21 09:43:41 -07003292/* a cfs_rq won't donate quota below this amount */
3293static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
3294/* minimum remaining period time to redistribute slack quota */
3295static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
3296/* how long we wait to gather additional slack before distributing */
3297static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
3298
Ben Segalldb06e782013-10-16 11:16:17 -07003299/*
3300 * Are we near the end of the current quota period?
3301 *
3302 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
3303 * hrtimer base being cleared by __hrtimer_start_range_ns. In the case of
3304 * migrate_hrtimers, base is never cleared, so we are fine.
3305 */
Paul Turnerd8b49862011-07-21 09:43:41 -07003306static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
3307{
3308 struct hrtimer *refresh_timer = &cfs_b->period_timer;
3309 u64 remaining;
3310
3311 /* if the call-back is running a quota refresh is already occurring */
3312 if (hrtimer_callback_running(refresh_timer))
3313 return 1;
3314
3315 /* is a quota refresh about to occur? */
3316 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
3317 if (remaining < min_expire)
3318 return 1;
3319
3320 return 0;
3321}
3322
3323static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
3324{
3325 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
3326
3327 /* if there's a quota refresh soon don't bother with slack */
3328 if (runtime_refresh_within(cfs_b, min_left))
3329 return;
3330
3331 start_bandwidth_timer(&cfs_b->slack_timer,
3332 ns_to_ktime(cfs_bandwidth_slack_period));
3333}
3334
3335/* we know any runtime found here is valid as update_curr() precedes return */
3336static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3337{
3338 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3339 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
3340
3341 if (slack_runtime <= 0)
3342 return;
3343
3344 raw_spin_lock(&cfs_b->lock);
3345 if (cfs_b->quota != RUNTIME_INF &&
3346 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
3347 cfs_b->runtime += slack_runtime;
3348
3349 /* we are under rq->lock, defer unthrottling using a timer */
3350 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
3351 !list_empty(&cfs_b->throttled_cfs_rq))
3352 start_cfs_slack_bandwidth(cfs_b);
3353 }
3354 raw_spin_unlock(&cfs_b->lock);
3355
3356 /* even if it's not valid for return we don't want to try again */
3357 cfs_rq->runtime_remaining -= slack_runtime;
3358}
3359
3360static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3361{
Paul Turner56f570e2011-11-07 20:26:33 -08003362 if (!cfs_bandwidth_used())
3363 return;
3364
Paul Turnerfccfdc62011-11-07 20:26:34 -08003365 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07003366 return;
3367
3368 __return_cfs_rq_runtime(cfs_rq);
3369}
3370
3371/*
3372 * This is done with a timer (instead of inline with bandwidth return) since
3373 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
3374 */
3375static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
3376{
3377 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
3378 u64 expires;
3379
3380 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07003381 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07003382 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
3383 raw_spin_unlock(&cfs_b->lock);
3384 return;
3385 }
3386
Paul Turnerd8b49862011-07-21 09:43:41 -07003387 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
3388 runtime = cfs_b->runtime;
3389 cfs_b->runtime = 0;
3390 }
3391 expires = cfs_b->runtime_expires;
3392 raw_spin_unlock(&cfs_b->lock);
3393
3394 if (!runtime)
3395 return;
3396
3397 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
3398
3399 raw_spin_lock(&cfs_b->lock);
3400 if (expires == cfs_b->runtime_expires)
3401 cfs_b->runtime = runtime;
3402 raw_spin_unlock(&cfs_b->lock);
3403}
3404
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003405/*
3406 * When a group wakes up we want to make sure that its quota is not already
3407 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
3408 * runtime as update_curr() throttling can not not trigger until it's on-rq.
3409 */
3410static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
3411{
Paul Turner56f570e2011-11-07 20:26:33 -08003412 if (!cfs_bandwidth_used())
3413 return;
3414
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003415 /* an active group must be handled by the update_curr()->put() path */
3416 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
3417 return;
3418
3419 /* ensure the group is not already throttled */
3420 if (cfs_rq_throttled(cfs_rq))
3421 return;
3422
3423 /* update runtime allocation */
3424 account_cfs_rq_runtime(cfs_rq, 0);
3425 if (cfs_rq->runtime_remaining <= 0)
3426 throttle_cfs_rq(cfs_rq);
3427}
3428
3429/* conditionally throttle active cfs_rq's from put_prev_entity() */
3430static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3431{
Paul Turner56f570e2011-11-07 20:26:33 -08003432 if (!cfs_bandwidth_used())
3433 return;
3434
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003435 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
3436 return;
3437
3438 /*
3439 * it's possible for a throttled entity to be forced into a running
3440 * state (e.g. set_curr_task), in this case we're finished.
3441 */
3442 if (cfs_rq_throttled(cfs_rq))
3443 return;
3444
3445 throttle_cfs_rq(cfs_rq);
3446}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003447
Peter Zijlstra029632f2011-10-25 10:00:11 +02003448static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
3449{
3450 struct cfs_bandwidth *cfs_b =
3451 container_of(timer, struct cfs_bandwidth, slack_timer);
3452 do_sched_cfs_slack_timer(cfs_b);
3453
3454 return HRTIMER_NORESTART;
3455}
3456
3457static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
3458{
3459 struct cfs_bandwidth *cfs_b =
3460 container_of(timer, struct cfs_bandwidth, period_timer);
3461 ktime_t now;
3462 int overrun;
3463 int idle = 0;
3464
3465 for (;;) {
3466 now = hrtimer_cb_get_time(timer);
3467 overrun = hrtimer_forward(timer, now, cfs_b->period);
3468
3469 if (!overrun)
3470 break;
3471
3472 idle = do_sched_cfs_period_timer(cfs_b, overrun);
3473 }
3474
3475 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
3476}
3477
3478void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3479{
3480 raw_spin_lock_init(&cfs_b->lock);
3481 cfs_b->runtime = 0;
3482 cfs_b->quota = RUNTIME_INF;
3483 cfs_b->period = ns_to_ktime(default_cfs_period());
3484
3485 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
3486 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3487 cfs_b->period_timer.function = sched_cfs_period_timer;
3488 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3489 cfs_b->slack_timer.function = sched_cfs_slack_timer;
3490}
3491
3492static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3493{
3494 cfs_rq->runtime_enabled = 0;
3495 INIT_LIST_HEAD(&cfs_rq->throttled_list);
3496}
3497
3498/* requires cfs_b->lock, may release to reprogram timer */
3499void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3500{
3501 /*
3502 * The timer may be active because we're trying to set a new bandwidth
3503 * period or because we're racing with the tear-down path
3504 * (timer_active==0 becomes visible before the hrtimer call-back
3505 * terminates). In either case we ensure that it's re-programmed
3506 */
Ben Segall927b54f2013-10-16 11:16:22 -07003507 while (unlikely(hrtimer_active(&cfs_b->period_timer)) &&
3508 hrtimer_try_to_cancel(&cfs_b->period_timer) < 0) {
3509 /* bounce the lock to allow do_sched_cfs_period_timer to run */
Peter Zijlstra029632f2011-10-25 10:00:11 +02003510 raw_spin_unlock(&cfs_b->lock);
Ben Segall927b54f2013-10-16 11:16:22 -07003511 cpu_relax();
Peter Zijlstra029632f2011-10-25 10:00:11 +02003512 raw_spin_lock(&cfs_b->lock);
3513 /* if someone else restarted the timer then we're done */
3514 if (cfs_b->timer_active)
3515 return;
3516 }
3517
3518 cfs_b->timer_active = 1;
3519 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
3520}
3521
3522static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3523{
3524 hrtimer_cancel(&cfs_b->period_timer);
3525 hrtimer_cancel(&cfs_b->slack_timer);
3526}
3527
Arnd Bergmann38dc3342013-01-25 14:14:22 +00003528static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003529{
3530 struct cfs_rq *cfs_rq;
3531
3532 for_each_leaf_cfs_rq(rq, cfs_rq) {
3533 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3534
3535 if (!cfs_rq->runtime_enabled)
3536 continue;
3537
3538 /*
3539 * clock_task is not advancing so we just need to make sure
3540 * there's some valid quota amount
3541 */
3542 cfs_rq->runtime_remaining = cfs_b->quota;
3543 if (cfs_rq_throttled(cfs_rq))
3544 unthrottle_cfs_rq(cfs_rq);
3545 }
3546}
3547
3548#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02003549static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
3550{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003551 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02003552}
3553
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003554static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003555static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
3556static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003557static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07003558
3559static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
3560{
3561 return 0;
3562}
Paul Turner64660c82011-07-21 09:43:36 -07003563
3564static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
3565{
3566 return 0;
3567}
3568
3569static inline int throttled_lb_pair(struct task_group *tg,
3570 int src_cpu, int dest_cpu)
3571{
3572 return 0;
3573}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003574
3575void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
3576
3577#ifdef CONFIG_FAIR_GROUP_SCHED
3578static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07003579#endif
3580
Peter Zijlstra029632f2011-10-25 10:00:11 +02003581static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
3582{
3583 return NULL;
3584}
3585static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07003586static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003587
3588#endif /* CONFIG_CFS_BANDWIDTH */
3589
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003590/**************************************************
3591 * CFS operations on tasks:
3592 */
3593
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003594#ifdef CONFIG_SCHED_HRTICK
3595static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
3596{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003597 struct sched_entity *se = &p->se;
3598 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3599
3600 WARN_ON(task_rq(p) != rq);
3601
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003602 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003603 u64 slice = sched_slice(cfs_rq, se);
3604 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
3605 s64 delta = slice - ran;
3606
3607 if (delta < 0) {
3608 if (rq->curr == p)
3609 resched_task(p);
3610 return;
3611 }
3612
3613 /*
3614 * Don't schedule slices shorter than 10000ns, that just
3615 * doesn't make sense. Rely on vruntime for fairness.
3616 */
Peter Zijlstra31656512008-07-18 18:01:23 +02003617 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +02003618 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003619
Peter Zijlstra31656512008-07-18 18:01:23 +02003620 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003621 }
3622}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003623
3624/*
3625 * called from enqueue/dequeue and updates the hrtick when the
3626 * current task is from our class and nr_running is low enough
3627 * to matter.
3628 */
3629static void hrtick_update(struct rq *rq)
3630{
3631 struct task_struct *curr = rq->curr;
3632
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003633 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003634 return;
3635
3636 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
3637 hrtick_start_fair(rq, curr);
3638}
Dhaval Giani55e12e52008-06-24 23:39:43 +05303639#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003640static inline void
3641hrtick_start_fair(struct rq *rq, struct task_struct *p)
3642{
3643}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003644
3645static inline void hrtick_update(struct rq *rq)
3646{
3647}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003648#endif
3649
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003650/*
3651 * The enqueue_task method is called before nr_running is
3652 * increased. Here we update the fair scheduling stats and
3653 * then put the task into the rbtree:
3654 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00003655static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003656enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003657{
3658 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003659 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003660
3661 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003662 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003663 break;
3664 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003665 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07003666
3667 /*
3668 * end evaluation on encountering a throttled cfs_rq
3669 *
3670 * note: in the case of encountering a throttled cfs_rq we will
3671 * post the final h_nr_running increment below.
3672 */
3673 if (cfs_rq_throttled(cfs_rq))
3674 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07003675 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07003676
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003677 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003678 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003679
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003680 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08003681 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07003682 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003683
Paul Turner85dac902011-07-21 09:43:33 -07003684 if (cfs_rq_throttled(cfs_rq))
3685 break;
3686
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003687 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02003688 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003689 }
3690
Ben Segall18bf2802012-10-04 12:51:20 +02003691 if (!se) {
3692 update_rq_runnable_avg(rq, rq->nr_running);
Paul Turner85dac902011-07-21 09:43:33 -07003693 inc_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02003694 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003695 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003696}
3697
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003698static void set_next_buddy(struct sched_entity *se);
3699
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003700/*
3701 * The dequeue_task method is called before nr_running is
3702 * decreased. We remove the task from the rbtree and
3703 * update the fair scheduling stats:
3704 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003705static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003706{
3707 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003708 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003709 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003710
3711 for_each_sched_entity(se) {
3712 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003713 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07003714
3715 /*
3716 * end evaluation on encountering a throttled cfs_rq
3717 *
3718 * note: in the case of encountering a throttled cfs_rq we will
3719 * post the final h_nr_running decrement below.
3720 */
3721 if (cfs_rq_throttled(cfs_rq))
3722 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07003723 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003724
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003725 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003726 if (cfs_rq->load.weight) {
3727 /*
3728 * Bias pick_next to pick a task from this cfs_rq, as
3729 * p is sleeping when it is within its sched_slice.
3730 */
3731 if (task_sleep && parent_entity(se))
3732 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07003733
3734 /* avoid re-evaluating load for this entity */
3735 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003736 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003737 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003738 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003739 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003740
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003741 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08003742 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07003743 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003744
Paul Turner85dac902011-07-21 09:43:33 -07003745 if (cfs_rq_throttled(cfs_rq))
3746 break;
3747
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003748 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02003749 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003750 }
3751
Ben Segall18bf2802012-10-04 12:51:20 +02003752 if (!se) {
Paul Turner85dac902011-07-21 09:43:33 -07003753 dec_nr_running(rq);
Ben Segall18bf2802012-10-04 12:51:20 +02003754 update_rq_runnable_avg(rq, 1);
3755 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003756 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003757}
3758
Gregory Haskinse7693a32008-01-25 21:08:09 +01003759#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02003760/* Used instead of source_load when we know the type == 0 */
3761static unsigned long weighted_cpuload(const int cpu)
3762{
Alex Shib92486c2013-06-20 10:18:50 +08003763 return cpu_rq(cpu)->cfs.runnable_load_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003764}
3765
3766/*
3767 * Return a low guess at the load of a migration-source cpu weighted
3768 * according to the scheduling class and "nice" value.
3769 *
3770 * We want to under-estimate the load of migration sources, to
3771 * balance conservatively.
3772 */
3773static unsigned long source_load(int cpu, int type)
3774{
3775 struct rq *rq = cpu_rq(cpu);
3776 unsigned long total = weighted_cpuload(cpu);
3777
3778 if (type == 0 || !sched_feat(LB_BIAS))
3779 return total;
3780
3781 return min(rq->cpu_load[type-1], total);
3782}
3783
3784/*
3785 * Return a high guess at the load of a migration-target cpu weighted
3786 * according to the scheduling class and "nice" value.
3787 */
3788static unsigned long target_load(int cpu, int type)
3789{
3790 struct rq *rq = cpu_rq(cpu);
3791 unsigned long total = weighted_cpuload(cpu);
3792
3793 if (type == 0 || !sched_feat(LB_BIAS))
3794 return total;
3795
3796 return max(rq->cpu_load[type-1], total);
3797}
3798
3799static unsigned long power_of(int cpu)
3800{
3801 return cpu_rq(cpu)->cpu_power;
3802}
3803
3804static unsigned long cpu_avg_load_per_task(int cpu)
3805{
3806 struct rq *rq = cpu_rq(cpu);
3807 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Alex Shib92486c2013-06-20 10:18:50 +08003808 unsigned long load_avg = rq->cfs.runnable_load_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003809
3810 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08003811 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003812
3813 return 0;
3814}
3815
Michael Wang62470412013-07-04 12:55:51 +08003816static void record_wakee(struct task_struct *p)
3817{
3818 /*
3819 * Rough decay (wiping) for cost saving, don't worry
3820 * about the boundary, really active task won't care
3821 * about the loss.
3822 */
3823 if (jiffies > current->wakee_flip_decay_ts + HZ) {
3824 current->wakee_flips = 0;
3825 current->wakee_flip_decay_ts = jiffies;
3826 }
3827
3828 if (current->last_wakee != p) {
3829 current->last_wakee = p;
3830 current->wakee_flips++;
3831 }
3832}
Ingo Molnar098fb9d2008-03-16 20:36:10 +01003833
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02003834static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003835{
3836 struct sched_entity *se = &p->se;
3837 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02003838 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003839
Peter Zijlstra3fe16982011-04-05 17:23:48 +02003840#ifndef CONFIG_64BIT
3841 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02003842
Peter Zijlstra3fe16982011-04-05 17:23:48 +02003843 do {
3844 min_vruntime_copy = cfs_rq->min_vruntime_copy;
3845 smp_rmb();
3846 min_vruntime = cfs_rq->min_vruntime;
3847 } while (min_vruntime != min_vruntime_copy);
3848#else
3849 min_vruntime = cfs_rq->min_vruntime;
3850#endif
3851
3852 se->vruntime -= min_vruntime;
Michael Wang62470412013-07-04 12:55:51 +08003853 record_wakee(p);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003854}
3855
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003856#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02003857/*
3858 * effective_load() calculates the load change as seen from the root_task_group
3859 *
3860 * Adding load to a group doesn't make a group heavier, but can cause movement
3861 * of group shares between cpus. Assuming the shares were perfectly aligned one
3862 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003863 *
3864 * Calculate the effective load difference if @wl is added (subtracted) to @tg
3865 * on this @cpu and results in a total addition (subtraction) of @wg to the
3866 * total group weight.
3867 *
3868 * Given a runqueue weight distribution (rw_i) we can compute a shares
3869 * distribution (s_i) using:
3870 *
3871 * s_i = rw_i / \Sum rw_j (1)
3872 *
3873 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
3874 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
3875 * shares distribution (s_i):
3876 *
3877 * rw_i = { 2, 4, 1, 0 }
3878 * s_i = { 2/7, 4/7, 1/7, 0 }
3879 *
3880 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
3881 * task used to run on and the CPU the waker is running on), we need to
3882 * compute the effect of waking a task on either CPU and, in case of a sync
3883 * wakeup, compute the effect of the current task going to sleep.
3884 *
3885 * So for a change of @wl to the local @cpu with an overall group weight change
3886 * of @wl we can compute the new shares distribution (s'_i) using:
3887 *
3888 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
3889 *
3890 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
3891 * differences in waking a task to CPU 0. The additional task changes the
3892 * weight and shares distributions like:
3893 *
3894 * rw'_i = { 3, 4, 1, 0 }
3895 * s'_i = { 3/8, 4/8, 1/8, 0 }
3896 *
3897 * We can then compute the difference in effective weight by using:
3898 *
3899 * dw_i = S * (s'_i - s_i) (3)
3900 *
3901 * Where 'S' is the group weight as seen by its parent.
3902 *
3903 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
3904 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
3905 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02003906 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003907static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003908{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003909 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02003910
Rik van Riel9722c2d2014-01-06 11:39:12 +00003911 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02003912 return wl;
3913
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003914 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003915 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003916
Paul Turner977dda72011-01-14 17:57:50 -08003917 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003918
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003919 /*
3920 * W = @wg + \Sum rw_j
3921 */
3922 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003923
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003924 /*
3925 * w = rw_i + @wl
3926 */
3927 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02003928
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003929 /*
3930 * wl = S * s'_i; see (2)
3931 */
3932 if (W > 0 && w < W)
3933 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08003934 else
3935 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02003936
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003937 /*
3938 * Per the above, wl is the new se->load.weight value; since
3939 * those are clipped to [MIN_SHARES, ...) do so now. See
3940 * calc_cfs_shares().
3941 */
Paul Turner977dda72011-01-14 17:57:50 -08003942 if (wl < MIN_SHARES)
3943 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003944
3945 /*
3946 * wl = dw_i = S * (s'_i - s_i); see (3)
3947 */
Paul Turner977dda72011-01-14 17:57:50 -08003948 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02003949
3950 /*
3951 * Recursively apply this logic to all parent groups to compute
3952 * the final effective load change on the root group. Since
3953 * only the @tg group gets extra weight, all parent groups can
3954 * only redistribute existing shares. @wl is the shift in shares
3955 * resulting from this level per the above.
3956 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003957 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003958 }
3959
3960 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003961}
3962#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003963
Mel Gorman58d081b2013-10-07 11:29:10 +01003964static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003965{
Peter Zijlstra83378262008-06-27 13:41:37 +02003966 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003967}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02003968
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02003969#endif
3970
Michael Wang62470412013-07-04 12:55:51 +08003971static int wake_wide(struct task_struct *p)
3972{
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08003973 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08003974
3975 /*
3976 * Yeah, it's the switching-frequency, could means many wakee or
3977 * rapidly switch, use factor here will just help to automatically
3978 * adjust the loose-degree, so bigger node will lead to more pull.
3979 */
3980 if (p->wakee_flips > factor) {
3981 /*
3982 * wakee is somewhat hot, it needs certain amount of cpu
3983 * resource, so if waker is far more hot, prefer to leave
3984 * it alone.
3985 */
3986 if (current->wakee_flips > (factor * p->wakee_flips))
3987 return 1;
3988 }
3989
3990 return 0;
3991}
3992
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003993static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01003994{
Paul Turnere37b6a72011-01-21 20:44:59 -08003995 s64 this_load, load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003996 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01003997 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02003998 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02003999 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004000 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004001
Michael Wang62470412013-07-04 12:55:51 +08004002 /*
4003 * If we wake multiple tasks be careful to not bounce
4004 * ourselves around too much.
4005 */
4006 if (wake_wide(p))
4007 return 0;
4008
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004009 idx = sd->wake_idx;
4010 this_cpu = smp_processor_id();
4011 prev_cpu = task_cpu(p);
4012 load = source_load(prev_cpu, idx);
4013 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004014
4015 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004016 * If sync wakeup then subtract the (maximum possible)
4017 * effect of the currently running task from the load
4018 * of the current CPU:
4019 */
Peter Zijlstra83378262008-06-27 13:41:37 +02004020 if (sync) {
4021 tg = task_group(current);
4022 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004023
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004024 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02004025 load += effective_load(tg, prev_cpu, 0, -weight);
4026 }
4027
4028 tg = task_group(p);
4029 weight = p->se.load.weight;
4030
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02004031 /*
4032 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004033 * due to the sync cause above having dropped this_load to 0, we'll
4034 * always have an imbalance, but there's really nothing you can do
4035 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02004036 *
4037 * Otherwise check if either cpus are near enough in load to allow this
4038 * task to be woken on this_cpu.
4039 */
Paul Turnere37b6a72011-01-21 20:44:59 -08004040 if (this_load > 0) {
4041 s64 this_eff_load, prev_eff_load;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004042
4043 this_eff_load = 100;
4044 this_eff_load *= power_of(prev_cpu);
4045 this_eff_load *= this_load +
4046 effective_load(tg, this_cpu, weight, weight);
4047
4048 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
4049 prev_eff_load *= power_of(this_cpu);
4050 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
4051
4052 balanced = this_eff_load <= prev_eff_load;
4053 } else
4054 balanced = true;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004055
4056 /*
4057 * If the currently running task will sleep within
4058 * a reasonable amount of time then attract this newly
4059 * woken task:
4060 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02004061 if (sync && balanced)
4062 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004063
Lucas De Marchi41acab82010-03-10 23:37:45 -03004064 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004065 tl_per_task = cpu_avg_load_per_task(this_cpu);
4066
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004067 if (balanced ||
4068 (this_load <= load &&
4069 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004070 /*
4071 * This domain has SD_WAKE_AFFINE and
4072 * p is cache cold in this domain, and
4073 * there is no bad imbalance.
4074 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004075 schedstat_inc(sd, ttwu_move_affine);
Lucas De Marchi41acab82010-03-10 23:37:45 -03004076 schedstat_inc(p, se.statistics.nr_wakeups_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004077
4078 return 1;
4079 }
4080 return 0;
4081}
4082
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004083/*
4084 * find_idlest_group finds and returns the least busy CPU group within the
4085 * domain.
4086 */
4087static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02004088find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004089 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01004090{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07004091 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004092 unsigned long min_load = ULONG_MAX, this_load = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004093 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004094 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004095
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004096 if (sd_flag & SD_BALANCE_WAKE)
4097 load_idx = sd->wake_idx;
4098
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004099 do {
4100 unsigned long load, avg_load;
4101 int local_group;
4102 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004103
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004104 /* Skip over this group if it has no CPUs allowed */
4105 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004106 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004107 continue;
4108
4109 local_group = cpumask_test_cpu(this_cpu,
4110 sched_group_cpus(group));
4111
4112 /* Tally up the load of all CPUs in the group */
4113 avg_load = 0;
4114
4115 for_each_cpu(i, sched_group_cpus(group)) {
4116 /* Bias balancing toward cpus of our domain */
4117 if (local_group)
4118 load = source_load(i, load_idx);
4119 else
4120 load = target_load(i, load_idx);
4121
4122 avg_load += load;
4123 }
4124
4125 /* Adjust by relative CPU power of the group */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02004126 avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004127
4128 if (local_group) {
4129 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004130 } else if (avg_load < min_load) {
4131 min_load = avg_load;
4132 idlest = group;
4133 }
4134 } while (group = group->next, group != sd->groups);
4135
4136 if (!idlest || 100*this_load < imbalance*min_load)
4137 return NULL;
4138 return idlest;
4139}
4140
4141/*
4142 * find_idlest_cpu - find the idlest cpu among the cpus in group.
4143 */
4144static int
4145find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
4146{
4147 unsigned long load, min_load = ULONG_MAX;
4148 int idlest = -1;
4149 int i;
4150
4151 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004152 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004153 load = weighted_cpuload(i);
4154
4155 if (load < min_load || (load == min_load && i == this_cpu)) {
4156 min_load = load;
4157 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004158 }
4159 }
4160
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004161 return idlest;
4162}
Gregory Haskinse7693a32008-01-25 21:08:09 +01004163
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004164/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004165 * Try and locate an idle CPU in the sched_domain.
4166 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004167static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004168{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004169 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07004170 struct sched_group *sg;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004171 int i = task_cpu(p);
4172
4173 if (idle_cpu(target))
4174 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004175
4176 /*
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004177 * If the prevous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004178 */
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004179 if (i != target && cpus_share_cache(i, target) && idle_cpu(i))
4180 return i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004181
4182 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07004183 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004184 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01004185 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08004186 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07004187 sg = sd->groups;
4188 do {
4189 if (!cpumask_intersects(sched_group_cpus(sg),
4190 tsk_cpus_allowed(p)))
4191 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02004192
Linus Torvalds37407ea2012-09-16 12:29:43 -07004193 for_each_cpu(i, sched_group_cpus(sg)) {
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004194 if (i == target || !idle_cpu(i))
Linus Torvalds37407ea2012-09-16 12:29:43 -07004195 goto next;
4196 }
4197
4198 target = cpumask_first_and(sched_group_cpus(sg),
4199 tsk_cpus_allowed(p));
4200 goto done;
4201next:
4202 sg = sg->next;
4203 } while (sg != sd->groups);
4204 }
4205done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004206 return target;
4207}
4208
4209/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004210 * sched_balance_self: balance the current task (running on cpu) in domains
4211 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
4212 * SD_BALANCE_EXEC.
4213 *
4214 * Balance, ie. select the least loaded group.
4215 *
4216 * Returns the target CPU number, or the same CPU if no balancing is needed.
4217 *
4218 * preempt must be disabled.
4219 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01004220static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01004221select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004222{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02004223 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004224 int cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004225 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004226 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02004227 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004228
Peter Zijlstra29baa742012-04-23 12:11:21 +02004229 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01004230 return prev_cpu;
4231
Peter Zijlstra0763a662009-09-14 19:37:39 +02004232 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004233 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004234 want_affine = 1;
4235 new_cpu = prev_cpu;
4236 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01004237
Peter Zijlstradce840a2011-04-07 14:09:50 +02004238 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004239 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01004240 if (!(tmp->flags & SD_LOAD_BALANCE))
4241 continue;
4242
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004243 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004244 * If both cpu and prev_cpu are part of this domain,
4245 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01004246 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004247 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
4248 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
4249 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08004250 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004251 }
4252
Alex Shif03542a2012-07-26 08:55:34 +08004253 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02004254 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004255 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004256
Mike Galbraith8b911ac2010-03-11 17:17:16 +01004257 if (affine_sd) {
Alex Shif03542a2012-07-26 08:55:34 +08004258 if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
Peter Zijlstradce840a2011-04-07 14:09:50 +02004259 prev_cpu = cpu;
4260
4261 new_cpu = select_idle_sibling(p, prev_cpu);
4262 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01004263 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02004264
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004265 while (sd) {
4266 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004267 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004268
Peter Zijlstra0763a662009-09-14 19:37:39 +02004269 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004270 sd = sd->child;
4271 continue;
4272 }
4273
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004274 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004275 if (!group) {
4276 sd = sd->child;
4277 continue;
4278 }
4279
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02004280 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004281 if (new_cpu == -1 || new_cpu == cpu) {
4282 /* Now try balancing at a lower domain level of cpu */
4283 sd = sd->child;
4284 continue;
4285 }
4286
4287 /* Now try balancing at a lower domain level of new_cpu */
4288 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004289 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004290 sd = NULL;
4291 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004292 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004293 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02004294 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004295 sd = tmp;
4296 }
4297 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01004298 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004299unlock:
4300 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01004301
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004302 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004303}
Paul Turner0a74bef2012-10-04 13:18:30 +02004304
4305/*
4306 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
4307 * cfs_rq_of(p) references at time of call are still valid and identify the
4308 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
4309 * other assumptions, including the state of rq->lock, should be made.
4310 */
4311static void
4312migrate_task_rq_fair(struct task_struct *p, int next_cpu)
4313{
Paul Turneraff3e492012-10-04 13:18:30 +02004314 struct sched_entity *se = &p->se;
4315 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4316
4317 /*
4318 * Load tracking: accumulate removed load so that it can be processed
4319 * when we next update owning cfs_rq under rq->lock. Tasks contribute
4320 * to blocked load iff they have a positive decay-count. It can never
4321 * be negative here since on-rq tasks have decay-count == 0.
4322 */
4323 if (se->avg.decay_count) {
4324 se->avg.decay_count = -__synchronize_entity_decay(se);
Alex Shi25099402013-06-20 10:18:55 +08004325 atomic_long_add(se->avg.load_avg_contrib,
4326 &cfs_rq->removed_load);
Paul Turneraff3e492012-10-04 13:18:30 +02004327 }
Paul Turner0a74bef2012-10-04 13:18:30 +02004328}
Gregory Haskinse7693a32008-01-25 21:08:09 +01004329#endif /* CONFIG_SMP */
4330
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004331static unsigned long
4332wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004333{
4334 unsigned long gran = sysctl_sched_wakeup_granularity;
4335
4336 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004337 * Since its curr running now, convert the gran from real-time
4338 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01004339 *
4340 * By using 'se' instead of 'curr' we penalize light tasks, so
4341 * they get preempted easier. That is, if 'se' < 'curr' then
4342 * the resulting gran will be larger, therefore penalizing the
4343 * lighter, if otoh 'se' > 'curr' then the resulting gran will
4344 * be smaller, again penalizing the lighter task.
4345 *
4346 * This is especially important for buddies when the leftmost
4347 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004348 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08004349 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004350}
4351
4352/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02004353 * Should 'se' preempt 'curr'.
4354 *
4355 * |s1
4356 * |s2
4357 * |s3
4358 * g
4359 * |<--->|c
4360 *
4361 * w(c, s1) = -1
4362 * w(c, s2) = 0
4363 * w(c, s3) = 1
4364 *
4365 */
4366static int
4367wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
4368{
4369 s64 gran, vdiff = curr->vruntime - se->vruntime;
4370
4371 if (vdiff <= 0)
4372 return -1;
4373
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004374 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02004375 if (vdiff > gran)
4376 return 1;
4377
4378 return 0;
4379}
4380
Peter Zijlstra02479092008-11-04 21:25:10 +01004381static void set_last_buddy(struct sched_entity *se)
4382{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004383 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
4384 return;
4385
4386 for_each_sched_entity(se)
4387 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01004388}
4389
4390static void set_next_buddy(struct sched_entity *se)
4391{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004392 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
4393 return;
4394
4395 for_each_sched_entity(se)
4396 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01004397}
4398
Rik van Rielac53db52011-02-01 09:51:03 -05004399static void set_skip_buddy(struct sched_entity *se)
4400{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004401 for_each_sched_entity(se)
4402 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05004403}
4404
Peter Zijlstra464b7522008-10-24 11:06:15 +02004405/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004406 * Preempt the current task with a newly woken task if needed:
4407 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02004408static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004409{
4410 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02004411 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01004412 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02004413 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004414 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01004415
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01004416 if (unlikely(se == pse))
4417 return;
4418
Paul Turner5238cdd2011-07-21 09:43:37 -07004419 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004420 * This is possible from callers such as move_task(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07004421 * unconditionally check_prempt_curr() after an enqueue (which may have
4422 * lead to a throttle). This both saves work and prevents false
4423 * next-buddy nomination below.
4424 */
4425 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
4426 return;
4427
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004428 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02004429 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004430 next_buddy_marked = 1;
4431 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02004432
Bharata B Raoaec0a512008-08-28 14:42:49 +05304433 /*
4434 * We can come here with TIF_NEED_RESCHED already set from new task
4435 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07004436 *
4437 * Note: this also catches the edge-case of curr being in a throttled
4438 * group (e.g. via set_curr_task), since update_curr() (in the
4439 * enqueue of curr) will have resulted in resched being set. This
4440 * prevents us from potentially nominating it as a false LAST_BUDDY
4441 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05304442 */
4443 if (test_tsk_need_resched(curr))
4444 return;
4445
Darren Harta2f5c9a2011-02-22 13:04:33 -08004446 /* Idle tasks are by definition preempted by non-idle tasks. */
4447 if (unlikely(curr->policy == SCHED_IDLE) &&
4448 likely(p->policy != SCHED_IDLE))
4449 goto preempt;
4450
Ingo Molnar91c234b2007-10-15 17:00:18 +02004451 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08004452 * Batch and idle tasks do not preempt non-idle tasks (their preemption
4453 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02004454 */
Ingo Molnar8ed92e512012-10-14 14:28:50 +02004455 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02004456 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004457
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004458 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07004459 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004460 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004461 if (wakeup_preempt_entity(se, pse) == 1) {
4462 /*
4463 * Bias pick_next to pick the sched entity that is
4464 * triggering this preemption.
4465 */
4466 if (!next_buddy_marked)
4467 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004468 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004469 }
Jupyung Leea65ac742009-11-17 18:51:40 +09004470
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004471 return;
4472
4473preempt:
4474 resched_task(curr);
4475 /*
4476 * Only set the backward buddy when the current task is still
4477 * on the rq. This can happen when a wakeup gets interleaved
4478 * with schedule on the ->pre_schedule() or idle_balance()
4479 * point, either of which can * drop the rq lock.
4480 *
4481 * Also, during early boot the idle thread is in the fair class,
4482 * for obvious reasons its a bad idea to schedule back to it.
4483 */
4484 if (unlikely(!se->on_rq || curr == rq->idle))
4485 return;
4486
4487 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
4488 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004489}
4490
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004491static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004492{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004493 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004494 struct cfs_rq *cfs_rq = &rq->cfs;
4495 struct sched_entity *se;
4496
Tim Blechmann36ace272009-11-24 11:55:45 +01004497 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004498 return NULL;
4499
4500 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02004501 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01004502 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004503 cfs_rq = group_cfs_rq(se);
4504 } while (cfs_rq);
4505
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004506 p = task_of(se);
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004507 if (hrtick_enabled(rq))
4508 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004509
4510 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004511}
4512
4513/*
4514 * Account for a descheduled task:
4515 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02004516static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004517{
4518 struct sched_entity *se = &prev->se;
4519 struct cfs_rq *cfs_rq;
4520
4521 for_each_sched_entity(se) {
4522 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02004523 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004524 }
4525}
4526
Rik van Rielac53db52011-02-01 09:51:03 -05004527/*
4528 * sched_yield() is very simple
4529 *
4530 * The magic of dealing with the ->skip buddy is in pick_next_entity.
4531 */
4532static void yield_task_fair(struct rq *rq)
4533{
4534 struct task_struct *curr = rq->curr;
4535 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
4536 struct sched_entity *se = &curr->se;
4537
4538 /*
4539 * Are we the only task in the tree?
4540 */
4541 if (unlikely(rq->nr_running == 1))
4542 return;
4543
4544 clear_buddies(cfs_rq, se);
4545
4546 if (curr->policy != SCHED_BATCH) {
4547 update_rq_clock(rq);
4548 /*
4549 * Update run-time statistics of the 'current'.
4550 */
4551 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01004552 /*
4553 * Tell update_rq_clock() that we've just updated,
4554 * so we don't do microscopic update in schedule()
4555 * and double the fastpath cost.
4556 */
4557 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05004558 }
4559
4560 set_skip_buddy(se);
4561}
4562
Mike Galbraithd95f4122011-02-01 09:50:51 -05004563static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
4564{
4565 struct sched_entity *se = &p->se;
4566
Paul Turner5238cdd2011-07-21 09:43:37 -07004567 /* throttled hierarchies are not runnable */
4568 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05004569 return false;
4570
4571 /* Tell the scheduler that we'd really like pse to run next. */
4572 set_next_buddy(se);
4573
Mike Galbraithd95f4122011-02-01 09:50:51 -05004574 yield_task_fair(rq);
4575
4576 return true;
4577}
4578
Peter Williams681f3e62007-10-24 18:23:51 +02004579#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004580/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02004581 * Fair scheduling class load-balancing methods.
4582 *
4583 * BASICS
4584 *
4585 * The purpose of load-balancing is to achieve the same basic fairness the
4586 * per-cpu scheduler provides, namely provide a proportional amount of compute
4587 * time to each task. This is expressed in the following equation:
4588 *
4589 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
4590 *
4591 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
4592 * W_i,0 is defined as:
4593 *
4594 * W_i,0 = \Sum_j w_i,j (2)
4595 *
4596 * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight
4597 * is derived from the nice value as per prio_to_weight[].
4598 *
4599 * The weight average is an exponential decay average of the instantaneous
4600 * weight:
4601 *
4602 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
4603 *
4604 * P_i is the cpu power (or compute capacity) of cpu i, typically it is the
4605 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
4606 * can also include other factors [XXX].
4607 *
4608 * To achieve this balance we define a measure of imbalance which follows
4609 * directly from (1):
4610 *
4611 * imb_i,j = max{ avg(W/P), W_i/P_i } - min{ avg(W/P), W_j/P_j } (4)
4612 *
4613 * We them move tasks around to minimize the imbalance. In the continuous
4614 * function space it is obvious this converges, in the discrete case we get
4615 * a few fun cases generally called infeasible weight scenarios.
4616 *
4617 * [XXX expand on:
4618 * - infeasible weights;
4619 * - local vs global optima in the discrete case. ]
4620 *
4621 *
4622 * SCHED DOMAINS
4623 *
4624 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
4625 * for all i,j solution, we create a tree of cpus that follows the hardware
4626 * topology where each level pairs two lower groups (or better). This results
4627 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
4628 * tree to only the first of the previous level and we decrease the frequency
4629 * of load-balance at each level inv. proportional to the number of cpus in
4630 * the groups.
4631 *
4632 * This yields:
4633 *
4634 * log_2 n 1 n
4635 * \Sum { --- * --- * 2^i } = O(n) (5)
4636 * i = 0 2^i 2^i
4637 * `- size of each group
4638 * | | `- number of cpus doing load-balance
4639 * | `- freq
4640 * `- sum over all levels
4641 *
4642 * Coupled with a limit on how many tasks we can migrate every balance pass,
4643 * this makes (5) the runtime complexity of the balancer.
4644 *
4645 * An important property here is that each CPU is still (indirectly) connected
4646 * to every other cpu in at most O(log n) steps:
4647 *
4648 * The adjacency matrix of the resulting graph is given by:
4649 *
4650 * log_2 n
4651 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
4652 * k = 0
4653 *
4654 * And you'll find that:
4655 *
4656 * A^(log_2 n)_i,j != 0 for all i,j (7)
4657 *
4658 * Showing there's indeed a path between every cpu in at most O(log n) steps.
4659 * The task movement gives a factor of O(m), giving a convergence complexity
4660 * of:
4661 *
4662 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
4663 *
4664 *
4665 * WORK CONSERVING
4666 *
4667 * In order to avoid CPUs going idle while there's still work to do, new idle
4668 * balancing is more aggressive and has the newly idle cpu iterate up the domain
4669 * tree itself instead of relying on other CPUs to bring it work.
4670 *
4671 * This adds some complexity to both (5) and (8) but it reduces the total idle
4672 * time.
4673 *
4674 * [XXX more?]
4675 *
4676 *
4677 * CGROUPS
4678 *
4679 * Cgroups make a horror show out of (2), instead of a simple sum we get:
4680 *
4681 * s_k,i
4682 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
4683 * S_k
4684 *
4685 * Where
4686 *
4687 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
4688 *
4689 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
4690 *
4691 * The big problem is S_k, its a global sum needed to compute a local (W_i)
4692 * property.
4693 *
4694 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
4695 * rewrite all of this once again.]
4696 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004697
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09004698static unsigned long __read_mostly max_load_balance_interval = HZ/10;
4699
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004700enum fbq_type { regular, remote, all };
4701
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004702#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01004703#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02004704#define LBF_DST_PINNED 0x04
4705#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004706
4707struct lb_env {
4708 struct sched_domain *sd;
4709
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004710 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05304711 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004712
4713 int dst_cpu;
4714 struct rq *dst_rq;
4715
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304716 struct cpumask *dst_grpmask;
4717 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004718 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004719 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08004720 /* The set of CPUs under consideration for load-balancing */
4721 struct cpumask *cpus;
4722
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004723 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01004724
4725 unsigned int loop;
4726 unsigned int loop_break;
4727 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01004728
4729 enum fbq_type fbq_type;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004730};
4731
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004732/*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004733 * move_task - move a task from one runqueue to another runqueue.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004734 * Both runqueues must be locked.
4735 */
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004736static void move_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004737{
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004738 deactivate_task(env->src_rq, p, 0);
4739 set_task_cpu(p, env->dst_cpu);
4740 activate_task(env->dst_rq, p, 0);
4741 check_preempt_curr(env->dst_rq, p, 0);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004742}
4743
4744/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02004745 * Is this task likely cache-hot:
4746 */
4747static int
4748task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
4749{
4750 s64 delta;
4751
4752 if (p->sched_class != &fair_sched_class)
4753 return 0;
4754
4755 if (unlikely(p->policy == SCHED_IDLE))
4756 return 0;
4757
4758 /*
4759 * Buddy candidates are cache hot:
4760 */
4761 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
4762 (&p->se == cfs_rq_of(&p->se)->next ||
4763 &p->se == cfs_rq_of(&p->se)->last))
4764 return 1;
4765
4766 if (sysctl_sched_migration_cost == -1)
4767 return 1;
4768 if (sysctl_sched_migration_cost == 0)
4769 return 0;
4770
4771 delta = now - p->se.exec_start;
4772
4773 return delta < (s64)sysctl_sched_migration_cost;
4774}
4775
Mel Gorman3a7053b2013-10-07 11:29:00 +01004776#ifdef CONFIG_NUMA_BALANCING
4777/* Returns true if the destination node has incurred more faults */
4778static bool migrate_improves_locality(struct task_struct *p, struct lb_env *env)
4779{
4780 int src_nid, dst_nid;
4781
4782 if (!sched_feat(NUMA_FAVOUR_HIGHER) || !p->numa_faults ||
4783 !(env->sd->flags & SD_NUMA)) {
4784 return false;
4785 }
4786
4787 src_nid = cpu_to_node(env->src_cpu);
4788 dst_nid = cpu_to_node(env->dst_cpu);
4789
Mel Gorman83e1d2c2013-10-07 11:29:27 +01004790 if (src_nid == dst_nid)
Mel Gorman3a7053b2013-10-07 11:29:00 +01004791 return false;
4792
Mel Gorman83e1d2c2013-10-07 11:29:27 +01004793 /* Always encourage migration to the preferred node. */
4794 if (dst_nid == p->numa_preferred_nid)
4795 return true;
4796
Rik van Riel887c2902013-10-07 11:29:31 +01004797 /* If both task and group weight improve, this move is a winner. */
4798 if (task_weight(p, dst_nid) > task_weight(p, src_nid) &&
4799 group_weight(p, dst_nid) > group_weight(p, src_nid))
Mel Gorman3a7053b2013-10-07 11:29:00 +01004800 return true;
4801
4802 return false;
4803}
Mel Gorman7a0f3082013-10-07 11:29:01 +01004804
4805
4806static bool migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
4807{
4808 int src_nid, dst_nid;
4809
4810 if (!sched_feat(NUMA) || !sched_feat(NUMA_RESIST_LOWER))
4811 return false;
4812
4813 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
4814 return false;
4815
4816 src_nid = cpu_to_node(env->src_cpu);
4817 dst_nid = cpu_to_node(env->dst_cpu);
4818
Mel Gorman83e1d2c2013-10-07 11:29:27 +01004819 if (src_nid == dst_nid)
Mel Gorman7a0f3082013-10-07 11:29:01 +01004820 return false;
4821
Mel Gorman83e1d2c2013-10-07 11:29:27 +01004822 /* Migrating away from the preferred node is always bad. */
4823 if (src_nid == p->numa_preferred_nid)
4824 return true;
4825
Rik van Riel887c2902013-10-07 11:29:31 +01004826 /* If either task or group weight get worse, don't do it. */
4827 if (task_weight(p, dst_nid) < task_weight(p, src_nid) ||
4828 group_weight(p, dst_nid) < group_weight(p, src_nid))
Mel Gorman7a0f3082013-10-07 11:29:01 +01004829 return true;
4830
4831 return false;
4832}
4833
Mel Gorman3a7053b2013-10-07 11:29:00 +01004834#else
4835static inline bool migrate_improves_locality(struct task_struct *p,
4836 struct lb_env *env)
4837{
4838 return false;
4839}
Mel Gorman7a0f3082013-10-07 11:29:01 +01004840
4841static inline bool migrate_degrades_locality(struct task_struct *p,
4842 struct lb_env *env)
4843{
4844 return false;
4845}
Mel Gorman3a7053b2013-10-07 11:29:00 +01004846#endif
4847
Peter Zijlstra029632f2011-10-25 10:00:11 +02004848/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004849 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
4850 */
4851static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004852int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004853{
4854 int tsk_cache_hot = 0;
4855 /*
4856 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09004857 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004858 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09004859 * 3) running (obviously), or
4860 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004861 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09004862 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
4863 return 0;
4864
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004865 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09004866 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304867
Lucas De Marchi41acab82010-03-10 23:37:45 -03004868 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304869
Peter Zijlstra62633222013-08-19 12:41:09 +02004870 env->flags |= LBF_SOME_PINNED;
4871
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304872 /*
4873 * Remember if this task can be migrated to any other cpu in
4874 * our sched_group. We may want to revisit it if we couldn't
4875 * meet load balance goals by pulling other tasks on src_cpu.
4876 *
4877 * Also avoid computing new_dst_cpu if we have already computed
4878 * one in current iteration.
4879 */
Peter Zijlstra62633222013-08-19 12:41:09 +02004880 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304881 return 0;
4882
Joonsoo Kime02e60c2013-04-23 17:27:42 +09004883 /* Prevent to re-select dst_cpu via env's cpus */
4884 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
4885 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02004886 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09004887 env->new_dst_cpu = cpu;
4888 break;
4889 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304890 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09004891
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004892 return 0;
4893 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05304894
4895 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004896 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004897
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01004898 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03004899 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004900 return 0;
4901 }
4902
4903 /*
4904 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01004905 * 1) destination numa is preferred
4906 * 2) task is cache cold, or
4907 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004908 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004909 tsk_cache_hot = task_hot(p, rq_clock_task(env->src_rq), env->sd);
Mel Gorman7a0f3082013-10-07 11:29:01 +01004910 if (!tsk_cache_hot)
4911 tsk_cache_hot = migrate_degrades_locality(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01004912
4913 if (migrate_improves_locality(p, env)) {
4914#ifdef CONFIG_SCHEDSTATS
4915 if (tsk_cache_hot) {
4916 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
4917 schedstat_inc(p, se.statistics.nr_forced_migrations);
4918 }
4919#endif
4920 return 1;
4921 }
4922
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004923 if (!tsk_cache_hot ||
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004924 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Zhang Hang4e2dcb72013-04-10 14:04:55 +08004925
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004926 if (tsk_cache_hot) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004927 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
Lucas De Marchi41acab82010-03-10 23:37:45 -03004928 schedstat_inc(p, se.statistics.nr_forced_migrations);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004929 }
Zhang Hang4e2dcb72013-04-10 14:04:55 +08004930
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004931 return 1;
4932 }
4933
Zhang Hang4e2dcb72013-04-10 14:04:55 +08004934 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
4935 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004936}
4937
Peter Zijlstra897c3952009-12-17 17:45:42 +01004938/*
4939 * move_one_task tries to move exactly one task from busiest to this_rq, as
4940 * part of active balancing operations within "domain".
4941 * Returns 1 if successful and 0 otherwise.
4942 *
4943 * Called with both runqueues locked.
4944 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004945static int move_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01004946{
4947 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01004948
Peter Zijlstra367456c2012-02-20 21:49:09 +01004949 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01004950 if (!can_migrate_task(p, env))
4951 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01004952
Peter Zijlstra367456c2012-02-20 21:49:09 +01004953 move_task(p, env);
4954 /*
4955 * Right now, this is only the second place move_task()
4956 * is called, so we can safely collect move_task()
4957 * stats here rather than inside move_task().
4958 */
4959 schedstat_inc(env->sd, lb_gained[env->idle]);
4960 return 1;
Peter Zijlstra897c3952009-12-17 17:45:42 +01004961 }
Peter Zijlstra897c3952009-12-17 17:45:42 +01004962 return 0;
4963}
4964
Peter Zijlstraeb953082012-04-17 13:38:40 +02004965static const unsigned int sched_nr_migrate_break = 32;
4966
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004967/*
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004968 * move_tasks tries to move up to imbalance weighted load from busiest to
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004969 * this_rq, as part of a balancing operation within domain "sd".
4970 * Returns 1 if successful and 0 otherwise.
4971 *
4972 * Called with both runqueues locked.
4973 */
4974static int move_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004975{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004976 struct list_head *tasks = &env->src_rq->cfs_tasks;
4977 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01004978 unsigned long load;
4979 int pulled = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004980
Peter Zijlstrabd939f42012-05-02 14:20:37 +02004981 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004982 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004983
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004984 while (!list_empty(tasks)) {
4985 p = list_first_entry(tasks, struct task_struct, se.group_node);
4986
Peter Zijlstra367456c2012-02-20 21:49:09 +01004987 env->loop++;
4988 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004989 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01004990 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01004991
4992 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01004993 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02004994 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01004995 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01004996 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02004997 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01004998
Joonsoo Kimd3198082013-04-23 17:27:40 +09004999 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01005000 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005001
Peter Zijlstra367456c2012-02-20 21:49:09 +01005002 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005003
Peter Zijlstraeb953082012-04-17 13:38:40 +02005004 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01005005 goto next;
5006
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005007 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01005008 goto next;
5009
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005010 move_task(p, env);
Peter Zijlstraee00e662009-12-17 17:25:20 +01005011 pulled++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005012 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005013
5014#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01005015 /*
5016 * NEWIDLE balancing is a source of latency, so preemptible
5017 * kernels will stop after the first task is pulled to minimize
5018 * the critical section.
5019 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005020 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01005021 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005022#endif
5023
Peter Zijlstraee00e662009-12-17 17:25:20 +01005024 /*
5025 * We only want to steal up to the prescribed amount of
5026 * weighted load.
5027 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005028 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01005029 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005030
Peter Zijlstra367456c2012-02-20 21:49:09 +01005031 continue;
5032next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005033 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005034 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005035
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005036 /*
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005037 * Right now, this is one of only two places move_task() is called,
5038 * so we can safely collect move_task() stats here rather than
5039 * inside move_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005040 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005041 schedstat_add(env->sd, lb_gained[env->idle], pulled);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005042
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005043 return pulled;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005044}
5045
Peter Zijlstra230059de2009-12-17 17:47:12 +01005046#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005047/*
5048 * update tg->load_weight by folding this cpu's load_avg
5049 */
Paul Turner48a16752012-10-04 13:18:31 +02005050static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005051{
Paul Turner48a16752012-10-04 13:18:31 +02005052 struct sched_entity *se = tg->se[cpu];
5053 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu];
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005054
Paul Turner48a16752012-10-04 13:18:31 +02005055 /* throttled entities do not contribute to load */
5056 if (throttled_hierarchy(cfs_rq))
5057 return;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005058
Paul Turneraff3e492012-10-04 13:18:30 +02005059 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005060
Paul Turner82958362012-10-04 13:18:31 +02005061 if (se) {
5062 update_entity_load_avg(se, 1);
5063 /*
5064 * We pivot on our runnable average having decayed to zero for
5065 * list removal. This generally implies that all our children
5066 * have also been removed (modulo rounding error or bandwidth
5067 * control); however, such cases are rare and we can fix these
5068 * at enqueue.
5069 *
5070 * TODO: fix up out-of-order children on enqueue.
5071 */
5072 if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
5073 list_del_leaf_cfs_rq(cfs_rq);
5074 } else {
Paul Turner48a16752012-10-04 13:18:31 +02005075 struct rq *rq = rq_of(cfs_rq);
Paul Turner82958362012-10-04 13:18:31 +02005076 update_rq_runnable_avg(rq, rq->nr_running);
5077 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005078}
5079
Paul Turner48a16752012-10-04 13:18:31 +02005080static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005081{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005082 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02005083 struct cfs_rq *cfs_rq;
5084 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005085
Paul Turner48a16752012-10-04 13:18:31 +02005086 raw_spin_lock_irqsave(&rq->lock, flags);
5087 update_rq_clock(rq);
Peter Zijlstra9763b672011-07-13 13:09:25 +02005088 /*
5089 * Iterates the task_group tree in a bottom up fashion, see
5090 * list_add_leaf_cfs_rq() for details.
5091 */
Paul Turner64660c82011-07-21 09:43:36 -07005092 for_each_leaf_cfs_rq(rq, cfs_rq) {
Paul Turner48a16752012-10-04 13:18:31 +02005093 /*
5094 * Note: We may want to consider periodically releasing
5095 * rq->lock about these updates so that creating many task
5096 * groups does not result in continually extending hold time.
5097 */
5098 __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu);
Paul Turner64660c82011-07-21 09:43:36 -07005099 }
Paul Turner48a16752012-10-04 13:18:31 +02005100
5101 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005102}
5103
Peter Zijlstra9763b672011-07-13 13:09:25 +02005104/*
Vladimir Davydov68520792013-07-15 17:49:19 +04005105 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02005106 * This needs to be done in a top-down fashion because the load of a child
5107 * group is a fraction of its parents load.
5108 */
Vladimir Davydov68520792013-07-15 17:49:19 +04005109static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02005110{
Vladimir Davydov68520792013-07-15 17:49:19 +04005111 struct rq *rq = rq_of(cfs_rq);
5112 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005113 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04005114 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005115
Vladimir Davydov68520792013-07-15 17:49:19 +04005116 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005117 return;
5118
Vladimir Davydov68520792013-07-15 17:49:19 +04005119 cfs_rq->h_load_next = NULL;
5120 for_each_sched_entity(se) {
5121 cfs_rq = cfs_rq_of(se);
5122 cfs_rq->h_load_next = se;
5123 if (cfs_rq->last_h_load_update == now)
5124 break;
5125 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005126
Vladimir Davydov68520792013-07-15 17:49:19 +04005127 if (!se) {
Vladimir Davydov7e3115e2013-09-14 19:39:46 +04005128 cfs_rq->h_load = cfs_rq->runnable_load_avg;
Vladimir Davydov68520792013-07-15 17:49:19 +04005129 cfs_rq->last_h_load_update = now;
5130 }
5131
5132 while ((se = cfs_rq->h_load_next) != NULL) {
5133 load = cfs_rq->h_load;
5134 load = div64_ul(load * se->avg.load_avg_contrib,
5135 cfs_rq->runnable_load_avg + 1);
5136 cfs_rq = group_cfs_rq(se);
5137 cfs_rq->h_load = load;
5138 cfs_rq->last_h_load_update = now;
5139 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02005140}
5141
Peter Zijlstra367456c2012-02-20 21:49:09 +01005142static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01005143{
Peter Zijlstra367456c2012-02-20 21:49:09 +01005144 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01005145
Vladimir Davydov68520792013-07-15 17:49:19 +04005146 update_cfs_rq_h_load(cfs_rq);
Alex Shia003a252013-06-20 10:18:51 +08005147 return div64_ul(p->se.avg.load_avg_contrib * cfs_rq->h_load,
5148 cfs_rq->runnable_load_avg + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01005149}
5150#else
Paul Turner48a16752012-10-04 13:18:31 +02005151static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005152{
5153}
5154
Peter Zijlstra367456c2012-02-20 21:49:09 +01005155static unsigned long task_h_load(struct task_struct *p)
5156{
Alex Shia003a252013-06-20 10:18:51 +08005157 return p->se.avg.load_avg_contrib;
Peter Zijlstra230059de2009-12-17 17:47:12 +01005158}
5159#endif
5160
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005161/********** Helpers for find_busiest_group ************************/
5162/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005163 * sg_lb_stats - stats of a sched_group required for load_balancing
5164 */
5165struct sg_lb_stats {
5166 unsigned long avg_load; /*Avg load across the CPUs of the group */
5167 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005168 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005169 unsigned long load_per_task;
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005170 unsigned long group_power;
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005171 unsigned int sum_nr_running; /* Nr tasks running in the group */
5172 unsigned int group_capacity;
5173 unsigned int idle_cpus;
5174 unsigned int group_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005175 int group_imb; /* Is there an imbalance in the group ? */
Nikhil Raofab47622010-10-15 13:12:29 -07005176 int group_has_capacity; /* Is there extra capacity in the group? */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005177#ifdef CONFIG_NUMA_BALANCING
5178 unsigned int nr_numa_running;
5179 unsigned int nr_preferred_running;
5180#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005181};
5182
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005183/*
5184 * sd_lb_stats - Structure to store the statistics of a sched_domain
5185 * during load balancing.
5186 */
5187struct sd_lb_stats {
5188 struct sched_group *busiest; /* Busiest group in this sd */
5189 struct sched_group *local; /* Local group in this sd */
5190 unsigned long total_load; /* Total load of all groups in sd */
5191 unsigned long total_pwr; /* Total power of all groups in sd */
5192 unsigned long avg_load; /* Average load across all groups in sd */
5193
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005194 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005195 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005196};
5197
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005198static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
5199{
5200 /*
5201 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
5202 * local_stat because update_sg_lb_stats() does a full clear/assignment.
5203 * We must however clear busiest_stat::avg_load because
5204 * update_sd_pick_busiest() reads this before assignment.
5205 */
5206 *sds = (struct sd_lb_stats){
5207 .busiest = NULL,
5208 .local = NULL,
5209 .total_load = 0UL,
5210 .total_pwr = 0UL,
5211 .busiest_stat = {
5212 .avg_load = 0UL,
5213 },
5214 };
5215}
5216
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005217/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005218 * get_sd_load_idx - Obtain the load index for a given sched domain.
5219 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05305220 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005221 *
5222 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005223 */
5224static inline int get_sd_load_idx(struct sched_domain *sd,
5225 enum cpu_idle_type idle)
5226{
5227 int load_idx;
5228
5229 switch (idle) {
5230 case CPU_NOT_IDLE:
5231 load_idx = sd->busy_idx;
5232 break;
5233
5234 case CPU_NEWLY_IDLE:
5235 load_idx = sd->newidle_idx;
5236 break;
5237 default:
5238 load_idx = sd->idle_idx;
5239 break;
5240 }
5241
5242 return load_idx;
5243}
5244
Li Zefan15f803c2013-03-05 16:07:11 +08005245static unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005246{
Nikhil Rao1399fa72011-05-18 10:09:39 -07005247 return SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005248}
5249
5250unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
5251{
5252 return default_scale_freq_power(sd, cpu);
5253}
5254
Li Zefan15f803c2013-03-05 16:07:11 +08005255static unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005256{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02005257 unsigned long weight = sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005258 unsigned long smt_gain = sd->smt_gain;
5259
5260 smt_gain /= weight;
5261
5262 return smt_gain;
5263}
5264
5265unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
5266{
5267 return default_scale_smt_power(sd, cpu);
5268}
5269
Li Zefan15f803c2013-03-05 16:07:11 +08005270static unsigned long scale_rt_power(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005271{
5272 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005273 u64 total, available, age_stamp, avg;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005274
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005275 /*
5276 * Since we're reading these variables without serialization make sure
5277 * we read them once before doing sanity checks on them.
5278 */
5279 age_stamp = ACCESS_ONCE(rq->age_stamp);
5280 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005281
Frederic Weisbecker78becc22013-04-12 01:51:02 +02005282 total = sched_avg_period() + (rq_clock(rq) - age_stamp);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005283
5284 if (unlikely(total < avg)) {
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005285 /* Ensures that power won't end up being negative */
5286 available = 0;
5287 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005288 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005289 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005290
Nikhil Rao1399fa72011-05-18 10:09:39 -07005291 if (unlikely((s64)total < SCHED_POWER_SCALE))
5292 total = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005293
Nikhil Rao1399fa72011-05-18 10:09:39 -07005294 total >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005295
5296 return div_u64(available, total);
5297}
5298
5299static void update_cpu_power(struct sched_domain *sd, int cpu)
5300{
Peter Zijlstra669c55e2010-04-16 14:59:29 +02005301 unsigned long weight = sd->span_weight;
Nikhil Rao1399fa72011-05-18 10:09:39 -07005302 unsigned long power = SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005303 struct sched_group *sdg = sd->groups;
5304
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005305 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
5306 if (sched_feat(ARCH_POWER))
5307 power *= arch_scale_smt_power(sd, cpu);
5308 else
5309 power *= default_scale_smt_power(sd, cpu);
5310
Nikhil Rao1399fa72011-05-18 10:09:39 -07005311 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005312 }
5313
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005314 sdg->sgp->power_orig = power;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005315
5316 if (sched_feat(ARCH_POWER))
5317 power *= arch_scale_freq_power(sd, cpu);
5318 else
5319 power *= default_scale_freq_power(sd, cpu);
5320
Nikhil Rao1399fa72011-05-18 10:09:39 -07005321 power >>= SCHED_POWER_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005322
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005323 power *= scale_rt_power(cpu);
Nikhil Rao1399fa72011-05-18 10:09:39 -07005324 power >>= SCHED_POWER_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005325
5326 if (!power)
5327 power = 1;
5328
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005329 cpu_rq(cpu)->cpu_power = power;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005330 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005331}
5332
Peter Zijlstra029632f2011-10-25 10:00:11 +02005333void update_group_power(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005334{
5335 struct sched_domain *child = sd->child;
5336 struct sched_group *group, *sdg = sd->groups;
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005337 unsigned long power, power_orig;
Vincent Guittot4ec44122011-12-12 20:21:08 +01005338 unsigned long interval;
5339
5340 interval = msecs_to_jiffies(sd->balance_interval);
5341 interval = clamp(interval, 1UL, max_load_balance_interval);
5342 sdg->sgp->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005343
5344 if (!child) {
5345 update_cpu_power(sd, cpu);
5346 return;
5347 }
5348
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005349 power_orig = power = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005350
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005351 if (child->flags & SD_OVERLAP) {
5352 /*
5353 * SD_OVERLAP domains cannot assume that child groups
5354 * span the current group.
5355 */
5356
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005357 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305358 struct sched_group_power *sgp;
5359 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005360
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305361 /*
5362 * build_sched_domains() -> init_sched_groups_power()
5363 * gets here before we've attached the domains to the
5364 * runqueues.
5365 *
5366 * Use power_of(), which is set irrespective of domains
5367 * in update_cpu_power().
5368 *
5369 * This avoids power/power_orig from being 0 and
5370 * causing divide-by-zero issues on boot.
5371 *
5372 * Runtime updates will correct power_orig.
5373 */
5374 if (unlikely(!rq->sd)) {
5375 power_orig += power_of(cpu);
5376 power += power_of(cpu);
5377 continue;
5378 }
5379
5380 sgp = rq->sd->groups->sgp;
5381 power_orig += sgp->power_orig;
5382 power += sgp->power;
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005383 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005384 } else {
5385 /*
5386 * !SD_OVERLAP domains can assume that child groups
5387 * span the current group.
5388 */
5389
5390 group = child->groups;
5391 do {
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005392 power_orig += group->sgp->power_orig;
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005393 power += group->sgp->power;
5394 group = group->next;
5395 } while (group != child->groups);
5396 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005397
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005398 sdg->sgp->power_orig = power_orig;
5399 sdg->sgp->power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005400}
5401
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005402/*
5403 * Try and fix up capacity for tiny siblings, this is needed when
5404 * things like SD_ASYM_PACKING need f_b_g to select another sibling
5405 * which on its own isn't powerful enough.
5406 *
5407 * See update_sd_pick_busiest() and check_asym_packing().
5408 */
5409static inline int
5410fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
5411{
5412 /*
Nikhil Rao1399fa72011-05-18 10:09:39 -07005413 * Only siblings can have significantly less than SCHED_POWER_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005414 */
Peter Zijlstraa6c75f22011-04-07 14:09:52 +02005415 if (!(sd->flags & SD_SHARE_CPUPOWER))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005416 return 0;
5417
5418 /*
5419 * If ~90% of the cpu_power is still there, we're good.
5420 */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005421 if (group->sgp->power * 32 > group->sgp->power_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005422 return 1;
5423
5424 return 0;
5425}
5426
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005427/*
5428 * Group imbalance indicates (and tries to solve) the problem where balancing
5429 * groups is inadequate due to tsk_cpus_allowed() constraints.
5430 *
5431 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
5432 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
5433 * Something like:
5434 *
5435 * { 0 1 2 3 } { 4 5 6 7 }
5436 * * * * *
5437 *
5438 * If we were to balance group-wise we'd place two tasks in the first group and
5439 * two tasks in the second group. Clearly this is undesired as it will overload
5440 * cpu 3 and leave one of the cpus in the second group unused.
5441 *
5442 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02005443 * by noticing the lower domain failed to reach balance and had difficulty
5444 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005445 *
5446 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05305447 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02005448 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005449 * to create an effective group imbalance.
5450 *
5451 * This is a somewhat tricky proposition since the next run might not find the
5452 * group imbalance and decide the groups need to be balanced again. A most
5453 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005454 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005455
Peter Zijlstra62633222013-08-19 12:41:09 +02005456static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005457{
Peter Zijlstra62633222013-08-19 12:41:09 +02005458 return group->sgp->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005459}
5460
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005461/*
5462 * Compute the group capacity.
5463 *
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005464 * Avoid the issue where N*frac(smt_power) >= 1 creates 'phantom' cores by
5465 * first dividing out the smt factor and computing the actual number of cores
5466 * and limit power unit capacity with that.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005467 */
5468static inline int sg_capacity(struct lb_env *env, struct sched_group *group)
5469{
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005470 unsigned int capacity, smt, cpus;
5471 unsigned int power, power_orig;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005472
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005473 power = group->sgp->power;
5474 power_orig = group->sgp->power_orig;
5475 cpus = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005476
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005477 /* smt := ceil(cpus / power), assumes: 1 < smt_power < 2 */
5478 smt = DIV_ROUND_UP(SCHED_POWER_SCALE * cpus, power_orig);
5479 capacity = cpus / smt; /* cores */
5480
5481 capacity = min_t(unsigned, capacity, DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE));
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005482 if (!capacity)
5483 capacity = fix_small_capacity(env->sd, group);
5484
5485 return capacity;
5486}
5487
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005488/**
5489 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
5490 * @env: The load balancing environment.
5491 * @group: sched_group whose statistics are to be updated.
5492 * @load_idx: Load index of sched_domain of this_cpu for load calc.
5493 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005494 * @sgs: variable to hold the statistics for this group.
5495 */
5496static inline void update_sg_lb_stats(struct lb_env *env,
5497 struct sched_group *group, int load_idx,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09005498 int local_group, struct sg_lb_stats *sgs)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005499{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005500 unsigned long load;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005501 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005502
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005503 memset(sgs, 0, sizeof(*sgs));
5504
Michael Wangb94031302012-07-12 16:10:13 +08005505 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005506 struct rq *rq = cpu_rq(i);
5507
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005508 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02005509 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005510 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02005511 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005512 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005513
5514 sgs->group_load += load;
Kamalesh Babulal380c9072013-11-15 15:06:52 +05305515 sgs->sum_nr_running += rq->nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005516#ifdef CONFIG_NUMA_BALANCING
5517 sgs->nr_numa_running += rq->nr_numa_running;
5518 sgs->nr_preferred_running += rq->nr_preferred_running;
5519#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005520 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07005521 if (idle_cpu(i))
5522 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005523 }
5524
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005525 /* Adjust by relative CPU power of the group */
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005526 sgs->group_power = group->sgp->power;
5527 sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / sgs->group_power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005528
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005529 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02005530 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005531
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07005532 sgs->group_weight = group->group_weight;
Nikhil Raofab47622010-10-15 13:12:29 -07005533
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005534 sgs->group_imb = sg_imbalanced(group);
5535 sgs->group_capacity = sg_capacity(env, group);
5536
Nikhil Raofab47622010-10-15 13:12:29 -07005537 if (sgs->group_capacity > sgs->sum_nr_running)
5538 sgs->group_has_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005539}
5540
5541/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10005542 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07005543 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10005544 * @sds: sched_domain statistics
5545 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10005546 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10005547 *
5548 * Determine if @sg is a busier group than the previously selected
5549 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005550 *
5551 * Return: %true if @sg is a busier group than the previously selected
5552 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10005553 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005554static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10005555 struct sd_lb_stats *sds,
5556 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005557 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10005558{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005559 if (sgs->avg_load <= sds->busiest_stat.avg_load)
Michael Neuling532cb4c2010-06-08 14:57:02 +10005560 return false;
5561
5562 if (sgs->sum_nr_running > sgs->group_capacity)
5563 return true;
5564
5565 if (sgs->group_imb)
5566 return true;
5567
5568 /*
5569 * ASYM_PACKING needs to move all the work to the lowest
5570 * numbered CPUs in the group, therefore mark all groups
5571 * higher than ourself as busy.
5572 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005573 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
5574 env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10005575 if (!sds->busiest)
5576 return true;
5577
5578 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
5579 return true;
5580 }
5581
5582 return false;
5583}
5584
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005585#ifdef CONFIG_NUMA_BALANCING
5586static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
5587{
5588 if (sgs->sum_nr_running > sgs->nr_numa_running)
5589 return regular;
5590 if (sgs->sum_nr_running > sgs->nr_preferred_running)
5591 return remote;
5592 return all;
5593}
5594
5595static inline enum fbq_type fbq_classify_rq(struct rq *rq)
5596{
5597 if (rq->nr_running > rq->nr_numa_running)
5598 return regular;
5599 if (rq->nr_running > rq->nr_preferred_running)
5600 return remote;
5601 return all;
5602}
5603#else
5604static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
5605{
5606 return all;
5607}
5608
5609static inline enum fbq_type fbq_classify_rq(struct rq *rq)
5610{
5611 return regular;
5612}
5613#endif /* CONFIG_NUMA_BALANCING */
5614
Michael Neuling532cb4c2010-06-08 14:57:02 +10005615/**
Hui Kang461819a2011-10-11 23:00:59 -04005616 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07005617 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005618 * @sds: variable to hold the statistics for this sched_domain.
5619 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005620static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005621{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005622 struct sched_domain *child = env->sd->child;
5623 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005624 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005625 int load_idx, prefer_sibling = 0;
5626
5627 if (child && child->flags & SD_PREFER_SIBLING)
5628 prefer_sibling = 1;
5629
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005630 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005631
5632 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005633 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005634 int local_group;
5635
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005636 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005637 if (local_group) {
5638 sds->local = sg;
5639 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005640
5641 if (env->idle != CPU_NEWLY_IDLE ||
5642 time_after_eq(jiffies, sg->sgp->next_update))
5643 update_group_power(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005644 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005645
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005646 update_sg_lb_stats(env, sg, load_idx, local_group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005647
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005648 if (local_group)
5649 goto next_group;
5650
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005651 /*
5652 * In case the child domain prefers tasks go to siblings
Michael Neuling532cb4c2010-06-08 14:57:02 +10005653 * first, lower the sg capacity to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07005654 * and move all the excess tasks away. We lower the capacity
5655 * of a group only if the local group has the capacity to fit
5656 * these excess tasks, i.e. nr_running < group_capacity. The
5657 * extra check prevents the case where you always pull from the
5658 * heaviest group when it is already under-utilized (possible
5659 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005660 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005661 if (prefer_sibling && sds->local &&
5662 sds->local_stat.group_has_capacity)
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005663 sgs->group_capacity = min(sgs->group_capacity, 1U);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005664
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005665 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10005666 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005667 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005668 }
5669
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005670next_group:
5671 /* Now, start updating sd_lb_stats */
5672 sds->total_load += sgs->group_load;
5673 sds->total_pwr += sgs->group_power;
5674
Michael Neuling532cb4c2010-06-08 14:57:02 +10005675 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005676 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005677
5678 if (env->sd->flags & SD_NUMA)
5679 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Michael Neuling532cb4c2010-06-08 14:57:02 +10005680}
5681
Michael Neuling532cb4c2010-06-08 14:57:02 +10005682/**
5683 * check_asym_packing - Check to see if the group is packed into the
5684 * sched doman.
5685 *
5686 * This is primarily intended to used at the sibling level. Some
5687 * cores like POWER7 prefer to use lower numbered SMT threads. In the
5688 * case of POWER7, it can move to lower SMT modes only when higher
5689 * threads are idle. When in lower SMT modes, the threads will
5690 * perform better since they share less core resources. Hence when we
5691 * have idle threads, we want them to be the higher ones.
5692 *
5693 * This packing function is run on idle threads. It checks to see if
5694 * the busiest CPU in this domain (core in the P7 case) has a higher
5695 * CPU number than the packing function is being run on. Here we are
5696 * assuming lower CPU number will be equivalent to lower a SMT thread
5697 * number.
5698 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005699 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10005700 * this CPU. The amount of the imbalance is returned in *imbalance.
5701 *
Randy Dunlapcd968912012-06-08 13:18:33 -07005702 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10005703 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10005704 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005705static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10005706{
5707 int busiest_cpu;
5708
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005709 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10005710 return 0;
5711
5712 if (!sds->busiest)
5713 return 0;
5714
5715 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005716 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10005717 return 0;
5718
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005719 env->imbalance = DIV_ROUND_CLOSEST(
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005720 sds->busiest_stat.avg_load * sds->busiest_stat.group_power,
5721 SCHED_POWER_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005722
Michael Neuling532cb4c2010-06-08 14:57:02 +10005723 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005724}
5725
5726/**
5727 * fix_small_imbalance - Calculate the minor imbalance that exists
5728 * amongst the groups of a sched_domain, during
5729 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07005730 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005731 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005732 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005733static inline
5734void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005735{
5736 unsigned long tmp, pwr_now = 0, pwr_move = 0;
5737 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005738 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005739 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005740
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005741 local = &sds->local_stat;
5742 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005743
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005744 if (!local->sum_nr_running)
5745 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
5746 else if (busiest->load_per_task > local->load_per_task)
5747 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005748
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005749 scaled_busy_load_per_task =
5750 (busiest->load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005751 busiest->group_power;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005752
Vladimir Davydov3029ede2013-09-15 17:49:14 +04005753 if (busiest->avg_load + scaled_busy_load_per_task >=
5754 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005755 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005756 return;
5757 }
5758
5759 /*
5760 * OK, we don't have enough imbalance to justify moving tasks,
5761 * however we may be able to increase total CPU power used by
5762 * moving them.
5763 */
5764
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005765 pwr_now += busiest->group_power *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005766 min(busiest->load_per_task, busiest->avg_load);
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005767 pwr_now += local->group_power *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005768 min(local->load_per_task, local->avg_load);
Nikhil Rao1399fa72011-05-18 10:09:39 -07005769 pwr_now /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005770
5771 /* Amount of load we'd subtract */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005772 tmp = (busiest->load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005773 busiest->group_power;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005774 if (busiest->avg_load > tmp) {
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005775 pwr_move += busiest->group_power *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005776 min(busiest->load_per_task,
5777 busiest->avg_load - tmp);
5778 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005779
5780 /* Amount of load we'd add */
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005781 if (busiest->avg_load * busiest->group_power <
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005782 busiest->load_per_task * SCHED_POWER_SCALE) {
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005783 tmp = (busiest->avg_load * busiest->group_power) /
5784 local->group_power;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005785 } else {
5786 tmp = (busiest->load_per_task * SCHED_POWER_SCALE) /
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005787 local->group_power;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005788 }
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005789 pwr_move += local->group_power *
5790 min(local->load_per_task, local->avg_load + tmp);
Nikhil Rao1399fa72011-05-18 10:09:39 -07005791 pwr_move /= SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005792
5793 /* Move if we gain throughput */
5794 if (pwr_move > pwr_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005795 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005796}
5797
5798/**
5799 * calculate_imbalance - Calculate the amount of imbalance present within the
5800 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005801 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005802 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005803 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005804static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005805{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005806 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005807 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005808
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005809 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005810 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005811
5812 if (busiest->group_imb) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005813 /*
5814 * In the group_imb case we cannot rely on group-wide averages
5815 * to ensure cpu-load equilibrium, look at wider averages. XXX
5816 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005817 busiest->load_per_task =
5818 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005819 }
5820
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005821 /*
5822 * In the presence of smp nice balancing, certain scenarios can have
5823 * max load less than avg load(as we skip the groups at or below
5824 * its cpu_power, while calculating max_load..)
5825 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04005826 if (busiest->avg_load <= sds->avg_load ||
5827 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005828 env->imbalance = 0;
5829 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005830 }
5831
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005832 if (!busiest->group_imb) {
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005833 /*
5834 * Don't want to pull so many tasks that a group would go idle.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005835 * Except of course for the group_imb case, since then we might
5836 * have to drop below capacity to reach cpu-load equilibrium.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005837 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005838 load_above_capacity =
5839 (busiest->sum_nr_running - busiest->group_capacity);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005840
Nikhil Rao1399fa72011-05-18 10:09:39 -07005841 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005842 load_above_capacity /= busiest->group_power;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005843 }
5844
5845 /*
5846 * We're trying to get all the cpus to the average_load, so we don't
5847 * want to push ourselves above the average load, nor do we wish to
5848 * reduce the max loaded cpu below the average load. At the same time,
5849 * we also don't want to reduce the group load below the group capacity
5850 * (so that we can implement power-savings policies etc). Thus we look
5851 * for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08005852 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005853 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005854
5855 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005856 env->imbalance = min(
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02005857 max_pull * busiest->group_power,
5858 (sds->avg_load - local->avg_load) * local->group_power
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005859 ) / SCHED_POWER_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005860
5861 /*
5862 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005863 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005864 * a think about bumping its value to force at least one task to be
5865 * moved
5866 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005867 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005868 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005869}
Nikhil Raofab47622010-10-15 13:12:29 -07005870
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005871/******* find_busiest_group() helpers end here *********************/
5872
5873/**
5874 * find_busiest_group - Returns the busiest group within the sched_domain
5875 * if there is an imbalance. If there isn't an imbalance, and
5876 * the user has opted for power-savings, it returns a group whose
5877 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
5878 * such a group exists.
5879 *
5880 * Also calculates the amount of weighted load which should be moved
5881 * to restore balance.
5882 *
Randy Dunlapcd968912012-06-08 13:18:33 -07005883 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005884 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005885 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005886 * - If no imbalance and user has opted for power-savings balance,
5887 * return the least loaded group whose CPUs can be
5888 * put to idle by rebalancing its tasks onto our group.
5889 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005890static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005891{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005892 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005893 struct sd_lb_stats sds;
5894
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005895 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005896
5897 /*
5898 * Compute the various statistics relavent for load balancing at
5899 * this level.
5900 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09005901 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005902 local = &sds.local_stat;
5903 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005904
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005905 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
5906 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10005907 return sds.busiest;
5908
Peter Zijlstracc57aa82011-02-21 18:55:32 +01005909 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005910 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005911 goto out_balanced;
5912
Nikhil Rao1399fa72011-05-18 10:09:39 -07005913 sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
Ken Chenb0432d82011-04-07 17:23:22 -07005914
Peter Zijlstra866ab432011-02-21 18:56:47 +01005915 /*
5916 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005917 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01005918 * isn't true due to cpus_allowed constraints and the like.
5919 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005920 if (busiest->group_imb)
Peter Zijlstra866ab432011-02-21 18:56:47 +01005921 goto force_balance;
5922
Peter Zijlstracc57aa82011-02-21 18:55:32 +01005923 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005924 if (env->idle == CPU_NEWLY_IDLE && local->group_has_capacity &&
5925 !busiest->group_has_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07005926 goto force_balance;
5927
Peter Zijlstracc57aa82011-02-21 18:55:32 +01005928 /*
5929 * If the local group is more busy than the selected busiest group
5930 * don't try and pull any tasks.
5931 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005932 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005933 goto out_balanced;
5934
Peter Zijlstracc57aa82011-02-21 18:55:32 +01005935 /*
5936 * Don't pull any tasks if this group is already above the domain
5937 * average load.
5938 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005939 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005940 goto out_balanced;
5941
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005942 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07005943 /*
5944 * This cpu is idle. If the busiest group load doesn't
5945 * have more tasks than the number of available cpu's and
5946 * there is no imbalance between this and busiest group
5947 * wrt to idle cpu's, it is balanced.
5948 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005949 if ((local->idle_cpus < busiest->idle_cpus) &&
5950 busiest->sum_nr_running <= busiest->group_weight)
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07005951 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005952 } else {
5953 /*
5954 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
5955 * imbalance_pct to be conservative.
5956 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005957 if (100 * busiest->avg_load <=
5958 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01005959 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07005960 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005961
Nikhil Raofab47622010-10-15 13:12:29 -07005962force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005963 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005964 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005965 return sds.busiest;
5966
5967out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005968 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005969 return NULL;
5970}
5971
5972/*
5973 * find_busiest_queue - find the busiest runqueue among the cpus in group.
5974 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005975static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08005976 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005977{
5978 struct rq *busiest = NULL, *rq;
Joonsoo Kim95a79b82013-08-06 17:36:41 +09005979 unsigned long busiest_load = 0, busiest_power = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005980 int i;
5981
Peter Zijlstra6906a402013-08-19 15:20:21 +02005982 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005983 unsigned long power, capacity, wl;
5984 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005985
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005986 rq = cpu_rq(i);
5987 rt = fbq_classify_rq(rq);
5988
5989 /*
5990 * We classify groups/runqueues into three groups:
5991 * - regular: there are !numa tasks
5992 * - remote: there are numa tasks that run on the 'wrong' node
5993 * - all: there is no distinction
5994 *
5995 * In order to avoid migrating ideally placed numa tasks,
5996 * ignore those when there's better options.
5997 *
5998 * If we ignore the actual busiest queue to migrate another
5999 * task, the next balance pass can still reduce the busiest
6000 * queue by moving tasks around inside the node.
6001 *
6002 * If we cannot move enough load due to this classification
6003 * the next pass will adjust the group classification and
6004 * allow migration of more tasks.
6005 *
6006 * Both cases only affect the total convergence complexity.
6007 */
6008 if (rt > env->fbq_type)
6009 continue;
6010
6011 power = power_of(i);
6012 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10006013 if (!capacity)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006014 capacity = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10006015
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006016 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006017
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006018 /*
6019 * When comparing with imbalance, use weighted_cpuload()
6020 * which is not scaled with the cpu power.
6021 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006022 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006023 continue;
6024
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006025 /*
6026 * For the load comparisons with the other cpu's, consider
6027 * the weighted_cpuload() scaled with the cpu power, so that
6028 * the load can be moved away from the cpu that is potentially
6029 * running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09006030 *
6031 * Thus we're looking for max(wl_i / power_i), crosswise
6032 * multiplication to rid ourselves of the division works out
6033 * to: wl_i * power_j > wl_j * power_i; where j is our
6034 * previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006035 */
Joonsoo Kim95a79b82013-08-06 17:36:41 +09006036 if (wl * busiest_power > busiest_load * power) {
6037 busiest_load = wl;
6038 busiest_power = power;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006039 busiest = rq;
6040 }
6041 }
6042
6043 return busiest;
6044}
6045
6046/*
6047 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
6048 * so long as it is large enough.
6049 */
6050#define MAX_PINNED_INTERVAL 512
6051
6052/* Working cpumask for load_balance and load_balance_newidle. */
Joonsoo Kime6252c32013-04-23 17:27:41 +09006053DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006054
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006055static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01006056{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006057 struct sched_domain *sd = env->sd;
6058
6059 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10006060
6061 /*
6062 * ASYM_PACKING needs to force migrate tasks from busy but
6063 * higher numbered CPUs in order to pack all tasks in the
6064 * lowest numbered CPUs.
6065 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006066 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006067 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01006068 }
6069
6070 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
6071}
6072
Tejun Heo969c7922010-05-06 18:49:21 +02006073static int active_load_balance_cpu_stop(void *data);
6074
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006075static int should_we_balance(struct lb_env *env)
6076{
6077 struct sched_group *sg = env->sd->groups;
6078 struct cpumask *sg_cpus, *sg_mask;
6079 int cpu, balance_cpu = -1;
6080
6081 /*
6082 * In the newly idle case, we will allow all the cpu's
6083 * to do the newly idle load balance.
6084 */
6085 if (env->idle == CPU_NEWLY_IDLE)
6086 return 1;
6087
6088 sg_cpus = sched_group_cpus(sg);
6089 sg_mask = sched_group_mask(sg);
6090 /* Try to find first idle cpu */
6091 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
6092 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
6093 continue;
6094
6095 balance_cpu = cpu;
6096 break;
6097 }
6098
6099 if (balance_cpu == -1)
6100 balance_cpu = group_balance_cpu(sg);
6101
6102 /*
6103 * First idle cpu or the first cpu(busiest) in this sched group
6104 * is eligible for doing load balancing at this and above domains.
6105 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09006106 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006107}
6108
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006109/*
6110 * Check this_cpu to ensure it is balanced within domain. Attempt to move
6111 * tasks if there is an imbalance.
6112 */
6113static int load_balance(int this_cpu, struct rq *this_rq,
6114 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006115 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006116{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306117 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02006118 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006119 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006120 struct rq *busiest;
6121 unsigned long flags;
Joonsoo Kime6252c32013-04-23 17:27:41 +09006122 struct cpumask *cpus = __get_cpu_var(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006123
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006124 struct lb_env env = {
6125 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006126 .dst_cpu = this_cpu,
6127 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306128 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006129 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02006130 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08006131 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006132 .fbq_type = all,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006133 };
6134
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006135 /*
6136 * For NEWLY_IDLE load_balancing, we don't need to consider
6137 * other cpus in our group
6138 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006139 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006140 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006141
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006142 cpumask_copy(cpus, cpu_active_mask);
6143
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006144 schedstat_inc(sd, lb_count[idle]);
6145
6146redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006147 if (!should_we_balance(&env)) {
6148 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006149 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006150 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006151
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006152 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006153 if (!group) {
6154 schedstat_inc(sd, lb_nobusyg[idle]);
6155 goto out_balanced;
6156 }
6157
Michael Wangb94031302012-07-12 16:10:13 +08006158 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006159 if (!busiest) {
6160 schedstat_inc(sd, lb_nobusyq[idle]);
6161 goto out_balanced;
6162 }
6163
Michael Wang78feefc2012-08-06 16:41:59 +08006164 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006165
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006166 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006167
6168 ld_moved = 0;
6169 if (busiest->nr_running > 1) {
6170 /*
6171 * Attempt to move tasks. If find_busiest_group has found
6172 * an imbalance but busiest->nr_running <= 1, the group is
6173 * still unbalanced. ld_moved simply stays zero, so it is
6174 * correctly treated as an imbalance.
6175 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006176 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02006177 env.src_cpu = busiest->cpu;
6178 env.src_rq = busiest;
6179 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006180
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006181more_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006182 local_irq_save(flags);
Michael Wang78feefc2012-08-06 16:41:59 +08006183 double_rq_lock(env.dst_rq, busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306184
6185 /*
6186 * cur_ld_moved - load moved in current iteration
6187 * ld_moved - cumulative load moved across iterations
6188 */
6189 cur_ld_moved = move_tasks(&env);
6190 ld_moved += cur_ld_moved;
Michael Wang78feefc2012-08-06 16:41:59 +08006191 double_rq_unlock(env.dst_rq, busiest);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006192 local_irq_restore(flags);
6193
6194 /*
6195 * some other cpu did the load balance for us.
6196 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306197 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
6198 resched_cpu(env.dst_cpu);
6199
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09006200 if (env.flags & LBF_NEED_BREAK) {
6201 env.flags &= ~LBF_NEED_BREAK;
6202 goto more_balance;
6203 }
6204
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306205 /*
6206 * Revisit (affine) tasks on src_cpu that couldn't be moved to
6207 * us and move them to an alternate dst_cpu in our sched_group
6208 * where they can run. The upper limit on how many times we
6209 * iterate on same src_cpu is dependent on number of cpus in our
6210 * sched_group.
6211 *
6212 * This changes load balance semantics a bit on who can move
6213 * load to a given_cpu. In addition to the given_cpu itself
6214 * (or a ilb_cpu acting on its behalf where given_cpu is
6215 * nohz-idle), we now have balance_cpu in a position to move
6216 * load to given_cpu. In rare situations, this may cause
6217 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
6218 * _independently_ and at _same_ time to move some load to
6219 * given_cpu) causing exceess load to be moved to given_cpu.
6220 * This however should not happen so much in practice and
6221 * moreover subsequent load balance cycles should correct the
6222 * excess load moved.
6223 */
Peter Zijlstra62633222013-08-19 12:41:09 +02006224 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306225
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04006226 /* Prevent to re-select dst_cpu via env's cpus */
6227 cpumask_clear_cpu(env.dst_cpu, env.cpus);
6228
Michael Wang78feefc2012-08-06 16:41:59 +08006229 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306230 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02006231 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306232 env.loop = 0;
6233 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006234
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306235 /*
6236 * Go back to "more_balance" rather than "redo" since we
6237 * need to continue with same src_cpu.
6238 */
6239 goto more_balance;
6240 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006241
Peter Zijlstra62633222013-08-19 12:41:09 +02006242 /*
6243 * We failed to reach balance because of affinity.
6244 */
6245 if (sd_parent) {
6246 int *group_imbalance = &sd_parent->groups->sgp->imbalance;
6247
6248 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) {
6249 *group_imbalance = 1;
6250 } else if (*group_imbalance)
6251 *group_imbalance = 0;
6252 }
6253
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006254 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006255 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006256 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05306257 if (!cpumask_empty(cpus)) {
6258 env.loop = 0;
6259 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006260 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05306261 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006262 goto out_balanced;
6263 }
6264 }
6265
6266 if (!ld_moved) {
6267 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07006268 /*
6269 * Increment the failure counter only on periodic balance.
6270 * We do not want newidle balance, which can be very
6271 * frequent, pollute the failure counter causing
6272 * excessive cache_hot migrations and active balances.
6273 */
6274 if (idle != CPU_NEWLY_IDLE)
6275 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006276
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006277 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006278 raw_spin_lock_irqsave(&busiest->lock, flags);
6279
Tejun Heo969c7922010-05-06 18:49:21 +02006280 /* don't kick the active_load_balance_cpu_stop,
6281 * if the curr task on busiest cpu can't be
6282 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006283 */
6284 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006285 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006286 raw_spin_unlock_irqrestore(&busiest->lock,
6287 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006288 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006289 goto out_one_pinned;
6290 }
6291
Tejun Heo969c7922010-05-06 18:49:21 +02006292 /*
6293 * ->active_balance synchronizes accesses to
6294 * ->active_balance_work. Once set, it's cleared
6295 * only after active load balance is finished.
6296 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006297 if (!busiest->active_balance) {
6298 busiest->active_balance = 1;
6299 busiest->push_cpu = this_cpu;
6300 active_balance = 1;
6301 }
6302 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02006303
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006304 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02006305 stop_one_cpu_nowait(cpu_of(busiest),
6306 active_load_balance_cpu_stop, busiest,
6307 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006308 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006309
6310 /*
6311 * We've kicked active balancing, reset the failure
6312 * counter.
6313 */
6314 sd->nr_balance_failed = sd->cache_nice_tries+1;
6315 }
6316 } else
6317 sd->nr_balance_failed = 0;
6318
6319 if (likely(!active_balance)) {
6320 /* We were unbalanced, so reset the balancing interval */
6321 sd->balance_interval = sd->min_interval;
6322 } else {
6323 /*
6324 * If we've begun active balancing, start to back off. This
6325 * case may not be covered by the all_pinned logic if there
6326 * is only 1 task on the busy runqueue (because we don't call
6327 * move_tasks).
6328 */
6329 if (sd->balance_interval < sd->max_interval)
6330 sd->balance_interval *= 2;
6331 }
6332
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006333 goto out;
6334
6335out_balanced:
6336 schedstat_inc(sd, lb_balanced[idle]);
6337
6338 sd->nr_balance_failed = 0;
6339
6340out_one_pinned:
6341 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006342 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02006343 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006344 (sd->balance_interval < sd->max_interval))
6345 sd->balance_interval *= 2;
6346
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08006347 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006348out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006349 return ld_moved;
6350}
6351
6352/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006353 * idle_balance is called by schedule() if this_cpu is about to become
6354 * idle. Attempts to pull tasks from other CPUs.
6355 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02006356void idle_balance(int this_cpu, struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006357{
6358 struct sched_domain *sd;
6359 int pulled_task = 0;
6360 unsigned long next_balance = jiffies + HZ;
Jason Low9bd721c2013-09-13 11:26:52 -07006361 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006362
Frederic Weisbecker78becc22013-04-12 01:51:02 +02006363 this_rq->idle_stamp = rq_clock(this_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006364
6365 if (this_rq->avg_idle < sysctl_sched_migration_cost)
6366 return;
6367
Peter Zijlstraf492e122009-12-23 15:29:42 +01006368 /*
6369 * Drop the rq->lock, but keep IRQ/preempt disabled.
6370 */
6371 raw_spin_unlock(&this_rq->lock);
6372
Paul Turner48a16752012-10-04 13:18:31 +02006373 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006374 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006375 for_each_domain(this_cpu, sd) {
6376 unsigned long interval;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006377 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07006378 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006379
6380 if (!(sd->flags & SD_LOAD_BALANCE))
6381 continue;
6382
Jason Low9bd721c2013-09-13 11:26:52 -07006383 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost)
6384 break;
6385
Peter Zijlstraf492e122009-12-23 15:29:42 +01006386 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07006387 t0 = sched_clock_cpu(this_cpu);
6388
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006389 /* If we've pulled tasks over stop searching: */
Peter Zijlstraf492e122009-12-23 15:29:42 +01006390 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006391 sd, CPU_NEWLY_IDLE,
6392 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07006393
6394 domain_cost = sched_clock_cpu(this_cpu) - t0;
6395 if (domain_cost > sd->max_newidle_lb_cost)
6396 sd->max_newidle_lb_cost = domain_cost;
6397
6398 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01006399 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006400
6401 interval = msecs_to_jiffies(sd->balance_interval);
6402 if (time_after(next_balance, sd->last_balance + interval))
6403 next_balance = sd->last_balance + interval;
Nikhil Raod5ad1402010-11-17 11:42:04 -08006404 if (pulled_task) {
6405 this_rq->idle_stamp = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006406 break;
Nikhil Raod5ad1402010-11-17 11:42:04 -08006407 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006408 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006409 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01006410
6411 raw_spin_lock(&this_rq->lock);
6412
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006413 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
6414 /*
6415 * We are going idle. next_balance may be set based on
6416 * a busy processor. So reset next_balance.
6417 */
6418 this_rq->next_balance = next_balance;
6419 }
Jason Low9bd721c2013-09-13 11:26:52 -07006420
6421 if (curr_cost > this_rq->max_idle_balance_cost)
6422 this_rq->max_idle_balance_cost = curr_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006423}
6424
6425/*
Tejun Heo969c7922010-05-06 18:49:21 +02006426 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
6427 * running tasks off the busiest CPU onto idle CPUs. It requires at
6428 * least 1 task to be running on each physical CPU where possible, and
6429 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006430 */
Tejun Heo969c7922010-05-06 18:49:21 +02006431static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006432{
Tejun Heo969c7922010-05-06 18:49:21 +02006433 struct rq *busiest_rq = data;
6434 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006435 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02006436 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006437 struct sched_domain *sd;
Tejun Heo969c7922010-05-06 18:49:21 +02006438
6439 raw_spin_lock_irq(&busiest_rq->lock);
6440
6441 /* make sure the requested cpu hasn't gone down in the meantime */
6442 if (unlikely(busiest_cpu != smp_processor_id() ||
6443 !busiest_rq->active_balance))
6444 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006445
6446 /* Is there any task to move? */
6447 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02006448 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006449
6450 /*
6451 * This condition is "impossible", if it occurs
6452 * we need to fix it. Originally reported by
6453 * Bjorn Helgaas on a 128-cpu setup.
6454 */
6455 BUG_ON(busiest_rq == target_rq);
6456
6457 /* move a task from busiest_rq to target_rq */
6458 double_lock_balance(busiest_rq, target_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006459
6460 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006461 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006462 for_each_domain(target_cpu, sd) {
6463 if ((sd->flags & SD_LOAD_BALANCE) &&
6464 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
6465 break;
6466 }
6467
6468 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006469 struct lb_env env = {
6470 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006471 .dst_cpu = target_cpu,
6472 .dst_rq = target_rq,
6473 .src_cpu = busiest_rq->cpu,
6474 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006475 .idle = CPU_IDLE,
6476 };
6477
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006478 schedstat_inc(sd, alb_count);
6479
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006480 if (move_one_task(&env))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006481 schedstat_inc(sd, alb_pushed);
6482 else
6483 schedstat_inc(sd, alb_failed);
6484 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006485 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006486 double_unlock_balance(busiest_rq, target_rq);
Tejun Heo969c7922010-05-06 18:49:21 +02006487out_unlock:
6488 busiest_rq->active_balance = 0;
6489 raw_spin_unlock_irq(&busiest_rq->lock);
6490 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006491}
6492
Frederic Weisbecker3451d022011-08-10 23:21:01 +02006493#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006494/*
6495 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006496 * - When one of the busy CPUs notice that there may be an idle rebalancing
6497 * needed, they will kick the idle load balancer, which then does idle
6498 * load balancing for all the idle CPUs.
6499 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006500static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006501 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006502 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006503 unsigned long next_balance; /* in jiffy units */
6504} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006505
Daniel Lezcano3dd03372014-01-06 12:34:41 +01006506static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006507{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006508 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006509
Suresh Siddha786d6dc72011-12-01 17:07:35 -08006510 if (ilb < nr_cpu_ids && idle_cpu(ilb))
6511 return ilb;
6512
6513 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006514}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006515
6516/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006517 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
6518 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
6519 * CPU (if there is one).
6520 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01006521static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006522{
6523 int ilb_cpu;
6524
6525 nohz.next_balance++;
6526
Daniel Lezcano3dd03372014-01-06 12:34:41 +01006527 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006528
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006529 if (ilb_cpu >= nr_cpu_ids)
6530 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006531
Suresh Siddhacd490c52011-12-06 11:26:34 -08006532 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08006533 return;
6534 /*
6535 * Use smp_send_reschedule() instead of resched_cpu().
6536 * This way we generate a sched IPI on the target cpu which
6537 * is idle. And the softirq performing nohz idle load balance
6538 * will be run before returning from the IPI.
6539 */
6540 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006541 return;
6542}
6543
Alex Shic1cc0172012-09-10 15:10:58 +08006544static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08006545{
6546 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
6547 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
6548 atomic_dec(&nohz.nr_cpus);
6549 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
6550 }
6551}
6552
Suresh Siddha69e1e812011-12-01 17:07:33 -08006553static inline void set_cpu_sd_state_busy(void)
6554{
6555 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306556 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08006557
Suresh Siddha69e1e812011-12-01 17:07:33 -08006558 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306559 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02006560
6561 if (!sd || !sd->nohz_idle)
6562 goto unlock;
6563 sd->nohz_idle = 0;
6564
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306565 atomic_inc(&sd->groups->sgp->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02006566unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08006567 rcu_read_unlock();
6568}
6569
6570void set_cpu_sd_state_idle(void)
6571{
6572 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306573 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08006574
Suresh Siddha69e1e812011-12-01 17:07:33 -08006575 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306576 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02006577
6578 if (!sd || sd->nohz_idle)
6579 goto unlock;
6580 sd->nohz_idle = 1;
6581
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306582 atomic_dec(&sd->groups->sgp->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02006583unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08006584 rcu_read_unlock();
6585}
6586
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006587/*
Alex Shic1cc0172012-09-10 15:10:58 +08006588 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006589 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006590 */
Alex Shic1cc0172012-09-10 15:10:58 +08006591void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006592{
Suresh Siddha71325962012-01-19 18:28:57 -08006593 /*
6594 * If this cpu is going down, then nothing needs to be done.
6595 */
6596 if (!cpu_active(cpu))
6597 return;
6598
Alex Shic1cc0172012-09-10 15:10:58 +08006599 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
6600 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006601
Alex Shic1cc0172012-09-10 15:10:58 +08006602 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
6603 atomic_inc(&nohz.nr_cpus);
6604 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006605}
Suresh Siddha71325962012-01-19 18:28:57 -08006606
Paul Gortmaker0db06282013-06-19 14:53:51 -04006607static int sched_ilb_notifier(struct notifier_block *nfb,
Suresh Siddha71325962012-01-19 18:28:57 -08006608 unsigned long action, void *hcpu)
6609{
6610 switch (action & ~CPU_TASKS_FROZEN) {
6611 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08006612 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08006613 return NOTIFY_OK;
6614 default:
6615 return NOTIFY_DONE;
6616 }
6617}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006618#endif
6619
6620static DEFINE_SPINLOCK(balancing);
6621
Peter Zijlstra49c022e2011-04-05 10:14:25 +02006622/*
6623 * Scale the max load_balance interval with the number of CPUs in the system.
6624 * This trades load-balance latency on larger machines for less cross talk.
6625 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02006626void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02006627{
6628 max_load_balance_interval = HZ*num_online_cpus()/10;
6629}
6630
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006631/*
6632 * It checks each scheduling domain to see if it is due to be balanced,
6633 * and initiates a balancing operation if so.
6634 *
Libinb9b08532013-04-01 19:14:01 +08006635 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006636 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01006637static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006638{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006639 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01006640 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006641 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02006642 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006643 /* Earliest time when we have to do rebalance again */
6644 unsigned long next_balance = jiffies + 60*HZ;
6645 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07006646 int need_serialize, need_decay = 0;
6647 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006648
Paul Turner48a16752012-10-04 13:18:31 +02006649 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08006650
Peter Zijlstradce840a2011-04-07 14:09:50 +02006651 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006652 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07006653 /*
6654 * Decay the newidle max times here because this is a regular
6655 * visit to all the domains. Decay ~1% per second.
6656 */
6657 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
6658 sd->max_newidle_lb_cost =
6659 (sd->max_newidle_lb_cost * 253) / 256;
6660 sd->next_decay_max_lb_cost = jiffies + HZ;
6661 need_decay = 1;
6662 }
6663 max_cost += sd->max_newidle_lb_cost;
6664
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006665 if (!(sd->flags & SD_LOAD_BALANCE))
6666 continue;
6667
Jason Lowf48627e2013-09-13 11:26:53 -07006668 /*
6669 * Stop the load balance at this level. There is another
6670 * CPU in our sched group which is doing load balancing more
6671 * actively.
6672 */
6673 if (!continue_balancing) {
6674 if (need_decay)
6675 continue;
6676 break;
6677 }
6678
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006679 interval = sd->balance_interval;
6680 if (idle != CPU_IDLE)
6681 interval *= sd->busy_factor;
6682
6683 /* scale ms to jiffies */
6684 interval = msecs_to_jiffies(interval);
Peter Zijlstra49c022e2011-04-05 10:14:25 +02006685 interval = clamp(interval, 1UL, max_load_balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006686
6687 need_serialize = sd->flags & SD_SERIALIZE;
6688
6689 if (need_serialize) {
6690 if (!spin_trylock(&balancing))
6691 goto out;
6692 }
6693
6694 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006695 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006696 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02006697 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09006698 * env->dst_cpu, so we can't know our idle
6699 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006700 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09006701 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006702 }
6703 sd->last_balance = jiffies;
6704 }
6705 if (need_serialize)
6706 spin_unlock(&balancing);
6707out:
6708 if (time_after(next_balance, sd->last_balance + interval)) {
6709 next_balance = sd->last_balance + interval;
6710 update_next_balance = 1;
6711 }
Jason Lowf48627e2013-09-13 11:26:53 -07006712 }
6713 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006714 /*
Jason Lowf48627e2013-09-13 11:26:53 -07006715 * Ensure the rq-wide value also decays but keep it at a
6716 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006717 */
Jason Lowf48627e2013-09-13 11:26:53 -07006718 rq->max_idle_balance_cost =
6719 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006720 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006721 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006722
6723 /*
6724 * next_balance will be updated only when there is a need.
6725 * When the cpu is attached to null domain for ex, it will not be
6726 * updated.
6727 */
6728 if (likely(update_next_balance))
6729 rq->next_balance = next_balance;
6730}
6731
Frederic Weisbecker3451d022011-08-10 23:21:01 +02006732#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006733/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02006734 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006735 * rebalancing for all the cpus for whom scheduler ticks are stopped.
6736 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01006737static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006738{
Daniel Lezcano208cb162014-01-06 12:34:44 +01006739 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006740 struct rq *rq;
6741 int balance_cpu;
6742
Suresh Siddha1c792db2011-12-01 17:07:32 -08006743 if (idle != CPU_IDLE ||
6744 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
6745 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006746
6747 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08006748 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006749 continue;
6750
6751 /*
6752 * If this cpu gets work to do, stop the load balancing
6753 * work being done for other cpus. Next load
6754 * balancing owner will pick it up.
6755 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08006756 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006757 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006758
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02006759 rq = cpu_rq(balance_cpu);
6760
6761 raw_spin_lock_irq(&rq->lock);
6762 update_rq_clock(rq);
6763 update_idle_cpu_load(rq);
6764 raw_spin_unlock_irq(&rq->lock);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006765
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01006766 rebalance_domains(rq, CPU_IDLE);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006767
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006768 if (time_after(this_rq->next_balance, rq->next_balance))
6769 this_rq->next_balance = rq->next_balance;
6770 }
6771 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08006772end:
6773 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006774}
6775
6776/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006777 * Current heuristic for kicking the idle load balancer in the presence
6778 * of an idle cpu is the system.
6779 * - This rq has more than one task.
6780 * - At any scheduler domain level, this cpu's scheduler group has multiple
6781 * busy cpu's exceeding the group's power.
6782 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
6783 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006784 */
Daniel Lezcano4a725622014-01-06 12:34:39 +01006785static inline int nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006786{
6787 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006788 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306789 struct sched_group_power *sgp;
Daniel Lezcano4a725622014-01-06 12:34:39 +01006790 int nr_busy, cpu = rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006791
Daniel Lezcano4a725622014-01-06 12:34:39 +01006792 if (unlikely(rq->idle_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006793 return 0;
6794
Suresh Siddha1c792db2011-12-01 17:07:32 -08006795 /*
6796 * We may be recently in ticked or tickless idle mode. At the first
6797 * busy tick after returning from idle, we will update the busy stats.
6798 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08006799 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08006800 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006801
6802 /*
6803 * None are in tickless mode and hence no need for NOHZ idle load
6804 * balancing.
6805 */
6806 if (likely(!atomic_read(&nohz.nr_cpus)))
6807 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08006808
6809 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006810 return 0;
6811
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006812 if (rq->nr_running >= 2)
6813 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006814
Peter Zijlstra067491b2011-12-07 14:32:08 +01006815 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306816 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006817
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306818 if (sd) {
6819 sgp = sd->groups->sgp;
6820 nr_busy = atomic_read(&sgp->nr_busy_cpus);
6821
6822 if (nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01006823 goto need_kick_unlock;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006824 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306825
6826 sd = rcu_dereference(per_cpu(sd_asym, cpu));
6827
6828 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
6829 sched_domain_span(sd)) < cpu))
6830 goto need_kick_unlock;
6831
Peter Zijlstra067491b2011-12-07 14:32:08 +01006832 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006833 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01006834
6835need_kick_unlock:
6836 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08006837need_kick:
6838 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006839}
6840#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01006841static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006842#endif
6843
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006844/*
6845 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006846 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006847 */
6848static void run_rebalance_domains(struct softirq_action *h)
6849{
Daniel Lezcano208cb162014-01-06 12:34:44 +01006850 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07006851 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006852 CPU_IDLE : CPU_NOT_IDLE;
6853
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01006854 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006855
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006856 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006857 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006858 * balancing on behalf of the other idle cpus whose ticks are
6859 * stopped.
6860 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01006861 nohz_idle_balance(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006862}
6863
Daniel Lezcano63f609b2014-01-06 12:34:40 +01006864static inline int on_null_domain(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006865{
Daniel Lezcano63f609b2014-01-06 12:34:40 +01006866 return !rcu_dereference_sched(rq->sd);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006867}
6868
6869/*
6870 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006871 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01006872void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006873{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006874 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01006875 if (unlikely(on_null_domain(rq)))
6876 return;
6877
6878 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006879 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02006880#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01006881 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01006882 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006883#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006884}
6885
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006886static void rq_online_fair(struct rq *rq)
6887{
6888 update_sysctl();
6889}
6890
6891static void rq_offline_fair(struct rq *rq)
6892{
6893 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07006894
6895 /* Ensure any throttled groups are reachable by pick_next_task */
6896 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006897}
6898
Dhaval Giani55e12e52008-06-24 23:39:43 +05306899#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02006900
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006901/*
6902 * scheduler tick hitting a task of our scheduling class:
6903 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006904static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006905{
6906 struct cfs_rq *cfs_rq;
6907 struct sched_entity *se = &curr->se;
6908
6909 for_each_sched_entity(se) {
6910 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006911 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006912 }
Ben Segall18bf2802012-10-04 12:51:20 +02006913
Dave Kleikamp10e84b92013-07-31 13:53:35 -07006914 if (numabalancing_enabled)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02006915 task_tick_numa(rq, curr);
Linus Torvalds3d59eeb2012-12-16 14:33:25 -08006916
Ben Segall18bf2802012-10-04 12:51:20 +02006917 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006918}
6919
6920/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006921 * called on fork with the child task as argument from the parent's context
6922 * - child not yet on the tasklist
6923 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006924 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006925static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006926{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09006927 struct cfs_rq *cfs_rq;
6928 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02006929 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006930 struct rq *rq = this_rq();
6931 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006932
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006933 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006934
Peter Zijlstra861d0342010-08-19 13:31:43 +02006935 update_rq_clock(rq);
6936
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09006937 cfs_rq = task_cfs_rq(current);
6938 curr = cfs_rq->curr;
6939
Daisuke Nishimura6c9a27f2013-09-10 18:16:36 +09006940 /*
6941 * Not only the cpu but also the task_group of the parent might have
6942 * been changed after parent->se.parent,cfs_rq were copied to
6943 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
6944 * of child point to valid ones.
6945 */
6946 rcu_read_lock();
6947 __set_task_cpu(p, this_cpu);
6948 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006949
Ting Yang7109c442007-08-28 12:53:24 +02006950 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006951
Mike Galbraithb5d9d732009-09-08 11:12:28 +02006952 if (curr)
6953 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02006954 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02006955
Peter Zijlstracd29fe62009-11-27 17:32:46 +01006956 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02006957 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02006958 * Upon rescheduling, sched_class::put_prev_task() will place
6959 * 'current' within the tree based on its new key value.
6960 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02006961 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05306962 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02006963 }
6964
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01006965 se->vruntime -= cfs_rq->min_vruntime;
6966
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006967 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006968}
6969
Steven Rostedtcb469842008-01-25 21:08:22 +01006970/*
6971 * Priority of the task has changed. Check to see if we preempt
6972 * the current task.
6973 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01006974static void
6975prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01006976{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01006977 if (!p->se.on_rq)
6978 return;
6979
Steven Rostedtcb469842008-01-25 21:08:22 +01006980 /*
6981 * Reschedule if we are currently running on this runqueue and
6982 * our priority decreased, or if we are not currently running on
6983 * this runqueue and our priority is higher than the current's
6984 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01006985 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01006986 if (p->prio > oldprio)
6987 resched_task(rq->curr);
6988 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02006989 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01006990}
6991
Peter Zijlstrada7a7352011-01-17 17:03:27 +01006992static void switched_from_fair(struct rq *rq, struct task_struct *p)
6993{
6994 struct sched_entity *se = &p->se;
6995 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6996
6997 /*
6998 * Ensure the task's vruntime is normalized, so that when its
6999 * switched back to the fair class the enqueue_entity(.flags=0) will
7000 * do the right thing.
7001 *
7002 * If it was on_rq, then the dequeue_entity(.flags=0) will already
7003 * have normalized the vruntime, if it was !on_rq, then only when
7004 * the task is sleeping will it still have non-normalized vruntime.
7005 */
7006 if (!se->on_rq && p->state != TASK_RUNNING) {
7007 /*
7008 * Fix up our vruntime so that the current sleep doesn't
7009 * cause 'unlimited' sleep bonus.
7010 */
7011 place_entity(cfs_rq, se, 0);
7012 se->vruntime -= cfs_rq->min_vruntime;
7013 }
Paul Turner9ee474f2012-10-04 13:18:30 +02007014
Alex Shi141965c2013-06-26 13:05:39 +08007015#ifdef CONFIG_SMP
Paul Turner9ee474f2012-10-04 13:18:30 +02007016 /*
7017 * Remove our load from contribution when we leave sched_fair
7018 * and ensure we don't carry in an old decay_count if we
7019 * switch back.
7020 */
Kirill Tkhai87e3c8a2013-07-21 04:32:07 +04007021 if (se->avg.decay_count) {
7022 __synchronize_entity_decay(se);
7023 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turner9ee474f2012-10-04 13:18:30 +02007024 }
7025#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007026}
7027
Steven Rostedtcb469842008-01-25 21:08:22 +01007028/*
7029 * We switched to the sched_fair class.
7030 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007031static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01007032{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007033 if (!p->se.on_rq)
7034 return;
7035
Steven Rostedtcb469842008-01-25 21:08:22 +01007036 /*
7037 * We were most likely switched from sched_rt, so
7038 * kick off the schedule if running, otherwise just see
7039 * if we can still preempt the current task.
7040 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007041 if (rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01007042 resched_task(rq->curr);
7043 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02007044 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01007045}
7046
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007047/* Account for a task changing its policy or group.
7048 *
7049 * This routine is mostly called to set cfs_rq->curr field when a task
7050 * migrates between groups/classes.
7051 */
7052static void set_curr_task_fair(struct rq *rq)
7053{
7054 struct sched_entity *se = &rq->curr->se;
7055
Paul Turnerec12cb72011-07-21 09:43:30 -07007056 for_each_sched_entity(se) {
7057 struct cfs_rq *cfs_rq = cfs_rq_of(se);
7058
7059 set_next_entity(cfs_rq, se);
7060 /* ensure bandwidth has been allocated on our new cfs_rq */
7061 account_cfs_rq_runtime(cfs_rq, 0);
7062 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007063}
7064
Peter Zijlstra029632f2011-10-25 10:00:11 +02007065void init_cfs_rq(struct cfs_rq *cfs_rq)
7066{
7067 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007068 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
7069#ifndef CONFIG_64BIT
7070 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
7071#endif
Alex Shi141965c2013-06-26 13:05:39 +08007072#ifdef CONFIG_SMP
Paul Turner9ee474f2012-10-04 13:18:30 +02007073 atomic64_set(&cfs_rq->decay_counter, 1);
Alex Shi25099402013-06-20 10:18:55 +08007074 atomic_long_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02007075#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02007076}
7077
Peter Zijlstra810b3812008-02-29 15:21:01 -05007078#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007079static void task_move_group_fair(struct task_struct *p, int on_rq)
Peter Zijlstra810b3812008-02-29 15:21:01 -05007080{
Paul Turneraff3e492012-10-04 13:18:30 +02007081 struct cfs_rq *cfs_rq;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007082 /*
7083 * If the task was not on the rq at the time of this cgroup movement
7084 * it must have been asleep, sleeping tasks keep their ->vruntime
7085 * absolute on their old rq until wakeup (needed for the fair sleeper
7086 * bonus in place_entity()).
7087 *
7088 * If it was on the rq, we've just 'preempted' it, which does convert
7089 * ->vruntime to a relative base.
7090 *
7091 * Make sure both cases convert their relative position when migrating
7092 * to another cgroup's rq. This does somewhat interfere with the
7093 * fair sleeper stuff for the first placement, but who cares.
7094 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007095 /*
7096 * When !on_rq, vruntime of the task has usually NOT been normalized.
7097 * But there are some cases where it has already been normalized:
7098 *
7099 * - Moving a forked child which is waiting for being woken up by
7100 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09007101 * - Moving a task which has been woken up by try_to_wake_up() and
7102 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007103 *
7104 * To prevent boost or penalty in the new cfs_rq caused by delta
7105 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
7106 */
Daisuke Nishimura62af3782011-12-15 14:37:41 +09007107 if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007108 on_rq = 1;
7109
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01007110 if (!on_rq)
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007111 p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
7112 set_task_rq(p, task_cpu(p));
Paul Turneraff3e492012-10-04 13:18:30 +02007113 if (!on_rq) {
7114 cfs_rq = cfs_rq_of(&p->se);
7115 p->se.vruntime += cfs_rq->min_vruntime;
7116#ifdef CONFIG_SMP
7117 /*
7118 * migrate_task_rq_fair() will have removed our previous
7119 * contribution, but we must synchronize for ongoing future
7120 * decay.
7121 */
7122 p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
7123 cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
7124#endif
7125 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05007126}
Peter Zijlstra029632f2011-10-25 10:00:11 +02007127
7128void free_fair_sched_group(struct task_group *tg)
7129{
7130 int i;
7131
7132 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
7133
7134 for_each_possible_cpu(i) {
7135 if (tg->cfs_rq)
7136 kfree(tg->cfs_rq[i]);
7137 if (tg->se)
7138 kfree(tg->se[i]);
7139 }
7140
7141 kfree(tg->cfs_rq);
7142 kfree(tg->se);
7143}
7144
7145int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7146{
7147 struct cfs_rq *cfs_rq;
7148 struct sched_entity *se;
7149 int i;
7150
7151 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
7152 if (!tg->cfs_rq)
7153 goto err;
7154 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
7155 if (!tg->se)
7156 goto err;
7157
7158 tg->shares = NICE_0_LOAD;
7159
7160 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
7161
7162 for_each_possible_cpu(i) {
7163 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
7164 GFP_KERNEL, cpu_to_node(i));
7165 if (!cfs_rq)
7166 goto err;
7167
7168 se = kzalloc_node(sizeof(struct sched_entity),
7169 GFP_KERNEL, cpu_to_node(i));
7170 if (!se)
7171 goto err_free_rq;
7172
7173 init_cfs_rq(cfs_rq);
7174 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
7175 }
7176
7177 return 1;
7178
7179err_free_rq:
7180 kfree(cfs_rq);
7181err:
7182 return 0;
7183}
7184
7185void unregister_fair_sched_group(struct task_group *tg, int cpu)
7186{
7187 struct rq *rq = cpu_rq(cpu);
7188 unsigned long flags;
7189
7190 /*
7191 * Only empty task groups can be destroyed; so we can speculatively
7192 * check on_list without danger of it being re-added.
7193 */
7194 if (!tg->cfs_rq[cpu]->on_list)
7195 return;
7196
7197 raw_spin_lock_irqsave(&rq->lock, flags);
7198 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
7199 raw_spin_unlock_irqrestore(&rq->lock, flags);
7200}
7201
7202void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7203 struct sched_entity *se, int cpu,
7204 struct sched_entity *parent)
7205{
7206 struct rq *rq = cpu_rq(cpu);
7207
7208 cfs_rq->tg = tg;
7209 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007210 init_cfs_rq_runtime(cfs_rq);
7211
7212 tg->cfs_rq[cpu] = cfs_rq;
7213 tg->se[cpu] = se;
7214
7215 /* se could be NULL for root_task_group */
7216 if (!se)
7217 return;
7218
7219 if (!parent)
7220 se->cfs_rq = &rq->cfs;
7221 else
7222 se->cfs_rq = parent->my_q;
7223
7224 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07007225 /* guarantee group entities always have weight */
7226 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02007227 se->parent = parent;
7228}
7229
7230static DEFINE_MUTEX(shares_mutex);
7231
7232int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7233{
7234 int i;
7235 unsigned long flags;
7236
7237 /*
7238 * We can't change the weight of the root cgroup.
7239 */
7240 if (!tg->se[0])
7241 return -EINVAL;
7242
7243 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
7244
7245 mutex_lock(&shares_mutex);
7246 if (tg->shares == shares)
7247 goto done;
7248
7249 tg->shares = shares;
7250 for_each_possible_cpu(i) {
7251 struct rq *rq = cpu_rq(i);
7252 struct sched_entity *se;
7253
7254 se = tg->se[i];
7255 /* Propagate contribution to hierarchy */
7256 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02007257
7258 /* Possible calls to update_curr() need rq clock */
7259 update_rq_clock(rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08007260 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02007261 update_cfs_shares(group_cfs_rq(se));
7262 raw_spin_unlock_irqrestore(&rq->lock, flags);
7263 }
7264
7265done:
7266 mutex_unlock(&shares_mutex);
7267 return 0;
7268}
7269#else /* CONFIG_FAIR_GROUP_SCHED */
7270
7271void free_fair_sched_group(struct task_group *tg) { }
7272
7273int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7274{
7275 return 1;
7276}
7277
7278void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
7279
7280#endif /* CONFIG_FAIR_GROUP_SCHED */
7281
Peter Zijlstra810b3812008-02-29 15:21:01 -05007282
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07007283static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00007284{
7285 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00007286 unsigned int rr_interval = 0;
7287
7288 /*
7289 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
7290 * idle runqueue:
7291 */
Peter Williams0d721ce2009-09-21 01:31:53 +00007292 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08007293 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00007294
7295 return rr_interval;
7296}
7297
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007298/*
7299 * All the scheduling class methods:
7300 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007301const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02007302 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007303 .enqueue_task = enqueue_task_fair,
7304 .dequeue_task = dequeue_task_fair,
7305 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05007306 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007307
Ingo Molnar2e09bf52007-10-15 17:00:05 +02007308 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007309
7310 .pick_next_task = pick_next_task_fair,
7311 .put_prev_task = put_prev_task_fair,
7312
Peter Williams681f3e62007-10-24 18:23:51 +02007313#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08007314 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02007315 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08007316
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01007317 .rq_online = rq_online_fair,
7318 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01007319
7320 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02007321#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007322
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007323 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007324 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007325 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01007326
7327 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007328 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01007329 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05007330
Peter Williams0d721ce2009-09-21 01:31:53 +00007331 .get_rr_interval = get_rr_interval_fair,
7332
Peter Zijlstra810b3812008-02-29 15:21:01 -05007333#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007334 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05007335#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007336};
7337
7338#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02007339void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007340{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007341 struct cfs_rq *cfs_rq;
7342
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01007343 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02007344 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02007345 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01007346 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007347}
7348#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02007349
7350__init void init_sched_fair_class(void)
7351{
7352#ifdef CONFIG_SMP
7353 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
7354
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007355#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08007356 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007357 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08007358 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02007359#endif
7360#endif /* SMP */
7361
7362}