blob: dfdcbfde2c5bb9e69ffe6281f2ab53c9885296e4 [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>
Nicolas Pitre83a0a962014-09-04 11:32:10 -040026#include <linux/cpuidle.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020027#include <linux/slab.h>
28#include <linux/profile.h>
29#include <linux/interrupt.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020030#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000031#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020032#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020033
34#include <trace/events/sched.h>
35
36#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010037
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020038/*
Peter Zijlstra21805082007-08-25 18:41:53 +020039 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090040 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020041 *
Peter Zijlstra21805082007-08-25 18:41:53 +020042 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * 'timeslice length' - timeslices in CFS are of variable length
44 * and have no persistent notion like in traditional, time-slice
45 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020046 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020047 * (to see the precise effective timeslice length of your workload,
48 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020049 */
Mike Galbraith21406922010-03-11 17:17:15 +010050unsigned int sysctl_sched_latency = 6000000ULL;
51unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020052
53/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010054 * The initial- and re-scaling of tunables is configurable
55 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
56 *
57 * Options are:
58 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
59 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
60 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
61 */
62enum sched_tunable_scaling sysctl_sched_tunable_scaling
63 = SCHED_TUNABLESCALING_LOG;
64
65/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010066 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090067 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010068 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020069unsigned int sysctl_sched_min_granularity = 750000ULL;
70unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010071
72/*
73 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
74 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020075static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010076
77/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020078 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020079 * parent will (try to) run first.
80 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020081unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020082
83/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020084 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020085 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020086 *
87 * This option delays the preemption effects of decoupled workloads
88 * and reduces their over-scheduling. Synchronous workloads will still
89 * have immediate wakeup/sleep latencies.
90 */
Mike Galbraith172e0822009-09-09 15:41:37 +020091unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010092unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020093
Ingo Molnarda84d962007-10-15 17:00:18 +020094const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
95
Paul Turnera7a4f8a2010-11-15 15:47:06 -080096/*
97 * The exponential sliding window over which load is averaged for shares
98 * distribution.
99 * (default: 10msec)
100 */
101unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
102
Paul Turnerec12cb72011-07-21 09:43:30 -0700103#ifdef CONFIG_CFS_BANDWIDTH
104/*
105 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
106 * each time a cfs_rq requests quota.
107 *
108 * Note: in the case that the slice exceeds the runtime remaining (either due
109 * to consumption or the quota being specified to be smaller than the slice)
110 * we will always only issue the remaining available time.
111 *
112 * default: 5 msec, units: microseconds
113 */
114unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
115#endif
116
Paul Gortmaker85276322013-04-19 15:10:50 -0400117static inline void update_load_add(struct load_weight *lw, unsigned long inc)
118{
119 lw->weight += inc;
120 lw->inv_weight = 0;
121}
122
123static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
124{
125 lw->weight -= dec;
126 lw->inv_weight = 0;
127}
128
129static inline void update_load_set(struct load_weight *lw, unsigned long w)
130{
131 lw->weight = w;
132 lw->inv_weight = 0;
133}
134
Peter Zijlstra029632f2011-10-25 10:00:11 +0200135/*
136 * Increase the granularity value when there are more CPUs,
137 * because with more CPUs the 'effective latency' as visible
138 * to users decreases. But the relationship is not linear,
139 * so pick a second-best guess by going with the log2 of the
140 * number of CPUs.
141 *
142 * This idea comes from the SD scheduler of Con Kolivas:
143 */
144static int get_update_sysctl_factor(void)
145{
146 unsigned int cpus = min_t(int, num_online_cpus(), 8);
147 unsigned int factor;
148
149 switch (sysctl_sched_tunable_scaling) {
150 case SCHED_TUNABLESCALING_NONE:
151 factor = 1;
152 break;
153 case SCHED_TUNABLESCALING_LINEAR:
154 factor = cpus;
155 break;
156 case SCHED_TUNABLESCALING_LOG:
157 default:
158 factor = 1 + ilog2(cpus);
159 break;
160 }
161
162 return factor;
163}
164
165static void update_sysctl(void)
166{
167 unsigned int factor = get_update_sysctl_factor();
168
169#define SET_SYSCTL(name) \
170 (sysctl_##name = (factor) * normalized_sysctl_##name)
171 SET_SYSCTL(sched_min_granularity);
172 SET_SYSCTL(sched_latency);
173 SET_SYSCTL(sched_wakeup_granularity);
174#undef SET_SYSCTL
175}
176
177void sched_init_granularity(void)
178{
179 update_sysctl();
180}
181
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100182#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200183#define WMULT_SHIFT 32
184
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100185static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200186{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100187 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200188
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100189 if (likely(lw->inv_weight))
190 return;
191
192 w = scale_load_down(lw->weight);
193
194 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
195 lw->inv_weight = 1;
196 else if (unlikely(!w))
197 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200198 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100199 lw->inv_weight = WMULT_CONST / w;
200}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200201
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100202/*
203 * delta_exec * weight / lw.weight
204 * OR
205 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
206 *
207 * Either weight := NICE_0_LOAD and lw \e prio_to_wmult[], in which case
208 * we're guaranteed shift stays positive because inv_weight is guaranteed to
209 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
210 *
211 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
212 * weight/lw.weight <= 1, and therefore our shift will also be positive.
213 */
214static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
215{
216 u64 fact = scale_load_down(weight);
217 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200218
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100219 __update_inv_weight(lw);
220
221 if (unlikely(fact >> 32)) {
222 while (fact >> 32) {
223 fact >>= 1;
224 shift--;
225 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200226 }
227
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100228 /* hint to use a 32x32->64 mul */
229 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200230
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100231 while (fact >> 32) {
232 fact >>= 1;
233 shift--;
234 }
235
236 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200237}
238
239
240const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200241
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200242/**************************************************************
243 * CFS operations on generic schedulable entities:
244 */
245
246#ifdef CONFIG_FAIR_GROUP_SCHED
247
248/* cpu runqueue to which this cfs_rq is attached */
249static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
250{
251 return cfs_rq->rq;
252}
253
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200254/* An entity is a task if it doesn't "own" a runqueue */
255#define entity_is_task(se) (!se->my_q)
256
Peter Zijlstra8f488942009-07-24 12:25:30 +0200257static inline struct task_struct *task_of(struct sched_entity *se)
258{
259#ifdef CONFIG_SCHED_DEBUG
260 WARN_ON_ONCE(!entity_is_task(se));
261#endif
262 return container_of(se, struct task_struct, se);
263}
264
Peter Zijlstrab7581492008-04-19 19:45:00 +0200265/* Walk up scheduling entities hierarchy */
266#define for_each_sched_entity(se) \
267 for (; se; se = se->parent)
268
269static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
270{
271 return p->se.cfs_rq;
272}
273
274/* runqueue on which this entity is (to be) queued */
275static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
276{
277 return se->cfs_rq;
278}
279
280/* runqueue "owned" by this group */
281static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
282{
283 return grp->my_q;
284}
285
Paul Turneraff3e492012-10-04 13:18:30 +0200286static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
287 int force_update);
Paul Turner9ee474f2012-10-04 13:18:30 +0200288
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800289static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
290{
291 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800292 /*
293 * Ensure we either appear before our parent (if already
294 * enqueued) or force our parent to appear after us when it is
295 * enqueued. The fact that we always enqueue bottom-up
296 * reduces this to two cases.
297 */
298 if (cfs_rq->tg->parent &&
299 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
300 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800301 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800302 } else {
303 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
304 &rq_of(cfs_rq)->leaf_cfs_rq_list);
305 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800306
307 cfs_rq->on_list = 1;
Paul Turner9ee474f2012-10-04 13:18:30 +0200308 /* We should have no load, but we need to update last_decay. */
Paul Turneraff3e492012-10-04 13:18:30 +0200309 update_cfs_rq_blocked_load(cfs_rq, 0);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800310 }
311}
312
313static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
314{
315 if (cfs_rq->on_list) {
316 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
317 cfs_rq->on_list = 0;
318 }
319}
320
Peter Zijlstrab7581492008-04-19 19:45:00 +0200321/* Iterate thr' all leaf cfs_rq's on a runqueue */
322#define for_each_leaf_cfs_rq(rq, cfs_rq) \
323 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
324
325/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100326static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200327is_same_group(struct sched_entity *se, struct sched_entity *pse)
328{
329 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100330 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200331
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100332 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200333}
334
335static inline struct sched_entity *parent_entity(struct sched_entity *se)
336{
337 return se->parent;
338}
339
Peter Zijlstra464b7522008-10-24 11:06:15 +0200340static void
341find_matching_se(struct sched_entity **se, struct sched_entity **pse)
342{
343 int se_depth, pse_depth;
344
345 /*
346 * preemption test can be made between sibling entities who are in the
347 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
348 * both tasks until we find their ancestors who are siblings of common
349 * parent.
350 */
351
352 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100353 se_depth = (*se)->depth;
354 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200355
356 while (se_depth > pse_depth) {
357 se_depth--;
358 *se = parent_entity(*se);
359 }
360
361 while (pse_depth > se_depth) {
362 pse_depth--;
363 *pse = parent_entity(*pse);
364 }
365
366 while (!is_same_group(*se, *pse)) {
367 *se = parent_entity(*se);
368 *pse = parent_entity(*pse);
369 }
370}
371
Peter Zijlstra8f488942009-07-24 12:25:30 +0200372#else /* !CONFIG_FAIR_GROUP_SCHED */
373
374static inline struct task_struct *task_of(struct sched_entity *se)
375{
376 return container_of(se, struct task_struct, se);
377}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200378
379static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
380{
381 return container_of(cfs_rq, struct rq, cfs);
382}
383
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200384#define entity_is_task(se) 1
385
Peter Zijlstrab7581492008-04-19 19:45:00 +0200386#define for_each_sched_entity(se) \
387 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200388
Peter Zijlstrab7581492008-04-19 19:45:00 +0200389static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200390{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200391 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200392}
393
Peter Zijlstrab7581492008-04-19 19:45:00 +0200394static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
395{
396 struct task_struct *p = task_of(se);
397 struct rq *rq = task_rq(p);
398
399 return &rq->cfs;
400}
401
402/* runqueue "owned" by this group */
403static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
404{
405 return NULL;
406}
407
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800408static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
409{
410}
411
412static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
413{
414}
415
Peter Zijlstrab7581492008-04-19 19:45:00 +0200416#define for_each_leaf_cfs_rq(rq, cfs_rq) \
417 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
418
Peter Zijlstrab7581492008-04-19 19:45:00 +0200419static inline struct sched_entity *parent_entity(struct sched_entity *se)
420{
421 return NULL;
422}
423
Peter Zijlstra464b7522008-10-24 11:06:15 +0200424static inline void
425find_matching_se(struct sched_entity **se, struct sched_entity **pse)
426{
427}
428
Peter Zijlstrab7581492008-04-19 19:45:00 +0200429#endif /* CONFIG_FAIR_GROUP_SCHED */
430
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700431static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100432void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200433
434/**************************************************************
435 * Scheduling class tree data structure manipulation methods:
436 */
437
Andrei Epure1bf08232013-03-12 21:12:24 +0200438static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200439{
Andrei Epure1bf08232013-03-12 21:12:24 +0200440 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200441 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200442 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200443
Andrei Epure1bf08232013-03-12 21:12:24 +0200444 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200445}
446
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200447static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200448{
449 s64 delta = (s64)(vruntime - min_vruntime);
450 if (delta < 0)
451 min_vruntime = vruntime;
452
453 return min_vruntime;
454}
455
Fabio Checconi54fdc582009-07-16 12:32:27 +0200456static inline int entity_before(struct sched_entity *a,
457 struct sched_entity *b)
458{
459 return (s64)(a->vruntime - b->vruntime) < 0;
460}
461
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200462static void update_min_vruntime(struct cfs_rq *cfs_rq)
463{
464 u64 vruntime = cfs_rq->min_vruntime;
465
466 if (cfs_rq->curr)
467 vruntime = cfs_rq->curr->vruntime;
468
469 if (cfs_rq->rb_leftmost) {
470 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
471 struct sched_entity,
472 run_node);
473
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100474 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200475 vruntime = se->vruntime;
476 else
477 vruntime = min_vruntime(vruntime, se->vruntime);
478 }
479
Andrei Epure1bf08232013-03-12 21:12:24 +0200480 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200481 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200482#ifndef CONFIG_64BIT
483 smp_wmb();
484 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
485#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200486}
487
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200488/*
489 * Enqueue an entity into the rb-tree:
490 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200491static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200492{
493 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
494 struct rb_node *parent = NULL;
495 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200496 int leftmost = 1;
497
498 /*
499 * Find the right place in the rbtree:
500 */
501 while (*link) {
502 parent = *link;
503 entry = rb_entry(parent, struct sched_entity, run_node);
504 /*
505 * We dont care about collisions. Nodes with
506 * the same key stay together.
507 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200508 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200509 link = &parent->rb_left;
510 } else {
511 link = &parent->rb_right;
512 leftmost = 0;
513 }
514 }
515
516 /*
517 * Maintain a cache of leftmost tree entries (it is frequently
518 * used):
519 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200520 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200521 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200522
523 rb_link_node(&se->run_node, parent, link);
524 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200525}
526
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200527static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100529 if (cfs_rq->rb_leftmost == &se->run_node) {
530 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100531
532 next_node = rb_next(&se->run_node);
533 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100534 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200535
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200536 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200537}
538
Peter Zijlstra029632f2011-10-25 10:00:11 +0200539struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200540{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100541 struct rb_node *left = cfs_rq->rb_leftmost;
542
543 if (!left)
544 return NULL;
545
546 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200547}
548
Rik van Rielac53db52011-02-01 09:51:03 -0500549static struct sched_entity *__pick_next_entity(struct sched_entity *se)
550{
551 struct rb_node *next = rb_next(&se->run_node);
552
553 if (!next)
554 return NULL;
555
556 return rb_entry(next, struct sched_entity, run_node);
557}
558
559#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200560struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200561{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100562 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200563
Balbir Singh70eee742008-02-22 13:25:53 +0530564 if (!last)
565 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100566
567 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200568}
569
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200570/**************************************************************
571 * Scheduling class statistics methods:
572 */
573
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100574int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700575 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100576 loff_t *ppos)
577{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700578 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100579 int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100580
581 if (ret || !write)
582 return ret;
583
584 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
585 sysctl_sched_min_granularity);
586
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100587#define WRT_SYSCTL(name) \
588 (normalized_sysctl_##name = sysctl_##name / (factor))
589 WRT_SYSCTL(sched_min_granularity);
590 WRT_SYSCTL(sched_latency);
591 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100592#undef WRT_SYSCTL
593
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100594 return 0;
595}
596#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200597
598/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200599 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200600 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100601static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200602{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200603 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100604 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200605
606 return delta;
607}
608
609/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200610 * The idea is to set a period in which each task runs once.
611 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200612 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200613 * this period because otherwise the slices get too small.
614 *
615 * p = (nr <= nl) ? l : l*nr/nl
616 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200617static u64 __sched_period(unsigned long nr_running)
618{
619 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100620 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200621
622 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100623 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200624 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200625 }
626
627 return period;
628}
629
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200630/*
631 * We calculate the wall-time slice from the period by taking a part
632 * proportional to the weight.
633 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200634 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200635 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200636static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200637{
Mike Galbraith0a582442009-01-02 12:16:42 +0100638 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200639
Mike Galbraith0a582442009-01-02 12:16:42 +0100640 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100641 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200642 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100643
644 cfs_rq = cfs_rq_of(se);
645 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200646
Mike Galbraith0a582442009-01-02 12:16:42 +0100647 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200648 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100649
650 update_load_add(&lw, se->load.weight);
651 load = &lw;
652 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100653 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100654 }
655 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200656}
657
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200658/*
Andrei Epure660cc002013-03-11 12:03:20 +0200659 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200660 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200661 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200662 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200663static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200664{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200665 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200666}
667
Alex Shia75cdaa2013-06-20 10:18:47 +0800668#ifdef CONFIG_SMP
Rik van Rielba7e5a22014-09-04 16:35:30 -0400669static int select_idle_sibling(struct task_struct *p, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100670static unsigned long task_h_load(struct task_struct *p);
671
Alex Shia75cdaa2013-06-20 10:18:47 +0800672static inline void __update_task_entity_contrib(struct sched_entity *se);
673
674/* Give new task start runnable values to heavy its load in infant time */
675void init_task_runnable_average(struct task_struct *p)
676{
677 u32 slice;
678
679 p->se.avg.decay_count = 0;
680 slice = sched_slice(task_cfs_rq(p), &p->se) >> 10;
681 p->se.avg.runnable_avg_sum = slice;
682 p->se.avg.runnable_avg_period = slice;
683 __update_task_entity_contrib(&p->se);
684}
685#else
686void init_task_runnable_average(struct task_struct *p)
687{
688}
689#endif
690
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200691/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100692 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200694static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200695{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200696 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200697 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100698 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200699
700 if (unlikely(!curr))
701 return;
702
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100703 delta_exec = now - curr->exec_start;
704 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100705 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200706
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200707 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100708
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100709 schedstat_set(curr->statistics.exec_max,
710 max(delta_exec, curr->statistics.exec_max));
711
712 curr->sum_exec_runtime += delta_exec;
713 schedstat_add(cfs_rq, exec_clock, delta_exec);
714
715 curr->vruntime += calc_delta_fair(delta_exec, curr);
716 update_min_vruntime(cfs_rq);
717
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100718 if (entity_is_task(curr)) {
719 struct task_struct *curtask = task_of(curr);
720
Ingo Molnarf977bb42009-09-13 18:15:54 +0200721 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100722 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700723 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100724 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700725
726 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200727}
728
729static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200730update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200731{
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200732 schedstat_set(se->statistics.wait_start, rq_clock(rq_of(cfs_rq)));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200733}
734
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735/*
736 * Task is being enqueued - update stats:
737 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200738static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200739{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740 /*
741 * Are we enqueueing a waiting task? (for current tasks
742 * a dequeue/enqueue event is a NOP)
743 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200744 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200745 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200746}
747
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200748static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200749update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200750{
Lucas De Marchi41acab82010-03-10 23:37:45 -0300751 schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200752 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start));
Lucas De Marchi41acab82010-03-10 23:37:45 -0300753 schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
754 schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200755 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200756#ifdef CONFIG_SCHEDSTATS
757 if (entity_is_task(se)) {
758 trace_sched_stat_wait(task_of(se),
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200759 rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200760 }
761#endif
Lucas De Marchi41acab82010-03-10 23:37:45 -0300762 schedstat_set(se->statistics.wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200763}
764
765static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200766update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200767{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200768 /*
769 * Mark the end of the wait period if dequeueing a
770 * waiting task:
771 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200772 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200773 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200774}
775
776/*
777 * We are picking a new current task - update its stats:
778 */
779static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200780update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200781{
782 /*
783 * We are starting a new run period:
784 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200785 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200786}
787
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200788/**************************************************
789 * Scheduling class queueing methods:
790 */
791
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200792#ifdef CONFIG_NUMA_BALANCING
793/*
Mel Gorman598f0ec2013-10-07 11:28:55 +0100794 * Approximate time to scan a full NUMA task in ms. The task scan period is
795 * calculated based on the tasks virtual memory size and
796 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200797 */
Mel Gorman598f0ec2013-10-07 11:28:55 +0100798unsigned int sysctl_numa_balancing_scan_period_min = 1000;
799unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +0200800
801/* Portion of address space to scan in MB */
802unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200803
Peter Zijlstra4b96a292012-10-25 14:16:47 +0200804/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
805unsigned int sysctl_numa_balancing_scan_delay = 1000;
806
Mel Gorman598f0ec2013-10-07 11:28:55 +0100807static unsigned int task_nr_scan_windows(struct task_struct *p)
808{
809 unsigned long rss = 0;
810 unsigned long nr_scan_pages;
811
812 /*
813 * Calculations based on RSS as non-present and empty pages are skipped
814 * by the PTE scanner and NUMA hinting faults should be trapped based
815 * on resident pages
816 */
817 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
818 rss = get_mm_rss(p->mm);
819 if (!rss)
820 rss = nr_scan_pages;
821
822 rss = round_up(rss, nr_scan_pages);
823 return rss / nr_scan_pages;
824}
825
826/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
827#define MAX_SCAN_WINDOW 2560
828
829static unsigned int task_scan_min(struct task_struct *p)
830{
831 unsigned int scan, floor;
832 unsigned int windows = 1;
833
834 if (sysctl_numa_balancing_scan_size < MAX_SCAN_WINDOW)
835 windows = MAX_SCAN_WINDOW / sysctl_numa_balancing_scan_size;
836 floor = 1000 / windows;
837
838 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
839 return max_t(unsigned int, floor, scan);
840}
841
842static unsigned int task_scan_max(struct task_struct *p)
843{
844 unsigned int smin = task_scan_min(p);
845 unsigned int smax;
846
847 /* Watch for min being lower than max due to floor calculations */
848 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
849 return max(smin, smax);
850}
851
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100852static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
853{
854 rq->nr_numa_running += (p->numa_preferred_nid != -1);
855 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
856}
857
858static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
859{
860 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
861 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
862}
863
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100864struct numa_group {
865 atomic_t refcount;
866
867 spinlock_t lock; /* nr_tasks, tasks */
868 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +0100869 pid_t gid;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100870 struct list_head task_list;
871
872 struct rcu_head rcu;
Rik van Riel20e07de2014-01-27 17:03:43 -0500873 nodemask_t active_nodes;
Mel Gorman989348b2013-10-07 11:29:40 +0100874 unsigned long total_faults;
Rik van Riel7e2703e2014-01-27 17:03:45 -0500875 /*
876 * Faults_cpu is used to decide whether memory should move
877 * towards the CPU. As a consequence, these stats are weighted
878 * more by CPU use than by memory faults.
879 */
Rik van Riel50ec8a42014-01-27 17:03:42 -0500880 unsigned long *faults_cpu;
Mel Gorman989348b2013-10-07 11:29:40 +0100881 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +0100882};
883
Rik van Rielbe1e4e72014-01-27 17:03:48 -0500884/* Shared or private faults. */
885#define NR_NUMA_HINT_FAULT_TYPES 2
886
887/* Memory and CPU locality */
888#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
889
890/* Averaged statistics, and temporary buffers. */
891#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
892
Mel Gormane29cf082013-10-07 11:29:22 +0100893pid_t task_numa_group_id(struct task_struct *p)
894{
895 return p->numa_group ? p->numa_group->gid : 0;
896}
897
Mel Gormanac8e8952013-10-07 11:29:03 +0100898static inline int task_faults_idx(int nid, int priv)
899{
Rik van Rielbe1e4e72014-01-27 17:03:48 -0500900 return NR_NUMA_HINT_FAULT_TYPES * nid + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +0100901}
902
903static inline unsigned long task_faults(struct task_struct *p, int nid)
904{
Rik van Rielff1df892014-01-27 17:03:41 -0500905 if (!p->numa_faults_memory)
Mel Gormanac8e8952013-10-07 11:29:03 +0100906 return 0;
907
Rik van Rielff1df892014-01-27 17:03:41 -0500908 return p->numa_faults_memory[task_faults_idx(nid, 0)] +
909 p->numa_faults_memory[task_faults_idx(nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +0100910}
911
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100912static inline unsigned long group_faults(struct task_struct *p, int nid)
913{
914 if (!p->numa_group)
915 return 0;
916
Wanpeng Li82897b42013-12-12 15:23:25 +0800917 return p->numa_group->faults[task_faults_idx(nid, 0)] +
918 p->numa_group->faults[task_faults_idx(nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100919}
920
Rik van Riel20e07de2014-01-27 17:03:43 -0500921static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
922{
923 return group->faults_cpu[task_faults_idx(nid, 0)] +
924 group->faults_cpu[task_faults_idx(nid, 1)];
925}
926
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100927/*
928 * These return the fraction of accesses done by a particular task, or
929 * task group, on a particular numa node. The group weight is given a
930 * larger multiplier, in order to group tasks together that are almost
931 * evenly spread out between numa nodes.
932 */
933static inline unsigned long task_weight(struct task_struct *p, int nid)
934{
935 unsigned long total_faults;
936
Rik van Rielff1df892014-01-27 17:03:41 -0500937 if (!p->numa_faults_memory)
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100938 return 0;
939
940 total_faults = p->total_numa_faults;
941
942 if (!total_faults)
943 return 0;
944
945 return 1000 * task_faults(p, nid) / total_faults;
946}
947
948static inline unsigned long group_weight(struct task_struct *p, int nid)
949{
Mel Gorman989348b2013-10-07 11:29:40 +0100950 if (!p->numa_group || !p->numa_group->total_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100951 return 0;
952
Mel Gorman989348b2013-10-07 11:29:40 +0100953 return 1000 * group_faults(p, nid) / p->numa_group->total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +0100954}
955
Rik van Riel10f39042014-01-27 17:03:44 -0500956bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
957 int src_nid, int dst_cpu)
958{
959 struct numa_group *ng = p->numa_group;
960 int dst_nid = cpu_to_node(dst_cpu);
961 int last_cpupid, this_cpupid;
962
963 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
964
965 /*
966 * Multi-stage node selection is used in conjunction with a periodic
967 * migration fault to build a temporal task<->page relation. By using
968 * a two-stage filter we remove short/unlikely relations.
969 *
970 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
971 * a task's usage of a particular page (n_p) per total usage of this
972 * page (n_t) (in a given time-span) to a probability.
973 *
974 * Our periodic faults will sample this probability and getting the
975 * same result twice in a row, given these samples are fully
976 * independent, is then given by P(n)^2, provided our sample period
977 * is sufficiently short compared to the usage pattern.
978 *
979 * This quadric squishes small probabilities, making it less likely we
980 * act on an unlikely task<->page relation.
981 */
982 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
983 if (!cpupid_pid_unset(last_cpupid) &&
984 cpupid_to_nid(last_cpupid) != dst_nid)
985 return false;
986
987 /* Always allow migrate on private faults */
988 if (cpupid_match_pid(p, last_cpupid))
989 return true;
990
991 /* A shared fault, but p->numa_group has not been set up yet. */
992 if (!ng)
993 return true;
994
995 /*
996 * Do not migrate if the destination is not a node that
997 * is actively used by this numa group.
998 */
999 if (!node_isset(dst_nid, ng->active_nodes))
1000 return false;
1001
1002 /*
1003 * Source is a node that is not actively used by this
1004 * numa group, while the destination is. Migrate.
1005 */
1006 if (!node_isset(src_nid, ng->active_nodes))
1007 return true;
1008
1009 /*
1010 * Both source and destination are nodes in active
1011 * use by this numa group. Maximize memory bandwidth
1012 * by migrating from more heavily used groups, to less
1013 * heavily used ones, spreading the load around.
1014 * Use a 1/4 hysteresis to avoid spurious page movement.
1015 */
1016 return group_faults(p, dst_nid) < (group_faults(p, src_nid) * 3 / 4);
1017}
1018
Mel Gormane6628d52013-10-07 11:29:02 +01001019static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001020static unsigned long source_load(int cpu, int type);
1021static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001022static unsigned long capacity_of(int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001023static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +01001024
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001025/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001026struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001027 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001028 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001029
1030 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001031 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001032
1033 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001034 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001035 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001036};
Mel Gormane6628d52013-10-07 11:29:02 +01001037
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001038/*
1039 * XXX borrowed from update_sg_lb_stats
1040 */
1041static void update_numa_stats(struct numa_stats *ns, int nid)
1042{
Rik van Riel83d7f242014-08-04 13:23:28 -04001043 int smt, cpu, cpus = 0;
1044 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001045
1046 memset(ns, 0, sizeof(*ns));
1047 for_each_cpu(cpu, cpumask_of_node(nid)) {
1048 struct rq *rq = cpu_rq(cpu);
1049
1050 ns->nr_running += rq->nr_running;
1051 ns->load += weighted_cpuload(cpu);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001052 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001053
1054 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001055 }
1056
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001057 /*
1058 * If we raced with hotplug and there are no CPUs left in our mask
1059 * the @ns structure is NULL'ed and task_numa_compare() will
1060 * not find this node attractive.
1061 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001062 * We'll either bail at !has_free_capacity, or we'll detect a huge
1063 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001064 */
1065 if (!cpus)
1066 return;
1067
Rik van Riel83d7f242014-08-04 13:23:28 -04001068 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1069 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1070 capacity = cpus / smt; /* cores */
1071
1072 ns->task_capacity = min_t(unsigned, capacity,
1073 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001074 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001075}
1076
Mel Gorman58d081b2013-10-07 11:29:10 +01001077struct task_numa_env {
1078 struct task_struct *p;
1079
1080 int src_cpu, src_nid;
1081 int dst_cpu, dst_nid;
1082
1083 struct numa_stats src_stats, dst_stats;
1084
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001085 int imbalance_pct;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001086
1087 struct task_struct *best_task;
1088 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001089 int best_cpu;
1090};
1091
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001092static void task_numa_assign(struct task_numa_env *env,
1093 struct task_struct *p, long imp)
1094{
1095 if (env->best_task)
1096 put_task_struct(env->best_task);
1097 if (p)
1098 get_task_struct(p);
1099
1100 env->best_task = p;
1101 env->best_imp = imp;
1102 env->best_cpu = env->dst_cpu;
1103}
1104
Rik van Riel28a21742014-06-23 11:46:13 -04001105static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001106 struct task_numa_env *env)
1107{
1108 long imb, old_imb;
Rik van Riel28a21742014-06-23 11:46:13 -04001109 long orig_src_load, orig_dst_load;
1110 long src_capacity, dst_capacity;
1111
1112 /*
1113 * The load is corrected for the CPU capacity available on each node.
1114 *
1115 * src_load dst_load
1116 * ------------ vs ---------
1117 * src_capacity dst_capacity
1118 */
1119 src_capacity = env->src_stats.compute_capacity;
1120 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001121
1122 /* We care about the slope of the imbalance, not the direction. */
1123 if (dst_load < src_load)
1124 swap(dst_load, src_load);
1125
1126 /* Is the difference below the threshold? */
Rik van Riel28a21742014-06-23 11:46:13 -04001127 imb = dst_load * src_capacity * 100 -
1128 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001129 if (imb <= 0)
1130 return false;
1131
1132 /*
1133 * The imbalance is above the allowed threshold.
1134 * Compare it with the old imbalance.
1135 */
Rik van Riel28a21742014-06-23 11:46:13 -04001136 orig_src_load = env->src_stats.load;
1137 orig_dst_load = env->dst_stats.load;
1138
Rik van Riele63da032014-05-14 13:22:21 -04001139 if (orig_dst_load < orig_src_load)
1140 swap(orig_dst_load, orig_src_load);
1141
Rik van Riel28a21742014-06-23 11:46:13 -04001142 old_imb = orig_dst_load * src_capacity * 100 -
1143 orig_src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001144
1145 /* Would this change make things worse? */
Rik van Riel16628672014-06-08 16:55:57 -04001146 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001147}
1148
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001149/*
1150 * This checks if the overall compute and NUMA accesses of the system would
1151 * be improved if the source tasks was migrated to the target dst_cpu taking
1152 * into account that it might be best if task running on the dst_cpu should
1153 * be exchanged with the source task
1154 */
Rik van Riel887c2902013-10-07 11:29:31 +01001155static void task_numa_compare(struct task_numa_env *env,
1156 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001157{
1158 struct rq *src_rq = cpu_rq(env->src_cpu);
1159 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1160 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001161 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001162 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001163 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001164 long moveimp = imp;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001165
1166 rcu_read_lock();
1167 cur = ACCESS_ONCE(dst_rq->curr);
1168 if (cur->pid == 0) /* idle */
1169 cur = NULL;
1170
1171 /*
1172 * "imp" is the fault differential for the source task between the
1173 * source and destination node. Calculate the total differential for
1174 * the source task and potential destination task. The more negative
1175 * the value is, the more rmeote accesses that would be expected to
1176 * be incurred if the tasks were swapped.
1177 */
1178 if (cur) {
1179 /* Skip this swap candidate if cannot move to the source cpu */
1180 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1181 goto unlock;
1182
Rik van Riel887c2902013-10-07 11:29:31 +01001183 /*
1184 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001185 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001186 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001187 if (cur->numa_group == env->p->numa_group) {
Rik van Riel887c2902013-10-07 11:29:31 +01001188 imp = taskimp + task_weight(cur, env->src_nid) -
1189 task_weight(cur, env->dst_nid);
Rik van Rielca28aa532013-10-07 11:29:32 +01001190 /*
1191 * Add some hysteresis to prevent swapping the
1192 * tasks within a group over tiny differences.
1193 */
1194 if (cur->numa_group)
1195 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001196 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001197 /*
1198 * Compare the group weights. If a task is all by
1199 * itself (not part of a group), use the task weight
1200 * instead.
1201 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001202 if (cur->numa_group)
1203 imp += group_weight(cur, env->src_nid) -
1204 group_weight(cur, env->dst_nid);
1205 else
1206 imp += task_weight(cur, env->src_nid) -
1207 task_weight(cur, env->dst_nid);
Rik van Riel887c2902013-10-07 11:29:31 +01001208 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001209 }
1210
Rik van Riel0132c3e2014-06-23 11:46:16 -04001211 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001212 goto unlock;
1213
1214 if (!cur) {
1215 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001216 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001217 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001218 goto unlock;
1219
1220 goto balance;
1221 }
1222
1223 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001224 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1225 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001226 goto assign;
1227
1228 /*
1229 * In the overloaded case, try and keep the load balanced.
1230 */
1231balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001232 load = task_h_load(env->p);
1233 dst_load = env->dst_stats.load + load;
1234 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001235
Rik van Riel0132c3e2014-06-23 11:46:16 -04001236 if (moveimp > imp && moveimp > env->best_imp) {
1237 /*
1238 * If the improvement from just moving env->p direction is
1239 * better than swapping tasks around, check if a move is
1240 * possible. Store a slightly smaller score than moveimp,
1241 * so an actually idle CPU will win.
1242 */
1243 if (!load_too_imbalanced(src_load, dst_load, env)) {
1244 imp = moveimp - 1;
1245 cur = NULL;
1246 goto assign;
1247 }
1248 }
1249
1250 if (imp <= env->best_imp)
1251 goto unlock;
1252
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001253 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001254 load = task_h_load(cur);
1255 dst_load -= load;
1256 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001257 }
1258
Rik van Riel28a21742014-06-23 11:46:13 -04001259 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001260 goto unlock;
1261
Rik van Rielba7e5a22014-09-04 16:35:30 -04001262 /*
1263 * One idle CPU per node is evaluated for a task numa move.
1264 * Call select_idle_sibling to maybe find a better one.
1265 */
1266 if (!cur)
1267 env->dst_cpu = select_idle_sibling(env->p, env->dst_cpu);
1268
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001269assign:
1270 task_numa_assign(env, cur, imp);
1271unlock:
1272 rcu_read_unlock();
1273}
1274
Rik van Riel887c2902013-10-07 11:29:31 +01001275static void task_numa_find_cpu(struct task_numa_env *env,
1276 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001277{
1278 int cpu;
1279
1280 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1281 /* Skip this CPU if the source task cannot migrate */
1282 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1283 continue;
1284
1285 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001286 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001287 }
1288}
1289
Mel Gorman58d081b2013-10-07 11:29:10 +01001290static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001291{
Mel Gorman58d081b2013-10-07 11:29:10 +01001292 struct task_numa_env env = {
1293 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001294
Mel Gorman58d081b2013-10-07 11:29:10 +01001295 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001296 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001297
1298 .imbalance_pct = 112,
1299
1300 .best_task = NULL,
1301 .best_imp = 0,
1302 .best_cpu = -1
Mel Gorman58d081b2013-10-07 11:29:10 +01001303 };
1304 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001305 unsigned long taskweight, groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001306 int nid, ret;
Rik van Riel887c2902013-10-07 11:29:31 +01001307 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001308
Mel Gorman58d081b2013-10-07 11:29:10 +01001309 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001310 * Pick the lowest SD_NUMA domain, as that would have the smallest
1311 * imbalance and would be the first to start moving tasks about.
1312 *
1313 * And we want to avoid any moving of tasks about, as that would create
1314 * random movement of tasks -- counter the numa conditions we're trying
1315 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001316 */
Mel Gormane6628d52013-10-07 11:29:02 +01001317 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001318 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001319 if (sd)
1320 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001321 rcu_read_unlock();
1322
Rik van Riel46a73e82013-11-11 19:29:25 -05001323 /*
1324 * Cpusets can break the scheduler domain tree into smaller
1325 * balance domains, some of which do not cross NUMA boundaries.
1326 * Tasks that are "trapped" in such domains cannot be migrated
1327 * elsewhere, so there is no point in (re)trying.
1328 */
1329 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001330 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001331 return -EINVAL;
1332 }
1333
Rik van Riel887c2902013-10-07 11:29:31 +01001334 taskweight = task_weight(p, env.src_nid);
1335 groupweight = group_weight(p, env.src_nid);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001336 update_numa_stats(&env.src_stats, env.src_nid);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001337 env.dst_nid = p->numa_preferred_nid;
Rik van Riel887c2902013-10-07 11:29:31 +01001338 taskimp = task_weight(p, env.dst_nid) - taskweight;
1339 groupimp = group_weight(p, env.dst_nid) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001340 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001341
Rik van Riela43455a2014-06-04 16:09:42 -04001342 /* Try to find a spot on the preferred nid. */
1343 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001344
1345 /* No space available on the preferred nid. Look elsewhere. */
1346 if (env.best_cpu == -1) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001347 for_each_online_node(nid) {
1348 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001349 continue;
1350
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001351 /* Only consider nodes where both task and groups benefit */
Rik van Riel887c2902013-10-07 11:29:31 +01001352 taskimp = task_weight(p, nid) - taskweight;
1353 groupimp = group_weight(p, nid) - groupweight;
1354 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001355 continue;
1356
1357 env.dst_nid = nid;
1358 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel887c2902013-10-07 11:29:31 +01001359 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001360 }
1361 }
1362
Rik van Riel68d1b022014-04-11 13:00:29 -04001363 /*
1364 * If the task is part of a workload that spans multiple NUMA nodes,
1365 * and is migrating into one of the workload's active nodes, remember
1366 * this node as the task's preferred numa node, so the workload can
1367 * settle down.
1368 * A task that migrated to a second choice node will be better off
1369 * trying for a better one later. Do not set the preferred node here.
1370 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001371 if (p->numa_group) {
1372 if (env.best_cpu == -1)
1373 nid = env.src_nid;
1374 else
1375 nid = env.dst_nid;
1376
1377 if (node_isset(nid, p->numa_group->active_nodes))
1378 sched_setnuma(p, env.dst_nid);
1379 }
1380
1381 /* No better CPU than the current one was found. */
1382 if (env.best_cpu == -1)
1383 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001384
Rik van Riel04bb2f92013-10-07 11:29:36 +01001385 /*
1386 * Reset the scan period if the task is being rescheduled on an
1387 * alternative node to recheck if the tasks is now properly placed.
1388 */
1389 p->numa_scan_period = task_scan_min(p);
1390
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001391 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001392 ret = migrate_task_to(p, env.best_cpu);
1393 if (ret != 0)
1394 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001395 return ret;
1396 }
1397
1398 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001399 if (ret != 0)
1400 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001401 put_task_struct(env.best_task);
1402 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001403}
1404
Mel Gorman6b9a7462013-10-07 11:29:11 +01001405/* Attempt to migrate a task to a CPU on the preferred node. */
1406static void numa_migrate_preferred(struct task_struct *p)
1407{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001408 unsigned long interval = HZ;
1409
Rik van Riel2739d3e2013-10-07 11:29:41 +01001410 /* This task has no NUMA fault statistics yet */
Rik van Rielff1df892014-01-27 17:03:41 -05001411 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults_memory))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001412 return;
1413
1414 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001415 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1416 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001417
Mel Gorman6b9a7462013-10-07 11:29:11 +01001418 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001419 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001420 return;
1421
Mel Gorman6b9a7462013-10-07 11:29:11 +01001422 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001423 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001424}
1425
Rik van Riel04bb2f92013-10-07 11:29:36 +01001426/*
Rik van Riel20e07de2014-01-27 17:03:43 -05001427 * Find the nodes on which the workload is actively running. We do this by
1428 * tracking the nodes from which NUMA hinting faults are triggered. This can
1429 * be different from the set of nodes where the workload's memory is currently
1430 * located.
1431 *
1432 * The bitmask is used to make smarter decisions on when to do NUMA page
1433 * migrations, To prevent flip-flopping, and excessive page migrations, nodes
1434 * are added when they cause over 6/16 of the maximum number of faults, but
1435 * only removed when they drop below 3/16.
1436 */
1437static void update_numa_active_node_mask(struct numa_group *numa_group)
1438{
1439 unsigned long faults, max_faults = 0;
1440 int nid;
1441
1442 for_each_online_node(nid) {
1443 faults = group_faults_cpu(numa_group, nid);
1444 if (faults > max_faults)
1445 max_faults = faults;
1446 }
1447
1448 for_each_online_node(nid) {
1449 faults = group_faults_cpu(numa_group, nid);
1450 if (!node_isset(nid, numa_group->active_nodes)) {
1451 if (faults > max_faults * 6 / 16)
1452 node_set(nid, numa_group->active_nodes);
1453 } else if (faults < max_faults * 3 / 16)
1454 node_clear(nid, numa_group->active_nodes);
1455 }
1456}
1457
1458/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001459 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1460 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001461 * period will be for the next scan window. If local/(local+remote) ratio is
1462 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1463 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001464 */
1465#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001466#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001467
1468/*
1469 * Increase the scan period (slow down scanning) if the majority of
1470 * our memory is already on our local node, or if the majority of
1471 * the page accesses are shared with other processes.
1472 * Otherwise, decrease the scan period.
1473 */
1474static void update_task_scan_period(struct task_struct *p,
1475 unsigned long shared, unsigned long private)
1476{
1477 unsigned int period_slot;
1478 int ratio;
1479 int diff;
1480
1481 unsigned long remote = p->numa_faults_locality[0];
1482 unsigned long local = p->numa_faults_locality[1];
1483
1484 /*
1485 * If there were no record hinting faults then either the task is
1486 * completely idle or all activity is areas that are not of interest
1487 * to automatic numa balancing. Scan slower
1488 */
1489 if (local + shared == 0) {
1490 p->numa_scan_period = min(p->numa_scan_period_max,
1491 p->numa_scan_period << 1);
1492
1493 p->mm->numa_next_scan = jiffies +
1494 msecs_to_jiffies(p->numa_scan_period);
1495
1496 return;
1497 }
1498
1499 /*
1500 * Prepare to scale scan period relative to the current period.
1501 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1502 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1503 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1504 */
1505 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1506 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1507 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1508 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1509 if (!slot)
1510 slot = 1;
1511 diff = slot * period_slot;
1512 } else {
1513 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1514
1515 /*
1516 * Scale scan rate increases based on sharing. There is an
1517 * inverse relationship between the degree of sharing and
1518 * the adjustment made to the scanning period. Broadly
1519 * speaking the intent is that there is little point
1520 * scanning faster if shared accesses dominate as it may
1521 * simply bounce migrations uselessly
1522 */
Rik van Riel04bb2f92013-10-07 11:29:36 +01001523 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared));
1524 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1525 }
1526
1527 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1528 task_scan_min(p), task_scan_max(p));
1529 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1530}
1531
Rik van Riel7e2703e2014-01-27 17:03:45 -05001532/*
1533 * Get the fraction of time the task has been running since the last
1534 * NUMA placement cycle. The scheduler keeps similar statistics, but
1535 * decays those on a 32ms period, which is orders of magnitude off
1536 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
1537 * stats only if the task is so new there are no NUMA statistics yet.
1538 */
1539static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
1540{
1541 u64 runtime, delta, now;
1542 /* Use the start of this time slice to avoid calculations. */
1543 now = p->se.exec_start;
1544 runtime = p->se.sum_exec_runtime;
1545
1546 if (p->last_task_numa_placement) {
1547 delta = runtime - p->last_sum_exec_runtime;
1548 *period = now - p->last_task_numa_placement;
1549 } else {
1550 delta = p->se.avg.runnable_avg_sum;
1551 *period = p->se.avg.runnable_avg_period;
1552 }
1553
1554 p->last_sum_exec_runtime = runtime;
1555 p->last_task_numa_placement = now;
1556
1557 return delta;
1558}
1559
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001560static void task_numa_placement(struct task_struct *p)
1561{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001562 int seq, nid, max_nid = -1, max_group_nid = -1;
1563 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001564 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05001565 unsigned long total_faults;
1566 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001567 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001568
Hugh Dickins2832bc12012-12-19 17:42:16 -08001569 seq = ACCESS_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001570 if (p->numa_scan_seq == seq)
1571 return;
1572 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001573 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001574
Rik van Riel7e2703e2014-01-27 17:03:45 -05001575 total_faults = p->numa_faults_locality[0] +
1576 p->numa_faults_locality[1];
1577 runtime = numa_get_avg_runtime(p, &period);
1578
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001579 /* If the task is part of a group prevent parallel updates to group stats */
1580 if (p->numa_group) {
1581 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001582 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001583 }
1584
Mel Gorman688b7582013-10-07 11:28:58 +01001585 /* Find the node with the highest number of faults */
1586 for_each_online_node(nid) {
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001587 unsigned long faults = 0, group_faults = 0;
Mel Gormanac8e8952013-10-07 11:29:03 +01001588 int priv, i;
Mel Gorman745d6142013-10-07 11:28:59 +01001589
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001590 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05001591 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001592
Mel Gormanac8e8952013-10-07 11:29:03 +01001593 i = task_faults_idx(nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01001594
Mel Gormanac8e8952013-10-07 11:29:03 +01001595 /* Decay existing window, copy faults since last scan */
Rik van Riel35664fd2014-01-27 17:03:46 -05001596 diff = p->numa_faults_buffer_memory[i] - p->numa_faults_memory[i] / 2;
Rik van Rielff1df892014-01-27 17:03:41 -05001597 fault_types[priv] += p->numa_faults_buffer_memory[i];
1598 p->numa_faults_buffer_memory[i] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001599
Rik van Riel7e2703e2014-01-27 17:03:45 -05001600 /*
1601 * Normalize the faults_from, so all tasks in a group
1602 * count according to CPU use, instead of by the raw
1603 * number of faults. Tasks with little runtime have
1604 * little over-all impact on throughput, and thus their
1605 * faults are less important.
1606 */
1607 f_weight = div64_u64(runtime << 16, period + 1);
1608 f_weight = (f_weight * p->numa_faults_buffer_cpu[i]) /
1609 (total_faults + 1);
Rik van Riel35664fd2014-01-27 17:03:46 -05001610 f_diff = f_weight - p->numa_faults_cpu[i] / 2;
Rik van Riel50ec8a42014-01-27 17:03:42 -05001611 p->numa_faults_buffer_cpu[i] = 0;
1612
Rik van Riel35664fd2014-01-27 17:03:46 -05001613 p->numa_faults_memory[i] += diff;
1614 p->numa_faults_cpu[i] += f_diff;
Rik van Rielff1df892014-01-27 17:03:41 -05001615 faults += p->numa_faults_memory[i];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001616 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001617 if (p->numa_group) {
1618 /* safe because we can only change our own group */
Mel Gorman989348b2013-10-07 11:29:40 +01001619 p->numa_group->faults[i] += diff;
Rik van Riel50ec8a42014-01-27 17:03:42 -05001620 p->numa_group->faults_cpu[i] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01001621 p->numa_group->total_faults += diff;
1622 group_faults += p->numa_group->faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001623 }
Mel Gormanac8e8952013-10-07 11:29:03 +01001624 }
1625
Mel Gorman688b7582013-10-07 11:28:58 +01001626 if (faults > max_faults) {
1627 max_faults = faults;
1628 max_nid = nid;
1629 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001630
1631 if (group_faults > max_group_faults) {
1632 max_group_faults = group_faults;
1633 max_group_nid = nid;
1634 }
1635 }
1636
Rik van Riel04bb2f92013-10-07 11:29:36 +01001637 update_task_scan_period(p, fault_types[0], fault_types[1]);
1638
Mel Gorman7dbd13e2013-10-07 11:29:29 +01001639 if (p->numa_group) {
Rik van Riel20e07de2014-01-27 17:03:43 -05001640 update_numa_active_node_mask(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001641 spin_unlock_irq(group_lock);
Rik van Rielf0b8a4a2014-06-23 11:41:29 -04001642 max_nid = max_group_nid;
Mel Gorman688b7582013-10-07 11:28:58 +01001643 }
1644
Rik van Rielbb97fc32014-06-04 16:33:15 -04001645 if (max_faults) {
1646 /* Set the new preferred node */
1647 if (max_nid != p->numa_preferred_nid)
1648 sched_setnuma(p, max_nid);
1649
1650 if (task_node(p) != p->numa_preferred_nid)
1651 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01001652 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001653}
1654
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001655static inline int get_numa_group(struct numa_group *grp)
1656{
1657 return atomic_inc_not_zero(&grp->refcount);
1658}
1659
1660static inline void put_numa_group(struct numa_group *grp)
1661{
1662 if (atomic_dec_and_test(&grp->refcount))
1663 kfree_rcu(grp, rcu);
1664}
1665
Mel Gorman3e6a9412013-10-07 11:29:35 +01001666static void task_numa_group(struct task_struct *p, int cpupid, int flags,
1667 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001668{
1669 struct numa_group *grp, *my_grp;
1670 struct task_struct *tsk;
1671 bool join = false;
1672 int cpu = cpupid_to_cpu(cpupid);
1673 int i;
1674
1675 if (unlikely(!p->numa_group)) {
1676 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05001677 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001678
1679 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1680 if (!grp)
1681 return;
1682
1683 atomic_set(&grp->refcount, 1);
1684 spin_lock_init(&grp->lock);
1685 INIT_LIST_HEAD(&grp->task_list);
Mel Gormane29cf082013-10-07 11:29:22 +01001686 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05001687 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001688 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
1689 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001690
Rik van Riel20e07de2014-01-27 17:03:43 -05001691 node_set(task_node(current), grp->active_nodes);
1692
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001693 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Rik van Rielff1df892014-01-27 17:03:41 -05001694 grp->faults[i] = p->numa_faults_memory[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001695
Mel Gorman989348b2013-10-07 11:29:40 +01001696 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001697
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001698 list_add(&p->numa_entry, &grp->task_list);
1699 grp->nr_tasks++;
1700 rcu_assign_pointer(p->numa_group, grp);
1701 }
1702
1703 rcu_read_lock();
1704 tsk = ACCESS_ONCE(cpu_rq(cpu)->curr);
1705
1706 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02001707 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001708
1709 grp = rcu_dereference(tsk->numa_group);
1710 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001711 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001712
1713 my_grp = p->numa_group;
1714 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001715 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001716
1717 /*
1718 * Only join the other group if its bigger; if we're the bigger group,
1719 * the other task will join us.
1720 */
1721 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02001722 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001723
1724 /*
1725 * Tie-break on the grp address.
1726 */
1727 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02001728 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001729
Rik van Rieldabe1d92013-10-07 11:29:34 +01001730 /* Always join threads in the same process. */
1731 if (tsk->mm == current->mm)
1732 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001733
Rik van Rieldabe1d92013-10-07 11:29:34 +01001734 /* Simple filter to avoid false positives due to PID collisions */
1735 if (flags & TNF_SHARED)
1736 join = true;
1737
Mel Gorman3e6a9412013-10-07 11:29:35 +01001738 /* Update priv based on whether false sharing was detected */
1739 *priv = !join;
1740
Rik van Rieldabe1d92013-10-07 11:29:34 +01001741 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02001742 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001743
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001744 rcu_read_unlock();
1745
1746 if (!join)
1747 return;
1748
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001749 BUG_ON(irqs_disabled());
1750 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001751
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001752 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Rik van Rielff1df892014-01-27 17:03:41 -05001753 my_grp->faults[i] -= p->numa_faults_memory[i];
1754 grp->faults[i] += p->numa_faults_memory[i];
Mel Gorman989348b2013-10-07 11:29:40 +01001755 }
1756 my_grp->total_faults -= p->total_numa_faults;
1757 grp->total_faults += p->total_numa_faults;
1758
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001759 list_move(&p->numa_entry, &grp->task_list);
1760 my_grp->nr_tasks--;
1761 grp->nr_tasks++;
1762
1763 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001764 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001765
1766 rcu_assign_pointer(p->numa_group, grp);
1767
1768 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02001769 return;
1770
1771no_join:
1772 rcu_read_unlock();
1773 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001774}
1775
1776void task_numa_free(struct task_struct *p)
1777{
1778 struct numa_group *grp = p->numa_group;
Rik van Rielff1df892014-01-27 17:03:41 -05001779 void *numa_faults = p->numa_faults_memory;
Steven Rostedte9dd6852014-05-27 17:02:04 -04001780 unsigned long flags;
1781 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001782
1783 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04001784 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001785 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Rik van Rielff1df892014-01-27 17:03:41 -05001786 grp->faults[i] -= p->numa_faults_memory[i];
Mel Gorman989348b2013-10-07 11:29:40 +01001787 grp->total_faults -= p->total_numa_faults;
1788
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001789 list_del(&p->numa_entry);
1790 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04001791 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03001792 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001793 put_numa_group(grp);
1794 }
1795
Rik van Rielff1df892014-01-27 17:03:41 -05001796 p->numa_faults_memory = NULL;
1797 p->numa_faults_buffer_memory = NULL;
Rik van Riel50ec8a42014-01-27 17:03:42 -05001798 p->numa_faults_cpu= NULL;
1799 p->numa_faults_buffer_cpu = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01001800 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001801}
1802
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001803/*
1804 * Got a PROT_NONE fault for a page on @node.
1805 */
Rik van Riel58b46da2014-01-27 17:03:47 -05001806void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001807{
1808 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001809 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05001810 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04001811 int local = !!(flags & TNF_FAULT_LOCAL);
Mel Gormanac8e8952013-10-07 11:29:03 +01001812 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001813
Dave Kleikamp10e84b92013-07-31 13:53:35 -07001814 if (!numabalancing_enabled)
Mel Gorman1a687c22012-11-22 11:16:36 +00001815 return;
1816
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01001817 /* for example, ksmd faulting in a user's mm */
1818 if (!p->mm)
1819 return;
1820
Mel Gormanf809ca92013-10-07 11:28:57 +01001821 /* Allocate buffer to track faults on a per-node basis */
Rik van Rielff1df892014-01-27 17:03:41 -05001822 if (unlikely(!p->numa_faults_memory)) {
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001823 int size = sizeof(*p->numa_faults_memory) *
1824 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01001825
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001826 p->numa_faults_memory = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
Rik van Rielff1df892014-01-27 17:03:41 -05001827 if (!p->numa_faults_memory)
Mel Gormanf809ca92013-10-07 11:28:57 +01001828 return;
Mel Gorman745d6142013-10-07 11:28:59 +01001829
Rik van Rielff1df892014-01-27 17:03:41 -05001830 BUG_ON(p->numa_faults_buffer_memory);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001831 /*
1832 * The averaged statistics, shared & private, memory & cpu,
1833 * occupy the first half of the array. The second half of the
1834 * array is for current counters, which are averaged into the
1835 * first set by task_numa_placement.
1836 */
Rik van Riel50ec8a42014-01-27 17:03:42 -05001837 p->numa_faults_cpu = p->numa_faults_memory + (2 * nr_node_ids);
1838 p->numa_faults_buffer_memory = p->numa_faults_memory + (4 * nr_node_ids);
1839 p->numa_faults_buffer_cpu = p->numa_faults_memory + (6 * nr_node_ids);
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001840 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001841 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01001842 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001843
Mel Gormanfb003b82012-11-15 09:01:14 +00001844 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001845 * First accesses are treated as private, otherwise consider accesses
1846 * to be private if the accessing pid has not changed
1847 */
1848 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
1849 priv = 1;
1850 } else {
1851 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001852 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01001853 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001854 }
1855
Rik van Riel792568e2014-04-11 13:00:27 -04001856 /*
1857 * If a workload spans multiple NUMA nodes, a shared fault that
1858 * occurs wholly within the set of nodes that the workload is
1859 * actively using should be counted as local. This allows the
1860 * scan rate to slow down when a workload has settled down.
1861 */
1862 if (!priv && !local && p->numa_group &&
1863 node_isset(cpu_node, p->numa_group->active_nodes) &&
1864 node_isset(mem_node, p->numa_group->active_nodes))
1865 local = 1;
1866
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001867 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01001868
Rik van Riel2739d3e2013-10-07 11:29:41 +01001869 /*
1870 * Retry task to preferred node migration periodically, in case it
1871 * case it previously failed, or the scheduler moved us.
1872 */
1873 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01001874 numa_migrate_preferred(p);
1875
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001876 if (migrated)
1877 p->numa_pages_migrated += pages;
1878
Rik van Riel58b46da2014-01-27 17:03:47 -05001879 p->numa_faults_buffer_memory[task_faults_idx(mem_node, priv)] += pages;
1880 p->numa_faults_buffer_cpu[task_faults_idx(cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04001881 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001882}
1883
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001884static void reset_ptenuma_scan(struct task_struct *p)
1885{
1886 ACCESS_ONCE(p->mm->numa_scan_seq)++;
1887 p->mm->numa_scan_offset = 0;
1888}
1889
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001890/*
1891 * The expensive part of numa migration is done from task_work context.
1892 * Triggered from task_tick_numa().
1893 */
1894void task_numa_work(struct callback_head *work)
1895{
1896 unsigned long migrate, next_scan, now = jiffies;
1897 struct task_struct *p = current;
1898 struct mm_struct *mm = p->mm;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001899 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00001900 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001901 unsigned long nr_pte_updates = 0;
Mel Gorman9f406042012-11-14 18:34:32 +00001902 long pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001903
1904 WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
1905
1906 work->next = work; /* protect against double add */
1907 /*
1908 * Who cares about NUMA placement when they're dying.
1909 *
1910 * NOTE: make sure not to dereference p->mm before this check,
1911 * exit_task_work() happens _after_ exit_mm() so we could be called
1912 * without p->mm even though we still had it when we enqueued this
1913 * work.
1914 */
1915 if (p->flags & PF_EXITING)
1916 return;
1917
Mel Gorman930aa172013-10-07 11:29:37 +01001918 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01001919 mm->numa_next_scan = now +
1920 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00001921 }
1922
1923 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001924 * Enforce maximal scan/migration frequency..
1925 */
1926 migrate = mm->numa_next_scan;
1927 if (time_before(now, migrate))
1928 return;
1929
Mel Gorman598f0ec2013-10-07 11:28:55 +01001930 if (p->numa_scan_period == 0) {
1931 p->numa_scan_period_max = task_scan_max(p);
1932 p->numa_scan_period = task_scan_min(p);
1933 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001934
Mel Gormanfb003b82012-11-15 09:01:14 +00001935 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001936 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
1937 return;
1938
Mel Gormane14808b2012-11-19 10:59:15 +00001939 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01001940 * Delay this task enough that another task of this mm will likely win
1941 * the next time around.
1942 */
1943 p->node_stamp += 2 * TICK_NSEC;
1944
Mel Gorman9f406042012-11-14 18:34:32 +00001945 start = mm->numa_scan_offset;
1946 pages = sysctl_numa_balancing_scan_size;
1947 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
1948 if (!pages)
1949 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001950
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001951 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00001952 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001953 if (!vma) {
1954 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00001955 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001956 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001957 }
Mel Gorman9f406042012-11-14 18:34:32 +00001958 for (; vma; vma = vma->vm_next) {
Mel Gormanfc3147242013-10-07 11:29:09 +01001959 if (!vma_migratable(vma) || !vma_policy_mof(p, vma))
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001960 continue;
1961
Mel Gorman4591ce4f2013-10-07 11:29:13 +01001962 /*
1963 * Shared library pages mapped by multiple processes are not
1964 * migrated as it is expected they are cache replicated. Avoid
1965 * hinting faults in read-only file-backed mappings or the vdso
1966 * as migrating the pages will be of marginal benefit.
1967 */
1968 if (!vma->vm_mm ||
1969 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
1970 continue;
1971
Mel Gorman3c67f472013-12-18 17:08:40 -08001972 /*
1973 * Skip inaccessible VMAs to avoid any confusion between
1974 * PROT_NONE and NUMA hinting ptes
1975 */
1976 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
1977 continue;
1978
Mel Gorman9f406042012-11-14 18:34:32 +00001979 do {
1980 start = max(start, vma->vm_start);
1981 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
1982 end = min(end, vma->vm_end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001983 nr_pte_updates += change_prot_numa(vma, start, end);
1984
1985 /*
1986 * Scan sysctl_numa_balancing_scan_size but ensure that
1987 * at least one PTE is updated so that unused virtual
1988 * address space is quickly skipped.
1989 */
1990 if (nr_pte_updates)
1991 pages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001992
Mel Gorman9f406042012-11-14 18:34:32 +00001993 start = end;
1994 if (pages <= 0)
1995 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05001996
1997 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00001998 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001999 }
2000
Mel Gorman9f406042012-11-14 18:34:32 +00002001out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002002 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002003 * It is possible to reach the end of the VMA list but the last few
2004 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2005 * would find the !migratable VMA on the next scan but not reset the
2006 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002007 */
2008 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002009 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002010 else
2011 reset_ptenuma_scan(p);
2012 up_read(&mm->mmap_sem);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002013}
2014
2015/*
2016 * Drive the periodic memory faults..
2017 */
2018void task_tick_numa(struct rq *rq, struct task_struct *curr)
2019{
2020 struct callback_head *work = &curr->numa_work;
2021 u64 period, now;
2022
2023 /*
2024 * We don't care about NUMA placement if we don't have memory.
2025 */
2026 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2027 return;
2028
2029 /*
2030 * Using runtime rather than walltime has the dual advantage that
2031 * we (mostly) drive the selection from busy threads and that the
2032 * task needs to have done some actual work before we bother with
2033 * NUMA placement.
2034 */
2035 now = curr->se.sum_exec_runtime;
2036 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2037
2038 if (now - curr->node_stamp > period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002039 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01002040 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002041 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002042
2043 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2044 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2045 task_work_add(curr, work, true);
2046 }
2047 }
2048}
2049#else
2050static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2051{
2052}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002053
2054static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2055{
2056}
2057
2058static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2059{
2060}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002061#endif /* CONFIG_NUMA_BALANCING */
2062
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002063static void
2064account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2065{
2066 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002067 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002068 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002069#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002070 if (entity_is_task(se)) {
2071 struct rq *rq = rq_of(cfs_rq);
2072
2073 account_numa_enqueue(rq, task_of(se));
2074 list_add(&se->group_node, &rq->cfs_tasks);
2075 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002076#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002077 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002078}
2079
2080static void
2081account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2082{
2083 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002084 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002085 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002086 if (entity_is_task(se)) {
2087 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302088 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002089 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002090 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002091}
2092
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002093#ifdef CONFIG_FAIR_GROUP_SCHED
2094# ifdef CONFIG_SMP
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002095static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
2096{
2097 long tg_weight;
2098
2099 /*
2100 * Use this CPU's actual weight instead of the last load_contribution
2101 * to gain a more accurate current total weight. See
2102 * update_cfs_rq_load_contribution().
2103 */
Alex Shibf5b9862013-06-20 10:18:54 +08002104 tg_weight = atomic_long_read(&tg->load_avg);
Paul Turner82958362012-10-04 13:18:31 +02002105 tg_weight -= cfs_rq->tg_load_contrib;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002106 tg_weight += cfs_rq->load.weight;
2107
2108 return tg_weight;
2109}
2110
Paul Turner6d5ab292011-01-21 20:45:01 -08002111static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002112{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002113 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002114
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002115 tg_weight = calc_tg_weight(tg, cfs_rq);
Paul Turner6d5ab292011-01-21 20:45:01 -08002116 load = cfs_rq->load.weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002117
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002118 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002119 if (tg_weight)
2120 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002121
2122 if (shares < MIN_SHARES)
2123 shares = MIN_SHARES;
2124 if (shares > tg->shares)
2125 shares = tg->shares;
2126
2127 return shares;
2128}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002129# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002130static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002131{
2132 return tg->shares;
2133}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002134# endif /* CONFIG_SMP */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002135static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2136 unsigned long weight)
2137{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002138 if (se->on_rq) {
2139 /* commit outstanding execution time */
2140 if (cfs_rq->curr == se)
2141 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002142 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002143 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002144
2145 update_load_set(&se->load, weight);
2146
2147 if (se->on_rq)
2148 account_entity_enqueue(cfs_rq, se);
2149}
2150
Paul Turner82958362012-10-04 13:18:31 +02002151static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2152
Paul Turner6d5ab292011-01-21 20:45:01 -08002153static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002154{
2155 struct task_group *tg;
2156 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002157 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002158
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002159 tg = cfs_rq->tg;
2160 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07002161 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002162 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002163#ifndef CONFIG_SMP
2164 if (likely(se->load.weight == tg->shares))
2165 return;
2166#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002167 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002168
2169 reweight_entity(cfs_rq_of(se), se, shares);
2170}
2171#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -08002172static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002173{
2174}
2175#endif /* CONFIG_FAIR_GROUP_SCHED */
2176
Alex Shi141965c2013-06-26 13:05:39 +08002177#ifdef CONFIG_SMP
Paul Turner9d85f212012-10-04 13:18:29 +02002178/*
Paul Turner5b51f2f2012-10-04 13:18:32 +02002179 * We choose a half-life close to 1 scheduling period.
2180 * Note: The tables below are dependent on this value.
2181 */
2182#define LOAD_AVG_PERIOD 32
2183#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
2184#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */
2185
2186/* Precomputed fixed inverse multiplies for multiplication by y^n */
2187static const u32 runnable_avg_yN_inv[] = {
2188 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
2189 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
2190 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
2191 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
2192 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
2193 0x85aac367, 0x82cd8698,
2194};
2195
2196/*
2197 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
2198 * over-estimates when re-combining.
2199 */
2200static const u32 runnable_avg_yN_sum[] = {
2201 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
2202 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
2203 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
2204};
2205
2206/*
Paul Turner9d85f212012-10-04 13:18:29 +02002207 * Approximate:
2208 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2209 */
2210static __always_inline u64 decay_load(u64 val, u64 n)
2211{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002212 unsigned int local_n;
2213
2214 if (!n)
2215 return val;
2216 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
2217 return 0;
2218
2219 /* after bounds checking we can collapse to 32-bit */
2220 local_n = n;
2221
2222 /*
2223 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002224 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2225 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002226 *
2227 * To achieve constant time decay_load.
2228 */
2229 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2230 val >>= local_n / LOAD_AVG_PERIOD;
2231 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002232 }
2233
Paul Turner5b51f2f2012-10-04 13:18:32 +02002234 val *= runnable_avg_yN_inv[local_n];
2235 /* We don't use SRR here since we always want to round down. */
2236 return val >> 32;
2237}
2238
2239/*
2240 * For updates fully spanning n periods, the contribution to runnable
2241 * average will be: \Sum 1024*y^n
2242 *
2243 * We can compute this reasonably efficiently by combining:
2244 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2245 */
2246static u32 __compute_runnable_contrib(u64 n)
2247{
2248 u32 contrib = 0;
2249
2250 if (likely(n <= LOAD_AVG_PERIOD))
2251 return runnable_avg_yN_sum[n];
2252 else if (unlikely(n >= LOAD_AVG_MAX_N))
2253 return LOAD_AVG_MAX;
2254
2255 /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */
2256 do {
2257 contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */
2258 contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD];
2259
2260 n -= LOAD_AVG_PERIOD;
2261 } while (n > LOAD_AVG_PERIOD);
2262
2263 contrib = decay_load(contrib, n);
2264 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002265}
2266
2267/*
2268 * We can represent the historical contribution to runnable average as the
2269 * coefficients of a geometric series. To do this we sub-divide our runnable
2270 * history into segments of approximately 1ms (1024us); label the segment that
2271 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2272 *
2273 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2274 * p0 p1 p2
2275 * (now) (~1ms ago) (~2ms ago)
2276 *
2277 * Let u_i denote the fraction of p_i that the entity was runnable.
2278 *
2279 * We then designate the fractions u_i as our co-efficients, yielding the
2280 * following representation of historical load:
2281 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2282 *
2283 * We choose y based on the with of a reasonably scheduling period, fixing:
2284 * y^32 = 0.5
2285 *
2286 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2287 * approximately half as much as the contribution to load within the last ms
2288 * (u_0).
2289 *
2290 * When a period "rolls over" and we have new u_0`, multiplying the previous
2291 * sum again by y is sufficient to update:
2292 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2293 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2294 */
2295static __always_inline int __update_entity_runnable_avg(u64 now,
2296 struct sched_avg *sa,
2297 int runnable)
2298{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002299 u64 delta, periods;
2300 u32 runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002301 int delta_w, decayed = 0;
2302
2303 delta = now - sa->last_runnable_update;
2304 /*
2305 * This should only happen when time goes backwards, which it
2306 * unfortunately does during sched clock init when we swap over to TSC.
2307 */
2308 if ((s64)delta < 0) {
2309 sa->last_runnable_update = now;
2310 return 0;
2311 }
2312
2313 /*
2314 * Use 1024ns as the unit of measurement since it's a reasonable
2315 * approximation of 1us and fast to compute.
2316 */
2317 delta >>= 10;
2318 if (!delta)
2319 return 0;
2320 sa->last_runnable_update = now;
2321
2322 /* delta_w is the amount already accumulated against our next period */
2323 delta_w = sa->runnable_avg_period % 1024;
2324 if (delta + delta_w >= 1024) {
2325 /* period roll-over */
2326 decayed = 1;
2327
2328 /*
2329 * Now that we know we're crossing a period boundary, figure
2330 * out how much from delta we need to complete the current
2331 * period and accrue it.
2332 */
2333 delta_w = 1024 - delta_w;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002334 if (runnable)
2335 sa->runnable_avg_sum += delta_w;
2336 sa->runnable_avg_period += delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002337
Paul Turner5b51f2f2012-10-04 13:18:32 +02002338 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002339
Paul Turner5b51f2f2012-10-04 13:18:32 +02002340 /* Figure out how many additional periods this update spans */
2341 periods = delta / 1024;
2342 delta %= 1024;
2343
2344 sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum,
2345 periods + 1);
2346 sa->runnable_avg_period = decay_load(sa->runnable_avg_period,
2347 periods + 1);
2348
2349 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
2350 runnable_contrib = __compute_runnable_contrib(periods);
2351 if (runnable)
2352 sa->runnable_avg_sum += runnable_contrib;
2353 sa->runnable_avg_period += runnable_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002354 }
2355
2356 /* Remainder of delta accrued against u_0` */
2357 if (runnable)
2358 sa->runnable_avg_sum += delta;
2359 sa->runnable_avg_period += delta;
2360
2361 return decayed;
2362}
2363
Paul Turner9ee474f2012-10-04 13:18:30 +02002364/* Synchronize an entity's decay with its parenting cfs_rq.*/
Paul Turneraff3e492012-10-04 13:18:30 +02002365static inline u64 __synchronize_entity_decay(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02002366{
2367 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2368 u64 decays = atomic64_read(&cfs_rq->decay_counter);
2369
2370 decays -= se->avg.decay_count;
2371 if (!decays)
Paul Turneraff3e492012-10-04 13:18:30 +02002372 return 0;
Paul Turner9ee474f2012-10-04 13:18:30 +02002373
2374 se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
2375 se->avg.decay_count = 0;
Paul Turneraff3e492012-10-04 13:18:30 +02002376
2377 return decays;
Paul Turner9ee474f2012-10-04 13:18:30 +02002378}
2379
Paul Turnerc566e8e2012-10-04 13:18:30 +02002380#ifdef CONFIG_FAIR_GROUP_SCHED
2381static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
2382 int force_update)
2383{
2384 struct task_group *tg = cfs_rq->tg;
Alex Shibf5b9862013-06-20 10:18:54 +08002385 long tg_contrib;
Paul Turnerc566e8e2012-10-04 13:18:30 +02002386
2387 tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
2388 tg_contrib -= cfs_rq->tg_load_contrib;
2389
Jason Low8236d902014-09-02 00:41:24 -07002390 if (!tg_contrib)
2391 return;
2392
Alex Shibf5b9862013-06-20 10:18:54 +08002393 if (force_update || abs(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
2394 atomic_long_add(tg_contrib, &tg->load_avg);
Paul Turnerc566e8e2012-10-04 13:18:30 +02002395 cfs_rq->tg_load_contrib += tg_contrib;
2396 }
2397}
Paul Turner8165e142012-10-04 13:18:31 +02002398
Paul Turnerbb17f652012-10-04 13:18:31 +02002399/*
2400 * Aggregate cfs_rq runnable averages into an equivalent task_group
2401 * representation for computing load contributions.
2402 */
2403static inline void __update_tg_runnable_avg(struct sched_avg *sa,
2404 struct cfs_rq *cfs_rq)
2405{
2406 struct task_group *tg = cfs_rq->tg;
2407 long contrib;
2408
2409 /* The fraction of a cpu used by this cfs_rq */
Michal Nazarewicz85b088e2013-11-10 20:42:01 +01002410 contrib = div_u64((u64)sa->runnable_avg_sum << NICE_0_SHIFT,
Paul Turnerbb17f652012-10-04 13:18:31 +02002411 sa->runnable_avg_period + 1);
2412 contrib -= cfs_rq->tg_runnable_contrib;
2413
2414 if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
2415 atomic_add(contrib, &tg->runnable_avg);
2416 cfs_rq->tg_runnable_contrib += contrib;
2417 }
2418}
2419
Paul Turner8165e142012-10-04 13:18:31 +02002420static inline void __update_group_entity_contrib(struct sched_entity *se)
2421{
2422 struct cfs_rq *cfs_rq = group_cfs_rq(se);
2423 struct task_group *tg = cfs_rq->tg;
Paul Turnerbb17f652012-10-04 13:18:31 +02002424 int runnable_avg;
2425
Paul Turner8165e142012-10-04 13:18:31 +02002426 u64 contrib;
2427
2428 contrib = cfs_rq->tg_load_contrib * tg->shares;
Alex Shibf5b9862013-06-20 10:18:54 +08002429 se->avg.load_avg_contrib = div_u64(contrib,
2430 atomic_long_read(&tg->load_avg) + 1);
Paul Turnerbb17f652012-10-04 13:18:31 +02002431
2432 /*
2433 * For group entities we need to compute a correction term in the case
2434 * that they are consuming <1 cpu so that we would contribute the same
2435 * load as a task of equal weight.
2436 *
2437 * Explicitly co-ordinating this measurement would be expensive, but
2438 * fortunately the sum of each cpus contribution forms a usable
2439 * lower-bound on the true value.
2440 *
2441 * Consider the aggregate of 2 contributions. Either they are disjoint
2442 * (and the sum represents true value) or they are disjoint and we are
2443 * understating by the aggregate of their overlap.
2444 *
2445 * Extending this to N cpus, for a given overlap, the maximum amount we
2446 * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
2447 * cpus that overlap for this interval and w_i is the interval width.
2448 *
2449 * On a small machine; the first term is well-bounded which bounds the
2450 * total error since w_i is a subset of the period. Whereas on a
2451 * larger machine, while this first term can be larger, if w_i is the
2452 * of consequential size guaranteed to see n_i*w_i quickly converge to
2453 * our upper bound of 1-cpu.
2454 */
2455 runnable_avg = atomic_read(&tg->runnable_avg);
2456 if (runnable_avg < NICE_0_LOAD) {
2457 se->avg.load_avg_contrib *= runnable_avg;
2458 se->avg.load_avg_contrib >>= NICE_0_SHIFT;
2459 }
Paul Turner8165e142012-10-04 13:18:31 +02002460}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00002461
2462static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
2463{
2464 __update_entity_runnable_avg(rq_clock_task(rq), &rq->avg, runnable);
2465 __update_tg_runnable_avg(&rq->avg, &rq->cfs);
2466}
Peter Zijlstra6e831252014-02-11 16:11:48 +01002467#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02002468static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
2469 int force_update) {}
Paul Turnerbb17f652012-10-04 13:18:31 +02002470static inline void __update_tg_runnable_avg(struct sched_avg *sa,
2471 struct cfs_rq *cfs_rq) {}
Paul Turner8165e142012-10-04 13:18:31 +02002472static inline void __update_group_entity_contrib(struct sched_entity *se) {}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00002473static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01002474#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02002475
Paul Turner8165e142012-10-04 13:18:31 +02002476static inline void __update_task_entity_contrib(struct sched_entity *se)
2477{
2478 u32 contrib;
2479
2480 /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
2481 contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
2482 contrib /= (se->avg.runnable_avg_period + 1);
2483 se->avg.load_avg_contrib = scale_load(contrib);
2484}
2485
Paul Turner2dac7542012-10-04 13:18:30 +02002486/* Compute the current contribution to load_avg by se, return any delta */
2487static long __update_entity_load_avg_contrib(struct sched_entity *se)
2488{
2489 long old_contrib = se->avg.load_avg_contrib;
2490
Paul Turner8165e142012-10-04 13:18:31 +02002491 if (entity_is_task(se)) {
2492 __update_task_entity_contrib(se);
2493 } else {
Paul Turnerbb17f652012-10-04 13:18:31 +02002494 __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
Paul Turner8165e142012-10-04 13:18:31 +02002495 __update_group_entity_contrib(se);
2496 }
Paul Turner2dac7542012-10-04 13:18:30 +02002497
2498 return se->avg.load_avg_contrib - old_contrib;
2499}
2500
Paul Turner9ee474f2012-10-04 13:18:30 +02002501static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
2502 long load_contrib)
2503{
2504 if (likely(load_contrib < cfs_rq->blocked_load_avg))
2505 cfs_rq->blocked_load_avg -= load_contrib;
2506 else
2507 cfs_rq->blocked_load_avg = 0;
2508}
2509
Paul Turnerf1b17282012-10-04 13:18:31 +02002510static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
2511
Paul Turner9d85f212012-10-04 13:18:29 +02002512/* Update a sched_entity's runnable average */
Paul Turner9ee474f2012-10-04 13:18:30 +02002513static inline void update_entity_load_avg(struct sched_entity *se,
2514 int update_cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002515{
Paul Turner2dac7542012-10-04 13:18:30 +02002516 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2517 long contrib_delta;
Paul Turnerf1b17282012-10-04 13:18:31 +02002518 u64 now;
Paul Turner2dac7542012-10-04 13:18:30 +02002519
Paul Turnerf1b17282012-10-04 13:18:31 +02002520 /*
2521 * For a group entity we need to use their owned cfs_rq_clock_task() in
2522 * case they are the parent of a throttled hierarchy.
2523 */
2524 if (entity_is_task(se))
2525 now = cfs_rq_clock_task(cfs_rq);
2526 else
2527 now = cfs_rq_clock_task(group_cfs_rq(se));
2528
2529 if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
Paul Turner2dac7542012-10-04 13:18:30 +02002530 return;
2531
2532 contrib_delta = __update_entity_load_avg_contrib(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02002533
2534 if (!update_cfs_rq)
2535 return;
2536
Paul Turner2dac7542012-10-04 13:18:30 +02002537 if (se->on_rq)
2538 cfs_rq->runnable_load_avg += contrib_delta;
Paul Turner9ee474f2012-10-04 13:18:30 +02002539 else
2540 subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
2541}
2542
2543/*
2544 * Decay the load contributed by all blocked children and account this so that
2545 * their contribution may appropriately discounted when they wake up.
2546 */
Paul Turneraff3e492012-10-04 13:18:30 +02002547static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02002548{
Paul Turnerf1b17282012-10-04 13:18:31 +02002549 u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
Paul Turner9ee474f2012-10-04 13:18:30 +02002550 u64 decays;
2551
2552 decays = now - cfs_rq->last_decay;
Paul Turneraff3e492012-10-04 13:18:30 +02002553 if (!decays && !force_update)
Paul Turner9ee474f2012-10-04 13:18:30 +02002554 return;
2555
Alex Shi25099402013-06-20 10:18:55 +08002556 if (atomic_long_read(&cfs_rq->removed_load)) {
2557 unsigned long removed_load;
2558 removed_load = atomic_long_xchg(&cfs_rq->removed_load, 0);
Paul Turneraff3e492012-10-04 13:18:30 +02002559 subtract_blocked_load_contrib(cfs_rq, removed_load);
2560 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002561
Paul Turneraff3e492012-10-04 13:18:30 +02002562 if (decays) {
2563 cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
2564 decays);
2565 atomic64_add(decays, &cfs_rq->decay_counter);
2566 cfs_rq->last_decay = now;
2567 }
Paul Turnerc566e8e2012-10-04 13:18:30 +02002568
2569 __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
Paul Turner9d85f212012-10-04 13:18:29 +02002570}
Ben Segall18bf2802012-10-04 12:51:20 +02002571
Paul Turner2dac7542012-10-04 13:18:30 +02002572/* Add the load generated by se into cfs_rq's child load-average */
2573static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002574 struct sched_entity *se,
2575 int wakeup)
Paul Turner2dac7542012-10-04 13:18:30 +02002576{
Paul Turneraff3e492012-10-04 13:18:30 +02002577 /*
2578 * We track migrations using entity decay_count <= 0, on a wake-up
2579 * migration we use a negative decay count to track the remote decays
2580 * accumulated while sleeping.
Alex Shia75cdaa2013-06-20 10:18:47 +08002581 *
2582 * Newly forked tasks are enqueued with se->avg.decay_count == 0, they
2583 * are seen by enqueue_entity_load_avg() as a migration with an already
2584 * constructed load_avg_contrib.
Paul Turneraff3e492012-10-04 13:18:30 +02002585 */
2586 if (unlikely(se->avg.decay_count <= 0)) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002587 se->avg.last_runnable_update = rq_clock_task(rq_of(cfs_rq));
Paul Turneraff3e492012-10-04 13:18:30 +02002588 if (se->avg.decay_count) {
2589 /*
2590 * In a wake-up migration we have to approximate the
2591 * time sleeping. This is because we can't synchronize
2592 * clock_task between the two cpus, and it is not
2593 * guaranteed to be read-safe. Instead, we can
2594 * approximate this using our carried decays, which are
2595 * explicitly atomically readable.
2596 */
2597 se->avg.last_runnable_update -= (-se->avg.decay_count)
2598 << 20;
2599 update_entity_load_avg(se, 0);
2600 /* Indicate that we're now synchronized and on-rq */
2601 se->avg.decay_count = 0;
2602 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002603 wakeup = 0;
2604 } else {
Vincent Guittot93906752014-01-22 08:45:34 +01002605 __synchronize_entity_decay(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02002606 }
2607
Paul Turneraff3e492012-10-04 13:18:30 +02002608 /* migrated tasks did not contribute to our blocked load */
2609 if (wakeup) {
Paul Turner9ee474f2012-10-04 13:18:30 +02002610 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turneraff3e492012-10-04 13:18:30 +02002611 update_entity_load_avg(se, 0);
2612 }
Paul Turner9ee474f2012-10-04 13:18:30 +02002613
Paul Turner2dac7542012-10-04 13:18:30 +02002614 cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02002615 /* we force update consideration on load-balancer moves */
2616 update_cfs_rq_blocked_load(cfs_rq, !wakeup);
Paul Turner2dac7542012-10-04 13:18:30 +02002617}
2618
Paul Turner9ee474f2012-10-04 13:18:30 +02002619/*
2620 * Remove se's load from this cfs_rq child load-average, if the entity is
2621 * transitioning to a blocked state we track its projected decay using
2622 * blocked_load_avg.
2623 */
Paul Turner2dac7542012-10-04 13:18:30 +02002624static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002625 struct sched_entity *se,
2626 int sleep)
Paul Turner2dac7542012-10-04 13:18:30 +02002627{
Paul Turner9ee474f2012-10-04 13:18:30 +02002628 update_entity_load_avg(se, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02002629 /* we force update consideration on load-balancer moves */
2630 update_cfs_rq_blocked_load(cfs_rq, !sleep);
Paul Turner9ee474f2012-10-04 13:18:30 +02002631
Paul Turner2dac7542012-10-04 13:18:30 +02002632 cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
Paul Turner9ee474f2012-10-04 13:18:30 +02002633 if (sleep) {
2634 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
2635 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
2636 } /* migrations, e.g. sleep=0 leave decay_count == 0 */
Paul Turner2dac7542012-10-04 13:18:30 +02002637}
Vincent Guittot642dbc32013-04-18 18:34:26 +02002638
2639/*
2640 * Update the rq's load with the elapsed running time before entering
2641 * idle. if the last scheduled task is not a CFS task, idle_enter will
2642 * be the only way to update the runnable statistic.
2643 */
2644void idle_enter_fair(struct rq *this_rq)
2645{
2646 update_rq_runnable_avg(this_rq, 1);
2647}
2648
2649/*
2650 * Update the rq's load with the elapsed idle time before a task is
2651 * scheduled. if the newly scheduled task is not a CFS task, idle_exit will
2652 * be the only way to update the runnable statistic.
2653 */
2654void idle_exit_fair(struct rq *this_rq)
2655{
2656 update_rq_runnable_avg(this_rq, 0);
2657}
2658
Peter Zijlstra6e831252014-02-11 16:11:48 +01002659static int idle_balance(struct rq *this_rq);
2660
Peter Zijlstra38033c32014-01-23 20:32:21 +01002661#else /* CONFIG_SMP */
2662
Paul Turner9ee474f2012-10-04 13:18:30 +02002663static inline void update_entity_load_avg(struct sched_entity *se,
2664 int update_cfs_rq) {}
Ben Segall18bf2802012-10-04 12:51:20 +02002665static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
Paul Turner2dac7542012-10-04 13:18:30 +02002666static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002667 struct sched_entity *se,
2668 int wakeup) {}
Paul Turner2dac7542012-10-04 13:18:30 +02002669static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
Paul Turner9ee474f2012-10-04 13:18:30 +02002670 struct sched_entity *se,
2671 int sleep) {}
Paul Turneraff3e492012-10-04 13:18:30 +02002672static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
2673 int force_update) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01002674
2675static inline int idle_balance(struct rq *rq)
2676{
2677 return 0;
2678}
2679
Peter Zijlstra38033c32014-01-23 20:32:21 +01002680#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02002681
Ingo Molnar2396af62007-08-09 11:16:48 +02002682static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002683{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002684#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +02002685 struct task_struct *tsk = NULL;
2686
2687 if (entity_is_task(se))
2688 tsk = task_of(se);
2689
Lucas De Marchi41acab82010-03-10 23:37:45 -03002690 if (se->statistics.sleep_start) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002691 u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002692
2693 if ((s64)delta < 0)
2694 delta = 0;
2695
Lucas De Marchi41acab82010-03-10 23:37:45 -03002696 if (unlikely(delta > se->statistics.sleep_max))
2697 se->statistics.sleep_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002698
Peter Zijlstra8c79a042012-01-30 14:51:37 +01002699 se->statistics.sleep_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03002700 se->statistics.sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +01002701
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002702 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +02002703 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002704 trace_sched_stat_sleep(tsk, delta);
2705 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002706 }
Lucas De Marchi41acab82010-03-10 23:37:45 -03002707 if (se->statistics.block_start) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002708 u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002709
2710 if ((s64)delta < 0)
2711 delta = 0;
2712
Lucas De Marchi41acab82010-03-10 23:37:45 -03002713 if (unlikely(delta > se->statistics.block_max))
2714 se->statistics.block_max = delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002715
Peter Zijlstra8c79a042012-01-30 14:51:37 +01002716 se->statistics.block_start = 0;
Lucas De Marchi41acab82010-03-10 23:37:45 -03002717 se->statistics.sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +02002718
Peter Zijlstrae4143142009-07-23 20:13:26 +02002719 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07002720 if (tsk->in_iowait) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002721 se->statistics.iowait_sum += delta;
2722 se->statistics.iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +02002723 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07002724 }
2725
Andrew Vaginb781a602011-11-28 12:03:35 +03002726 trace_sched_stat_blocked(tsk, delta);
2727
Peter Zijlstrae4143142009-07-23 20:13:26 +02002728 /*
2729 * Blocking time is in units of nanosecs, so shift by
2730 * 20 to get a milliseconds-range estimation of the
2731 * amount of time that the task spent sleeping:
2732 */
2733 if (unlikely(prof_on == SLEEP_PROFILING)) {
2734 profile_hits(SLEEP_PROFILING,
2735 (void *)get_wchan(tsk),
2736 delta >> 20);
2737 }
2738 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +02002739 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002740 }
2741#endif
2742}
2743
Peter Zijlstraddc97292007-10-15 17:00:10 +02002744static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
2745{
2746#ifdef CONFIG_SCHED_DEBUG
2747 s64 d = se->vruntime - cfs_rq->min_vruntime;
2748
2749 if (d < 0)
2750 d = -d;
2751
2752 if (d > 3*sysctl_sched_latency)
2753 schedstat_inc(cfs_rq, nr_spread_over);
2754#endif
2755}
2756
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002757static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002758place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
2759{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02002760 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02002761
Peter Zijlstra2cb86002007-11-09 22:39:37 +01002762 /*
2763 * The 'current' period is already promised to the current tasks,
2764 * however the extra weight of the new task will slow them down a
2765 * little, place the new task so that it fits in the slot that
2766 * stays open at the end.
2767 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02002768 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02002769 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002770
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002771 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01002772 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002773 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02002774
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002775 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002776 * Halve their sleep time's effect, to allow
2777 * for a gentler effect of sleepers:
2778 */
2779 if (sched_feat(GENTLE_FAIR_SLEEPERS))
2780 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02002781
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02002782 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002783 }
2784
Mike Galbraithb5d9d732009-09-08 11:12:28 +02002785 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05302786 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002787}
2788
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002789static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
2790
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002791static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002792enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002793{
2794 /*
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002795 * Update the normalized vruntime before updating min_vruntime
Kamalesh Babulal0fc576d2013-06-27 11:24:18 +05302796 * through calling update_curr().
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002797 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002798 if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002799 se->vruntime += cfs_rq->min_vruntime;
2800
2801 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002802 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002803 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +02002804 update_curr(cfs_rq);
Paul Turnerf269ae02012-10-04 13:18:31 +02002805 enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002806 account_entity_enqueue(cfs_rq, se);
2807 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002808
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002809 if (flags & ENQUEUE_WAKEUP) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02002810 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +02002811 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +02002812 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002813
Ingo Molnard2417e52007-08-09 11:16:47 +02002814 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02002815 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002816 if (se != cfs_rq->curr)
2817 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002818 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08002819
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002820 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08002821 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07002822 check_enqueue_throttle(cfs_rq);
2823 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002824}
2825
Rik van Riel2c13c9192011-02-01 09:48:37 -05002826static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01002827{
Rik van Riel2c13c9192011-02-01 09:48:37 -05002828 for_each_sched_entity(se) {
2829 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01002830 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05002831 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01002832
2833 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05002834 }
2835}
Peter Zijlstra2002c692008-11-11 11:52:33 +01002836
Rik van Riel2c13c9192011-02-01 09:48:37 -05002837static void __clear_buddies_next(struct sched_entity *se)
2838{
2839 for_each_sched_entity(se) {
2840 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01002841 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05002842 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01002843
2844 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05002845 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01002846}
2847
Rik van Rielac53db52011-02-01 09:51:03 -05002848static void __clear_buddies_skip(struct sched_entity *se)
2849{
2850 for_each_sched_entity(se) {
2851 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01002852 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05002853 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01002854
2855 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05002856 }
2857}
2858
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01002859static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
2860{
Rik van Riel2c13c9192011-02-01 09:48:37 -05002861 if (cfs_rq->last == se)
2862 __clear_buddies_last(se);
2863
2864 if (cfs_rq->next == se)
2865 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05002866
2867 if (cfs_rq->skip == se)
2868 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01002869}
2870
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07002871static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07002872
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002873static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002874dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002875{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002876 /*
2877 * Update run-time statistics of the 'current'.
2878 */
2879 update_curr(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002880 dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02002881
Ingo Molnar19b6a2e2007-08-09 11:16:48 +02002882 update_stats_dequeue(cfs_rq, se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002883 if (flags & DEQUEUE_SLEEP) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02002884#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002885 if (entity_is_task(se)) {
2886 struct task_struct *tsk = task_of(se);
2887
2888 if (tsk->state & TASK_INTERRUPTIBLE)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002889 se->statistics.sleep_start = rq_clock(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002890 if (tsk->state & TASK_UNINTERRUPTIBLE)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002891 se->statistics.block_start = rq_clock(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002892 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +02002893#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02002894 }
2895
Peter Zijlstra2002c692008-11-11 11:52:33 +01002896 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01002897
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002898 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002899 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002900 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002901 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002902
2903 /*
2904 * Normalize the entity after updating the min_vruntime because the
2905 * update can refer to the ->curr item and we need to reflect this
2906 * movement in our normalized position.
2907 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002908 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01002909 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07002910
Paul Turnerd8b49862011-07-21 09:43:41 -07002911 /* return excess runtime on last dequeue */
2912 return_cfs_rq_runtime(cfs_rq);
2913
Peter Zijlstra1e876232011-05-17 16:21:10 -07002914 update_min_vruntime(cfs_rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08002915 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002916}
2917
2918/*
2919 * Preempt the current task with a newly woken task if needed:
2920 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02002921static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02002922check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002923{
Peter Zijlstra11697832007-09-05 14:32:49 +02002924 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002925 struct sched_entity *se;
2926 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02002927
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02002928 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02002929 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01002930 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04002931 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01002932 /*
2933 * The current task ran long enough, ensure it doesn't get
2934 * re-elected due to buddy favours.
2935 */
2936 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02002937 return;
2938 }
2939
2940 /*
2941 * Ensure that a task that missed wakeup preemption by a
2942 * narrow margin doesn't have to wait for a full slice.
2943 * This also mitigates buddy induced latencies under load.
2944 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002945 if (delta_exec < sysctl_sched_min_granularity)
2946 return;
2947
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002948 se = __pick_first_entity(cfs_rq);
2949 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02002950
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002951 if (delta < 0)
2952 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01002953
Wang Xingchaof4cfb332011-09-16 13:35:52 -04002954 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04002955 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002956}
2957
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002958static void
Ingo Molnar8494f412007-08-09 11:16:48 +02002959set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002960{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002961 /* 'current' is not kept within the tree. */
2962 if (se->on_rq) {
2963 /*
2964 * Any task has to be enqueued before it get to execute on
2965 * a CPU. So account for the time it spent waiting on the
2966 * runqueue.
2967 */
2968 update_stats_wait_end(cfs_rq, se);
2969 __dequeue_entity(cfs_rq, se);
2970 }
2971
Ingo Molnar79303e92007-08-09 11:16:47 +02002972 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02002973 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002974#ifdef CONFIG_SCHEDSTATS
2975 /*
2976 * Track our maximum slice length, if the CPU's load is at
2977 * least twice that of our own weight (i.e. dont track it
2978 * when there are only lesser-weight tasks around):
2979 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002980 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03002981 se->statistics.slice_max = max(se->statistics.slice_max,
Ingo Molnareba1ed42007-10-15 17:00:02 +02002982 se->sum_exec_runtime - se->prev_sum_exec_runtime);
2983 }
2984#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +02002985 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002986}
2987
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02002988static int
2989wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
2990
Rik van Rielac53db52011-02-01 09:51:03 -05002991/*
2992 * Pick the next process, keeping these things in mind, in this order:
2993 * 1) keep things fair between processes/task groups
2994 * 2) pick the "next" process, since someone really wants that to run
2995 * 3) pick the "last" process, for cache locality
2996 * 4) do not run the "skip" process, if something else is available
2997 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01002998static struct sched_entity *
2999pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003000{
Peter Zijlstra678d5712012-02-11 06:05:00 +01003001 struct sched_entity *left = __pick_first_entity(cfs_rq);
3002 struct sched_entity *se;
3003
3004 /*
3005 * If curr is set we have to see if its left of the leftmost entity
3006 * still in the tree, provided there was anything in the tree at all.
3007 */
3008 if (!left || (curr && entity_before(curr, left)))
3009 left = curr;
3010
3011 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003012
Rik van Rielac53db52011-02-01 09:51:03 -05003013 /*
3014 * Avoid running the skip buddy, if running something else can
3015 * be done without getting too unfair.
3016 */
3017 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01003018 struct sched_entity *second;
3019
3020 if (se == curr) {
3021 second = __pick_first_entity(cfs_rq);
3022 } else {
3023 second = __pick_next_entity(se);
3024 if (!second || (curr && entity_before(curr, second)))
3025 second = curr;
3026 }
3027
Rik van Rielac53db52011-02-01 09:51:03 -05003028 if (second && wakeup_preempt_entity(second, left) < 1)
3029 se = second;
3030 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003031
Mike Galbraithf685cea2009-10-23 23:09:22 +02003032 /*
3033 * Prefer last buddy, try to return the CPU to a preempted task.
3034 */
3035 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
3036 se = cfs_rq->last;
3037
Rik van Rielac53db52011-02-01 09:51:03 -05003038 /*
3039 * Someone really wants this to run. If it's not unfair, run it.
3040 */
3041 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
3042 se = cfs_rq->next;
3043
Mike Galbraithf685cea2009-10-23 23:09:22 +02003044 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003045
3046 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003047}
3048
Peter Zijlstra678d5712012-02-11 06:05:00 +01003049static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003050
Ingo Molnarab6cde22007-08-09 11:16:48 +02003051static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003052{
3053 /*
3054 * If still on the runqueue then deactivate_task()
3055 * was not called and update_curr() has to be done:
3056 */
3057 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003058 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003059
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003060 /* throttle cfs_rqs exceeding runtime */
3061 check_cfs_rq_runtime(cfs_rq);
3062
Peter Zijlstraddc97292007-10-15 17:00:10 +02003063 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003064 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +02003065 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003066 /* Put 'current' back into the tree. */
3067 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02003068 /* in !on_rq case, update occurred at dequeue */
Paul Turner9ee474f2012-10-04 13:18:30 +02003069 update_entity_load_avg(prev, 1);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003070 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02003071 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003072}
3073
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003074static void
3075entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003076{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003077 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003078 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003079 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003080 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003081
Paul Turner43365bd2010-12-15 19:10:17 -08003082 /*
Paul Turner9d85f212012-10-04 13:18:29 +02003083 * Ensure that runnable average is periodically updated.
3084 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003085 update_entity_load_avg(curr, 1);
Paul Turneraff3e492012-10-04 13:18:30 +02003086 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstrabf0bd942013-07-26 23:48:42 +02003087 update_cfs_shares(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02003088
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003089#ifdef CONFIG_SCHED_HRTICK
3090 /*
3091 * queued ticks are scheduled to match the slice, so don't bother
3092 * validating it and just reschedule.
3093 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003094 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003095 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003096 return;
3097 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003098 /*
3099 * don't let the period tick interfere with the hrtick preemption
3100 */
3101 if (!sched_feat(DOUBLE_TICK) &&
3102 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
3103 return;
3104#endif
3105
Yong Zhang2c2efae2011-07-29 16:20:33 +08003106 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003107 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003108}
3109
Paul Turnerab84d312011-07-21 09:43:28 -07003110
3111/**************************************************
3112 * CFS bandwidth control machinery
3113 */
3114
3115#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02003116
3117#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01003118static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003119
3120static inline bool cfs_bandwidth_used(void)
3121{
Ingo Molnarc5905af2012-02-24 08:31:31 +01003122 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003123}
3124
Ben Segall1ee14e62013-10-16 11:16:12 -07003125void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003126{
Ben Segall1ee14e62013-10-16 11:16:12 -07003127 static_key_slow_inc(&__cfs_bandwidth_used);
3128}
3129
3130void cfs_bandwidth_usage_dec(void)
3131{
3132 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003133}
3134#else /* HAVE_JUMP_LABEL */
3135static bool cfs_bandwidth_used(void)
3136{
3137 return true;
3138}
3139
Ben Segall1ee14e62013-10-16 11:16:12 -07003140void cfs_bandwidth_usage_inc(void) {}
3141void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003142#endif /* HAVE_JUMP_LABEL */
3143
Paul Turnerab84d312011-07-21 09:43:28 -07003144/*
3145 * default period for cfs group bandwidth.
3146 * default: 0.1s, units: nanoseconds
3147 */
3148static inline u64 default_cfs_period(void)
3149{
3150 return 100000000ULL;
3151}
Paul Turnerec12cb72011-07-21 09:43:30 -07003152
3153static inline u64 sched_cfs_bandwidth_slice(void)
3154{
3155 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
3156}
3157
Paul Turnera9cf55b2011-07-21 09:43:32 -07003158/*
3159 * Replenish runtime according to assigned quota and update expiration time.
3160 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
3161 * additional synchronization around rq->lock.
3162 *
3163 * requires cfs_b->lock
3164 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02003165void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07003166{
3167 u64 now;
3168
3169 if (cfs_b->quota == RUNTIME_INF)
3170 return;
3171
3172 now = sched_clock_cpu(smp_processor_id());
3173 cfs_b->runtime = cfs_b->quota;
3174 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
3175}
3176
Peter Zijlstra029632f2011-10-25 10:00:11 +02003177static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
3178{
3179 return &tg->cfs_bandwidth;
3180}
3181
Paul Turnerf1b17282012-10-04 13:18:31 +02003182/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
3183static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
3184{
3185 if (unlikely(cfs_rq->throttle_count))
3186 return cfs_rq->throttled_clock_task;
3187
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003188 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02003189}
3190
Paul Turner85dac902011-07-21 09:43:33 -07003191/* returns 0 on failure to allocate runtime */
3192static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07003193{
3194 struct task_group *tg = cfs_rq->tg;
3195 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003196 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003197
3198 /* note: this is a positive sum as runtime_remaining <= 0 */
3199 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
3200
3201 raw_spin_lock(&cfs_b->lock);
3202 if (cfs_b->quota == RUNTIME_INF)
3203 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07003204 else {
Paul Turnera9cf55b2011-07-21 09:43:32 -07003205 /*
3206 * If the bandwidth pool has become inactive, then at least one
3207 * period must have elapsed since the last consumption.
3208 * Refresh the global state and ensure bandwidth timer becomes
3209 * active.
3210 */
3211 if (!cfs_b->timer_active) {
3212 __refill_cfs_bandwidth_runtime(cfs_b);
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04003213 __start_cfs_bandwidth(cfs_b, false);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003214 }
Paul Turner58088ad2011-07-21 09:43:31 -07003215
3216 if (cfs_b->runtime > 0) {
3217 amount = min(cfs_b->runtime, min_amount);
3218 cfs_b->runtime -= amount;
3219 cfs_b->idle = 0;
3220 }
Paul Turnerec12cb72011-07-21 09:43:30 -07003221 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07003222 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003223 raw_spin_unlock(&cfs_b->lock);
3224
3225 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003226 /*
3227 * we may have advanced our local expiration to account for allowed
3228 * spread between our sched_clock and the one on which runtime was
3229 * issued.
3230 */
3231 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
3232 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07003233
3234 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003235}
3236
3237/*
3238 * Note: This depends on the synchronization provided by sched_clock and the
3239 * fact that rq->clock snapshots this value.
3240 */
3241static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3242{
3243 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003244
3245 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003246 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07003247 return;
3248
3249 if (cfs_rq->runtime_remaining < 0)
3250 return;
3251
3252 /*
3253 * If the local deadline has passed we have to consider the
3254 * possibility that our sched_clock is 'fast' and the global deadline
3255 * has not truly expired.
3256 *
3257 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07003258 * whether the global deadline has advanced. It is valid to compare
3259 * cfs_b->runtime_expires without any locks since we only care about
3260 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07003261 */
3262
Ben Segall51f21762014-05-19 15:49:45 -07003263 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07003264 /* extend local deadline, drift is bounded above by 2 ticks */
3265 cfs_rq->runtime_expires += TICK_NSEC;
3266 } else {
3267 /* global deadline is ahead, expiration has passed */
3268 cfs_rq->runtime_remaining = 0;
3269 }
Paul Turnerec12cb72011-07-21 09:43:30 -07003270}
3271
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003272static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07003273{
Paul Turnera9cf55b2011-07-21 09:43:32 -07003274 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07003275 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003276 expire_cfs_rq_runtime(cfs_rq);
3277
3278 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07003279 return;
3280
Paul Turner85dac902011-07-21 09:43:33 -07003281 /*
3282 * if we're unable to extend our runtime we resched so that the active
3283 * hierarchy can be throttled
3284 */
3285 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04003286 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07003287}
3288
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003289static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003290void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07003291{
Paul Turner56f570e2011-11-07 20:26:33 -08003292 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07003293 return;
3294
3295 __account_cfs_rq_runtime(cfs_rq, delta_exec);
3296}
3297
Paul Turner85dac902011-07-21 09:43:33 -07003298static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
3299{
Paul Turner56f570e2011-11-07 20:26:33 -08003300 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07003301}
3302
Paul Turner64660c82011-07-21 09:43:36 -07003303/* check whether cfs_rq, or any parent, is throttled */
3304static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
3305{
Paul Turner56f570e2011-11-07 20:26:33 -08003306 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07003307}
3308
3309/*
3310 * Ensure that neither of the group entities corresponding to src_cpu or
3311 * dest_cpu are members of a throttled hierarchy when performing group
3312 * load-balance operations.
3313 */
3314static inline int throttled_lb_pair(struct task_group *tg,
3315 int src_cpu, int dest_cpu)
3316{
3317 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
3318
3319 src_cfs_rq = tg->cfs_rq[src_cpu];
3320 dest_cfs_rq = tg->cfs_rq[dest_cpu];
3321
3322 return throttled_hierarchy(src_cfs_rq) ||
3323 throttled_hierarchy(dest_cfs_rq);
3324}
3325
3326/* updated child weight may affect parent so we have to do this bottom up */
3327static int tg_unthrottle_up(struct task_group *tg, void *data)
3328{
3329 struct rq *rq = data;
3330 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3331
3332 cfs_rq->throttle_count--;
3333#ifdef CONFIG_SMP
3334 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02003335 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003336 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02003337 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07003338 }
3339#endif
3340
3341 return 0;
3342}
3343
3344static int tg_throttle_down(struct task_group *tg, void *data)
3345{
3346 struct rq *rq = data;
3347 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3348
Paul Turner82958362012-10-04 13:18:31 +02003349 /* group is entering throttled state, stop time */
3350 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003351 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07003352 cfs_rq->throttle_count++;
3353
3354 return 0;
3355}
3356
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003357static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07003358{
3359 struct rq *rq = rq_of(cfs_rq);
3360 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3361 struct sched_entity *se;
3362 long task_delta, dequeue = 1;
3363
3364 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
3365
Paul Turnerf1b17282012-10-04 13:18:31 +02003366 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07003367 rcu_read_lock();
3368 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
3369 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07003370
3371 task_delta = cfs_rq->h_nr_running;
3372 for_each_sched_entity(se) {
3373 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
3374 /* throttled entity or throttle-on-deactivate */
3375 if (!se->on_rq)
3376 break;
3377
3378 if (dequeue)
3379 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
3380 qcfs_rq->h_nr_running -= task_delta;
3381
3382 if (qcfs_rq->load.weight)
3383 dequeue = 0;
3384 }
3385
3386 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04003387 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07003388
3389 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003390 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07003391 raw_spin_lock(&cfs_b->lock);
Ben Segallc06f04c2014-06-20 15:21:20 -07003392 /*
3393 * Add to the _head_ of the list, so that an already-started
3394 * distribute_cfs_runtime will not see us
3395 */
3396 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Ben Segallf9f9ffc2013-10-16 11:16:32 -07003397 if (!cfs_b->timer_active)
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04003398 __start_cfs_bandwidth(cfs_b, false);
Paul Turner85dac902011-07-21 09:43:33 -07003399 raw_spin_unlock(&cfs_b->lock);
3400}
3401
Peter Zijlstra029632f2011-10-25 10:00:11 +02003402void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07003403{
3404 struct rq *rq = rq_of(cfs_rq);
3405 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3406 struct sched_entity *se;
3407 int enqueue = 1;
3408 long task_delta;
3409
Michael Wang22b958d2013-06-04 14:23:39 +08003410 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07003411
3412 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02003413
3414 update_rq_clock(rq);
3415
Paul Turner671fd9d2011-07-21 09:43:34 -07003416 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003417 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07003418 list_del_rcu(&cfs_rq->throttled_list);
3419 raw_spin_unlock(&cfs_b->lock);
3420
Paul Turner64660c82011-07-21 09:43:36 -07003421 /* update hierarchical throttle state */
3422 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
3423
Paul Turner671fd9d2011-07-21 09:43:34 -07003424 if (!cfs_rq->load.weight)
3425 return;
3426
3427 task_delta = cfs_rq->h_nr_running;
3428 for_each_sched_entity(se) {
3429 if (se->on_rq)
3430 enqueue = 0;
3431
3432 cfs_rq = cfs_rq_of(se);
3433 if (enqueue)
3434 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
3435 cfs_rq->h_nr_running += task_delta;
3436
3437 if (cfs_rq_throttled(cfs_rq))
3438 break;
3439 }
3440
3441 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04003442 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07003443
3444 /* determine whether we need to wake up potentially idle cpu */
3445 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04003446 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07003447}
3448
3449static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
3450 u64 remaining, u64 expires)
3451{
3452 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07003453 u64 runtime;
3454 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07003455
3456 rcu_read_lock();
3457 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
3458 throttled_list) {
3459 struct rq *rq = rq_of(cfs_rq);
3460
3461 raw_spin_lock(&rq->lock);
3462 if (!cfs_rq_throttled(cfs_rq))
3463 goto next;
3464
3465 runtime = -cfs_rq->runtime_remaining + 1;
3466 if (runtime > remaining)
3467 runtime = remaining;
3468 remaining -= runtime;
3469
3470 cfs_rq->runtime_remaining += runtime;
3471 cfs_rq->runtime_expires = expires;
3472
3473 /* we check whether we're throttled above */
3474 if (cfs_rq->runtime_remaining > 0)
3475 unthrottle_cfs_rq(cfs_rq);
3476
3477next:
3478 raw_spin_unlock(&rq->lock);
3479
3480 if (!remaining)
3481 break;
3482 }
3483 rcu_read_unlock();
3484
Ben Segallc06f04c2014-06-20 15:21:20 -07003485 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07003486}
3487
Paul Turner58088ad2011-07-21 09:43:31 -07003488/*
3489 * Responsible for refilling a task_group's bandwidth and unthrottling its
3490 * cfs_rqs as appropriate. If there has been no activity within the last
3491 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
3492 * used to track this state.
3493 */
3494static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
3495{
Paul Turner671fd9d2011-07-21 09:43:34 -07003496 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07003497 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07003498
Paul Turner58088ad2011-07-21 09:43:31 -07003499 /* no need to continue the timer with no bandwidth constraint */
3500 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07003501 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07003502
Paul Turner671fd9d2011-07-21 09:43:34 -07003503 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07003504 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07003505
Ben Segall51f21762014-05-19 15:49:45 -07003506 /*
3507 * idle depends on !throttled (for the case of a large deficit), and if
3508 * we're going inactive then everything else can be deferred
3509 */
3510 if (cfs_b->idle && !throttled)
3511 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003512
Ben Segall927b54f2013-10-16 11:16:22 -07003513 /*
3514 * if we have relooped after returning idle once, we need to update our
3515 * status as actually running, so that other cpus doing
3516 * __start_cfs_bandwidth will stop trying to cancel us.
3517 */
3518 cfs_b->timer_active = 1;
3519
Paul Turnera9cf55b2011-07-21 09:43:32 -07003520 __refill_cfs_bandwidth_runtime(cfs_b);
3521
Paul Turner671fd9d2011-07-21 09:43:34 -07003522 if (!throttled) {
3523 /* mark as potentially idle for the upcoming period */
3524 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07003525 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07003526 }
Paul Turner58088ad2011-07-21 09:43:31 -07003527
Nikhil Raoe8da1b12011-07-21 09:43:40 -07003528 /* account preceding periods in which throttling occurred */
3529 cfs_b->nr_throttled += overrun;
3530
Paul Turner671fd9d2011-07-21 09:43:34 -07003531 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07003532
3533 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07003534 * This check is repeated as we are holding onto the new bandwidth while
3535 * we unthrottle. This can potentially race with an unthrottled group
3536 * trying to acquire new bandwidth from the global pool. This can result
3537 * in us over-using our runtime if it is all used during this loop, but
3538 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07003539 */
Ben Segallc06f04c2014-06-20 15:21:20 -07003540 while (throttled && cfs_b->runtime > 0) {
3541 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07003542 raw_spin_unlock(&cfs_b->lock);
3543 /* we can't nest cfs_b->lock while distributing bandwidth */
3544 runtime = distribute_cfs_runtime(cfs_b, runtime,
3545 runtime_expires);
3546 raw_spin_lock(&cfs_b->lock);
3547
3548 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07003549
3550 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07003551 }
3552
Paul Turner671fd9d2011-07-21 09:43:34 -07003553 /*
3554 * While we are ensured activity in the period following an
3555 * unthrottle, this also covers the case in which the new bandwidth is
3556 * insufficient to cover the existing bandwidth deficit. (Forcing the
3557 * timer to remain active while there are any throttled entities.)
3558 */
3559 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07003560
Ben Segall51f21762014-05-19 15:49:45 -07003561 return 0;
3562
3563out_deactivate:
3564 cfs_b->timer_active = 0;
3565 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07003566}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003567
Paul Turnerd8b49862011-07-21 09:43:41 -07003568/* a cfs_rq won't donate quota below this amount */
3569static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
3570/* minimum remaining period time to redistribute slack quota */
3571static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
3572/* how long we wait to gather additional slack before distributing */
3573static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
3574
Ben Segalldb06e782013-10-16 11:16:17 -07003575/*
3576 * Are we near the end of the current quota period?
3577 *
3578 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
3579 * hrtimer base being cleared by __hrtimer_start_range_ns. In the case of
3580 * migrate_hrtimers, base is never cleared, so we are fine.
3581 */
Paul Turnerd8b49862011-07-21 09:43:41 -07003582static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
3583{
3584 struct hrtimer *refresh_timer = &cfs_b->period_timer;
3585 u64 remaining;
3586
3587 /* if the call-back is running a quota refresh is already occurring */
3588 if (hrtimer_callback_running(refresh_timer))
3589 return 1;
3590
3591 /* is a quota refresh about to occur? */
3592 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
3593 if (remaining < min_expire)
3594 return 1;
3595
3596 return 0;
3597}
3598
3599static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
3600{
3601 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
3602
3603 /* if there's a quota refresh soon don't bother with slack */
3604 if (runtime_refresh_within(cfs_b, min_left))
3605 return;
3606
3607 start_bandwidth_timer(&cfs_b->slack_timer,
3608 ns_to_ktime(cfs_bandwidth_slack_period));
3609}
3610
3611/* we know any runtime found here is valid as update_curr() precedes return */
3612static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3613{
3614 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3615 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
3616
3617 if (slack_runtime <= 0)
3618 return;
3619
3620 raw_spin_lock(&cfs_b->lock);
3621 if (cfs_b->quota != RUNTIME_INF &&
3622 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
3623 cfs_b->runtime += slack_runtime;
3624
3625 /* we are under rq->lock, defer unthrottling using a timer */
3626 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
3627 !list_empty(&cfs_b->throttled_cfs_rq))
3628 start_cfs_slack_bandwidth(cfs_b);
3629 }
3630 raw_spin_unlock(&cfs_b->lock);
3631
3632 /* even if it's not valid for return we don't want to try again */
3633 cfs_rq->runtime_remaining -= slack_runtime;
3634}
3635
3636static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3637{
Paul Turner56f570e2011-11-07 20:26:33 -08003638 if (!cfs_bandwidth_used())
3639 return;
3640
Paul Turnerfccfdc62011-11-07 20:26:34 -08003641 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07003642 return;
3643
3644 __return_cfs_rq_runtime(cfs_rq);
3645}
3646
3647/*
3648 * This is done with a timer (instead of inline with bandwidth return) since
3649 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
3650 */
3651static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
3652{
3653 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
3654 u64 expires;
3655
3656 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07003657 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07003658 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
3659 raw_spin_unlock(&cfs_b->lock);
3660 return;
3661 }
3662
Ben Segallc06f04c2014-06-20 15:21:20 -07003663 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07003664 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07003665
Paul Turnerd8b49862011-07-21 09:43:41 -07003666 expires = cfs_b->runtime_expires;
3667 raw_spin_unlock(&cfs_b->lock);
3668
3669 if (!runtime)
3670 return;
3671
3672 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
3673
3674 raw_spin_lock(&cfs_b->lock);
3675 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07003676 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07003677 raw_spin_unlock(&cfs_b->lock);
3678}
3679
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003680/*
3681 * When a group wakes up we want to make sure that its quota is not already
3682 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
3683 * runtime as update_curr() throttling can not not trigger until it's on-rq.
3684 */
3685static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
3686{
Paul Turner56f570e2011-11-07 20:26:33 -08003687 if (!cfs_bandwidth_used())
3688 return;
3689
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003690 /* an active group must be handled by the update_curr()->put() path */
3691 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
3692 return;
3693
3694 /* ensure the group is not already throttled */
3695 if (cfs_rq_throttled(cfs_rq))
3696 return;
3697
3698 /* update runtime allocation */
3699 account_cfs_rq_runtime(cfs_rq, 0);
3700 if (cfs_rq->runtime_remaining <= 0)
3701 throttle_cfs_rq(cfs_rq);
3702}
3703
3704/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01003705static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003706{
Paul Turner56f570e2011-11-07 20:26:33 -08003707 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01003708 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08003709
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003710 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01003711 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003712
3713 /*
3714 * it's possible for a throttled entity to be forced into a running
3715 * state (e.g. set_curr_task), in this case we're finished.
3716 */
3717 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01003718 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003719
3720 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01003721 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003722}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003723
Peter Zijlstra029632f2011-10-25 10:00:11 +02003724static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
3725{
3726 struct cfs_bandwidth *cfs_b =
3727 container_of(timer, struct cfs_bandwidth, slack_timer);
3728 do_sched_cfs_slack_timer(cfs_b);
3729
3730 return HRTIMER_NORESTART;
3731}
3732
3733static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
3734{
3735 struct cfs_bandwidth *cfs_b =
3736 container_of(timer, struct cfs_bandwidth, period_timer);
3737 ktime_t now;
3738 int overrun;
3739 int idle = 0;
3740
Ben Segall51f21762014-05-19 15:49:45 -07003741 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003742 for (;;) {
3743 now = hrtimer_cb_get_time(timer);
3744 overrun = hrtimer_forward(timer, now, cfs_b->period);
3745
3746 if (!overrun)
3747 break;
3748
3749 idle = do_sched_cfs_period_timer(cfs_b, overrun);
3750 }
Ben Segall51f21762014-05-19 15:49:45 -07003751 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003752
3753 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
3754}
3755
3756void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3757{
3758 raw_spin_lock_init(&cfs_b->lock);
3759 cfs_b->runtime = 0;
3760 cfs_b->quota = RUNTIME_INF;
3761 cfs_b->period = ns_to_ktime(default_cfs_period());
3762
3763 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
3764 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3765 cfs_b->period_timer.function = sched_cfs_period_timer;
3766 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3767 cfs_b->slack_timer.function = sched_cfs_slack_timer;
3768}
3769
3770static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3771{
3772 cfs_rq->runtime_enabled = 0;
3773 INIT_LIST_HEAD(&cfs_rq->throttled_list);
3774}
3775
3776/* requires cfs_b->lock, may release to reprogram timer */
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04003777void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b, bool force)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003778{
3779 /*
3780 * The timer may be active because we're trying to set a new bandwidth
3781 * period or because we're racing with the tear-down path
3782 * (timer_active==0 becomes visible before the hrtimer call-back
3783 * terminates). In either case we ensure that it's re-programmed
3784 */
Ben Segall927b54f2013-10-16 11:16:22 -07003785 while (unlikely(hrtimer_active(&cfs_b->period_timer)) &&
3786 hrtimer_try_to_cancel(&cfs_b->period_timer) < 0) {
3787 /* bounce the lock to allow do_sched_cfs_period_timer to run */
Peter Zijlstra029632f2011-10-25 10:00:11 +02003788 raw_spin_unlock(&cfs_b->lock);
Ben Segall927b54f2013-10-16 11:16:22 -07003789 cpu_relax();
Peter Zijlstra029632f2011-10-25 10:00:11 +02003790 raw_spin_lock(&cfs_b->lock);
3791 /* if someone else restarted the timer then we're done */
Roman Gushchin09dc4ab2014-05-19 15:10:09 +04003792 if (!force && cfs_b->timer_active)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003793 return;
3794 }
3795
3796 cfs_b->timer_active = 1;
3797 start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
3798}
3799
3800static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3801{
3802 hrtimer_cancel(&cfs_b->period_timer);
3803 hrtimer_cancel(&cfs_b->slack_timer);
3804}
3805
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04003806static void __maybe_unused update_runtime_enabled(struct rq *rq)
3807{
3808 struct cfs_rq *cfs_rq;
3809
3810 for_each_leaf_cfs_rq(rq, cfs_rq) {
3811 struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
3812
3813 raw_spin_lock(&cfs_b->lock);
3814 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
3815 raw_spin_unlock(&cfs_b->lock);
3816 }
3817}
3818
Arnd Bergmann38dc3342013-01-25 14:14:22 +00003819static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003820{
3821 struct cfs_rq *cfs_rq;
3822
3823 for_each_leaf_cfs_rq(rq, cfs_rq) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02003824 if (!cfs_rq->runtime_enabled)
3825 continue;
3826
3827 /*
3828 * clock_task is not advancing so we just need to make sure
3829 * there's some valid quota amount
3830 */
Ben Segall51f21762014-05-19 15:49:45 -07003831 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04003832 /*
3833 * Offline rq is schedulable till cpu is completely disabled
3834 * in take_cpu_down(), so we prevent new cfs throttling here.
3835 */
3836 cfs_rq->runtime_enabled = 0;
3837
Peter Zijlstra029632f2011-10-25 10:00:11 +02003838 if (cfs_rq_throttled(cfs_rq))
3839 unthrottle_cfs_rq(cfs_rq);
3840 }
3841}
3842
3843#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02003844static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
3845{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003846 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02003847}
3848
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003849static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01003850static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003851static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003852static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07003853
3854static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
3855{
3856 return 0;
3857}
Paul Turner64660c82011-07-21 09:43:36 -07003858
3859static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
3860{
3861 return 0;
3862}
3863
3864static inline int throttled_lb_pair(struct task_group *tg,
3865 int src_cpu, int dest_cpu)
3866{
3867 return 0;
3868}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003869
3870void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
3871
3872#ifdef CONFIG_FAIR_GROUP_SCHED
3873static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07003874#endif
3875
Peter Zijlstra029632f2011-10-25 10:00:11 +02003876static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
3877{
3878 return NULL;
3879}
3880static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04003881static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07003882static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003883
3884#endif /* CONFIG_CFS_BANDWIDTH */
3885
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003886/**************************************************
3887 * CFS operations on tasks:
3888 */
3889
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003890#ifdef CONFIG_SCHED_HRTICK
3891static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
3892{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003893 struct sched_entity *se = &p->se;
3894 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3895
3896 WARN_ON(task_rq(p) != rq);
3897
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003898 if (cfs_rq->nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003899 u64 slice = sched_slice(cfs_rq, se);
3900 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
3901 s64 delta = slice - ran;
3902
3903 if (delta < 0) {
3904 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04003905 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003906 return;
3907 }
Peter Zijlstra31656512008-07-18 18:01:23 +02003908 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003909 }
3910}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003911
3912/*
3913 * called from enqueue/dequeue and updates the hrtick when the
3914 * current task is from our class and nr_running is low enough
3915 * to matter.
3916 */
3917static void hrtick_update(struct rq *rq)
3918{
3919 struct task_struct *curr = rq->curr;
3920
Mike Galbraithb39e66e2011-11-22 15:20:07 +01003921 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003922 return;
3923
3924 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
3925 hrtick_start_fair(rq, curr);
3926}
Dhaval Giani55e12e52008-06-24 23:39:43 +05303927#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003928static inline void
3929hrtick_start_fair(struct rq *rq, struct task_struct *p)
3930{
3931}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003932
3933static inline void hrtick_update(struct rq *rq)
3934{
3935}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003936#endif
3937
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003938/*
3939 * The enqueue_task method is called before nr_running is
3940 * increased. Here we update the fair scheduling stats and
3941 * then put the task into the rbtree:
3942 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00003943static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003944enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003945{
3946 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003947 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003948
3949 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003950 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003951 break;
3952 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003953 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07003954
3955 /*
3956 * end evaluation on encountering a throttled cfs_rq
3957 *
3958 * note: in the case of encountering a throttled cfs_rq we will
3959 * post the final h_nr_running increment below.
3960 */
3961 if (cfs_rq_throttled(cfs_rq))
3962 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07003963 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07003964
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003965 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003966 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003967
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003968 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08003969 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07003970 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003971
Paul Turner85dac902011-07-21 09:43:33 -07003972 if (cfs_rq_throttled(cfs_rq))
3973 break;
3974
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003975 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02003976 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003977 }
3978
Ben Segall18bf2802012-10-04 12:51:20 +02003979 if (!se) {
3980 update_rq_runnable_avg(rq, rq->nr_running);
Kirill Tkhai72465442014-05-09 03:00:14 +04003981 add_nr_running(rq, 1);
Ben Segall18bf2802012-10-04 12:51:20 +02003982 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02003983 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003984}
3985
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003986static void set_next_buddy(struct sched_entity *se);
3987
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003988/*
3989 * The dequeue_task method is called before nr_running is
3990 * decreased. We remove the task from the rbtree and
3991 * update the fair scheduling stats:
3992 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003993static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003994{
3995 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01003996 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07003997 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003998
3999 for_each_sched_entity(se) {
4000 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004001 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004002
4003 /*
4004 * end evaluation on encountering a throttled cfs_rq
4005 *
4006 * note: in the case of encountering a throttled cfs_rq we will
4007 * post the final h_nr_running decrement below.
4008 */
4009 if (cfs_rq_throttled(cfs_rq))
4010 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004011 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004012
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004013 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004014 if (cfs_rq->load.weight) {
4015 /*
4016 * Bias pick_next to pick a task from this cfs_rq, as
4017 * p is sleeping when it is within its sched_slice.
4018 */
4019 if (task_sleep && parent_entity(se))
4020 set_next_buddy(parent_entity(se));
Paul Turner9598c822011-07-06 22:30:37 -07004021
4022 /* avoid re-evaluating load for this entity */
4023 se = parent_entity(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004024 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004025 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004026 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004027 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004028
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004029 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004030 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004031 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004032
Paul Turner85dac902011-07-21 09:43:33 -07004033 if (cfs_rq_throttled(cfs_rq))
4034 break;
4035
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004036 update_cfs_shares(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02004037 update_entity_load_avg(se, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004038 }
4039
Ben Segall18bf2802012-10-04 12:51:20 +02004040 if (!se) {
Kirill Tkhai72465442014-05-09 03:00:14 +04004041 sub_nr_running(rq, 1);
Ben Segall18bf2802012-10-04 12:51:20 +02004042 update_rq_runnable_avg(rq, 1);
4043 }
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004044 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004045}
4046
Gregory Haskinse7693a32008-01-25 21:08:09 +01004047#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02004048/* Used instead of source_load when we know the type == 0 */
4049static unsigned long weighted_cpuload(const int cpu)
4050{
Alex Shib92486c2013-06-20 10:18:50 +08004051 return cpu_rq(cpu)->cfs.runnable_load_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004052}
4053
4054/*
4055 * Return a low guess at the load of a migration-source cpu weighted
4056 * according to the scheduling class and "nice" value.
4057 *
4058 * We want to under-estimate the load of migration sources, to
4059 * balance conservatively.
4060 */
4061static unsigned long source_load(int cpu, int type)
4062{
4063 struct rq *rq = cpu_rq(cpu);
4064 unsigned long total = weighted_cpuload(cpu);
4065
4066 if (type == 0 || !sched_feat(LB_BIAS))
4067 return total;
4068
4069 return min(rq->cpu_load[type-1], total);
4070}
4071
4072/*
4073 * Return a high guess at the load of a migration-target cpu weighted
4074 * according to the scheduling class and "nice" value.
4075 */
4076static unsigned long target_load(int cpu, int type)
4077{
4078 struct rq *rq = cpu_rq(cpu);
4079 unsigned long total = weighted_cpuload(cpu);
4080
4081 if (type == 0 || !sched_feat(LB_BIAS))
4082 return total;
4083
4084 return max(rq->cpu_load[type-1], total);
4085}
4086
Nicolas Pitreced549f2014-05-26 18:19:38 -04004087static unsigned long capacity_of(int cpu)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004088{
Nicolas Pitreced549f2014-05-26 18:19:38 -04004089 return cpu_rq(cpu)->cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004090}
4091
4092static unsigned long cpu_avg_load_per_task(int cpu)
4093{
4094 struct rq *rq = cpu_rq(cpu);
Vincent Guittot65fdac02014-08-26 13:06:46 +02004095 unsigned long nr_running = ACCESS_ONCE(rq->cfs.h_nr_running);
Alex Shib92486c2013-06-20 10:18:50 +08004096 unsigned long load_avg = rq->cfs.runnable_load_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004097
4098 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08004099 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004100
4101 return 0;
4102}
4103
Michael Wang62470412013-07-04 12:55:51 +08004104static void record_wakee(struct task_struct *p)
4105{
4106 /*
4107 * Rough decay (wiping) for cost saving, don't worry
4108 * about the boundary, really active task won't care
4109 * about the loss.
4110 */
Manuel Schölling2538d962014-05-22 19:45:23 +02004111 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
Rik van Riel096aa332014-05-16 00:13:32 -04004112 current->wakee_flips >>= 1;
Michael Wang62470412013-07-04 12:55:51 +08004113 current->wakee_flip_decay_ts = jiffies;
4114 }
4115
4116 if (current->last_wakee != p) {
4117 current->last_wakee = p;
4118 current->wakee_flips++;
4119 }
4120}
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004121
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02004122static void task_waking_fair(struct task_struct *p)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004123{
4124 struct sched_entity *se = &p->se;
4125 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra3fe16982011-04-05 17:23:48 +02004126 u64 min_vruntime;
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004127
Peter Zijlstra3fe16982011-04-05 17:23:48 +02004128#ifndef CONFIG_64BIT
4129 u64 min_vruntime_copy;
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02004130
Peter Zijlstra3fe16982011-04-05 17:23:48 +02004131 do {
4132 min_vruntime_copy = cfs_rq->min_vruntime_copy;
4133 smp_rmb();
4134 min_vruntime = cfs_rq->min_vruntime;
4135 } while (min_vruntime != min_vruntime_copy);
4136#else
4137 min_vruntime = cfs_rq->min_vruntime;
4138#endif
4139
4140 se->vruntime -= min_vruntime;
Michael Wang62470412013-07-04 12:55:51 +08004141 record_wakee(p);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004142}
4143
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004144#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02004145/*
4146 * effective_load() calculates the load change as seen from the root_task_group
4147 *
4148 * Adding load to a group doesn't make a group heavier, but can cause movement
4149 * of group shares between cpus. Assuming the shares were perfectly aligned one
4150 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004151 *
4152 * Calculate the effective load difference if @wl is added (subtracted) to @tg
4153 * on this @cpu and results in a total addition (subtraction) of @wg to the
4154 * total group weight.
4155 *
4156 * Given a runqueue weight distribution (rw_i) we can compute a shares
4157 * distribution (s_i) using:
4158 *
4159 * s_i = rw_i / \Sum rw_j (1)
4160 *
4161 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
4162 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
4163 * shares distribution (s_i):
4164 *
4165 * rw_i = { 2, 4, 1, 0 }
4166 * s_i = { 2/7, 4/7, 1/7, 0 }
4167 *
4168 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
4169 * task used to run on and the CPU the waker is running on), we need to
4170 * compute the effect of waking a task on either CPU and, in case of a sync
4171 * wakeup, compute the effect of the current task going to sleep.
4172 *
4173 * So for a change of @wl to the local @cpu with an overall group weight change
4174 * of @wl we can compute the new shares distribution (s'_i) using:
4175 *
4176 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
4177 *
4178 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
4179 * differences in waking a task to CPU 0. The additional task changes the
4180 * weight and shares distributions like:
4181 *
4182 * rw'_i = { 3, 4, 1, 0 }
4183 * s'_i = { 3/8, 4/8, 1/8, 0 }
4184 *
4185 * We can then compute the difference in effective weight by using:
4186 *
4187 * dw_i = S * (s'_i - s_i) (3)
4188 *
4189 * Where 'S' is the group weight as seen by its parent.
4190 *
4191 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
4192 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
4193 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02004194 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004195static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004196{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004197 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02004198
Rik van Riel9722c2d2014-01-06 11:39:12 +00004199 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02004200 return wl;
4201
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004202 for_each_sched_entity(se) {
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004203 long w, W;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004204
Paul Turner977dda72011-01-14 17:57:50 -08004205 tg = se->my_q->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004206
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004207 /*
4208 * W = @wg + \Sum rw_j
4209 */
4210 W = wg + calc_tg_weight(tg, se->my_q);
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004211
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004212 /*
4213 * w = rw_i + @wl
4214 */
4215 w = se->my_q->load.weight + wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02004216
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004217 /*
4218 * wl = S * s'_i; see (2)
4219 */
4220 if (W > 0 && w < W)
4221 wl = (w * tg->shares) / W;
Paul Turner977dda72011-01-14 17:57:50 -08004222 else
4223 wl = tg->shares;
Peter Zijlstra940959e2008-09-23 15:33:42 +02004224
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004225 /*
4226 * Per the above, wl is the new se->load.weight value; since
4227 * those are clipped to [MIN_SHARES, ...) do so now. See
4228 * calc_cfs_shares().
4229 */
Paul Turner977dda72011-01-14 17:57:50 -08004230 if (wl < MIN_SHARES)
4231 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004232
4233 /*
4234 * wl = dw_i = S * (s'_i - s_i); see (3)
4235 */
Paul Turner977dda72011-01-14 17:57:50 -08004236 wl -= se->load.weight;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02004237
4238 /*
4239 * Recursively apply this logic to all parent groups to compute
4240 * the final effective load change on the root group. Since
4241 * only the @tg group gets extra weight, all parent groups can
4242 * only redistribute existing shares. @wl is the shift in shares
4243 * resulting from this level per the above.
4244 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004245 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004246 }
4247
4248 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004249}
4250#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004251
Mel Gorman58d081b2013-10-07 11:29:10 +01004252static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004253{
Peter Zijlstra83378262008-06-27 13:41:37 +02004254 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004255}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02004256
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02004257#endif
4258
Michael Wang62470412013-07-04 12:55:51 +08004259static int wake_wide(struct task_struct *p)
4260{
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08004261 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08004262
4263 /*
4264 * Yeah, it's the switching-frequency, could means many wakee or
4265 * rapidly switch, use factor here will just help to automatically
4266 * adjust the loose-degree, so bigger node will lead to more pull.
4267 */
4268 if (p->wakee_flips > factor) {
4269 /*
4270 * wakee is somewhat hot, it needs certain amount of cpu
4271 * resource, so if waker is far more hot, prefer to leave
4272 * it alone.
4273 */
4274 if (current->wakee_flips > (factor * p->wakee_flips))
4275 return 1;
4276 }
4277
4278 return 0;
4279}
4280
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004281static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004282{
Paul Turnere37b6a72011-01-21 20:44:59 -08004283 s64 this_load, load;
Vincent Guittotbd61c982014-08-26 13:06:50 +02004284 s64 this_eff_load, prev_eff_load;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004285 int idx, this_cpu, prev_cpu;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004286 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02004287 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004288 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004289
Michael Wang62470412013-07-04 12:55:51 +08004290 /*
4291 * If we wake multiple tasks be careful to not bounce
4292 * ourselves around too much.
4293 */
4294 if (wake_wide(p))
4295 return 0;
4296
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004297 idx = sd->wake_idx;
4298 this_cpu = smp_processor_id();
4299 prev_cpu = task_cpu(p);
4300 load = source_load(prev_cpu, idx);
4301 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004302
4303 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004304 * If sync wakeup then subtract the (maximum possible)
4305 * effect of the currently running task from the load
4306 * of the current CPU:
4307 */
Peter Zijlstra83378262008-06-27 13:41:37 +02004308 if (sync) {
4309 tg = task_group(current);
4310 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004311
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004312 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02004313 load += effective_load(tg, prev_cpu, 0, -weight);
4314 }
4315
4316 tg = task_group(p);
4317 weight = p->se.load.weight;
4318
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02004319 /*
4320 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004321 * due to the sync cause above having dropped this_load to 0, we'll
4322 * always have an imbalance, but there's really nothing you can do
4323 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02004324 *
4325 * Otherwise check if either cpus are near enough in load to allow this
4326 * task to be woken on this_cpu.
4327 */
Vincent Guittotbd61c982014-08-26 13:06:50 +02004328 this_eff_load = 100;
4329 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004330
Vincent Guittotbd61c982014-08-26 13:06:50 +02004331 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
4332 prev_eff_load *= capacity_of(this_cpu);
4333
4334 if (this_load > 0) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004335 this_eff_load *= this_load +
4336 effective_load(tg, this_cpu, weight, weight);
4337
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004338 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
Vincent Guittotbd61c982014-08-26 13:06:50 +02004339 }
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02004340
Vincent Guittotbd61c982014-08-26 13:06:50 +02004341 balanced = this_eff_load <= prev_eff_load;
4342
Lucas De Marchi41acab82010-03-10 23:37:45 -03004343 schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02004344
Vincent Guittot05bfb652014-08-26 13:06:45 +02004345 if (!balanced)
4346 return 0;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004347
Vincent Guittot05bfb652014-08-26 13:06:45 +02004348 schedstat_inc(sd, ttwu_move_affine);
4349 schedstat_inc(p, se.statistics.nr_wakeups_affine);
4350
4351 return 1;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01004352}
4353
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004354/*
4355 * find_idlest_group finds and returns the least busy CPU group within the
4356 * domain.
4357 */
4358static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02004359find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004360 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01004361{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07004362 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004363 unsigned long min_load = ULONG_MAX, this_load = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004364 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004365 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004366
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004367 if (sd_flag & SD_BALANCE_WAKE)
4368 load_idx = sd->wake_idx;
4369
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004370 do {
4371 unsigned long load, avg_load;
4372 int local_group;
4373 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004374
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004375 /* Skip over this group if it has no CPUs allowed */
4376 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004377 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004378 continue;
4379
4380 local_group = cpumask_test_cpu(this_cpu,
4381 sched_group_cpus(group));
4382
4383 /* Tally up the load of all CPUs in the group */
4384 avg_load = 0;
4385
4386 for_each_cpu(i, sched_group_cpus(group)) {
4387 /* Bias balancing toward cpus of our domain */
4388 if (local_group)
4389 load = source_load(i, load_idx);
4390 else
4391 load = target_load(i, load_idx);
4392
4393 avg_load += load;
4394 }
4395
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04004396 /* Adjust by relative CPU capacity of the group */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04004397 avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004398
4399 if (local_group) {
4400 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004401 } else if (avg_load < min_load) {
4402 min_load = avg_load;
4403 idlest = group;
4404 }
4405 } while (group = group->next, group != sd->groups);
4406
4407 if (!idlest || 100*this_load < imbalance*min_load)
4408 return NULL;
4409 return idlest;
4410}
4411
4412/*
4413 * find_idlest_cpu - find the idlest cpu among the cpus in group.
4414 */
4415static int
4416find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
4417{
4418 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04004419 unsigned int min_exit_latency = UINT_MAX;
4420 u64 latest_idle_timestamp = 0;
4421 int least_loaded_cpu = this_cpu;
4422 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004423 int i;
4424
4425 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02004426 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04004427 if (idle_cpu(i)) {
4428 struct rq *rq = cpu_rq(i);
4429 struct cpuidle_state *idle = idle_get_state(rq);
4430 if (idle && idle->exit_latency < min_exit_latency) {
4431 /*
4432 * We give priority to a CPU whose idle state
4433 * has the smallest exit latency irrespective
4434 * of any idle timestamp.
4435 */
4436 min_exit_latency = idle->exit_latency;
4437 latest_idle_timestamp = rq->idle_stamp;
4438 shallowest_idle_cpu = i;
4439 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
4440 rq->idle_stamp > latest_idle_timestamp) {
4441 /*
4442 * If equal or no active idle state, then
4443 * the most recently idled CPU might have
4444 * a warmer cache.
4445 */
4446 latest_idle_timestamp = rq->idle_stamp;
4447 shallowest_idle_cpu = i;
4448 }
4449 } else {
4450 load = weighted_cpuload(i);
4451 if (load < min_load || (load == min_load && i == this_cpu)) {
4452 min_load = load;
4453 least_loaded_cpu = i;
4454 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01004455 }
4456 }
4457
Nicolas Pitre83a0a962014-09-04 11:32:10 -04004458 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004459}
Gregory Haskinse7693a32008-01-25 21:08:09 +01004460
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004461/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004462 * Try and locate an idle CPU in the sched_domain.
4463 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004464static int select_idle_sibling(struct task_struct *p, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004465{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004466 struct sched_domain *sd;
Linus Torvalds37407ea2012-09-16 12:29:43 -07004467 struct sched_group *sg;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004468 int i = task_cpu(p);
4469
4470 if (idle_cpu(target))
4471 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004472
4473 /*
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004474 * If the prevous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004475 */
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004476 if (i != target && cpus_share_cache(i, target) && idle_cpu(i))
4477 return i;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004478
4479 /*
Linus Torvalds37407ea2012-09-16 12:29:43 -07004480 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004481 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01004482 sd = rcu_dereference(per_cpu(sd_llc, target));
Suresh Siddha77e81362011-11-17 11:08:23 -08004483 for_each_lower_domain(sd) {
Linus Torvalds37407ea2012-09-16 12:29:43 -07004484 sg = sd->groups;
4485 do {
4486 if (!cpumask_intersects(sched_group_cpus(sg),
4487 tsk_cpus_allowed(p)))
4488 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02004489
Linus Torvalds37407ea2012-09-16 12:29:43 -07004490 for_each_cpu(i, sched_group_cpus(sg)) {
Mike Galbraithe0a79f52013-01-28 12:19:25 +01004491 if (i == target || !idle_cpu(i))
Linus Torvalds37407ea2012-09-16 12:29:43 -07004492 goto next;
4493 }
4494
4495 target = cpumask_first_and(sched_group_cpus(sg),
4496 tsk_cpus_allowed(p));
4497 goto done;
4498next:
4499 sg = sg->next;
4500 } while (sg != sd->groups);
4501 }
4502done:
Peter Zijlstraa50bde52009-11-12 15:55:28 +01004503 return target;
4504}
4505
4506/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00004507 * select_task_rq_fair: Select target runqueue for the waking task in domains
4508 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
4509 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004510 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00004511 * Balances load by selecting the idlest cpu in the idlest group, or under
4512 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004513 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00004514 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004515 *
4516 * preempt must be disabled.
4517 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01004518static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01004519select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004520{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02004521 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004522 int cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004523 int new_cpu = cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004524 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02004525 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004526
Peter Zijlstra29baa742012-04-23 12:11:21 +02004527 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01004528 return prev_cpu;
4529
Kirill Tkhaia8edd072014-09-12 17:41:16 +04004530 if (sd_flag & SD_BALANCE_WAKE)
4531 want_affine = cpumask_test_cpu(cpu, tsk_cpus_allowed(p));
Gregory Haskinse7693a32008-01-25 21:08:09 +01004532
Peter Zijlstradce840a2011-04-07 14:09:50 +02004533 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004534 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01004535 if (!(tmp->flags & SD_LOAD_BALANCE))
4536 continue;
4537
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004538 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004539 * If both cpu and prev_cpu are part of this domain,
4540 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01004541 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07004542 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
4543 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
4544 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08004545 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004546 }
4547
Alex Shif03542a2012-07-26 08:55:34 +08004548 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02004549 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004550 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004551
Rik van Riel8bf21432014-05-14 11:40:37 -04004552 if (affine_sd && cpu != prev_cpu && wake_affine(affine_sd, p, sync))
4553 prev_cpu = cpu;
Peter Zijlstradce840a2011-04-07 14:09:50 +02004554
Rik van Riel8bf21432014-05-14 11:40:37 -04004555 if (sd_flag & SD_BALANCE_WAKE) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02004556 new_cpu = select_idle_sibling(p, prev_cpu);
4557 goto unlock;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01004558 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02004559
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004560 while (sd) {
4561 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004562 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004563
Peter Zijlstra0763a662009-09-14 19:37:39 +02004564 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004565 sd = sd->child;
4566 continue;
4567 }
4568
Vincent Guittotc44f2a02013-10-18 13:52:21 +02004569 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004570 if (!group) {
4571 sd = sd->child;
4572 continue;
4573 }
4574
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02004575 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004576 if (new_cpu == -1 || new_cpu == cpu) {
4577 /* Now try balancing at a lower domain level of cpu */
4578 sd = sd->child;
4579 continue;
4580 }
4581
4582 /* Now try balancing at a lower domain level of new_cpu */
4583 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004584 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004585 sd = NULL;
4586 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02004587 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004588 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02004589 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02004590 sd = tmp;
4591 }
4592 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01004593 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02004594unlock:
4595 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01004596
Peter Zijlstrac88d5912009-09-10 13:50:02 +02004597 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01004598}
Paul Turner0a74bef2012-10-04 13:18:30 +02004599
4600/*
4601 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
4602 * cfs_rq_of(p) references at time of call are still valid and identify the
4603 * previous cpu. However, the caller only guarantees p->pi_lock is held; no
4604 * other assumptions, including the state of rq->lock, should be made.
4605 */
4606static void
4607migrate_task_rq_fair(struct task_struct *p, int next_cpu)
4608{
Paul Turneraff3e492012-10-04 13:18:30 +02004609 struct sched_entity *se = &p->se;
4610 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4611
4612 /*
4613 * Load tracking: accumulate removed load so that it can be processed
4614 * when we next update owning cfs_rq under rq->lock. Tasks contribute
4615 * to blocked load iff they have a positive decay-count. It can never
4616 * be negative here since on-rq tasks have decay-count == 0.
4617 */
4618 if (se->avg.decay_count) {
4619 se->avg.decay_count = -__synchronize_entity_decay(se);
Alex Shi25099402013-06-20 10:18:55 +08004620 atomic_long_add(se->avg.load_avg_contrib,
4621 &cfs_rq->removed_load);
Paul Turneraff3e492012-10-04 13:18:30 +02004622 }
Ben Segall3944a922014-05-15 15:59:20 -07004623
4624 /* We have migrated, no longer consider this task hot */
4625 se->exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02004626}
Gregory Haskinse7693a32008-01-25 21:08:09 +01004627#endif /* CONFIG_SMP */
4628
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004629static unsigned long
4630wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004631{
4632 unsigned long gran = sysctl_sched_wakeup_granularity;
4633
4634 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004635 * Since its curr running now, convert the gran from real-time
4636 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01004637 *
4638 * By using 'se' instead of 'curr' we penalize light tasks, so
4639 * they get preempted easier. That is, if 'se' < 'curr' then
4640 * the resulting gran will be larger, therefore penalizing the
4641 * lighter, if otoh 'se' > 'curr' then the resulting gran will
4642 * be smaller, again penalizing the lighter task.
4643 *
4644 * This is especially important for buddies when the leftmost
4645 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004646 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08004647 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02004648}
4649
4650/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02004651 * Should 'se' preempt 'curr'.
4652 *
4653 * |s1
4654 * |s2
4655 * |s3
4656 * g
4657 * |<--->|c
4658 *
4659 * w(c, s1) = -1
4660 * w(c, s2) = 0
4661 * w(c, s3) = 1
4662 *
4663 */
4664static int
4665wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
4666{
4667 s64 gran, vdiff = curr->vruntime - se->vruntime;
4668
4669 if (vdiff <= 0)
4670 return -1;
4671
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01004672 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02004673 if (vdiff > gran)
4674 return 1;
4675
4676 return 0;
4677}
4678
Peter Zijlstra02479092008-11-04 21:25:10 +01004679static void set_last_buddy(struct sched_entity *se)
4680{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004681 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
4682 return;
4683
4684 for_each_sched_entity(se)
4685 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01004686}
4687
4688static void set_next_buddy(struct sched_entity *se)
4689{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004690 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
4691 return;
4692
4693 for_each_sched_entity(se)
4694 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01004695}
4696
Rik van Rielac53db52011-02-01 09:51:03 -05004697static void set_skip_buddy(struct sched_entity *se)
4698{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07004699 for_each_sched_entity(se)
4700 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05004701}
4702
Peter Zijlstra464b7522008-10-24 11:06:15 +02004703/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004704 * Preempt the current task with a newly woken task if needed:
4705 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02004706static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004707{
4708 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02004709 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01004710 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02004711 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004712 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01004713
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01004714 if (unlikely(se == pse))
4715 return;
4716
Paul Turner5238cdd2011-07-21 09:43:37 -07004717 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04004718 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07004719 * unconditionally check_prempt_curr() after an enqueue (which may have
4720 * lead to a throttle). This both saves work and prevents false
4721 * next-buddy nomination below.
4722 */
4723 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
4724 return;
4725
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004726 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02004727 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004728 next_buddy_marked = 1;
4729 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02004730
Bharata B Raoaec0a512008-08-28 14:42:49 +05304731 /*
4732 * We can come here with TIF_NEED_RESCHED already set from new task
4733 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07004734 *
4735 * Note: this also catches the edge-case of curr being in a throttled
4736 * group (e.g. via set_curr_task), since update_curr() (in the
4737 * enqueue of curr) will have resulted in resched being set. This
4738 * prevents us from potentially nominating it as a false LAST_BUDDY
4739 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05304740 */
4741 if (test_tsk_need_resched(curr))
4742 return;
4743
Darren Harta2f5c9a2011-02-22 13:04:33 -08004744 /* Idle tasks are by definition preempted by non-idle tasks. */
4745 if (unlikely(curr->policy == SCHED_IDLE) &&
4746 likely(p->policy != SCHED_IDLE))
4747 goto preempt;
4748
Ingo Molnar91c234b2007-10-15 17:00:18 +02004749 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08004750 * Batch and idle tasks do not preempt non-idle tasks (their preemption
4751 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02004752 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02004753 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02004754 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004755
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004756 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07004757 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004758 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004759 if (wakeup_preempt_entity(se, pse) == 1) {
4760 /*
4761 * Bias pick_next to pick the sched entity that is
4762 * triggering this preemption.
4763 */
4764 if (!next_buddy_marked)
4765 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004766 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004767 }
Jupyung Leea65ac742009-11-17 18:51:40 +09004768
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004769 return;
4770
4771preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04004772 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01004773 /*
4774 * Only set the backward buddy when the current task is still
4775 * on the rq. This can happen when a wakeup gets interleaved
4776 * with schedule on the ->pre_schedule() or idle_balance()
4777 * point, either of which can * drop the rq lock.
4778 *
4779 * Also, during early boot the idle thread is in the fair class,
4780 * for obvious reasons its a bad idea to schedule back to it.
4781 */
4782 if (unlikely(!se->on_rq || curr == rq->idle))
4783 return;
4784
4785 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
4786 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004787}
4788
Peter Zijlstra606dba22012-02-11 06:05:00 +01004789static struct task_struct *
4790pick_next_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004791{
4792 struct cfs_rq *cfs_rq = &rq->cfs;
4793 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01004794 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01004795 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01004796
Peter Zijlstra6e831252014-02-11 16:11:48 +01004797again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01004798#ifdef CONFIG_FAIR_GROUP_SCHED
4799 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01004800 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01004801
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01004802 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01004803 goto simple;
4804
4805 /*
4806 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
4807 * likely that a next task is from the same cgroup as the current.
4808 *
4809 * Therefore attempt to avoid putting and setting the entire cgroup
4810 * hierarchy, only change the part that actually changes.
4811 */
4812
4813 do {
4814 struct sched_entity *curr = cfs_rq->curr;
4815
4816 /*
4817 * Since we got here without doing put_prev_entity() we also
4818 * have to consider cfs_rq->curr. If it is still a runnable
4819 * entity, update_curr() will update its vruntime, otherwise
4820 * forget we've ever seen it.
4821 */
4822 if (curr && curr->on_rq)
4823 update_curr(cfs_rq);
4824 else
4825 curr = NULL;
4826
4827 /*
4828 * This call to check_cfs_rq_runtime() will do the throttle and
4829 * dequeue its entity in the parent(s). Therefore the 'simple'
4830 * nr_running test will indeed be correct.
4831 */
4832 if (unlikely(check_cfs_rq_runtime(cfs_rq)))
4833 goto simple;
4834
4835 se = pick_next_entity(cfs_rq, curr);
4836 cfs_rq = group_cfs_rq(se);
4837 } while (cfs_rq);
4838
4839 p = task_of(se);
4840
4841 /*
4842 * Since we haven't yet done put_prev_entity and if the selected task
4843 * is a different task than we started out with, try and touch the
4844 * least amount of cfs_rqs.
4845 */
4846 if (prev != p) {
4847 struct sched_entity *pse = &prev->se;
4848
4849 while (!(cfs_rq = is_same_group(se, pse))) {
4850 int se_depth = se->depth;
4851 int pse_depth = pse->depth;
4852
4853 if (se_depth <= pse_depth) {
4854 put_prev_entity(cfs_rq_of(pse), pse);
4855 pse = parent_entity(pse);
4856 }
4857 if (se_depth >= pse_depth) {
4858 set_next_entity(cfs_rq_of(se), se);
4859 se = parent_entity(se);
4860 }
4861 }
4862
4863 put_prev_entity(cfs_rq, pse);
4864 set_next_entity(cfs_rq, se);
4865 }
4866
4867 if (hrtick_enabled(rq))
4868 hrtick_start_fair(rq, p);
4869
4870 return p;
4871simple:
4872 cfs_rq = &rq->cfs;
4873#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004874
Tim Blechmann36ace272009-11-24 11:55:45 +01004875 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01004876 goto idle;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004877
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01004878 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01004879
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004880 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01004881 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01004882 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004883 cfs_rq = group_cfs_rq(se);
4884 } while (cfs_rq);
4885
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004886 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004887
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004888 if (hrtick_enabled(rq))
4889 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004890
4891 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01004892
4893idle:
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04004894 new_tasks = idle_balance(rq);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01004895 /*
4896 * Because idle_balance() releases (and re-acquires) rq->lock, it is
4897 * possible for any higher priority task to appear. In that case we
4898 * must re-start the pick_next_entity() loop.
4899 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04004900 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01004901 return RETRY_TASK;
4902
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04004903 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01004904 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01004905
4906 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004907}
4908
4909/*
4910 * Account for a descheduled task:
4911 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02004912static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004913{
4914 struct sched_entity *se = &prev->se;
4915 struct cfs_rq *cfs_rq;
4916
4917 for_each_sched_entity(se) {
4918 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02004919 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004920 }
4921}
4922
Rik van Rielac53db52011-02-01 09:51:03 -05004923/*
4924 * sched_yield() is very simple
4925 *
4926 * The magic of dealing with the ->skip buddy is in pick_next_entity.
4927 */
4928static void yield_task_fair(struct rq *rq)
4929{
4930 struct task_struct *curr = rq->curr;
4931 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
4932 struct sched_entity *se = &curr->se;
4933
4934 /*
4935 * Are we the only task in the tree?
4936 */
4937 if (unlikely(rq->nr_running == 1))
4938 return;
4939
4940 clear_buddies(cfs_rq, se);
4941
4942 if (curr->policy != SCHED_BATCH) {
4943 update_rq_clock(rq);
4944 /*
4945 * Update run-time statistics of the 'current'.
4946 */
4947 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01004948 /*
4949 * Tell update_rq_clock() that we've just updated,
4950 * so we don't do microscopic update in schedule()
4951 * and double the fastpath cost.
4952 */
4953 rq->skip_clock_update = 1;
Rik van Rielac53db52011-02-01 09:51:03 -05004954 }
4955
4956 set_skip_buddy(se);
4957}
4958
Mike Galbraithd95f4122011-02-01 09:50:51 -05004959static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
4960{
4961 struct sched_entity *se = &p->se;
4962
Paul Turner5238cdd2011-07-21 09:43:37 -07004963 /* throttled hierarchies are not runnable */
4964 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05004965 return false;
4966
4967 /* Tell the scheduler that we'd really like pse to run next. */
4968 set_next_buddy(se);
4969
Mike Galbraithd95f4122011-02-01 09:50:51 -05004970 yield_task_fair(rq);
4971
4972 return true;
4973}
4974
Peter Williams681f3e62007-10-24 18:23:51 +02004975#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004976/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02004977 * Fair scheduling class load-balancing methods.
4978 *
4979 * BASICS
4980 *
4981 * The purpose of load-balancing is to achieve the same basic fairness the
4982 * per-cpu scheduler provides, namely provide a proportional amount of compute
4983 * time to each task. This is expressed in the following equation:
4984 *
4985 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
4986 *
4987 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
4988 * W_i,0 is defined as:
4989 *
4990 * W_i,0 = \Sum_j w_i,j (2)
4991 *
4992 * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight
4993 * is derived from the nice value as per prio_to_weight[].
4994 *
4995 * The weight average is an exponential decay average of the instantaneous
4996 * weight:
4997 *
4998 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
4999 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04005000 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02005001 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
5002 * can also include other factors [XXX].
5003 *
5004 * To achieve this balance we define a measure of imbalance which follows
5005 * directly from (1):
5006 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04005007 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02005008 *
5009 * We them move tasks around to minimize the imbalance. In the continuous
5010 * function space it is obvious this converges, in the discrete case we get
5011 * a few fun cases generally called infeasible weight scenarios.
5012 *
5013 * [XXX expand on:
5014 * - infeasible weights;
5015 * - local vs global optima in the discrete case. ]
5016 *
5017 *
5018 * SCHED DOMAINS
5019 *
5020 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
5021 * for all i,j solution, we create a tree of cpus that follows the hardware
5022 * topology where each level pairs two lower groups (or better). This results
5023 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
5024 * tree to only the first of the previous level and we decrease the frequency
5025 * of load-balance at each level inv. proportional to the number of cpus in
5026 * the groups.
5027 *
5028 * This yields:
5029 *
5030 * log_2 n 1 n
5031 * \Sum { --- * --- * 2^i } = O(n) (5)
5032 * i = 0 2^i 2^i
5033 * `- size of each group
5034 * | | `- number of cpus doing load-balance
5035 * | `- freq
5036 * `- sum over all levels
5037 *
5038 * Coupled with a limit on how many tasks we can migrate every balance pass,
5039 * this makes (5) the runtime complexity of the balancer.
5040 *
5041 * An important property here is that each CPU is still (indirectly) connected
5042 * to every other cpu in at most O(log n) steps:
5043 *
5044 * The adjacency matrix of the resulting graph is given by:
5045 *
5046 * log_2 n
5047 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
5048 * k = 0
5049 *
5050 * And you'll find that:
5051 *
5052 * A^(log_2 n)_i,j != 0 for all i,j (7)
5053 *
5054 * Showing there's indeed a path between every cpu in at most O(log n) steps.
5055 * The task movement gives a factor of O(m), giving a convergence complexity
5056 * of:
5057 *
5058 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
5059 *
5060 *
5061 * WORK CONSERVING
5062 *
5063 * In order to avoid CPUs going idle while there's still work to do, new idle
5064 * balancing is more aggressive and has the newly idle cpu iterate up the domain
5065 * tree itself instead of relying on other CPUs to bring it work.
5066 *
5067 * This adds some complexity to both (5) and (8) but it reduces the total idle
5068 * time.
5069 *
5070 * [XXX more?]
5071 *
5072 *
5073 * CGROUPS
5074 *
5075 * Cgroups make a horror show out of (2), instead of a simple sum we get:
5076 *
5077 * s_k,i
5078 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
5079 * S_k
5080 *
5081 * Where
5082 *
5083 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
5084 *
5085 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
5086 *
5087 * The big problem is S_k, its a global sum needed to compute a local (W_i)
5088 * property.
5089 *
5090 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
5091 * rewrite all of this once again.]
5092 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005093
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09005094static unsigned long __read_mostly max_load_balance_interval = HZ/10;
5095
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005096enum fbq_type { regular, remote, all };
5097
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005098#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01005099#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02005100#define LBF_DST_PINNED 0x04
5101#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005102
5103struct lb_env {
5104 struct sched_domain *sd;
5105
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005106 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05305107 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005108
5109 int dst_cpu;
5110 struct rq *dst_rq;
5111
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305112 struct cpumask *dst_grpmask;
5113 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005114 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005115 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08005116 /* The set of CPUs under consideration for load-balancing */
5117 struct cpumask *cpus;
5118
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005119 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01005120
5121 unsigned int loop;
5122 unsigned int loop_break;
5123 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005124
5125 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04005126 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005127};
5128
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005129/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02005130 * Is this task likely cache-hot:
5131 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02005132static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005133{
5134 s64 delta;
5135
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005136 lockdep_assert_held(&env->src_rq->lock);
5137
Peter Zijlstra029632f2011-10-25 10:00:11 +02005138 if (p->sched_class != &fair_sched_class)
5139 return 0;
5140
5141 if (unlikely(p->policy == SCHED_IDLE))
5142 return 0;
5143
5144 /*
5145 * Buddy candidates are cache hot:
5146 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02005147 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02005148 (&p->se == cfs_rq_of(&p->se)->next ||
5149 &p->se == cfs_rq_of(&p->se)->last))
5150 return 1;
5151
5152 if (sysctl_sched_migration_cost == -1)
5153 return 1;
5154 if (sysctl_sched_migration_cost == 0)
5155 return 0;
5156
Hillf Danton5d5e2b12014-06-10 10:58:43 +02005157 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005158
5159 return delta < (s64)sysctl_sched_migration_cost;
5160}
5161
Mel Gorman3a7053b2013-10-07 11:29:00 +01005162#ifdef CONFIG_NUMA_BALANCING
5163/* Returns true if the destination node has incurred more faults */
5164static bool migrate_improves_locality(struct task_struct *p, struct lb_env *env)
5165{
Rik van Rielb1ad0652014-05-15 13:03:06 -04005166 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Mel Gorman3a7053b2013-10-07 11:29:00 +01005167 int src_nid, dst_nid;
5168
Rik van Rielff1df892014-01-27 17:03:41 -05005169 if (!sched_feat(NUMA_FAVOUR_HIGHER) || !p->numa_faults_memory ||
Mel Gorman3a7053b2013-10-07 11:29:00 +01005170 !(env->sd->flags & SD_NUMA)) {
5171 return false;
5172 }
5173
5174 src_nid = cpu_to_node(env->src_cpu);
5175 dst_nid = cpu_to_node(env->dst_cpu);
5176
Mel Gorman83e1d2c2013-10-07 11:29:27 +01005177 if (src_nid == dst_nid)
Mel Gorman3a7053b2013-10-07 11:29:00 +01005178 return false;
5179
Rik van Rielb1ad0652014-05-15 13:03:06 -04005180 if (numa_group) {
5181 /* Task is already in the group's interleave set. */
5182 if (node_isset(src_nid, numa_group->active_nodes))
5183 return false;
5184
5185 /* Task is moving into the group's interleave set. */
5186 if (node_isset(dst_nid, numa_group->active_nodes))
5187 return true;
5188
5189 return group_faults(p, dst_nid) > group_faults(p, src_nid);
5190 }
5191
5192 /* Encourage migration to the preferred node. */
Mel Gorman83e1d2c2013-10-07 11:29:27 +01005193 if (dst_nid == p->numa_preferred_nid)
5194 return true;
5195
Rik van Rielb1ad0652014-05-15 13:03:06 -04005196 return task_faults(p, dst_nid) > task_faults(p, src_nid);
Mel Gorman3a7053b2013-10-07 11:29:00 +01005197}
Mel Gorman7a0f3082013-10-07 11:29:01 +01005198
5199
5200static bool migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
5201{
Rik van Rielb1ad0652014-05-15 13:03:06 -04005202 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Mel Gorman7a0f3082013-10-07 11:29:01 +01005203 int src_nid, dst_nid;
5204
5205 if (!sched_feat(NUMA) || !sched_feat(NUMA_RESIST_LOWER))
5206 return false;
5207
Rik van Rielff1df892014-01-27 17:03:41 -05005208 if (!p->numa_faults_memory || !(env->sd->flags & SD_NUMA))
Mel Gorman7a0f3082013-10-07 11:29:01 +01005209 return false;
5210
5211 src_nid = cpu_to_node(env->src_cpu);
5212 dst_nid = cpu_to_node(env->dst_cpu);
5213
Mel Gorman83e1d2c2013-10-07 11:29:27 +01005214 if (src_nid == dst_nid)
Mel Gorman7a0f3082013-10-07 11:29:01 +01005215 return false;
5216
Rik van Rielb1ad0652014-05-15 13:03:06 -04005217 if (numa_group) {
5218 /* Task is moving within/into the group's interleave set. */
5219 if (node_isset(dst_nid, numa_group->active_nodes))
5220 return false;
5221
5222 /* Task is moving out of the group's interleave set. */
5223 if (node_isset(src_nid, numa_group->active_nodes))
5224 return true;
5225
5226 return group_faults(p, dst_nid) < group_faults(p, src_nid);
5227 }
5228
Mel Gorman83e1d2c2013-10-07 11:29:27 +01005229 /* Migrating away from the preferred node is always bad. */
5230 if (src_nid == p->numa_preferred_nid)
5231 return true;
5232
Rik van Rielb1ad0652014-05-15 13:03:06 -04005233 return task_faults(p, dst_nid) < task_faults(p, src_nid);
Mel Gorman7a0f3082013-10-07 11:29:01 +01005234}
5235
Mel Gorman3a7053b2013-10-07 11:29:00 +01005236#else
5237static inline bool migrate_improves_locality(struct task_struct *p,
5238 struct lb_env *env)
5239{
5240 return false;
5241}
Mel Gorman7a0f3082013-10-07 11:29:01 +01005242
5243static inline bool migrate_degrades_locality(struct task_struct *p,
5244 struct lb_env *env)
5245{
5246 return false;
5247}
Mel Gorman3a7053b2013-10-07 11:29:00 +01005248#endif
5249
Peter Zijlstra029632f2011-10-25 10:00:11 +02005250/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005251 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
5252 */
5253static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005254int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005255{
5256 int tsk_cache_hot = 0;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005257
5258 lockdep_assert_held(&env->src_rq->lock);
5259
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005260 /*
5261 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09005262 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005263 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09005264 * 3) running (obviously), or
5265 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005266 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09005267 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
5268 return 0;
5269
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005270 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09005271 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305272
Lucas De Marchi41acab82010-03-10 23:37:45 -03005273 schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305274
Peter Zijlstra62633222013-08-19 12:41:09 +02005275 env->flags |= LBF_SOME_PINNED;
5276
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305277 /*
5278 * Remember if this task can be migrated to any other cpu in
5279 * our sched_group. We may want to revisit it if we couldn't
5280 * meet load balance goals by pulling other tasks on src_cpu.
5281 *
5282 * Also avoid computing new_dst_cpu if we have already computed
5283 * one in current iteration.
5284 */
Peter Zijlstra62633222013-08-19 12:41:09 +02005285 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305286 return 0;
5287
Joonsoo Kime02e60c2013-04-23 17:27:42 +09005288 /* Prevent to re-select dst_cpu via env's cpus */
5289 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
5290 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02005291 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09005292 env->new_dst_cpu = cpu;
5293 break;
5294 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305295 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09005296
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005297 return 0;
5298 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05305299
5300 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005301 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005302
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01005303 if (task_running(env->src_rq, p)) {
Lucas De Marchi41acab82010-03-10 23:37:45 -03005304 schedstat_inc(p, se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005305 return 0;
5306 }
5307
5308 /*
5309 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01005310 * 1) destination numa is preferred
5311 * 2) task is cache cold, or
5312 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005313 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02005314 tsk_cache_hot = task_hot(p, env);
Mel Gorman7a0f3082013-10-07 11:29:01 +01005315 if (!tsk_cache_hot)
5316 tsk_cache_hot = migrate_degrades_locality(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01005317
Kirill Tkhai7a96c232014-09-22 22:36:12 +04005318 if (migrate_improves_locality(p, env) || !tsk_cache_hot ||
5319 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Mel Gorman3a7053b2013-10-07 11:29:00 +01005320 if (tsk_cache_hot) {
5321 schedstat_inc(env->sd, lb_hot_gained[env->idle]);
5322 schedstat_inc(p, se.statistics.nr_forced_migrations);
5323 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005324 return 1;
5325 }
5326
Zhang Hang4e2dcb72013-04-10 14:04:55 +08005327 schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
5328 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005329}
5330
Peter Zijlstra897c3952009-12-17 17:45:42 +01005331/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04005332 * detach_task() -- detach the task for the migration specified in env
5333 */
5334static void detach_task(struct task_struct *p, struct lb_env *env)
5335{
5336 lockdep_assert_held(&env->src_rq->lock);
5337
5338 deactivate_task(env->src_rq, p, 0);
5339 p->on_rq = TASK_ON_RQ_MIGRATING;
5340 set_task_cpu(p, env->dst_cpu);
5341}
5342
5343/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005344 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01005345 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01005346 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005347 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01005348 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005349static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01005350{
5351 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01005352
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005353 lockdep_assert_held(&env->src_rq->lock);
5354
Peter Zijlstra367456c2012-02-20 21:49:09 +01005355 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01005356 if (!can_migrate_task(p, env))
5357 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01005358
Kirill Tkhai163122b2014-08-20 13:48:29 +04005359 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005360
Peter Zijlstra367456c2012-02-20 21:49:09 +01005361 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005362 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04005363 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005364 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04005365 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01005366 */
5367 schedstat_inc(env->sd, lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005368 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01005369 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04005370 return NULL;
5371}
5372
Peter Zijlstraeb953082012-04-17 13:38:40 +02005373static const unsigned int sched_nr_migrate_break = 32;
5374
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005375/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04005376 * detach_tasks() -- tries to detach up to imbalance weighted load from
5377 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005378 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04005379 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005380 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04005381static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005382{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005383 struct list_head *tasks = &env->src_rq->cfs_tasks;
5384 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01005385 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04005386 int detached = 0;
5387
5388 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005389
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005390 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005391 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005392
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005393 while (!list_empty(tasks)) {
5394 p = list_first_entry(tasks, struct task_struct, se.group_node);
5395
Peter Zijlstra367456c2012-02-20 21:49:09 +01005396 env->loop++;
5397 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005398 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01005399 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005400
5401 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01005402 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02005403 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01005404 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01005405 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02005406 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005407
Joonsoo Kimd3198082013-04-23 17:27:40 +09005408 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01005409 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005410
Peter Zijlstra367456c2012-02-20 21:49:09 +01005411 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005412
Peter Zijlstraeb953082012-04-17 13:38:40 +02005413 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01005414 goto next;
5415
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005416 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01005417 goto next;
5418
Kirill Tkhai163122b2014-08-20 13:48:29 +04005419 detach_task(p, env);
5420 list_add(&p->se.group_node, &env->tasks);
5421
5422 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005423 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005424
5425#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01005426 /*
5427 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04005428 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01005429 * the critical section.
5430 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005431 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01005432 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005433#endif
5434
Peter Zijlstraee00e662009-12-17 17:25:20 +01005435 /*
5436 * We only want to steal up to the prescribed amount of
5437 * weighted load.
5438 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005439 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01005440 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005441
Peter Zijlstra367456c2012-02-20 21:49:09 +01005442 continue;
5443next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005444 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005445 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01005446
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005447 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04005448 * Right now, this is one of only two places we collect this stat
5449 * so we can safely collect detach_one_task() stats here rather
5450 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005451 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04005452 schedstat_add(env->sd, lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005453
Kirill Tkhai163122b2014-08-20 13:48:29 +04005454 return detached;
5455}
5456
5457/*
5458 * attach_task() -- attach the task detached by detach_task() to its new rq.
5459 */
5460static void attach_task(struct rq *rq, struct task_struct *p)
5461{
5462 lockdep_assert_held(&rq->lock);
5463
5464 BUG_ON(task_rq(p) != rq);
5465 p->on_rq = TASK_ON_RQ_QUEUED;
5466 activate_task(rq, p, 0);
5467 check_preempt_curr(rq, p, 0);
5468}
5469
5470/*
5471 * attach_one_task() -- attaches the task returned from detach_one_task() to
5472 * its new rq.
5473 */
5474static void attach_one_task(struct rq *rq, struct task_struct *p)
5475{
5476 raw_spin_lock(&rq->lock);
5477 attach_task(rq, p);
5478 raw_spin_unlock(&rq->lock);
5479}
5480
5481/*
5482 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
5483 * new rq.
5484 */
5485static void attach_tasks(struct lb_env *env)
5486{
5487 struct list_head *tasks = &env->tasks;
5488 struct task_struct *p;
5489
5490 raw_spin_lock(&env->dst_rq->lock);
5491
5492 while (!list_empty(tasks)) {
5493 p = list_first_entry(tasks, struct task_struct, se.group_node);
5494 list_del_init(&p->se.group_node);
5495
5496 attach_task(env->dst_rq, p);
5497 }
5498
5499 raw_spin_unlock(&env->dst_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005500}
5501
Peter Zijlstra230059de2009-12-17 17:47:12 +01005502#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005503/*
5504 * update tg->load_weight by folding this cpu's load_avg
5505 */
Paul Turner48a16752012-10-04 13:18:31 +02005506static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005507{
Paul Turner48a16752012-10-04 13:18:31 +02005508 struct sched_entity *se = tg->se[cpu];
5509 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu];
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005510
Paul Turner48a16752012-10-04 13:18:31 +02005511 /* throttled entities do not contribute to load */
5512 if (throttled_hierarchy(cfs_rq))
5513 return;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005514
Paul Turneraff3e492012-10-04 13:18:30 +02005515 update_cfs_rq_blocked_load(cfs_rq, 1);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005516
Paul Turner82958362012-10-04 13:18:31 +02005517 if (se) {
5518 update_entity_load_avg(se, 1);
5519 /*
5520 * We pivot on our runnable average having decayed to zero for
5521 * list removal. This generally implies that all our children
5522 * have also been removed (modulo rounding error or bandwidth
5523 * control); however, such cases are rare and we can fix these
5524 * at enqueue.
5525 *
5526 * TODO: fix up out-of-order children on enqueue.
5527 */
5528 if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
5529 list_del_leaf_cfs_rq(cfs_rq);
5530 } else {
Paul Turner48a16752012-10-04 13:18:31 +02005531 struct rq *rq = rq_of(cfs_rq);
Paul Turner82958362012-10-04 13:18:31 +02005532 update_rq_runnable_avg(rq, rq->nr_running);
5533 }
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005534}
5535
Paul Turner48a16752012-10-04 13:18:31 +02005536static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005537{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005538 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02005539 struct cfs_rq *cfs_rq;
5540 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005541
Paul Turner48a16752012-10-04 13:18:31 +02005542 raw_spin_lock_irqsave(&rq->lock, flags);
5543 update_rq_clock(rq);
Peter Zijlstra9763b672011-07-13 13:09:25 +02005544 /*
5545 * Iterates the task_group tree in a bottom up fashion, see
5546 * list_add_leaf_cfs_rq() for details.
5547 */
Paul Turner64660c82011-07-21 09:43:36 -07005548 for_each_leaf_cfs_rq(rq, cfs_rq) {
Paul Turner48a16752012-10-04 13:18:31 +02005549 /*
5550 * Note: We may want to consider periodically releasing
5551 * rq->lock about these updates so that creating many task
5552 * groups does not result in continually extending hold time.
5553 */
5554 __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu);
Paul Turner64660c82011-07-21 09:43:36 -07005555 }
Paul Turner48a16752012-10-04 13:18:31 +02005556
5557 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005558}
5559
Peter Zijlstra9763b672011-07-13 13:09:25 +02005560/*
Vladimir Davydov68520792013-07-15 17:49:19 +04005561 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02005562 * This needs to be done in a top-down fashion because the load of a child
5563 * group is a fraction of its parents load.
5564 */
Vladimir Davydov68520792013-07-15 17:49:19 +04005565static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02005566{
Vladimir Davydov68520792013-07-15 17:49:19 +04005567 struct rq *rq = rq_of(cfs_rq);
5568 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005569 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04005570 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005571
Vladimir Davydov68520792013-07-15 17:49:19 +04005572 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005573 return;
5574
Vladimir Davydov68520792013-07-15 17:49:19 +04005575 cfs_rq->h_load_next = NULL;
5576 for_each_sched_entity(se) {
5577 cfs_rq = cfs_rq_of(se);
5578 cfs_rq->h_load_next = se;
5579 if (cfs_rq->last_h_load_update == now)
5580 break;
5581 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02005582
Vladimir Davydov68520792013-07-15 17:49:19 +04005583 if (!se) {
Vladimir Davydov7e3115e2013-09-14 19:39:46 +04005584 cfs_rq->h_load = cfs_rq->runnable_load_avg;
Vladimir Davydov68520792013-07-15 17:49:19 +04005585 cfs_rq->last_h_load_update = now;
5586 }
5587
5588 while ((se = cfs_rq->h_load_next) != NULL) {
5589 load = cfs_rq->h_load;
5590 load = div64_ul(load * se->avg.load_avg_contrib,
5591 cfs_rq->runnable_load_avg + 1);
5592 cfs_rq = group_cfs_rq(se);
5593 cfs_rq->h_load = load;
5594 cfs_rq->last_h_load_update = now;
5595 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02005596}
5597
Peter Zijlstra367456c2012-02-20 21:49:09 +01005598static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01005599{
Peter Zijlstra367456c2012-02-20 21:49:09 +01005600 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01005601
Vladimir Davydov68520792013-07-15 17:49:19 +04005602 update_cfs_rq_h_load(cfs_rq);
Alex Shia003a252013-06-20 10:18:51 +08005603 return div64_ul(p->se.avg.load_avg_contrib * cfs_rq->h_load,
5604 cfs_rq->runnable_load_avg + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01005605}
5606#else
Paul Turner48a16752012-10-04 13:18:31 +02005607static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08005608{
5609}
5610
Peter Zijlstra367456c2012-02-20 21:49:09 +01005611static unsigned long task_h_load(struct task_struct *p)
5612{
Alex Shia003a252013-06-20 10:18:51 +08005613 return p->se.avg.load_avg_contrib;
Peter Zijlstra230059de2009-12-17 17:47:12 +01005614}
5615#endif
5616
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005617/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04005618
5619enum group_type {
5620 group_other = 0,
5621 group_imbalanced,
5622 group_overloaded,
5623};
5624
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005625/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005626 * sg_lb_stats - stats of a sched_group required for load_balancing
5627 */
5628struct sg_lb_stats {
5629 unsigned long avg_load; /*Avg load across the CPUs of the group */
5630 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005631 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005632 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005633 unsigned long group_capacity;
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005634 unsigned int sum_nr_running; /* Nr tasks running in the group */
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005635 unsigned int group_capacity_factor;
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005636 unsigned int idle_cpus;
5637 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04005638 enum group_type group_type;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04005639 int group_has_free_capacity;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005640#ifdef CONFIG_NUMA_BALANCING
5641 unsigned int nr_numa_running;
5642 unsigned int nr_preferred_running;
5643#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005644};
5645
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005646/*
5647 * sd_lb_stats - Structure to store the statistics of a sched_domain
5648 * during load balancing.
5649 */
5650struct sd_lb_stats {
5651 struct sched_group *busiest; /* Busiest group in this sd */
5652 struct sched_group *local; /* Local group in this sd */
5653 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005654 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005655 unsigned long avg_load; /* Average load across all groups in sd */
5656
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005657 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005658 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09005659};
5660
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005661static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
5662{
5663 /*
5664 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
5665 * local_stat because update_sg_lb_stats() does a full clear/assignment.
5666 * We must however clear busiest_stat::avg_load because
5667 * update_sd_pick_busiest() reads this before assignment.
5668 */
5669 *sds = (struct sd_lb_stats){
5670 .busiest = NULL,
5671 .local = NULL,
5672 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005673 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005674 .busiest_stat = {
5675 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04005676 .sum_nr_running = 0,
5677 .group_type = group_other,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02005678 },
5679 };
5680}
5681
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005682/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005683 * get_sd_load_idx - Obtain the load index for a given sched domain.
5684 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05305685 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005686 *
5687 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005688 */
5689static inline int get_sd_load_idx(struct sched_domain *sd,
5690 enum cpu_idle_type idle)
5691{
5692 int load_idx;
5693
5694 switch (idle) {
5695 case CPU_NOT_IDLE:
5696 load_idx = sd->busy_idx;
5697 break;
5698
5699 case CPU_NEWLY_IDLE:
5700 load_idx = sd->newidle_idx;
5701 break;
5702 default:
5703 load_idx = sd->idle_idx;
5704 break;
5705 }
5706
5707 return load_idx;
5708}
5709
Nicolas Pitreced549f2014-05-26 18:19:38 -04005710static unsigned long default_scale_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005711{
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005712 return SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005713}
5714
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005715unsigned long __weak arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005716{
Nicolas Pitreced549f2014-05-26 18:19:38 -04005717 return default_scale_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005718}
5719
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005720static unsigned long default_scale_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005721{
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005722 if ((sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
5723 return sd->smt_gain / sd->span_weight;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005724
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005725 return SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005726}
5727
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005728unsigned long __weak arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005729{
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005730 return default_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005731}
5732
Nicolas Pitreced549f2014-05-26 18:19:38 -04005733static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005734{
5735 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005736 u64 total, available, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01005737 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005738
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005739 /*
5740 * Since we're reading these variables without serialization make sure
5741 * we read them once before doing sanity checks on them.
5742 */
5743 age_stamp = ACCESS_ONCE(rq->age_stamp);
5744 avg = ACCESS_ONCE(rq->rt_avg);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005745
Peter Zijlstracadefd32014-02-27 10:40:35 +01005746 delta = rq_clock(rq) - age_stamp;
5747 if (unlikely(delta < 0))
5748 delta = 0;
5749
5750 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005751
5752 if (unlikely(total < avg)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04005753 /* Ensures that capacity won't end up being negative */
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005754 available = 0;
5755 } else {
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02005756 available = total - avg;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07005757 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005758
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005759 if (unlikely((s64)total < SCHED_CAPACITY_SCALE))
5760 total = SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005761
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005762 total >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005763
5764 return div_u64(available, total);
5765}
5766
Nicolas Pitreced549f2014-05-26 18:19:38 -04005767static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005768{
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005769 unsigned long capacity = SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005770 struct sched_group *sdg = sd->groups;
5771
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005772 if (sched_feat(ARCH_CAPACITY))
5773 capacity *= arch_scale_cpu_capacity(sd, cpu);
5774 else
5775 capacity *= default_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005776
Vincent Guittot26bc3c52014-08-26 13:06:47 +02005777 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005778
Nicolas Pitreced549f2014-05-26 18:19:38 -04005779 sdg->sgc->capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005780
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005781 if (sched_feat(ARCH_CAPACITY))
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005782 capacity *= arch_scale_freq_capacity(sd, cpu);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005783 else
Nicolas Pitreced549f2014-05-26 18:19:38 -04005784 capacity *= default_scale_capacity(sd, cpu);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005785
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005786 capacity >>= SCHED_CAPACITY_SHIFT;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005787
Nicolas Pitreced549f2014-05-26 18:19:38 -04005788 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005789 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005790
Nicolas Pitreced549f2014-05-26 18:19:38 -04005791 if (!capacity)
5792 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005793
Nicolas Pitreced549f2014-05-26 18:19:38 -04005794 cpu_rq(cpu)->cpu_capacity = capacity;
5795 sdg->sgc->capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005796}
5797
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005798void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005799{
5800 struct sched_domain *child = sd->child;
5801 struct sched_group *group, *sdg = sd->groups;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005802 unsigned long capacity, capacity_orig;
Vincent Guittot4ec44122011-12-12 20:21:08 +01005803 unsigned long interval;
5804
5805 interval = msecs_to_jiffies(sd->balance_interval);
5806 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005807 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005808
5809 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04005810 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005811 return;
5812 }
5813
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005814 capacity_orig = capacity = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005815
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005816 if (child->flags & SD_OVERLAP) {
5817 /*
5818 * SD_OVERLAP domains cannot assume that child groups
5819 * span the current group.
5820 */
5821
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005822 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005823 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305824 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005825
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305826 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005827 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305828 * gets here before we've attached the domains to the
5829 * runqueues.
5830 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04005831 * Use capacity_of(), which is set irrespective of domains
5832 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305833 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005834 * This avoids capacity/capacity_orig from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305835 * causing divide-by-zero issues on boot.
5836 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005837 * Runtime updates will correct capacity_orig.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305838 */
5839 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04005840 capacity_orig += capacity_of(cpu);
5841 capacity += capacity_of(cpu);
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05305842 continue;
5843 }
5844
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005845 sgc = rq->sd->groups->sgc;
5846 capacity_orig += sgc->capacity_orig;
5847 capacity += sgc->capacity;
Peter Zijlstra863bffc2013-08-28 11:44:39 +02005848 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005849 } else {
5850 /*
5851 * !SD_OVERLAP domains can assume that child groups
5852 * span the current group.
5853 */
5854
5855 group = child->groups;
5856 do {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005857 capacity_orig += group->sgc->capacity_orig;
5858 capacity += group->sgc->capacity;
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005859 group = group->next;
5860 } while (group != child->groups);
5861 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005862
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005863 sdg->sgc->capacity_orig = capacity_orig;
5864 sdg->sgc->capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005865}
5866
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005867/*
5868 * Try and fix up capacity for tiny siblings, this is needed when
5869 * things like SD_ASYM_PACKING need f_b_g to select another sibling
5870 * which on its own isn't powerful enough.
5871 *
5872 * See update_sd_pick_busiest() and check_asym_packing().
5873 */
5874static inline int
5875fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
5876{
5877 /*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005878 * Only siblings can have significantly less than SCHED_CAPACITY_SCALE
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005879 */
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005880 if (!(sd->flags & SD_SHARE_CPUCAPACITY))
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005881 return 0;
5882
5883 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005884 * If ~90% of the cpu_capacity is still there, we're good.
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005885 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005886 if (group->sgc->capacity * 32 > group->sgc->capacity_orig * 29)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10005887 return 1;
5888
5889 return 0;
5890}
5891
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005892/*
5893 * Group imbalance indicates (and tries to solve) the problem where balancing
5894 * groups is inadequate due to tsk_cpus_allowed() constraints.
5895 *
5896 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
5897 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
5898 * Something like:
5899 *
5900 * { 0 1 2 3 } { 4 5 6 7 }
5901 * * * * *
5902 *
5903 * If we were to balance group-wise we'd place two tasks in the first group and
5904 * two tasks in the second group. Clearly this is undesired as it will overload
5905 * cpu 3 and leave one of the cpus in the second group unused.
5906 *
5907 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02005908 * by noticing the lower domain failed to reach balance and had difficulty
5909 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005910 *
5911 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05305912 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02005913 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005914 * to create an effective group imbalance.
5915 *
5916 * This is a somewhat tricky proposition since the next run might not find the
5917 * group imbalance and decide the groups need to be balanced again. A most
5918 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005919 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005920
Peter Zijlstra62633222013-08-19 12:41:09 +02005921static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005922{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005923 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005924}
5925
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005926/*
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005927 * Compute the group capacity factor.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005928 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04005929 * Avoid the issue where N*frac(smt_capacity) >= 1 creates 'phantom' cores by
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005930 * first dividing out the smt factor and computing the actual number of cores
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005931 * and limit unit capacity with that.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005932 */
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005933static inline int sg_capacity_factor(struct lb_env *env, struct sched_group *group)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005934{
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005935 unsigned int capacity_factor, smt, cpus;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005936 unsigned int capacity, capacity_orig;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005937
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005938 capacity = group->sgc->capacity;
5939 capacity_orig = group->sgc->capacity_orig;
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005940 cpus = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005941
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005942 /* smt := ceil(cpus / capacity), assumes: 1 < smt_capacity < 2 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005943 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, capacity_orig);
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005944 capacity_factor = cpus / smt; /* cores */
Peter Zijlstrac61037e2013-08-28 12:40:38 +02005945
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005946 capacity_factor = min_t(unsigned,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005947 capacity_factor, DIV_ROUND_CLOSEST(capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005948 if (!capacity_factor)
5949 capacity_factor = fix_small_capacity(env->sd, group);
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005950
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04005951 return capacity_factor;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02005952}
5953
Rik van Rielcaeb1782014-07-28 14:16:28 -04005954static enum group_type
5955group_classify(struct sched_group *group, struct sg_lb_stats *sgs)
5956{
5957 if (sgs->sum_nr_running > sgs->group_capacity_factor)
5958 return group_overloaded;
5959
5960 if (sg_imbalanced(group))
5961 return group_imbalanced;
5962
5963 return group_other;
5964}
5965
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005966/**
5967 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
5968 * @env: The load balancing environment.
5969 * @group: sched_group whose statistics are to be updated.
5970 * @load_idx: Load index of sched_domain of this_cpu for load calc.
5971 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005972 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09005973 * @overload: Indicate more than one runnable task for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005974 */
5975static inline void update_sg_lb_stats(struct lb_env *env,
5976 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07005977 int local_group, struct sg_lb_stats *sgs,
5978 bool *overload)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005979{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02005980 unsigned long load;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02005981 int i;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005982
Peter Zijlstrab72ff132013-08-28 10:32:32 +02005983 memset(sgs, 0, sizeof(*sgs));
5984
Michael Wangb94031302012-07-12 16:10:13 +08005985 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005986 struct rq *rq = cpu_rq(i);
5987
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005988 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02005989 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02005990 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02005991 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005992 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01005993
5994 sgs->group_load += load;
Vincent Guittot65fdac02014-08-26 13:06:46 +02005995 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07005996
5997 if (rq->nr_running > 1)
5998 *overload = true;
5999
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006000#ifdef CONFIG_NUMA_BALANCING
6001 sgs->nr_numa_running += rq->nr_numa_running;
6002 sgs->nr_preferred_running += rq->nr_preferred_running;
6003#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006004 sgs->sum_weighted_load += weighted_cpuload(i);
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006005 if (idle_cpu(i))
6006 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006007 }
6008
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006009 /* Adjust by relative CPU capacity of the group */
6010 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006011 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006012
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006013 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02006014 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006015
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006016 sgs->group_weight = group->group_weight;
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006017 sgs->group_capacity_factor = sg_capacity_factor(env, group);
Rik van Rielcaeb1782014-07-28 14:16:28 -04006018 sgs->group_type = group_classify(group, sgs);
Peter Zijlstrab37d9312013-08-28 11:50:34 +02006019
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006020 if (sgs->group_capacity_factor > sgs->sum_nr_running)
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04006021 sgs->group_has_free_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006022}
6023
6024/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10006025 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07006026 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10006027 * @sds: sched_domain statistics
6028 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10006029 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10006030 *
6031 * Determine if @sg is a busier group than the previously selected
6032 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02006033 *
6034 * Return: %true if @sg is a busier group than the previously selected
6035 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10006036 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006037static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10006038 struct sd_lb_stats *sds,
6039 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006040 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006041{
Rik van Rielcaeb1782014-07-28 14:16:28 -04006042 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10006043
Rik van Rielcaeb1782014-07-28 14:16:28 -04006044 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006045 return true;
6046
Rik van Rielcaeb1782014-07-28 14:16:28 -04006047 if (sgs->group_type < busiest->group_type)
6048 return false;
6049
6050 if (sgs->avg_load <= busiest->avg_load)
6051 return false;
6052
6053 /* This is the busiest node in its class. */
6054 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10006055 return true;
6056
6057 /*
6058 * ASYM_PACKING needs to move all the work to the lowest
6059 * numbered CPUs in the group, therefore mark all groups
6060 * higher than ourself as busy.
6061 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04006062 if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10006063 if (!sds->busiest)
6064 return true;
6065
6066 if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
6067 return true;
6068 }
6069
6070 return false;
6071}
6072
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006073#ifdef CONFIG_NUMA_BALANCING
6074static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
6075{
6076 if (sgs->sum_nr_running > sgs->nr_numa_running)
6077 return regular;
6078 if (sgs->sum_nr_running > sgs->nr_preferred_running)
6079 return remote;
6080 return all;
6081}
6082
6083static inline enum fbq_type fbq_classify_rq(struct rq *rq)
6084{
6085 if (rq->nr_running > rq->nr_numa_running)
6086 return regular;
6087 if (rq->nr_running > rq->nr_preferred_running)
6088 return remote;
6089 return all;
6090}
6091#else
6092static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
6093{
6094 return all;
6095}
6096
6097static inline enum fbq_type fbq_classify_rq(struct rq *rq)
6098{
6099 return regular;
6100}
6101#endif /* CONFIG_NUMA_BALANCING */
6102
Michael Neuling532cb4c2010-06-08 14:57:02 +10006103/**
Hui Kang461819a2011-10-11 23:00:59 -04006104 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07006105 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006106 * @sds: variable to hold the statistics for this sched_domain.
6107 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006108static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006109{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006110 struct sched_domain *child = env->sd->child;
6111 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006112 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006113 int load_idx, prefer_sibling = 0;
Tim Chen4486edd2014-06-23 12:16:49 -07006114 bool overload = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006115
6116 if (child && child->flags & SD_PREFER_SIBLING)
6117 prefer_sibling = 1;
6118
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006119 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006120
6121 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006122 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006123 int local_group;
6124
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006125 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006126 if (local_group) {
6127 sds->local = sg;
6128 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006129
6130 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006131 time_after_eq(jiffies, sg->sgc->next_update))
6132 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006133 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006134
Tim Chen4486edd2014-06-23 12:16:49 -07006135 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
6136 &overload);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006137
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006138 if (local_group)
6139 goto next_group;
6140
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006141 /*
6142 * In case the child domain prefers tasks go to siblings
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006143 * first, lower the sg capacity factor to one so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07006144 * and move all the excess tasks away. We lower the capacity
6145 * of a group only if the local group has the capacity to fit
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006146 * these excess tasks, i.e. nr_running < group_capacity_factor. The
Nikhil Rao75dd3212010-10-15 13:12:30 -07006147 * extra check prevents the case where you always pull from the
6148 * heaviest group when it is already under-utilized (possible
6149 * with a large weight task outweighs the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006150 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006151 if (prefer_sibling && sds->local &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04006152 sds->local_stat.group_has_free_capacity)
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006153 sgs->group_capacity_factor = min(sgs->group_capacity_factor, 1U);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006154
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006155 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10006156 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006157 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006158 }
6159
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006160next_group:
6161 /* Now, start updating sd_lb_stats */
6162 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006163 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02006164
Michael Neuling532cb4c2010-06-08 14:57:02 +10006165 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006166 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006167
6168 if (env->sd->flags & SD_NUMA)
6169 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07006170
6171 if (!env->sd->parent) {
6172 /* update overload indicator if we are at root domain */
6173 if (env->dst_rq->rd->overload != overload)
6174 env->dst_rq->rd->overload = overload;
6175 }
6176
Michael Neuling532cb4c2010-06-08 14:57:02 +10006177}
6178
Michael Neuling532cb4c2010-06-08 14:57:02 +10006179/**
6180 * check_asym_packing - Check to see if the group is packed into the
6181 * sched doman.
6182 *
6183 * This is primarily intended to used at the sibling level. Some
6184 * cores like POWER7 prefer to use lower numbered SMT threads. In the
6185 * case of POWER7, it can move to lower SMT modes only when higher
6186 * threads are idle. When in lower SMT modes, the threads will
6187 * perform better since they share less core resources. Hence when we
6188 * have idle threads, we want them to be the higher ones.
6189 *
6190 * This packing function is run on idle threads. It checks to see if
6191 * the busiest CPU in this domain (core in the P7 case) has a higher
6192 * CPU number than the packing function is being run on. Here we are
6193 * assuming lower CPU number will be equivalent to lower a SMT thread
6194 * number.
6195 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006196 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10006197 * this CPU. The amount of the imbalance is returned in *imbalance.
6198 *
Randy Dunlapcd968912012-06-08 13:18:33 -07006199 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10006200 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10006201 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006202static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006203{
6204 int busiest_cpu;
6205
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006206 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10006207 return 0;
6208
6209 if (!sds->busiest)
6210 return 0;
6211
6212 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006213 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006214 return 0;
6215
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006216 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006217 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006218 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006219
Michael Neuling532cb4c2010-06-08 14:57:02 +10006220 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006221}
6222
6223/**
6224 * fix_small_imbalance - Calculate the minor imbalance that exists
6225 * amongst the groups of a sched_domain, during
6226 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07006227 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006228 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006229 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006230static inline
6231void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006232{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006233 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006234 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006235 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006236 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006237
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006238 local = &sds->local_stat;
6239 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006240
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006241 if (!local->sum_nr_running)
6242 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
6243 else if (busiest->load_per_task > local->load_per_task)
6244 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006245
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006246 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006247 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006248 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006249
Vladimir Davydov3029ede2013-09-15 17:49:14 +04006250 if (busiest->avg_load + scaled_busy_load_per_task >=
6251 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006252 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006253 return;
6254 }
6255
6256 /*
6257 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006258 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006259 * moving them.
6260 */
6261
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006262 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006263 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006264 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006265 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006266 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006267
6268 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01006269 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006270 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006271 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01006272 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006273 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006274
6275 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006276 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006277 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006278 tmp = (busiest->avg_load * busiest->group_capacity) /
6279 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006280 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006281 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006282 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006283 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006284 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02006285 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006286 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006287
6288 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006289 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006290 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006291}
6292
6293/**
6294 * calculate_imbalance - Calculate the amount of imbalance present within the
6295 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006296 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006297 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006298 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006299static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006300{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006301 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006302 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006303
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006304 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006305 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006306
Rik van Rielcaeb1782014-07-28 14:16:28 -04006307 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02006308 /*
6309 * In the group_imb case we cannot rely on group-wide averages
6310 * to ensure cpu-load equilibrium, look at wider averages. XXX
6311 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006312 busiest->load_per_task =
6313 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006314 }
6315
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006316 /*
6317 * In the presence of smp nice balancing, certain scenarios can have
6318 * max load less than avg load(as we skip the groups at or below
Nicolas Pitreced549f2014-05-26 18:19:38 -04006319 * its cpu_capacity, while calculating max_load..)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006320 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04006321 if (busiest->avg_load <= sds->avg_load ||
6322 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006323 env->imbalance = 0;
6324 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006325 }
6326
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02006327 /*
6328 * If there aren't any idle cpus, avoid creating some.
6329 */
6330 if (busiest->group_type == group_overloaded &&
6331 local->group_type == group_overloaded) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006332 load_above_capacity =
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006333 (busiest->sum_nr_running - busiest->group_capacity_factor);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006334
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006335 load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_CAPACITY_SCALE);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006336 load_above_capacity /= busiest->group_capacity;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006337 }
6338
6339 /*
6340 * We're trying to get all the cpus to the average_load, so we don't
6341 * want to push ourselves above the average load, nor do we wish to
6342 * reduce the max loaded cpu below the average load. At the same time,
6343 * we also don't want to reduce the group load below the group capacity
6344 * (so that we can implement power-savings policies etc). Thus we look
6345 * for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08006346 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02006347 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006348
6349 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006350 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006351 max_pull * busiest->group_capacity,
6352 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006353 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006354
6355 /*
6356 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006357 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006358 * a think about bumping its value to force at least one task to be
6359 * moved
6360 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006361 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006362 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006363}
Nikhil Raofab47622010-10-15 13:12:29 -07006364
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006365/******* find_busiest_group() helpers end here *********************/
6366
6367/**
6368 * find_busiest_group - Returns the busiest group within the sched_domain
6369 * if there is an imbalance. If there isn't an imbalance, and
6370 * the user has opted for power-savings, it returns a group whose
6371 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
6372 * such a group exists.
6373 *
6374 * Also calculates the amount of weighted load which should be moved
6375 * to restore balance.
6376 *
Randy Dunlapcd968912012-06-08 13:18:33 -07006377 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006378 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006379 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006380 * - If no imbalance and user has opted for power-savings balance,
6381 * return the least loaded group whose CPUs can be
6382 * put to idle by rebalancing its tasks onto our group.
6383 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006384static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006385{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006386 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006387 struct sd_lb_stats sds;
6388
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02006389 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006390
6391 /*
6392 * Compute the various statistics relavent for load balancing at
6393 * this level.
6394 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006395 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006396 local = &sds.local_stat;
6397 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006398
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006399 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
6400 check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10006401 return sds.busiest;
6402
Peter Zijlstracc57aa82011-02-21 18:55:32 +01006403 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006404 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006405 goto out_balanced;
6406
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006407 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
6408 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07006409
Peter Zijlstra866ab432011-02-21 18:56:47 +01006410 /*
6411 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02006412 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01006413 * isn't true due to cpus_allowed constraints and the like.
6414 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04006415 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01006416 goto force_balance;
6417
Peter Zijlstracc57aa82011-02-21 18:55:32 +01006418 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04006419 if (env->idle == CPU_NEWLY_IDLE && local->group_has_free_capacity &&
6420 !busiest->group_has_free_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07006421 goto force_balance;
6422
Peter Zijlstracc57aa82011-02-21 18:55:32 +01006423 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04006424 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01006425 * don't try and pull any tasks.
6426 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006427 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006428 goto out_balanced;
6429
Peter Zijlstracc57aa82011-02-21 18:55:32 +01006430 /*
6431 * Don't pull any tasks if this group is already above the domain
6432 * average load.
6433 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006434 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006435 goto out_balanced;
6436
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006437 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006438 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02006439 * This cpu is idle. If the busiest group is not overloaded
6440 * and there is no imbalance between this and busiest group
6441 * wrt idle cpus, it is balanced. The imbalance becomes
6442 * significant if the diff is greater than 1 otherwise we
6443 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006444 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02006445 if ((busiest->group_type != group_overloaded) &&
6446 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006447 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01006448 } else {
6449 /*
6450 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
6451 * imbalance_pct to be conservative.
6452 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006453 if (100 * busiest->avg_load <=
6454 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01006455 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006456 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006457
Nikhil Raofab47622010-10-15 13:12:29 -07006458force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006459 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006460 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006461 return sds.busiest;
6462
6463out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006464 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006465 return NULL;
6466}
6467
6468/*
6469 * find_busiest_queue - find the busiest runqueue among the cpus in group.
6470 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006471static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08006472 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006473{
6474 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04006475 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006476 int i;
6477
Peter Zijlstra6906a402013-08-19 15:20:21 +02006478 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04006479 unsigned long capacity, capacity_factor, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006480 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006481
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006482 rq = cpu_rq(i);
6483 rt = fbq_classify_rq(rq);
6484
6485 /*
6486 * We classify groups/runqueues into three groups:
6487 * - regular: there are !numa tasks
6488 * - remote: there are numa tasks that run on the 'wrong' node
6489 * - all: there is no distinction
6490 *
6491 * In order to avoid migrating ideally placed numa tasks,
6492 * ignore those when there's better options.
6493 *
6494 * If we ignore the actual busiest queue to migrate another
6495 * task, the next balance pass can still reduce the busiest
6496 * queue by moving tasks around inside the node.
6497 *
6498 * If we cannot move enough load due to this classification
6499 * the next pass will adjust the group classification and
6500 * allow migration of more tasks.
6501 *
6502 * Both cases only affect the total convergence complexity.
6503 */
6504 if (rt > env->fbq_type)
6505 continue;
6506
Nicolas Pitreced549f2014-05-26 18:19:38 -04006507 capacity = capacity_of(i);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006508 capacity_factor = DIV_ROUND_CLOSEST(capacity, SCHED_CAPACITY_SCALE);
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006509 if (!capacity_factor)
6510 capacity_factor = fix_small_capacity(env->sd, group);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10006511
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006512 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006513
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006514 /*
6515 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04006516 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006517 */
Nicolas Pitre0fedc6c2014-05-26 18:19:36 -04006518 if (capacity_factor && rq->nr_running == 1 && wl > env->imbalance)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006519 continue;
6520
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006521 /*
6522 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04006523 * the weighted_cpuload() scaled with the cpu capacity, so
6524 * that the load can be moved away from the cpu that is
6525 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09006526 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006527 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09006528 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04006529 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
6530 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01006531 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04006532 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09006533 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04006534 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006535 busiest = rq;
6536 }
6537 }
6538
6539 return busiest;
6540}
6541
6542/*
6543 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
6544 * so long as it is large enough.
6545 */
6546#define MAX_PINNED_INTERVAL 512
6547
6548/* Working cpumask for load_balance and load_balance_newidle. */
Joonsoo Kime6252c32013-04-23 17:27:41 +09006549DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006550
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006551static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01006552{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006553 struct sched_domain *sd = env->sd;
6554
6555 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10006556
6557 /*
6558 * ASYM_PACKING needs to force migrate tasks from busy but
6559 * higher numbered CPUs in order to pack all tasks in the
6560 * lowest numbered CPUs.
6561 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006562 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10006563 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01006564 }
6565
6566 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
6567}
6568
Tejun Heo969c7922010-05-06 18:49:21 +02006569static int active_load_balance_cpu_stop(void *data);
6570
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006571static int should_we_balance(struct lb_env *env)
6572{
6573 struct sched_group *sg = env->sd->groups;
6574 struct cpumask *sg_cpus, *sg_mask;
6575 int cpu, balance_cpu = -1;
6576
6577 /*
6578 * In the newly idle case, we will allow all the cpu's
6579 * to do the newly idle load balance.
6580 */
6581 if (env->idle == CPU_NEWLY_IDLE)
6582 return 1;
6583
6584 sg_cpus = sched_group_cpus(sg);
6585 sg_mask = sched_group_mask(sg);
6586 /* Try to find first idle cpu */
6587 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
6588 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
6589 continue;
6590
6591 balance_cpu = cpu;
6592 break;
6593 }
6594
6595 if (balance_cpu == -1)
6596 balance_cpu = group_balance_cpu(sg);
6597
6598 /*
6599 * First idle cpu or the first cpu(busiest) in this sched group
6600 * is eligible for doing load balancing at this and above domains.
6601 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09006602 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006603}
6604
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006605/*
6606 * Check this_cpu to ensure it is balanced within domain. Attempt to move
6607 * tasks if there is an imbalance.
6608 */
6609static int load_balance(int this_cpu, struct rq *this_rq,
6610 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006611 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006612{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306613 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02006614 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006615 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006616 struct rq *busiest;
6617 unsigned long flags;
Joonsoo Kime6252c32013-04-23 17:27:41 +09006618 struct cpumask *cpus = __get_cpu_var(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006619
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006620 struct lb_env env = {
6621 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006622 .dst_cpu = this_cpu,
6623 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306624 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006625 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02006626 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08006627 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006628 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04006629 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006630 };
6631
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006632 /*
6633 * For NEWLY_IDLE load_balancing, we don't need to consider
6634 * other cpus in our group
6635 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006636 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006637 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09006638
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006639 cpumask_copy(cpus, cpu_active_mask);
6640
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006641 schedstat_inc(sd, lb_count[idle]);
6642
6643redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006644 if (!should_we_balance(&env)) {
6645 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006646 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006647 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006648
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006649 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006650 if (!group) {
6651 schedstat_inc(sd, lb_nobusyg[idle]);
6652 goto out_balanced;
6653 }
6654
Michael Wangb94031302012-07-12 16:10:13 +08006655 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006656 if (!busiest) {
6657 schedstat_inc(sd, lb_nobusyq[idle]);
6658 goto out_balanced;
6659 }
6660
Michael Wang78feefc2012-08-06 16:41:59 +08006661 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006662
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006663 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006664
6665 ld_moved = 0;
6666 if (busiest->nr_running > 1) {
6667 /*
6668 * Attempt to move tasks. If find_busiest_group has found
6669 * an imbalance but busiest->nr_running <= 1, the group is
6670 * still unbalanced. ld_moved simply stays zero, so it is
6671 * correctly treated as an imbalance.
6672 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006673 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02006674 env.src_cpu = busiest->cpu;
6675 env.src_rq = busiest;
6676 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006677
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006678more_balance:
Kirill Tkhai163122b2014-08-20 13:48:29 +04006679 raw_spin_lock_irqsave(&busiest->lock, flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306680
6681 /*
6682 * cur_ld_moved - load moved in current iteration
6683 * ld_moved - cumulative load moved across iterations
6684 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04006685 cur_ld_moved = detach_tasks(&env);
6686
6687 /*
6688 * We've detached some tasks from busiest_rq. Every
6689 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
6690 * unlock busiest->lock, and we are able to be sure
6691 * that nobody can manipulate the tasks in parallel.
6692 * See task_rq_lock() family for the details.
6693 */
6694
6695 raw_spin_unlock(&busiest->lock);
6696
6697 if (cur_ld_moved) {
6698 attach_tasks(&env);
6699 ld_moved += cur_ld_moved;
6700 }
6701
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006702 local_irq_restore(flags);
6703
6704 /*
6705 * some other cpu did the load balance for us.
6706 */
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306707 if (cur_ld_moved && env.dst_cpu != smp_processor_id())
6708 resched_cpu(env.dst_cpu);
6709
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09006710 if (env.flags & LBF_NEED_BREAK) {
6711 env.flags &= ~LBF_NEED_BREAK;
6712 goto more_balance;
6713 }
6714
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306715 /*
6716 * Revisit (affine) tasks on src_cpu that couldn't be moved to
6717 * us and move them to an alternate dst_cpu in our sched_group
6718 * where they can run. The upper limit on how many times we
6719 * iterate on same src_cpu is dependent on number of cpus in our
6720 * sched_group.
6721 *
6722 * This changes load balance semantics a bit on who can move
6723 * load to a given_cpu. In addition to the given_cpu itself
6724 * (or a ilb_cpu acting on its behalf where given_cpu is
6725 * nohz-idle), we now have balance_cpu in a position to move
6726 * load to given_cpu. In rare situations, this may cause
6727 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
6728 * _independently_ and at _same_ time to move some load to
6729 * given_cpu) causing exceess load to be moved to given_cpu.
6730 * This however should not happen so much in practice and
6731 * moreover subsequent load balance cycles should correct the
6732 * excess load moved.
6733 */
Peter Zijlstra62633222013-08-19 12:41:09 +02006734 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306735
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04006736 /* Prevent to re-select dst_cpu via env's cpus */
6737 cpumask_clear_cpu(env.dst_cpu, env.cpus);
6738
Michael Wang78feefc2012-08-06 16:41:59 +08006739 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306740 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02006741 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306742 env.loop = 0;
6743 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006744
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306745 /*
6746 * Go back to "more_balance" rather than "redo" since we
6747 * need to continue with same src_cpu.
6748 */
6749 goto more_balance;
6750 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006751
Peter Zijlstra62633222013-08-19 12:41:09 +02006752 /*
6753 * We failed to reach balance because of affinity.
6754 */
6755 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006756 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02006757
Vincent Guittotafdeee02014-08-26 13:06:44 +02006758 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02006759 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02006760 }
6761
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006762 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006763 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006764 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05306765 if (!cpumask_empty(cpus)) {
6766 env.loop = 0;
6767 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006768 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05306769 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02006770 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006771 }
6772 }
6773
6774 if (!ld_moved) {
6775 schedstat_inc(sd, lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07006776 /*
6777 * Increment the failure counter only on periodic balance.
6778 * We do not want newidle balance, which can be very
6779 * frequent, pollute the failure counter causing
6780 * excessive cache_hot migrations and active balances.
6781 */
6782 if (idle != CPU_NEWLY_IDLE)
6783 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006784
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006785 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006786 raw_spin_lock_irqsave(&busiest->lock, flags);
6787
Tejun Heo969c7922010-05-06 18:49:21 +02006788 /* don't kick the active_load_balance_cpu_stop,
6789 * if the curr task on busiest cpu can't be
6790 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006791 */
6792 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006793 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006794 raw_spin_unlock_irqrestore(&busiest->lock,
6795 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006796 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006797 goto out_one_pinned;
6798 }
6799
Tejun Heo969c7922010-05-06 18:49:21 +02006800 /*
6801 * ->active_balance synchronizes accesses to
6802 * ->active_balance_work. Once set, it's cleared
6803 * only after active load balance is finished.
6804 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006805 if (!busiest->active_balance) {
6806 busiest->active_balance = 1;
6807 busiest->push_cpu = this_cpu;
6808 active_balance = 1;
6809 }
6810 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02006811
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006812 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02006813 stop_one_cpu_nowait(cpu_of(busiest),
6814 active_load_balance_cpu_stop, busiest,
6815 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006816 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006817
6818 /*
6819 * We've kicked active balancing, reset the failure
6820 * counter.
6821 */
6822 sd->nr_balance_failed = sd->cache_nice_tries+1;
6823 }
6824 } else
6825 sd->nr_balance_failed = 0;
6826
6827 if (likely(!active_balance)) {
6828 /* We were unbalanced, so reset the balancing interval */
6829 sd->balance_interval = sd->min_interval;
6830 } else {
6831 /*
6832 * If we've begun active balancing, start to back off. This
6833 * case may not be covered by the all_pinned logic if there
6834 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04006835 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006836 */
6837 if (sd->balance_interval < sd->max_interval)
6838 sd->balance_interval *= 2;
6839 }
6840
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006841 goto out;
6842
6843out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02006844 /*
6845 * We reach balance although we may have faced some affinity
6846 * constraints. Clear the imbalance flag if it was set.
6847 */
6848 if (sd_parent) {
6849 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
6850
6851 if (*group_imbalance)
6852 *group_imbalance = 0;
6853 }
6854
6855out_all_pinned:
6856 /*
6857 * We reach balance because all tasks are pinned at this level so
6858 * we can't migrate them. Let the imbalance flag set so parent level
6859 * can try to migrate them.
6860 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006861 schedstat_inc(sd, lb_balanced[idle]);
6862
6863 sd->nr_balance_failed = 0;
6864
6865out_one_pinned:
6866 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006867 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02006868 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006869 (sd->balance_interval < sd->max_interval))
6870 sd->balance_interval *= 2;
6871
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08006872 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006873out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006874 return ld_moved;
6875}
6876
Jason Low52a08ef2014-05-08 17:49:22 -07006877static inline unsigned long
6878get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
6879{
6880 unsigned long interval = sd->balance_interval;
6881
6882 if (cpu_busy)
6883 interval *= sd->busy_factor;
6884
6885 /* scale ms to jiffies */
6886 interval = msecs_to_jiffies(interval);
6887 interval = clamp(interval, 1UL, max_load_balance_interval);
6888
6889 return interval;
6890}
6891
6892static inline void
6893update_next_balance(struct sched_domain *sd, int cpu_busy, unsigned long *next_balance)
6894{
6895 unsigned long interval, next;
6896
6897 interval = get_sd_balance_interval(sd, cpu_busy);
6898 next = sd->last_balance + interval;
6899
6900 if (time_after(*next_balance, next))
6901 *next_balance = next;
6902}
6903
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006904/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006905 * idle_balance is called by schedule() if this_cpu is about to become
6906 * idle. Attempts to pull tasks from other CPUs.
6907 */
Peter Zijlstra6e831252014-02-11 16:11:48 +01006908static int idle_balance(struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006909{
Jason Low52a08ef2014-05-08 17:49:22 -07006910 unsigned long next_balance = jiffies + HZ;
6911 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006912 struct sched_domain *sd;
6913 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07006914 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006915
Peter Zijlstra6e831252014-02-11 16:11:48 +01006916 idle_enter_fair(this_rq);
Jason Low0e5b5332014-04-28 15:45:54 -07006917
Peter Zijlstra6e831252014-02-11 16:11:48 +01006918 /*
6919 * We must set idle_stamp _before_ calling idle_balance(), such that we
6920 * measure the duration of idle_balance() as idle time.
6921 */
6922 this_rq->idle_stamp = rq_clock(this_rq);
6923
Tim Chen4486edd2014-06-23 12:16:49 -07006924 if (this_rq->avg_idle < sysctl_sched_migration_cost ||
6925 !this_rq->rd->overload) {
Jason Low52a08ef2014-05-08 17:49:22 -07006926 rcu_read_lock();
6927 sd = rcu_dereference_check_sched_domain(this_rq->sd);
6928 if (sd)
6929 update_next_balance(sd, 0, &next_balance);
6930 rcu_read_unlock();
6931
Peter Zijlstra6e831252014-02-11 16:11:48 +01006932 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07006933 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006934
Peter Zijlstraf492e122009-12-23 15:29:42 +01006935 /*
6936 * Drop the rq->lock, but keep IRQ/preempt disabled.
6937 */
6938 raw_spin_unlock(&this_rq->lock);
6939
Paul Turner48a16752012-10-04 13:18:31 +02006940 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006941 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006942 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006943 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07006944 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006945
6946 if (!(sd->flags & SD_LOAD_BALANCE))
6947 continue;
6948
Jason Low52a08ef2014-05-08 17:49:22 -07006949 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
6950 update_next_balance(sd, 0, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07006951 break;
Jason Low52a08ef2014-05-08 17:49:22 -07006952 }
Jason Low9bd721c2013-09-13 11:26:52 -07006953
Peter Zijlstraf492e122009-12-23 15:29:42 +01006954 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07006955 t0 = sched_clock_cpu(this_cpu);
6956
Peter Zijlstraf492e122009-12-23 15:29:42 +01006957 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09006958 sd, CPU_NEWLY_IDLE,
6959 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07006960
6961 domain_cost = sched_clock_cpu(this_cpu) - t0;
6962 if (domain_cost > sd->max_newidle_lb_cost)
6963 sd->max_newidle_lb_cost = domain_cost;
6964
6965 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01006966 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006967
Jason Low52a08ef2014-05-08 17:49:22 -07006968 update_next_balance(sd, 0, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07006969
6970 /*
6971 * Stop searching for tasks to pull if there are
6972 * now runnable tasks on this rq.
6973 */
6974 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006975 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006976 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006977 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01006978
6979 raw_spin_lock(&this_rq->lock);
6980
Jason Low0e5b5332014-04-28 15:45:54 -07006981 if (curr_cost > this_rq->max_idle_balance_cost)
6982 this_rq->max_idle_balance_cost = curr_cost;
6983
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01006984 /*
Jason Low0e5b5332014-04-28 15:45:54 -07006985 * While browsing the domains, we released the rq lock, a task could
6986 * have been enqueued in the meantime. Since we're not going idle,
6987 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01006988 */
Jason Low0e5b5332014-04-28 15:45:54 -07006989 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01006990 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01006991
Peter Zijlstra6e831252014-02-11 16:11:48 +01006992out:
Jason Low52a08ef2014-05-08 17:49:22 -07006993 /* Move the next balance forward */
6994 if (time_after(this_rq->next_balance, next_balance))
6995 this_rq->next_balance = next_balance;
6996
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006997 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04006998 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006999 pulled_task = -1;
7000
7001 if (pulled_task) {
7002 idle_exit_fair(this_rq);
Peter Zijlstra6e831252014-02-11 16:11:48 +01007003 this_rq->idle_stamp = 0;
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007004 }
Peter Zijlstra6e831252014-02-11 16:11:48 +01007005
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01007006 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007007}
7008
7009/*
Tejun Heo969c7922010-05-06 18:49:21 +02007010 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
7011 * running tasks off the busiest CPU onto idle CPUs. It requires at
7012 * least 1 task to be running on each physical CPU where possible, and
7013 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007014 */
Tejun Heo969c7922010-05-06 18:49:21 +02007015static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007016{
Tejun Heo969c7922010-05-06 18:49:21 +02007017 struct rq *busiest_rq = data;
7018 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007019 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02007020 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007021 struct sched_domain *sd;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007022 struct task_struct *p = NULL;
Tejun Heo969c7922010-05-06 18:49:21 +02007023
7024 raw_spin_lock_irq(&busiest_rq->lock);
7025
7026 /* make sure the requested cpu hasn't gone down in the meantime */
7027 if (unlikely(busiest_cpu != smp_processor_id() ||
7028 !busiest_rq->active_balance))
7029 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007030
7031 /* Is there any task to move? */
7032 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02007033 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007034
7035 /*
7036 * This condition is "impossible", if it occurs
7037 * we need to fix it. Originally reported by
7038 * Bjorn Helgaas on a 128-cpu setup.
7039 */
7040 BUG_ON(busiest_rq == target_rq);
7041
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007042 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007043 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007044 for_each_domain(target_cpu, sd) {
7045 if ((sd->flags & SD_LOAD_BALANCE) &&
7046 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
7047 break;
7048 }
7049
7050 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007051 struct lb_env env = {
7052 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007053 .dst_cpu = target_cpu,
7054 .dst_rq = target_rq,
7055 .src_cpu = busiest_rq->cpu,
7056 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007057 .idle = CPU_IDLE,
7058 };
7059
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007060 schedstat_inc(sd, alb_count);
7061
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007062 p = detach_one_task(&env);
7063 if (p)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007064 schedstat_inc(sd, alb_pushed);
7065 else
7066 schedstat_inc(sd, alb_failed);
7067 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007068 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02007069out_unlock:
7070 busiest_rq->active_balance = 0;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007071 raw_spin_unlock(&busiest_rq->lock);
7072
7073 if (p)
7074 attach_one_task(target_rq, p);
7075
7076 local_irq_enable();
7077
Tejun Heo969c7922010-05-06 18:49:21 +02007078 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007079}
7080
Mike Galbraithd987fc72011-12-05 10:01:47 +01007081static inline int on_null_domain(struct rq *rq)
7082{
7083 return unlikely(!rcu_dereference_sched(rq->sd));
7084}
7085
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007086#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007087/*
7088 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007089 * - When one of the busy CPUs notice that there may be an idle rebalancing
7090 * needed, they will kick the idle load balancer, which then does idle
7091 * load balancing for all the idle CPUs.
7092 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007093static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007094 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007095 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007096 unsigned long next_balance; /* in jiffy units */
7097} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007098
Daniel Lezcano3dd03372014-01-06 12:34:41 +01007099static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007100{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007101 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007102
Suresh Siddha786d6dc2011-12-01 17:07:35 -08007103 if (ilb < nr_cpu_ids && idle_cpu(ilb))
7104 return ilb;
7105
7106 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007107}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007108
7109/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007110 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
7111 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
7112 * CPU (if there is one).
7113 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01007114static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007115{
7116 int ilb_cpu;
7117
7118 nohz.next_balance++;
7119
Daniel Lezcano3dd03372014-01-06 12:34:41 +01007120 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007121
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007122 if (ilb_cpu >= nr_cpu_ids)
7123 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007124
Suresh Siddhacd490c52011-12-06 11:26:34 -08007125 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08007126 return;
7127 /*
7128 * Use smp_send_reschedule() instead of resched_cpu().
7129 * This way we generate a sched IPI on the target cpu which
7130 * is idle. And the softirq performing nohz idle load balance
7131 * will be run before returning from the IPI.
7132 */
7133 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007134 return;
7135}
7136
Alex Shic1cc0172012-09-10 15:10:58 +08007137static inline void nohz_balance_exit_idle(int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08007138{
7139 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01007140 /*
7141 * Completely isolated CPUs don't ever set, so we must test.
7142 */
7143 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
7144 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
7145 atomic_dec(&nohz.nr_cpus);
7146 }
Suresh Siddha71325962012-01-19 18:28:57 -08007147 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
7148 }
7149}
7150
Suresh Siddha69e1e812011-12-01 17:07:33 -08007151static inline void set_cpu_sd_state_busy(void)
7152{
7153 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307154 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08007155
Suresh Siddha69e1e812011-12-01 17:07:33 -08007156 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307157 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02007158
7159 if (!sd || !sd->nohz_idle)
7160 goto unlock;
7161 sd->nohz_idle = 0;
7162
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007163 atomic_inc(&sd->groups->sgc->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02007164unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08007165 rcu_read_unlock();
7166}
7167
7168void set_cpu_sd_state_idle(void)
7169{
7170 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307171 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08007172
Suresh Siddha69e1e812011-12-01 17:07:33 -08007173 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307174 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02007175
7176 if (!sd || sd->nohz_idle)
7177 goto unlock;
7178 sd->nohz_idle = 1;
7179
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007180 atomic_dec(&sd->groups->sgc->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02007181unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08007182 rcu_read_unlock();
7183}
7184
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007185/*
Alex Shic1cc0172012-09-10 15:10:58 +08007186 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007187 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007188 */
Alex Shic1cc0172012-09-10 15:10:58 +08007189void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007190{
Suresh Siddha71325962012-01-19 18:28:57 -08007191 /*
7192 * If this cpu is going down, then nothing needs to be done.
7193 */
7194 if (!cpu_active(cpu))
7195 return;
7196
Alex Shic1cc0172012-09-10 15:10:58 +08007197 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
7198 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007199
Mike Galbraithd987fc72011-12-05 10:01:47 +01007200 /*
7201 * If we're a completely isolated CPU, we don't play.
7202 */
7203 if (on_null_domain(cpu_rq(cpu)))
7204 return;
7205
Alex Shic1cc0172012-09-10 15:10:58 +08007206 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
7207 atomic_inc(&nohz.nr_cpus);
7208 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007209}
Suresh Siddha71325962012-01-19 18:28:57 -08007210
Paul Gortmaker0db06282013-06-19 14:53:51 -04007211static int sched_ilb_notifier(struct notifier_block *nfb,
Suresh Siddha71325962012-01-19 18:28:57 -08007212 unsigned long action, void *hcpu)
7213{
7214 switch (action & ~CPU_TASKS_FROZEN) {
7215 case CPU_DYING:
Alex Shic1cc0172012-09-10 15:10:58 +08007216 nohz_balance_exit_idle(smp_processor_id());
Suresh Siddha71325962012-01-19 18:28:57 -08007217 return NOTIFY_OK;
7218 default:
7219 return NOTIFY_DONE;
7220 }
7221}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007222#endif
7223
7224static DEFINE_SPINLOCK(balancing);
7225
Peter Zijlstra49c022e2011-04-05 10:14:25 +02007226/*
7227 * Scale the max load_balance interval with the number of CPUs in the system.
7228 * This trades load-balance latency on larger machines for less cross talk.
7229 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007230void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02007231{
7232 max_load_balance_interval = HZ*num_online_cpus()/10;
7233}
7234
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007235/*
7236 * It checks each scheduling domain to see if it is due to be balanced,
7237 * and initiates a balancing operation if so.
7238 *
Libinb9b08532013-04-01 19:14:01 +08007239 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007240 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01007241static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007242{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007243 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01007244 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007245 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02007246 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007247 /* Earliest time when we have to do rebalance again */
7248 unsigned long next_balance = jiffies + 60*HZ;
7249 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07007250 int need_serialize, need_decay = 0;
7251 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007252
Paul Turner48a16752012-10-04 13:18:31 +02007253 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08007254
Peter Zijlstradce840a2011-04-07 14:09:50 +02007255 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007256 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07007257 /*
7258 * Decay the newidle max times here because this is a regular
7259 * visit to all the domains. Decay ~1% per second.
7260 */
7261 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
7262 sd->max_newidle_lb_cost =
7263 (sd->max_newidle_lb_cost * 253) / 256;
7264 sd->next_decay_max_lb_cost = jiffies + HZ;
7265 need_decay = 1;
7266 }
7267 max_cost += sd->max_newidle_lb_cost;
7268
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007269 if (!(sd->flags & SD_LOAD_BALANCE))
7270 continue;
7271
Jason Lowf48627e2013-09-13 11:26:53 -07007272 /*
7273 * Stop the load balance at this level. There is another
7274 * CPU in our sched group which is doing load balancing more
7275 * actively.
7276 */
7277 if (!continue_balancing) {
7278 if (need_decay)
7279 continue;
7280 break;
7281 }
7282
Jason Low52a08ef2014-05-08 17:49:22 -07007283 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007284
7285 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007286 if (need_serialize) {
7287 if (!spin_trylock(&balancing))
7288 goto out;
7289 }
7290
7291 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007292 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007293 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02007294 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09007295 * env->dst_cpu, so we can't know our idle
7296 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007297 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09007298 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007299 }
7300 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07007301 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007302 }
7303 if (need_serialize)
7304 spin_unlock(&balancing);
7305out:
7306 if (time_after(next_balance, sd->last_balance + interval)) {
7307 next_balance = sd->last_balance + interval;
7308 update_next_balance = 1;
7309 }
Jason Lowf48627e2013-09-13 11:26:53 -07007310 }
7311 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007312 /*
Jason Lowf48627e2013-09-13 11:26:53 -07007313 * Ensure the rq-wide value also decays but keep it at a
7314 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007315 */
Jason Lowf48627e2013-09-13 11:26:53 -07007316 rq->max_idle_balance_cost =
7317 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007318 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007319 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007320
7321 /*
7322 * next_balance will be updated only when there is a need.
7323 * When the cpu is attached to null domain for ex, it will not be
7324 * updated.
7325 */
7326 if (likely(update_next_balance))
7327 rq->next_balance = next_balance;
7328}
7329
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007330#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007331/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007332 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007333 * rebalancing for all the cpus for whom scheduler ticks are stopped.
7334 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01007335static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007336{
Daniel Lezcano208cb162014-01-06 12:34:44 +01007337 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007338 struct rq *rq;
7339 int balance_cpu;
7340
Suresh Siddha1c792db2011-12-01 17:07:32 -08007341 if (idle != CPU_IDLE ||
7342 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
7343 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007344
7345 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08007346 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007347 continue;
7348
7349 /*
7350 * If this cpu gets work to do, stop the load balancing
7351 * work being done for other cpus. Next load
7352 * balancing owner will pick it up.
7353 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08007354 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007355 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007356
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02007357 rq = cpu_rq(balance_cpu);
7358
Tim Chened61bbc2014-05-20 14:39:27 -07007359 /*
7360 * If time for next balance is due,
7361 * do the balance.
7362 */
7363 if (time_after_eq(jiffies, rq->next_balance)) {
7364 raw_spin_lock_irq(&rq->lock);
7365 update_rq_clock(rq);
7366 update_idle_cpu_load(rq);
7367 raw_spin_unlock_irq(&rq->lock);
7368 rebalance_domains(rq, CPU_IDLE);
7369 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007370
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007371 if (time_after(this_rq->next_balance, rq->next_balance))
7372 this_rq->next_balance = rq->next_balance;
7373 }
7374 nohz.next_balance = this_rq->next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08007375end:
7376 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007377}
7378
7379/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007380 * Current heuristic for kicking the idle load balancer in the presence
7381 * of an idle cpu is the system.
7382 * - This rq has more than one task.
7383 * - At any scheduler domain level, this cpu's scheduler group has multiple
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007384 * busy cpu's exceeding the group's capacity.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007385 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
7386 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007387 */
Daniel Lezcano4a725622014-01-06 12:34:39 +01007388static inline int nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007389{
7390 unsigned long now = jiffies;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007391 struct sched_domain *sd;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007392 struct sched_group_capacity *sgc;
Daniel Lezcano4a725622014-01-06 12:34:39 +01007393 int nr_busy, cpu = rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007394
Daniel Lezcano4a725622014-01-06 12:34:39 +01007395 if (unlikely(rq->idle_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007396 return 0;
7397
Suresh Siddha1c792db2011-12-01 17:07:32 -08007398 /*
7399 * We may be recently in ticked or tickless idle mode. At the first
7400 * busy tick after returning from idle, we will update the busy stats.
7401 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08007402 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08007403 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007404
7405 /*
7406 * None are in tickless mode and hence no need for NOHZ idle load
7407 * balancing.
7408 */
7409 if (likely(!atomic_read(&nohz.nr_cpus)))
7410 return 0;
Suresh Siddha1c792db2011-12-01 17:07:32 -08007411
7412 if (time_before(now, nohz.next_balance))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007413 return 0;
7414
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007415 if (rq->nr_running >= 2)
7416 goto need_kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007417
Peter Zijlstra067491b2011-12-07 14:32:08 +01007418 rcu_read_lock();
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307419 sd = rcu_dereference(per_cpu(sd_busy, cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007420
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307421 if (sd) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007422 sgc = sd->groups->sgc;
7423 nr_busy = atomic_read(&sgc->nr_busy_cpus);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307424
7425 if (nr_busy > 1)
Peter Zijlstra067491b2011-12-07 14:32:08 +01007426 goto need_kick_unlock;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007427 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05307428
7429 sd = rcu_dereference(per_cpu(sd_asym, cpu));
7430
7431 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
7432 sched_domain_span(sd)) < cpu))
7433 goto need_kick_unlock;
7434
Peter Zijlstra067491b2011-12-07 14:32:08 +01007435 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007436 return 0;
Peter Zijlstra067491b2011-12-07 14:32:08 +01007437
7438need_kick_unlock:
7439 rcu_read_unlock();
Suresh Siddha0b005cf2011-12-01 17:07:34 -08007440need_kick:
7441 return 1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007442}
7443#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01007444static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007445#endif
7446
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007447/*
7448 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007449 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007450 */
7451static void run_rebalance_domains(struct softirq_action *h)
7452{
Daniel Lezcano208cb162014-01-06 12:34:44 +01007453 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07007454 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007455 CPU_IDLE : CPU_NOT_IDLE;
7456
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01007457 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007458
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007459 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007460 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007461 * balancing on behalf of the other idle cpus whose ticks are
7462 * stopped.
7463 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01007464 nohz_idle_balance(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007465}
7466
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007467/*
7468 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007469 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01007470void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007471{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007472 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01007473 if (unlikely(on_null_domain(rq)))
7474 return;
7475
7476 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007477 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007478#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01007479 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01007480 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007481#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007482}
7483
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01007484static void rq_online_fair(struct rq *rq)
7485{
7486 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007487
7488 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01007489}
7490
7491static void rq_offline_fair(struct rq *rq)
7492{
7493 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07007494
7495 /* Ensure any throttled groups are reachable by pick_next_task */
7496 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01007497}
7498
Dhaval Giani55e12e52008-06-24 23:39:43 +05307499#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02007500
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007501/*
7502 * scheduler tick hitting a task of our scheduling class:
7503 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007504static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007505{
7506 struct cfs_rq *cfs_rq;
7507 struct sched_entity *se = &curr->se;
7508
7509 for_each_sched_entity(se) {
7510 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007511 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007512 }
Ben Segall18bf2802012-10-04 12:51:20 +02007513
Dave Kleikamp10e84b92013-07-31 13:53:35 -07007514 if (numabalancing_enabled)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02007515 task_tick_numa(rq, curr);
Linus Torvalds3d59eeb2012-12-16 14:33:25 -08007516
Ben Segall18bf2802012-10-04 12:51:20 +02007517 update_rq_runnable_avg(rq, 1);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007518}
7519
7520/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007521 * called on fork with the child task as argument from the parent's context
7522 * - child not yet on the tasklist
7523 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007524 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007525static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007526{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09007527 struct cfs_rq *cfs_rq;
7528 struct sched_entity *se = &p->se, *curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02007529 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007530 struct rq *rq = this_rq();
7531 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007532
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007533 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007534
Peter Zijlstra861d0342010-08-19 13:31:43 +02007535 update_rq_clock(rq);
7536
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09007537 cfs_rq = task_cfs_rq(current);
7538 curr = cfs_rq->curr;
7539
Daisuke Nishimura6c9a27f2013-09-10 18:16:36 +09007540 /*
7541 * Not only the cpu but also the task_group of the parent might have
7542 * been changed after parent->se.parent,cfs_rq were copied to
7543 * child->se.parent,cfs_rq. So call __set_task_cpu() to make those
7544 * of child point to valid ones.
7545 */
7546 rcu_read_lock();
7547 __set_task_cpu(p, this_cpu);
7548 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007549
Ting Yang7109c4422007-08-28 12:53:24 +02007550 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007551
Mike Galbraithb5d9d732009-09-08 11:12:28 +02007552 if (curr)
7553 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02007554 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02007555
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007556 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02007557 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02007558 * Upon rescheduling, sched_class::put_prev_task() will place
7559 * 'current' within the tree based on its new key value.
7560 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02007561 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +04007562 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02007563 }
7564
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01007565 se->vruntime -= cfs_rq->min_vruntime;
7566
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007567 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007568}
7569
Steven Rostedtcb469842008-01-25 21:08:22 +01007570/*
7571 * Priority of the task has changed. Check to see if we preempt
7572 * the current task.
7573 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007574static void
7575prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01007576{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007577 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007578 return;
7579
Steven Rostedtcb469842008-01-25 21:08:22 +01007580 /*
7581 * Reschedule if we are currently running on this runqueue and
7582 * our priority decreased, or if we are not currently running on
7583 * this runqueue and our priority is higher than the current's
7584 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007585 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01007586 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +04007587 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01007588 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02007589 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01007590}
7591
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007592static void switched_from_fair(struct rq *rq, struct task_struct *p)
7593{
7594 struct sched_entity *se = &p->se;
7595 struct cfs_rq *cfs_rq = cfs_rq_of(se);
7596
7597 /*
George McCollister791c9e02014-02-18 17:56:51 -06007598 * Ensure the task's vruntime is normalized, so that when it's
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007599 * switched back to the fair class the enqueue_entity(.flags=0) will
7600 * do the right thing.
7601 *
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007602 * If it's queued, then the dequeue_entity(.flags=0) will already
7603 * have normalized the vruntime, if it's !queued, then only when
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007604 * the task is sleeping will it still have non-normalized vruntime.
7605 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007606 if (!task_on_rq_queued(p) && p->state != TASK_RUNNING) {
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007607 /*
7608 * Fix up our vruntime so that the current sleep doesn't
7609 * cause 'unlimited' sleep bonus.
7610 */
7611 place_entity(cfs_rq, se, 0);
7612 se->vruntime -= cfs_rq->min_vruntime;
7613 }
Paul Turner9ee474f2012-10-04 13:18:30 +02007614
Alex Shi141965c2013-06-26 13:05:39 +08007615#ifdef CONFIG_SMP
Paul Turner9ee474f2012-10-04 13:18:30 +02007616 /*
7617 * Remove our load from contribution when we leave sched_fair
7618 * and ensure we don't carry in an old decay_count if we
7619 * switch back.
7620 */
Kirill Tkhai87e3c8a2013-07-21 04:32:07 +04007621 if (se->avg.decay_count) {
7622 __synchronize_entity_decay(se);
7623 subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
Paul Turner9ee474f2012-10-04 13:18:30 +02007624 }
7625#endif
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007626}
7627
Steven Rostedtcb469842008-01-25 21:08:22 +01007628/*
7629 * We switched to the sched_fair class.
7630 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007631static void switched_to_fair(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01007632{
Michael wangeb7a59b2014-02-20 11:14:53 +08007633#ifdef CONFIG_FAIR_GROUP_SCHED
Kirill Tkhaif36c0192014-08-06 12:06:01 +04007634 struct sched_entity *se = &p->se;
Michael wangeb7a59b2014-02-20 11:14:53 +08007635 /*
7636 * Since the real-depth could have been changed (only FAIR
7637 * class maintain depth value), reset depth properly.
7638 */
7639 se->depth = se->parent ? se->parent->depth + 1 : 0;
7640#endif
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007641 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007642 return;
7643
Steven Rostedtcb469842008-01-25 21:08:22 +01007644 /*
7645 * We were most likely switched from sched_rt, so
7646 * kick off the schedule if running, otherwise just see
7647 * if we can still preempt the current task.
7648 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007649 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04007650 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01007651 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02007652 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01007653}
7654
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007655/* Account for a task changing its policy or group.
7656 *
7657 * This routine is mostly called to set cfs_rq->curr field when a task
7658 * migrates between groups/classes.
7659 */
7660static void set_curr_task_fair(struct rq *rq)
7661{
7662 struct sched_entity *se = &rq->curr->se;
7663
Paul Turnerec12cb72011-07-21 09:43:30 -07007664 for_each_sched_entity(se) {
7665 struct cfs_rq *cfs_rq = cfs_rq_of(se);
7666
7667 set_next_entity(cfs_rq, se);
7668 /* ensure bandwidth has been allocated on our new cfs_rq */
7669 account_cfs_rq_runtime(cfs_rq, 0);
7670 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007671}
7672
Peter Zijlstra029632f2011-10-25 10:00:11 +02007673void init_cfs_rq(struct cfs_rq *cfs_rq)
7674{
7675 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007676 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
7677#ifndef CONFIG_64BIT
7678 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
7679#endif
Alex Shi141965c2013-06-26 13:05:39 +08007680#ifdef CONFIG_SMP
Paul Turner9ee474f2012-10-04 13:18:30 +02007681 atomic64_set(&cfs_rq->decay_counter, 1);
Alex Shi25099402013-06-20 10:18:55 +08007682 atomic_long_set(&cfs_rq->removed_load, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02007683#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02007684}
7685
Peter Zijlstra810b3812008-02-29 15:21:01 -05007686#ifdef CONFIG_FAIR_GROUP_SCHED
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007687static void task_move_group_fair(struct task_struct *p, int queued)
Peter Zijlstra810b3812008-02-29 15:21:01 -05007688{
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007689 struct sched_entity *se = &p->se;
Paul Turneraff3e492012-10-04 13:18:30 +02007690 struct cfs_rq *cfs_rq;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007691
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007692 /*
7693 * If the task was not on the rq at the time of this cgroup movement
7694 * it must have been asleep, sleeping tasks keep their ->vruntime
7695 * absolute on their old rq until wakeup (needed for the fair sleeper
7696 * bonus in place_entity()).
7697 *
7698 * If it was on the rq, we've just 'preempted' it, which does convert
7699 * ->vruntime to a relative base.
7700 *
7701 * Make sure both cases convert their relative position when migrating
7702 * to another cgroup's rq. This does somewhat interfere with the
7703 * fair sleeper stuff for the first placement, but who cares.
7704 */
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007705 /*
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007706 * When !queued, vruntime of the task has usually NOT been normalized.
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007707 * But there are some cases where it has already been normalized:
7708 *
7709 * - Moving a forked child which is waiting for being woken up by
7710 * wake_up_new_task().
Daisuke Nishimura62af3782011-12-15 14:37:41 +09007711 * - Moving a task which has been woken up by try_to_wake_up() and
7712 * waiting for actually being woken up by sched_ttwu_pending().
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007713 *
7714 * To prevent boost or penalty in the new cfs_rq caused by delta
7715 * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
7716 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007717 if (!queued && (!se->sum_exec_runtime || p->state == TASK_WAKING))
7718 queued = 1;
Daisuke Nishimura7ceff012011-12-15 14:36:07 +09007719
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007720 if (!queued)
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007721 se->vruntime -= cfs_rq_of(se)->min_vruntime;
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007722 set_task_rq(p, task_cpu(p));
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007723 se->depth = se->parent ? se->parent->depth + 1 : 0;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007724 if (!queued) {
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007725 cfs_rq = cfs_rq_of(se);
7726 se->vruntime += cfs_rq->min_vruntime;
Paul Turneraff3e492012-10-04 13:18:30 +02007727#ifdef CONFIG_SMP
7728 /*
7729 * migrate_task_rq_fair() will have removed our previous
7730 * contribution, but we must synchronize for ongoing future
7731 * decay.
7732 */
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007733 se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
7734 cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
Paul Turneraff3e492012-10-04 13:18:30 +02007735#endif
7736 }
Peter Zijlstra810b3812008-02-29 15:21:01 -05007737}
Peter Zijlstra029632f2011-10-25 10:00:11 +02007738
7739void free_fair_sched_group(struct task_group *tg)
7740{
7741 int i;
7742
7743 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
7744
7745 for_each_possible_cpu(i) {
7746 if (tg->cfs_rq)
7747 kfree(tg->cfs_rq[i]);
7748 if (tg->se)
7749 kfree(tg->se[i]);
7750 }
7751
7752 kfree(tg->cfs_rq);
7753 kfree(tg->se);
7754}
7755
7756int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7757{
7758 struct cfs_rq *cfs_rq;
7759 struct sched_entity *se;
7760 int i;
7761
7762 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
7763 if (!tg->cfs_rq)
7764 goto err;
7765 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
7766 if (!tg->se)
7767 goto err;
7768
7769 tg->shares = NICE_0_LOAD;
7770
7771 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
7772
7773 for_each_possible_cpu(i) {
7774 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
7775 GFP_KERNEL, cpu_to_node(i));
7776 if (!cfs_rq)
7777 goto err;
7778
7779 se = kzalloc_node(sizeof(struct sched_entity),
7780 GFP_KERNEL, cpu_to_node(i));
7781 if (!se)
7782 goto err_free_rq;
7783
7784 init_cfs_rq(cfs_rq);
7785 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
7786 }
7787
7788 return 1;
7789
7790err_free_rq:
7791 kfree(cfs_rq);
7792err:
7793 return 0;
7794}
7795
7796void unregister_fair_sched_group(struct task_group *tg, int cpu)
7797{
7798 struct rq *rq = cpu_rq(cpu);
7799 unsigned long flags;
7800
7801 /*
7802 * Only empty task groups can be destroyed; so we can speculatively
7803 * check on_list without danger of it being re-added.
7804 */
7805 if (!tg->cfs_rq[cpu]->on_list)
7806 return;
7807
7808 raw_spin_lock_irqsave(&rq->lock, flags);
7809 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
7810 raw_spin_unlock_irqrestore(&rq->lock, flags);
7811}
7812
7813void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7814 struct sched_entity *se, int cpu,
7815 struct sched_entity *parent)
7816{
7817 struct rq *rq = cpu_rq(cpu);
7818
7819 cfs_rq->tg = tg;
7820 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007821 init_cfs_rq_runtime(cfs_rq);
7822
7823 tg->cfs_rq[cpu] = cfs_rq;
7824 tg->se[cpu] = se;
7825
7826 /* se could be NULL for root_task_group */
7827 if (!se)
7828 return;
7829
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007830 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02007831 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007832 se->depth = 0;
7833 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02007834 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01007835 se->depth = parent->depth + 1;
7836 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02007837
7838 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07007839 /* guarantee group entities always have weight */
7840 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02007841 se->parent = parent;
7842}
7843
7844static DEFINE_MUTEX(shares_mutex);
7845
7846int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7847{
7848 int i;
7849 unsigned long flags;
7850
7851 /*
7852 * We can't change the weight of the root cgroup.
7853 */
7854 if (!tg->se[0])
7855 return -EINVAL;
7856
7857 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
7858
7859 mutex_lock(&shares_mutex);
7860 if (tg->shares == shares)
7861 goto done;
7862
7863 tg->shares = shares;
7864 for_each_possible_cpu(i) {
7865 struct rq *rq = cpu_rq(i);
7866 struct sched_entity *se;
7867
7868 se = tg->se[i];
7869 /* Propagate contribution to hierarchy */
7870 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02007871
7872 /* Possible calls to update_curr() need rq clock */
7873 update_rq_clock(rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08007874 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02007875 update_cfs_shares(group_cfs_rq(se));
7876 raw_spin_unlock_irqrestore(&rq->lock, flags);
7877 }
7878
7879done:
7880 mutex_unlock(&shares_mutex);
7881 return 0;
7882}
7883#else /* CONFIG_FAIR_GROUP_SCHED */
7884
7885void free_fair_sched_group(struct task_group *tg) { }
7886
7887int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7888{
7889 return 1;
7890}
7891
7892void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
7893
7894#endif /* CONFIG_FAIR_GROUP_SCHED */
7895
Peter Zijlstra810b3812008-02-29 15:21:01 -05007896
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07007897static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00007898{
7899 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00007900 unsigned int rr_interval = 0;
7901
7902 /*
7903 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
7904 * idle runqueue:
7905 */
Peter Williams0d721ce2009-09-21 01:31:53 +00007906 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08007907 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00007908
7909 return rr_interval;
7910}
7911
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007912/*
7913 * All the scheduling class methods:
7914 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007915const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02007916 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007917 .enqueue_task = enqueue_task_fair,
7918 .dequeue_task = dequeue_task_fair,
7919 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05007920 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007921
Ingo Molnar2e09bf52007-10-15 17:00:05 +02007922 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007923
7924 .pick_next_task = pick_next_task_fair,
7925 .put_prev_task = put_prev_task_fair,
7926
Peter Williams681f3e62007-10-24 18:23:51 +02007927#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08007928 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02007929 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08007930
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01007931 .rq_online = rq_online_fair,
7932 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01007933
7934 .task_waking = task_waking_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02007935#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007936
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007937 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007938 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01007939 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01007940
7941 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01007942 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01007943 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05007944
Peter Williams0d721ce2009-09-21 01:31:53 +00007945 .get_rr_interval = get_rr_interval_fair,
7946
Peter Zijlstra810b3812008-02-29 15:21:01 -05007947#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007948 .task_move_group = task_move_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05007949#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007950};
7951
7952#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02007953void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007954{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007955 struct cfs_rq *cfs_rq;
7956
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01007957 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02007958 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02007959 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01007960 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007961}
7962#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02007963
7964__init void init_sched_fair_class(void)
7965{
7966#ifdef CONFIG_SMP
7967 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
7968
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007969#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08007970 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007971 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Suresh Siddha71325962012-01-19 18:28:57 -08007972 cpu_notifier(sched_ilb_notifier, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02007973#endif
7974#endif /* SMP */
7975
7976}