blob: 1f1a73c8b7c9b17972f1d80102b6fd83cd0fac84 [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
Peter Zijlstra90eec102015-11-16 11:08:45 +010020 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Christian Ehrhardt1983a922009-11-30 12:16:47 +010023#include <linux/sched.h>
Mel Gormancb251762016-02-05 09:08:36 +000024#include <linux/latencytop.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>
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -070033#include <linux/module.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020034
35#include <trace/events/sched.h>
36
37#include "sched.h"
Patrick Bellasiedd28d32015-07-07 15:33:20 +010038#include "tune.h"
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -070039#include "walt.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010040
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020041/*
Peter Zijlstra21805082007-08-25 18:41:53 +020042 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090043 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020044 *
Peter Zijlstra21805082007-08-25 18:41:53 +020045 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020046 * 'timeslice length' - timeslices in CFS are of variable length
47 * and have no persistent notion like in traditional, time-slice
48 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020049 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020050 * (to see the precise effective timeslice length of your workload,
51 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020052 */
Mike Galbraith21406922010-03-11 17:17:15 +010053unsigned int sysctl_sched_latency = 6000000ULL;
54unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020055
Juri Lelli1931b932016-07-29 14:04:11 +010056unsigned int sysctl_sched_sync_hint_enable = 1;
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +010057unsigned int sysctl_sched_cstate_aware = 1;
Juri Lelli1931b932016-07-29 14:04:11 +010058
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -070059#ifdef CONFIG_SCHED_WALT
60unsigned int sysctl_sched_use_walt_cpu_util = 1;
61unsigned int sysctl_sched_use_walt_task_util = 1;
Srinath Sridharan3a73c962016-07-22 13:21:15 +010062__read_mostly unsigned int sysctl_sched_walt_cpu_high_irqload =
63 (10 * NSEC_PER_MSEC);
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -070064#endif
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020065/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010066 * The initial- and re-scaling of tunables is configurable
67 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
68 *
69 * Options are:
70 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
71 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
72 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
73 */
74enum sched_tunable_scaling sysctl_sched_tunable_scaling
75 = SCHED_TUNABLESCALING_LOG;
76
77/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010078 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090079 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010080 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020081unsigned int sysctl_sched_min_granularity = 750000ULL;
82unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010083
84/*
85 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
86 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020087static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010088
89/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020090 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020091 * parent will (try to) run first.
92 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020093unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020094
95/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020096 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020097 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020098 *
99 * This option delays the preemption effects of decoupled workloads
100 * and reduces their over-scheduling. Synchronous workloads will still
101 * have immediate wakeup/sleep latencies.
102 */
Mike Galbraith172e0822009-09-09 15:41:37 +0200103unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +0100104unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200105
Ingo Molnarda84d962007-10-15 17:00:18 +0200106const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
107
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800108/*
109 * The exponential sliding window over which load is averaged for shares
110 * distribution.
111 * (default: 10msec)
112 */
113unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
114
Paul Turnerec12cb72011-07-21 09:43:30 -0700115#ifdef CONFIG_CFS_BANDWIDTH
116/*
117 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
118 * each time a cfs_rq requests quota.
119 *
120 * Note: in the case that the slice exceeds the runtime remaining (either due
121 * to consumption or the quota being specified to be smaller than the slice)
122 * we will always only issue the remaining available time.
123 *
124 * default: 5 msec, units: microseconds
125 */
126unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
127#endif
128
Morten Rasmussen32731632016-07-25 14:34:26 +0100129/*
130 * The margin used when comparing utilization with CPU capacity:
Morten Rasmussen5dbcdde2016-10-14 14:41:12 +0100131 * util * margin < capacity * 1024
Morten Rasmussen32731632016-07-25 14:34:26 +0100132 */
133unsigned int capacity_margin = 1280; /* ~20% */
134
Paul Gortmaker85276322013-04-19 15:10:50 -0400135static inline void update_load_add(struct load_weight *lw, unsigned long inc)
136{
137 lw->weight += inc;
138 lw->inv_weight = 0;
139}
140
141static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
142{
143 lw->weight -= dec;
144 lw->inv_weight = 0;
145}
146
147static inline void update_load_set(struct load_weight *lw, unsigned long w)
148{
149 lw->weight = w;
150 lw->inv_weight = 0;
151}
152
Peter Zijlstra029632f2011-10-25 10:00:11 +0200153/*
154 * Increase the granularity value when there are more CPUs,
155 * because with more CPUs the 'effective latency' as visible
156 * to users decreases. But the relationship is not linear,
157 * so pick a second-best guess by going with the log2 of the
158 * number of CPUs.
159 *
160 * This idea comes from the SD scheduler of Con Kolivas:
161 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200162static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200163{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200164 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200165 unsigned int factor;
166
167 switch (sysctl_sched_tunable_scaling) {
168 case SCHED_TUNABLESCALING_NONE:
169 factor = 1;
170 break;
171 case SCHED_TUNABLESCALING_LINEAR:
172 factor = cpus;
173 break;
174 case SCHED_TUNABLESCALING_LOG:
175 default:
176 factor = 1 + ilog2(cpus);
177 break;
178 }
179
180 return factor;
181}
182
183static void update_sysctl(void)
184{
185 unsigned int factor = get_update_sysctl_factor();
186
187#define SET_SYSCTL(name) \
188 (sysctl_##name = (factor) * normalized_sysctl_##name)
189 SET_SYSCTL(sched_min_granularity);
190 SET_SYSCTL(sched_latency);
191 SET_SYSCTL(sched_wakeup_granularity);
192#undef SET_SYSCTL
193}
194
195void sched_init_granularity(void)
196{
197 update_sysctl();
198}
199
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100200#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200201#define WMULT_SHIFT 32
202
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100203static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200204{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100205 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200206
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100207 if (likely(lw->inv_weight))
208 return;
209
210 w = scale_load_down(lw->weight);
211
212 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
213 lw->inv_weight = 1;
214 else if (unlikely(!w))
215 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200216 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100217 lw->inv_weight = WMULT_CONST / w;
218}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200219
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100220/*
221 * delta_exec * weight / lw.weight
222 * OR
223 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
224 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800225 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100226 * we're guaranteed shift stays positive because inv_weight is guaranteed to
227 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
228 *
229 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
230 * weight/lw.weight <= 1, and therefore our shift will also be positive.
231 */
232static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
233{
234 u64 fact = scale_load_down(weight);
235 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200236
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100237 __update_inv_weight(lw);
238
239 if (unlikely(fact >> 32)) {
240 while (fact >> 32) {
241 fact >>= 1;
242 shift--;
243 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200244 }
245
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100246 /* hint to use a 32x32->64 mul */
247 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200248
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100249 while (fact >> 32) {
250 fact >>= 1;
251 shift--;
252 }
253
254 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200255}
256
257
258const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200259
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200260/**************************************************************
261 * CFS operations on generic schedulable entities:
262 */
263
264#ifdef CONFIG_FAIR_GROUP_SCHED
265
266/* cpu runqueue to which this cfs_rq is attached */
267static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
268{
269 return cfs_rq->rq;
270}
271
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200272/* An entity is a task if it doesn't "own" a runqueue */
273#define entity_is_task(se) (!se->my_q)
274
Peter Zijlstra8f488942009-07-24 12:25:30 +0200275static inline struct task_struct *task_of(struct sched_entity *se)
276{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200277 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200278 return container_of(se, struct task_struct, se);
279}
280
Peter Zijlstrab7581492008-04-19 19:45:00 +0200281/* Walk up scheduling entities hierarchy */
282#define for_each_sched_entity(se) \
283 for (; se; se = se->parent)
284
285static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
286{
287 return p->se.cfs_rq;
288}
289
290/* runqueue on which this entity is (to be) queued */
291static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
292{
293 return se->cfs_rq;
294}
295
296/* runqueue "owned" by this group */
297static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
298{
299 return grp->my_q;
300}
301
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800302static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
303{
304 if (!cfs_rq->on_list) {
Vincent Guittot96956e22016-11-08 10:53:44 +0100305 struct rq *rq = rq_of(cfs_rq);
306 int cpu = cpu_of(rq);
Paul Turner67e86252010-11-15 15:47:05 -0800307 /*
308 * Ensure we either appear before our parent (if already
309 * enqueued) or force our parent to appear after us when it is
Vincent Guittot96956e22016-11-08 10:53:44 +0100310 * enqueued. The fact that we always enqueue bottom-up
311 * reduces this to two cases and a special case for the root
312 * cfs_rq. Furthermore, it also means that we will always reset
313 * tmp_alone_branch either when the branch is connected
314 * to a tree or when we reach the beg of the tree
Paul Turner67e86252010-11-15 15:47:05 -0800315 */
316 if (cfs_rq->tg->parent &&
Vincent Guittot96956e22016-11-08 10:53:44 +0100317 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) {
318 /*
319 * If parent is already on the list, we add the child
320 * just before. Thanks to circular linked property of
321 * the list, this means to put the child at the tail
322 * of the list that starts by parent.
323 */
Paul Turner67e86252010-11-15 15:47:05 -0800324 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
Vincent Guittot96956e22016-11-08 10:53:44 +0100325 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list));
326 /*
327 * The branch is now connected to its tree so we can
328 * reset tmp_alone_branch to the beginning of the
329 * list.
330 */
331 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
332 } else if (!cfs_rq->tg->parent) {
333 /*
334 * cfs rq without parent should be put
335 * at the tail of the list.
336 */
337 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
338 &rq->leaf_cfs_rq_list);
339 /*
340 * We have reach the beg of a tree so we can reset
341 * tmp_alone_branch to the beginning of the list.
342 */
343 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
344 } else {
345 /*
346 * The parent has not already been added so we want to
347 * make sure that it will be put after us.
348 * tmp_alone_branch points to the beg of the branch
349 * where we will add parent.
350 */
351 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
352 rq->tmp_alone_branch);
353 /*
354 * update tmp_alone_branch to points to the new beg
355 * of the branch
356 */
357 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list;
Paul Turner67e86252010-11-15 15:47:05 -0800358 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800359
360 cfs_rq->on_list = 1;
361 }
362}
363
364static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
365{
366 if (cfs_rq->on_list) {
367 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
368 cfs_rq->on_list = 0;
369 }
370}
371
Peter Zijlstrab7581492008-04-19 19:45:00 +0200372/* Iterate thr' all leaf cfs_rq's on a runqueue */
373#define for_each_leaf_cfs_rq(rq, cfs_rq) \
374 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
375
376/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100377static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378is_same_group(struct sched_entity *se, struct sched_entity *pse)
379{
380 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100381 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200382
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100383 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200384}
385
386static inline struct sched_entity *parent_entity(struct sched_entity *se)
387{
388 return se->parent;
389}
390
Peter Zijlstra464b7522008-10-24 11:06:15 +0200391static void
392find_matching_se(struct sched_entity **se, struct sched_entity **pse)
393{
394 int se_depth, pse_depth;
395
396 /*
397 * preemption test can be made between sibling entities who are in the
398 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
399 * both tasks until we find their ancestors who are siblings of common
400 * parent.
401 */
402
403 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100404 se_depth = (*se)->depth;
405 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200406
407 while (se_depth > pse_depth) {
408 se_depth--;
409 *se = parent_entity(*se);
410 }
411
412 while (pse_depth > se_depth) {
413 pse_depth--;
414 *pse = parent_entity(*pse);
415 }
416
417 while (!is_same_group(*se, *pse)) {
418 *se = parent_entity(*se);
419 *pse = parent_entity(*pse);
420 }
421}
422
Peter Zijlstra8f488942009-07-24 12:25:30 +0200423#else /* !CONFIG_FAIR_GROUP_SCHED */
424
425static inline struct task_struct *task_of(struct sched_entity *se)
426{
427 return container_of(se, struct task_struct, se);
428}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200429
430static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
431{
432 return container_of(cfs_rq, struct rq, cfs);
433}
434
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200435#define entity_is_task(se) 1
436
Peter Zijlstrab7581492008-04-19 19:45:00 +0200437#define for_each_sched_entity(se) \
438 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200439
Peter Zijlstrab7581492008-04-19 19:45:00 +0200440static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200441{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200442 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200443}
444
Peter Zijlstrab7581492008-04-19 19:45:00 +0200445static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
446{
447 struct task_struct *p = task_of(se);
448 struct rq *rq = task_rq(p);
449
450 return &rq->cfs;
451}
452
453/* runqueue "owned" by this group */
454static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
455{
456 return NULL;
457}
458
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800459static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
460{
461}
462
463static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
464{
465}
466
Peter Zijlstrab7581492008-04-19 19:45:00 +0200467#define for_each_leaf_cfs_rq(rq, cfs_rq) \
468 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
469
Peter Zijlstrab7581492008-04-19 19:45:00 +0200470static inline struct sched_entity *parent_entity(struct sched_entity *se)
471{
472 return NULL;
473}
474
Peter Zijlstra464b7522008-10-24 11:06:15 +0200475static inline void
476find_matching_se(struct sched_entity **se, struct sched_entity **pse)
477{
478}
479
Peter Zijlstrab7581492008-04-19 19:45:00 +0200480#endif /* CONFIG_FAIR_GROUP_SCHED */
481
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700482static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100483void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484
485/**************************************************************
486 * Scheduling class tree data structure manipulation methods:
487 */
488
Andrei Epure1bf08232013-03-12 21:12:24 +0200489static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200490{
Andrei Epure1bf08232013-03-12 21:12:24 +0200491 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200492 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200493 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200494
Andrei Epure1bf08232013-03-12 21:12:24 +0200495 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200496}
497
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200498static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200499{
500 s64 delta = (s64)(vruntime - min_vruntime);
501 if (delta < 0)
502 min_vruntime = vruntime;
503
504 return min_vruntime;
505}
506
Fabio Checconi54fdc582009-07-16 12:32:27 +0200507static inline int entity_before(struct sched_entity *a,
508 struct sched_entity *b)
509{
510 return (s64)(a->vruntime - b->vruntime) < 0;
511}
512
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200513static void update_min_vruntime(struct cfs_rq *cfs_rq)
514{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200515 struct sched_entity *curr = cfs_rq->curr;
516
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200517 u64 vruntime = cfs_rq->min_vruntime;
518
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200519 if (curr) {
520 if (curr->on_rq)
521 vruntime = curr->vruntime;
522 else
523 curr = NULL;
524 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200525
526 if (cfs_rq->rb_leftmost) {
527 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
528 struct sched_entity,
529 run_node);
530
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200531 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200532 vruntime = se->vruntime;
533 else
534 vruntime = min_vruntime(vruntime, se->vruntime);
535 }
536
Andrei Epure1bf08232013-03-12 21:12:24 +0200537 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200538 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200539#ifndef CONFIG_64BIT
540 smp_wmb();
541 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
542#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200543}
544
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200545/*
546 * Enqueue an entity into the rb-tree:
547 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200548static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200549{
550 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
551 struct rb_node *parent = NULL;
552 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200553 int leftmost = 1;
554
555 /*
556 * Find the right place in the rbtree:
557 */
558 while (*link) {
559 parent = *link;
560 entry = rb_entry(parent, struct sched_entity, run_node);
561 /*
562 * We dont care about collisions. Nodes with
563 * the same key stay together.
564 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200565 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200566 link = &parent->rb_left;
567 } else {
568 link = &parent->rb_right;
569 leftmost = 0;
570 }
571 }
572
573 /*
574 * Maintain a cache of leftmost tree entries (it is frequently
575 * used):
576 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200577 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200578 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200579
580 rb_link_node(&se->run_node, parent, link);
581 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200582}
583
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200584static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200585{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100586 if (cfs_rq->rb_leftmost == &se->run_node) {
587 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100588
589 next_node = rb_next(&se->run_node);
590 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100591 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200592
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200593 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200594}
595
Peter Zijlstra029632f2011-10-25 10:00:11 +0200596struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200597{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100598 struct rb_node *left = cfs_rq->rb_leftmost;
599
600 if (!left)
601 return NULL;
602
603 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200604}
605
Rik van Rielac53db52011-02-01 09:51:03 -0500606static struct sched_entity *__pick_next_entity(struct sched_entity *se)
607{
608 struct rb_node *next = rb_next(&se->run_node);
609
610 if (!next)
611 return NULL;
612
613 return rb_entry(next, struct sched_entity, run_node);
614}
615
616#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200617struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200618{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100619 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200620
Balbir Singh70eee742008-02-22 13:25:53 +0530621 if (!last)
622 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100623
624 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200625}
626
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200627/**************************************************************
628 * Scheduling class statistics methods:
629 */
630
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100631int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700632 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100633 loff_t *ppos)
634{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700635 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200636 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100637
638 if (ret || !write)
639 return ret;
640
641 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
642 sysctl_sched_min_granularity);
643
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100644#define WRT_SYSCTL(name) \
645 (normalized_sysctl_##name = sysctl_##name / (factor))
646 WRT_SYSCTL(sched_min_granularity);
647 WRT_SYSCTL(sched_latency);
648 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100649#undef WRT_SYSCTL
650
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100651 return 0;
652}
653#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200654
655/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200657 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100658static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200659{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200660 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100661 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200662
663 return delta;
664}
665
666/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200667 * The idea is to set a period in which each task runs once.
668 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200669 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200670 * this period because otherwise the slices get too small.
671 *
672 * p = (nr <= nl) ? l : l*nr/nl
673 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200674static u64 __sched_period(unsigned long nr_running)
675{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800676 if (unlikely(nr_running > sched_nr_latency))
677 return nr_running * sysctl_sched_min_granularity;
678 else
679 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200680}
681
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200682/*
683 * We calculate the wall-time slice from the period by taking a part
684 * proportional to the weight.
685 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200686 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200687 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200688static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200689{
Mike Galbraith0a582442009-01-02 12:16:42 +0100690 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200691
Mike Galbraith0a582442009-01-02 12:16:42 +0100692 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100693 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200694 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100695
696 cfs_rq = cfs_rq_of(se);
697 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200698
Mike Galbraith0a582442009-01-02 12:16:42 +0100699 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200700 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100701
702 update_load_add(&lw, se->load.weight);
703 load = &lw;
704 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100705 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100706 }
707 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200708}
709
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200710/*
Andrei Epure660cc002013-03-11 12:03:20 +0200711 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200712 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200713 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200714 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200715static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200716{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200717 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200718}
719
Alex Shia75cdaa2013-06-20 10:18:47 +0800720#ifdef CONFIG_SMP
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100721static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100722static unsigned long task_h_load(struct task_struct *p);
723
Yuyang Du9d89c252015-07-15 08:04:37 +0800724/*
725 * We choose a half-life close to 1 scheduling period.
Leo Yan84fb5a12015-09-15 18:57:37 +0800726 * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are
727 * dependent on this value.
Yuyang Du9d89c252015-07-15 08:04:37 +0800728 */
729#define LOAD_AVG_PERIOD 32
730#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
Leo Yan84fb5a12015-09-15 18:57:37 +0800731#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */
Alex Shia75cdaa2013-06-20 10:18:47 +0800732
Yuyang Du540247f2015-07-15 08:04:39 +0800733/* Give new sched_entity start runnable values to heavy its load in infant time */
734void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800735{
Yuyang Du540247f2015-07-15 08:04:39 +0800736 struct sched_avg *sa = &se->avg;
Alex Shia75cdaa2013-06-20 10:18:47 +0800737
Yuyang Du9d89c252015-07-15 08:04:37 +0800738 sa->last_update_time = 0;
739 /*
740 * sched_avg's period_contrib should be strictly less then 1024, so
741 * we give it 1023 to make sure it is almost a period (1024us), and
742 * will definitely be update (after enqueue).
743 */
744 sa->period_contrib = 1023;
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200745 /*
746 * Tasks are intialized with full load to be seen as heavy tasks until
747 * they get a chance to stabilize to their real load level.
748 * Group entities are intialized with zero load to reflect the fact that
749 * nothing has been attached to the task group yet.
750 */
751 if (entity_is_task(se))
752 sa->load_avg = scale_load_down(se->load.weight);
Yuyang Du9d89c252015-07-15 08:04:37 +0800753 sa->load_sum = sa->load_avg * LOAD_AVG_MAX;
Todd Kjosc6a6f3b2016-03-11 16:44:16 -0800754
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800755 /*
756 * At this point, util_avg won't be used in select_task_rq_fair anyway
757 */
Viresh Kumare0907552017-11-02 15:13:26 +0530758 sa->util_avg = 0;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800759 sa->util_sum = 0;
Yuyang Du9d89c252015-07-15 08:04:37 +0800760 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800761}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800762
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200763static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
Vincent Guittot793cfff2016-11-08 10:53:42 +0100764static void attach_entity_cfs_rq(struct sched_entity *se);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200765
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800766/*
767 * With new tasks being created, their initial util_avgs are extrapolated
768 * based on the cfs_rq's current util_avg:
769 *
770 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
771 *
772 * However, in many cases, the above util_avg does not give a desired
773 * value. Moreover, the sum of the util_avgs may be divergent, such
774 * as when the series is a harmonic series.
775 *
776 * To solve this problem, we also cap the util_avg of successive tasks to
777 * only 1/2 of the left utilization budget:
778 *
779 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
780 *
781 * where n denotes the nth task.
782 *
783 * For example, a simplest series from the beginning would be like:
784 *
785 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
786 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
787 *
788 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
789 * if util_avg > util_avg_cap.
790 */
791void post_init_entity_util_avg(struct sched_entity *se)
792{
793 struct cfs_rq *cfs_rq = cfs_rq_of(se);
794 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800795 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800796
797 if (cap > 0) {
798 if (cfs_rq->avg.util_avg != 0) {
799 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
800 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
801
802 if (sa->util_avg > cap)
803 sa->util_avg = cap;
804 } else {
805 sa->util_avg = cap;
806 }
807 sa->util_sum = sa->util_avg * LOAD_AVG_MAX;
808 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200809
810 if (entity_is_task(se)) {
811 struct task_struct *p = task_of(se);
812 if (p->sched_class != &fair_sched_class) {
813 /*
814 * For !fair tasks do:
815 *
816 update_cfs_rq_load_avg(now, cfs_rq, false);
817 attach_entity_load_avg(cfs_rq, se);
818 switched_from_fair(rq, p);
819 *
820 * such that the next switched_to_fair() has the
821 * expected state.
822 */
Vincent Guittot793cfff2016-11-08 10:53:42 +0100823 se->avg.last_update_time = cfs_rq_clock_task(cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200824 return;
825 }
826 }
827
Vincent Guittot793cfff2016-11-08 10:53:42 +0100828 attach_entity_cfs_rq(se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800829}
830
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200831#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800832void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800833{
834}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800835void post_init_entity_util_avg(struct sched_entity *se)
836{
837}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200838static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
839{
840}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200841#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800842
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200843/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100844 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200845 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200846static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200847{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200848 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200849 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100850 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200851
852 if (unlikely(!curr))
853 return;
854
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100855 delta_exec = now - curr->exec_start;
856 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100857 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200858
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200859 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100860
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100861 schedstat_set(curr->statistics.exec_max,
862 max(delta_exec, curr->statistics.exec_max));
863
864 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500865 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100866
867 curr->vruntime += calc_delta_fair(delta_exec, curr);
868 update_min_vruntime(cfs_rq);
869
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100870 if (entity_is_task(curr)) {
871 struct task_struct *curtask = task_of(curr);
872
Ingo Molnarf977bb42009-09-13 18:15:54 +0200873 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100874 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700875 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100876 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700877
878 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200879}
880
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100881static void update_curr_fair(struct rq *rq)
882{
883 update_curr(cfs_rq_of(&rq->curr->se));
884}
885
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200886static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200887update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200888{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500889 u64 wait_start, prev_wait_start;
890
891 if (!schedstat_enabled())
892 return;
893
894 wait_start = rq_clock(rq_of(cfs_rq));
895 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800896
897 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500898 likely(wait_start > prev_wait_start))
899 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800900
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500901 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200902}
903
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500904static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800905update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
906{
907 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000908 u64 delta;
909
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500910 if (!schedstat_enabled())
911 return;
912
913 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800914
915 if (entity_is_task(se)) {
916 p = task_of(se);
917 if (task_on_rq_migrating(p)) {
918 /*
919 * Preserve migrating task's wait time so wait_start
920 * time stamp can be adjusted to accumulate wait time
921 * prior to migration.
922 */
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500923 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800924 return;
925 }
926 trace_sched_stat_wait(p, delta);
927 }
928
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500929 schedstat_set(se->statistics.wait_max,
930 max(schedstat_val(se->statistics.wait_max), delta));
931 schedstat_inc(se->statistics.wait_count);
932 schedstat_add(se->statistics.wait_sum, delta);
933 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800934}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800935
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500936static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500937update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
938{
939 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500940 u64 sleep_start, block_start;
941
942 if (!schedstat_enabled())
943 return;
944
945 sleep_start = schedstat_val(se->statistics.sleep_start);
946 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500947
948 if (entity_is_task(se))
949 tsk = task_of(se);
950
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500951 if (sleep_start) {
952 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500953
954 if ((s64)delta < 0)
955 delta = 0;
956
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500957 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
958 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500959
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500960 schedstat_set(se->statistics.sleep_start, 0);
961 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500962
963 if (tsk) {
964 account_scheduler_latency(tsk, delta >> 10, 1);
965 trace_sched_stat_sleep(tsk, delta);
966 }
967 }
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500968 if (block_start) {
969 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500970
971 if ((s64)delta < 0)
972 delta = 0;
973
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500974 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
975 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500976
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500977 schedstat_set(se->statistics.block_start, 0);
978 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500979
980 if (tsk) {
981 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500982 schedstat_add(se->statistics.iowait_sum, delta);
983 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500984 trace_sched_stat_iowait(tsk, delta);
985 }
986
987 trace_sched_stat_blocked(tsk, delta);
Riley Andrewsa5a44472015-10-02 00:39:53 -0700988 trace_sched_blocked_reason(tsk);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500989
990 /*
991 * Blocking time is in units of nanosecs, so shift by
992 * 20 to get a milliseconds-range estimation of the
993 * amount of time that the task spent sleeping:
994 */
995 if (unlikely(prof_on == SLEEP_PROFILING)) {
996 profile_hits(SLEEP_PROFILING,
997 (void *)get_wchan(tsk),
998 delta >> 20);
999 }
1000 account_scheduler_latency(tsk, delta >> 10, 0);
1001 }
1002 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001003}
1004
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001005/*
1006 * Task is being enqueued - update stats:
1007 */
Mel Gormancb251762016-02-05 09:08:36 +00001008static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05001009update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001010{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001011 if (!schedstat_enabled())
1012 return;
1013
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001014 /*
1015 * Are we enqueueing a waiting task? (for current tasks
1016 * a dequeue/enqueue event is a NOP)
1017 */
Ingo Molnar429d43b2007-10-15 17:00:03 +02001018 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +02001019 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05001020
1021 if (flags & ENQUEUE_WAKEUP)
1022 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001023}
1024
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001025static inline void
Mel Gormancb251762016-02-05 09:08:36 +00001026update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001027{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001028
1029 if (!schedstat_enabled())
1030 return;
1031
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001032 /*
1033 * Mark the end of the wait period if dequeueing a
1034 * waiting task:
1035 */
Ingo Molnar429d43b2007-10-15 17:00:03 +02001036 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +02001037 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +00001038
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001039 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
1040 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +00001041
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001042 if (tsk->state & TASK_INTERRUPTIBLE)
1043 schedstat_set(se->statistics.sleep_start,
1044 rq_clock(rq_of(cfs_rq)));
1045 if (tsk->state & TASK_UNINTERRUPTIBLE)
1046 schedstat_set(se->statistics.block_start,
1047 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001048 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001049}
1050
1051/*
1052 * We are picking a new current task - update its stats:
1053 */
1054static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001055update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001056{
1057 /*
1058 * We are starting a new run period:
1059 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001060 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001061}
1062
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001063/**************************************************
1064 * Scheduling class queueing methods:
1065 */
1066
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001067#ifdef CONFIG_NUMA_BALANCING
1068/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001069 * Approximate time to scan a full NUMA task in ms. The task scan period is
1070 * calculated based on the tasks virtual memory size and
1071 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001072 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001073unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1074unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001075
1076/* Portion of address space to scan in MB */
1077unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001078
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02001079/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1080unsigned int sysctl_numa_balancing_scan_delay = 1000;
1081
Mel Gorman598f0ec2013-10-07 11:28:55 +01001082static unsigned int task_nr_scan_windows(struct task_struct *p)
1083{
1084 unsigned long rss = 0;
1085 unsigned long nr_scan_pages;
1086
1087 /*
1088 * Calculations based on RSS as non-present and empty pages are skipped
1089 * by the PTE scanner and NUMA hinting faults should be trapped based
1090 * on resident pages
1091 */
1092 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1093 rss = get_mm_rss(p->mm);
1094 if (!rss)
1095 rss = nr_scan_pages;
1096
1097 rss = round_up(rss, nr_scan_pages);
1098 return rss / nr_scan_pages;
1099}
1100
1101/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1102#define MAX_SCAN_WINDOW 2560
1103
1104static unsigned int task_scan_min(struct task_struct *p)
1105{
Jason Low316c1608d2015-04-28 13:00:20 -07001106 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001107 unsigned int scan, floor;
1108 unsigned int windows = 1;
1109
Kirill Tkhai64192652014-10-16 14:39:37 +04001110 if (scan_size < MAX_SCAN_WINDOW)
1111 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001112 floor = 1000 / windows;
1113
1114 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1115 return max_t(unsigned int, floor, scan);
1116}
1117
1118static unsigned int task_scan_max(struct task_struct *p)
1119{
1120 unsigned int smin = task_scan_min(p);
1121 unsigned int smax;
1122
1123 /* Watch for min being lower than max due to floor calculations */
1124 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
1125 return max(smin, smax);
1126}
1127
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001128static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1129{
1130 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1131 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1132}
1133
1134static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1135{
1136 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1137 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1138}
1139
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001140struct numa_group {
1141 atomic_t refcount;
1142
1143 spinlock_t lock; /* nr_tasks, tasks */
1144 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +01001145 pid_t gid;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001146 int active_nodes;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001147
1148 struct rcu_head rcu;
Mel Gorman989348b2013-10-07 11:29:40 +01001149 unsigned long total_faults;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001150 unsigned long max_faults_cpu;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001151 /*
1152 * Faults_cpu is used to decide whether memory should move
1153 * towards the CPU. As a consequence, these stats are weighted
1154 * more by CPU use than by memory faults.
1155 */
Rik van Riel50ec8a42014-01-27 17:03:42 -05001156 unsigned long *faults_cpu;
Mel Gorman989348b2013-10-07 11:29:40 +01001157 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001158};
1159
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001160/* Shared or private faults. */
1161#define NR_NUMA_HINT_FAULT_TYPES 2
1162
1163/* Memory and CPU locality */
1164#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1165
1166/* Averaged statistics, and temporary buffers. */
1167#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1168
Mel Gormane29cf082013-10-07 11:29:22 +01001169pid_t task_numa_group_id(struct task_struct *p)
1170{
1171 return p->numa_group ? p->numa_group->gid : 0;
1172}
1173
Iulia Manda44dba3d2014-10-31 02:13:31 +02001174/*
1175 * The averaged statistics, shared & private, memory & cpu,
1176 * occupy the first half of the array. The second half of the
1177 * array is for current counters, which are averaged into the
1178 * first set by task_numa_placement.
1179 */
1180static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001181{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001182 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001183}
1184
1185static inline unsigned long task_faults(struct task_struct *p, int nid)
1186{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001187 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001188 return 0;
1189
Iulia Manda44dba3d2014-10-31 02:13:31 +02001190 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1191 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001192}
1193
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001194static inline unsigned long group_faults(struct task_struct *p, int nid)
1195{
1196 if (!p->numa_group)
1197 return 0;
1198
Iulia Manda44dba3d2014-10-31 02:13:31 +02001199 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1200 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001201}
1202
Rik van Riel20e07de2014-01-27 17:03:43 -05001203static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1204{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001205 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1206 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001207}
1208
Rik van Riel4142c3e2016-01-25 17:07:39 -05001209/*
1210 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1211 * considered part of a numa group's pseudo-interleaving set. Migrations
1212 * between these nodes are slowed down, to allow things to settle down.
1213 */
1214#define ACTIVE_NODE_FRACTION 3
1215
1216static bool numa_is_active_node(int nid, struct numa_group *ng)
1217{
1218 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1219}
1220
Rik van Riel6c6b1192014-10-17 03:29:52 -04001221/* Handle placement on systems where not all nodes are directly connected. */
1222static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1223 int maxdist, bool task)
1224{
1225 unsigned long score = 0;
1226 int node;
1227
1228 /*
1229 * All nodes are directly connected, and the same distance
1230 * from each other. No need for fancy placement algorithms.
1231 */
1232 if (sched_numa_topology_type == NUMA_DIRECT)
1233 return 0;
1234
1235 /*
1236 * This code is called for each node, introducing N^2 complexity,
1237 * which should be ok given the number of nodes rarely exceeds 8.
1238 */
1239 for_each_online_node(node) {
1240 unsigned long faults;
1241 int dist = node_distance(nid, node);
1242
1243 /*
1244 * The furthest away nodes in the system are not interesting
1245 * for placement; nid was already counted.
1246 */
1247 if (dist == sched_max_numa_distance || node == nid)
1248 continue;
1249
1250 /*
1251 * On systems with a backplane NUMA topology, compare groups
1252 * of nodes, and move tasks towards the group with the most
1253 * memory accesses. When comparing two nodes at distance
1254 * "hoplimit", only nodes closer by than "hoplimit" are part
1255 * of each group. Skip other nodes.
1256 */
1257 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1258 dist > maxdist)
1259 continue;
1260
1261 /* Add up the faults from nearby nodes. */
1262 if (task)
1263 faults = task_faults(p, node);
1264 else
1265 faults = group_faults(p, node);
1266
1267 /*
1268 * On systems with a glueless mesh NUMA topology, there are
1269 * no fixed "groups of nodes". Instead, nodes that are not
1270 * directly connected bounce traffic through intermediate
1271 * nodes; a numa_group can occupy any set of nodes.
1272 * The further away a node is, the less the faults count.
1273 * This seems to result in good task placement.
1274 */
1275 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1276 faults *= (sched_max_numa_distance - dist);
1277 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1278 }
1279
1280 score += faults;
1281 }
1282
1283 return score;
1284}
1285
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001286/*
1287 * These return the fraction of accesses done by a particular task, or
1288 * task group, on a particular numa node. The group weight is given a
1289 * larger multiplier, in order to group tasks together that are almost
1290 * evenly spread out between numa nodes.
1291 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001292static inline unsigned long task_weight(struct task_struct *p, int nid,
1293 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001294{
Rik van Riel7bd95322014-10-17 03:29:51 -04001295 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001296
Iulia Manda44dba3d2014-10-31 02:13:31 +02001297 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001298 return 0;
1299
1300 total_faults = p->total_numa_faults;
1301
1302 if (!total_faults)
1303 return 0;
1304
Rik van Riel7bd95322014-10-17 03:29:51 -04001305 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001306 faults += score_nearby_nodes(p, nid, dist, true);
1307
Rik van Riel7bd95322014-10-17 03:29:51 -04001308 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001309}
1310
Rik van Riel7bd95322014-10-17 03:29:51 -04001311static inline unsigned long group_weight(struct task_struct *p, int nid,
1312 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001313{
Rik van Riel7bd95322014-10-17 03:29:51 -04001314 unsigned long faults, total_faults;
1315
1316 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001317 return 0;
1318
Rik van Riel7bd95322014-10-17 03:29:51 -04001319 total_faults = p->numa_group->total_faults;
1320
1321 if (!total_faults)
1322 return 0;
1323
1324 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001325 faults += score_nearby_nodes(p, nid, dist, false);
1326
Rik van Riel7bd95322014-10-17 03:29:51 -04001327 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001328}
1329
Rik van Riel10f39042014-01-27 17:03:44 -05001330bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1331 int src_nid, int dst_cpu)
1332{
1333 struct numa_group *ng = p->numa_group;
1334 int dst_nid = cpu_to_node(dst_cpu);
1335 int last_cpupid, this_cpupid;
1336
1337 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1338
1339 /*
1340 * Multi-stage node selection is used in conjunction with a periodic
1341 * migration fault to build a temporal task<->page relation. By using
1342 * a two-stage filter we remove short/unlikely relations.
1343 *
1344 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1345 * a task's usage of a particular page (n_p) per total usage of this
1346 * page (n_t) (in a given time-span) to a probability.
1347 *
1348 * Our periodic faults will sample this probability and getting the
1349 * same result twice in a row, given these samples are fully
1350 * independent, is then given by P(n)^2, provided our sample period
1351 * is sufficiently short compared to the usage pattern.
1352 *
1353 * This quadric squishes small probabilities, making it less likely we
1354 * act on an unlikely task<->page relation.
1355 */
1356 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1357 if (!cpupid_pid_unset(last_cpupid) &&
1358 cpupid_to_nid(last_cpupid) != dst_nid)
1359 return false;
1360
1361 /* Always allow migrate on private faults */
1362 if (cpupid_match_pid(p, last_cpupid))
1363 return true;
1364
1365 /* A shared fault, but p->numa_group has not been set up yet. */
1366 if (!ng)
1367 return true;
1368
1369 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001370 * Destination node is much more heavily used than the source
1371 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001372 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001373 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1374 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001375 return true;
1376
1377 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001378 * Distribute memory according to CPU & memory use on each node,
1379 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1380 *
1381 * faults_cpu(dst) 3 faults_cpu(src)
1382 * --------------- * - > ---------------
1383 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001384 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001385 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1386 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001387}
1388
Mel Gormane6628d52013-10-07 11:29:02 +01001389static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001390static unsigned long source_load(int cpu, int type);
1391static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001392static unsigned long capacity_of(int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001393static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +01001394
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001395/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001396struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001397 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001398 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001399
1400 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001401 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001402
1403 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001404 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001405 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001406};
Mel Gormane6628d52013-10-07 11:29:02 +01001407
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001408/*
1409 * XXX borrowed from update_sg_lb_stats
1410 */
1411static void update_numa_stats(struct numa_stats *ns, int nid)
1412{
Rik van Riel83d7f242014-08-04 13:23:28 -04001413 int smt, cpu, cpus = 0;
1414 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001415
1416 memset(ns, 0, sizeof(*ns));
1417 for_each_cpu(cpu, cpumask_of_node(nid)) {
1418 struct rq *rq = cpu_rq(cpu);
1419
1420 ns->nr_running += rq->nr_running;
1421 ns->load += weighted_cpuload(cpu);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001422 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001423
1424 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001425 }
1426
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001427 /*
1428 * If we raced with hotplug and there are no CPUs left in our mask
1429 * the @ns structure is NULL'ed and task_numa_compare() will
1430 * not find this node attractive.
1431 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001432 * We'll either bail at !has_free_capacity, or we'll detect a huge
1433 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001434 */
1435 if (!cpus)
1436 return;
1437
Rik van Riel83d7f242014-08-04 13:23:28 -04001438 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1439 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1440 capacity = cpus / smt; /* cores */
1441
1442 ns->task_capacity = min_t(unsigned, capacity,
1443 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001444 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001445}
1446
Mel Gorman58d081b2013-10-07 11:29:10 +01001447struct task_numa_env {
1448 struct task_struct *p;
1449
1450 int src_cpu, src_nid;
1451 int dst_cpu, dst_nid;
1452
1453 struct numa_stats src_stats, dst_stats;
1454
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001455 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001456 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001457
1458 struct task_struct *best_task;
1459 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001460 int best_cpu;
1461};
1462
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001463static void task_numa_assign(struct task_numa_env *env,
1464 struct task_struct *p, long imp)
1465{
1466 if (env->best_task)
1467 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001468 if (p)
1469 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001470
1471 env->best_task = p;
1472 env->best_imp = imp;
1473 env->best_cpu = env->dst_cpu;
1474}
1475
Rik van Riel28a21742014-06-23 11:46:13 -04001476static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001477 struct task_numa_env *env)
1478{
Rik van Riele4991b22015-05-27 15:04:27 -04001479 long imb, old_imb;
1480 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001481 long src_capacity, dst_capacity;
1482
1483 /*
1484 * The load is corrected for the CPU capacity available on each node.
1485 *
1486 * src_load dst_load
1487 * ------------ vs ---------
1488 * src_capacity dst_capacity
1489 */
1490 src_capacity = env->src_stats.compute_capacity;
1491 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001492
1493 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001494 if (dst_load < src_load)
1495 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001496
1497 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001498 imb = dst_load * src_capacity * 100 -
1499 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001500 if (imb <= 0)
1501 return false;
1502
1503 /*
1504 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001505 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001506 */
Rik van Riel28a21742014-06-23 11:46:13 -04001507 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001508 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001509
Rik van Riele4991b22015-05-27 15:04:27 -04001510 if (orig_dst_load < orig_src_load)
1511 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001512
Rik van Riele4991b22015-05-27 15:04:27 -04001513 old_imb = orig_dst_load * src_capacity * 100 -
1514 orig_src_load * dst_capacity * env->imbalance_pct;
1515
1516 /* Would this change make things worse? */
1517 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001518}
1519
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001520/*
1521 * This checks if the overall compute and NUMA accesses of the system would
1522 * be improved if the source tasks was migrated to the target dst_cpu taking
1523 * into account that it might be best if task running on the dst_cpu should
1524 * be exchanged with the source task
1525 */
Rik van Riel887c2902013-10-07 11:29:31 +01001526static void task_numa_compare(struct task_numa_env *env,
1527 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001528{
1529 struct rq *src_rq = cpu_rq(env->src_cpu);
1530 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1531 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001532 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001533 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001534 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001535 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001536 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001537
1538 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001539 cur = task_rcu_dereference(&dst_rq->curr);
1540 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001541 cur = NULL;
1542
1543 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001544 * Because we have preemption enabled we can get migrated around and
1545 * end try selecting ourselves (current == env->p) as a swap candidate.
1546 */
1547 if (cur == env->p)
1548 goto unlock;
1549
1550 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001551 * "imp" is the fault differential for the source task between the
1552 * source and destination node. Calculate the total differential for
1553 * the source task and potential destination task. The more negative
1554 * the value is, the more rmeote accesses that would be expected to
1555 * be incurred if the tasks were swapped.
1556 */
1557 if (cur) {
1558 /* Skip this swap candidate if cannot move to the source cpu */
1559 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1560 goto unlock;
1561
Rik van Riel887c2902013-10-07 11:29:31 +01001562 /*
1563 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001564 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001565 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001566 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001567 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1568 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001569 /*
1570 * Add some hysteresis to prevent swapping the
1571 * tasks within a group over tiny differences.
1572 */
1573 if (cur->numa_group)
1574 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001575 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001576 /*
1577 * Compare the group weights. If a task is all by
1578 * itself (not part of a group), use the task weight
1579 * instead.
1580 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001581 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001582 imp += group_weight(cur, env->src_nid, dist) -
1583 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001584 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001585 imp += task_weight(cur, env->src_nid, dist) -
1586 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001587 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001588 }
1589
Rik van Riel0132c3e2014-06-23 11:46:16 -04001590 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001591 goto unlock;
1592
1593 if (!cur) {
1594 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001595 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001596 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001597 goto unlock;
1598
1599 goto balance;
1600 }
1601
1602 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001603 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1604 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001605 goto assign;
1606
1607 /*
1608 * In the overloaded case, try and keep the load balanced.
1609 */
1610balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001611 load = task_h_load(env->p);
1612 dst_load = env->dst_stats.load + load;
1613 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001614
Rik van Riel0132c3e2014-06-23 11:46:16 -04001615 if (moveimp > imp && moveimp > env->best_imp) {
1616 /*
1617 * If the improvement from just moving env->p direction is
1618 * better than swapping tasks around, check if a move is
1619 * possible. Store a slightly smaller score than moveimp,
1620 * so an actually idle CPU will win.
1621 */
1622 if (!load_too_imbalanced(src_load, dst_load, env)) {
1623 imp = moveimp - 1;
1624 cur = NULL;
1625 goto assign;
1626 }
1627 }
1628
1629 if (imp <= env->best_imp)
1630 goto unlock;
1631
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001632 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001633 load = task_h_load(cur);
1634 dst_load -= load;
1635 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001636 }
1637
Rik van Riel28a21742014-06-23 11:46:13 -04001638 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001639 goto unlock;
1640
Rik van Rielba7e5a22014-09-04 16:35:30 -04001641 /*
1642 * One idle CPU per node is evaluated for a task numa move.
1643 * Call select_idle_sibling to maybe find a better one.
1644 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001645 if (!cur) {
1646 /*
1647 * select_idle_siblings() uses an per-cpu cpumask that
1648 * can be used from IRQ context.
1649 */
1650 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001651 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1652 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001653 local_irq_enable();
1654 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001655
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001656assign:
1657 task_numa_assign(env, cur, imp);
1658unlock:
1659 rcu_read_unlock();
1660}
1661
Rik van Riel887c2902013-10-07 11:29:31 +01001662static void task_numa_find_cpu(struct task_numa_env *env,
1663 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001664{
1665 int cpu;
1666
1667 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1668 /* Skip this CPU if the source task cannot migrate */
1669 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1670 continue;
1671
1672 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001673 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001674 }
1675}
1676
Rik van Riel6f9aad02015-05-28 09:52:49 -04001677/* Only move tasks to a NUMA node less busy than the current node. */
1678static bool numa_has_capacity(struct task_numa_env *env)
1679{
1680 struct numa_stats *src = &env->src_stats;
1681 struct numa_stats *dst = &env->dst_stats;
1682
1683 if (src->has_free_capacity && !dst->has_free_capacity)
1684 return false;
1685
1686 /*
1687 * Only consider a task move if the source has a higher load
1688 * than the destination, corrected for CPU capacity on each node.
1689 *
1690 * src->load dst->load
1691 * --------------------- vs ---------------------
1692 * src->compute_capacity dst->compute_capacity
1693 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301694 if (src->load * dst->compute_capacity * env->imbalance_pct >
1695
1696 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001697 return true;
1698
1699 return false;
1700}
1701
Mel Gorman58d081b2013-10-07 11:29:10 +01001702static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001703{
Mel Gorman58d081b2013-10-07 11:29:10 +01001704 struct task_numa_env env = {
1705 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001706
Mel Gorman58d081b2013-10-07 11:29:10 +01001707 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001708 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001709
1710 .imbalance_pct = 112,
1711
1712 .best_task = NULL,
1713 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001714 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001715 };
1716 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001717 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001718 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001719 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001720
Mel Gorman58d081b2013-10-07 11:29:10 +01001721 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001722 * Pick the lowest SD_NUMA domain, as that would have the smallest
1723 * imbalance and would be the first to start moving tasks about.
1724 *
1725 * And we want to avoid any moving of tasks about, as that would create
1726 * random movement of tasks -- counter the numa conditions we're trying
1727 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001728 */
Mel Gormane6628d52013-10-07 11:29:02 +01001729 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001730 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001731 if (sd)
1732 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001733 rcu_read_unlock();
1734
Rik van Riel46a73e82013-11-11 19:29:25 -05001735 /*
1736 * Cpusets can break the scheduler domain tree into smaller
1737 * balance domains, some of which do not cross NUMA boundaries.
1738 * Tasks that are "trapped" in such domains cannot be migrated
1739 * elsewhere, so there is no point in (re)trying.
1740 */
1741 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001742 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001743 return -EINVAL;
1744 }
1745
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001746 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001747 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1748 taskweight = task_weight(p, env.src_nid, dist);
1749 groupweight = group_weight(p, env.src_nid, dist);
1750 update_numa_stats(&env.src_stats, env.src_nid);
1751 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1752 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001753 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001754
Rik van Riela43455a2014-06-04 16:09:42 -04001755 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001756 if (numa_has_capacity(&env))
1757 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001758
Rik van Riel9de05d42014-10-09 17:27:47 -04001759 /*
1760 * Look at other nodes in these cases:
1761 * - there is no space available on the preferred_nid
1762 * - the task is part of a numa_group that is interleaved across
1763 * multiple NUMA nodes; in order to better consolidate the group,
1764 * we need to check other locations.
1765 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001766 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001767 for_each_online_node(nid) {
1768 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001769 continue;
1770
Rik van Riel7bd95322014-10-17 03:29:51 -04001771 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001772 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1773 dist != env.dist) {
1774 taskweight = task_weight(p, env.src_nid, dist);
1775 groupweight = group_weight(p, env.src_nid, dist);
1776 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001777
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001778 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001779 taskimp = task_weight(p, nid, dist) - taskweight;
1780 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001781 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001782 continue;
1783
Rik van Riel7bd95322014-10-17 03:29:51 -04001784 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001785 env.dst_nid = nid;
1786 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001787 if (numa_has_capacity(&env))
1788 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001789 }
1790 }
1791
Rik van Riel68d1b022014-04-11 13:00:29 -04001792 /*
1793 * If the task is part of a workload that spans multiple NUMA nodes,
1794 * and is migrating into one of the workload's active nodes, remember
1795 * this node as the task's preferred numa node, so the workload can
1796 * settle down.
1797 * A task that migrated to a second choice node will be better off
1798 * trying for a better one later. Do not set the preferred node here.
1799 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001800 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001801 struct numa_group *ng = p->numa_group;
1802
Rik van Rieldb015da2014-06-23 11:41:34 -04001803 if (env.best_cpu == -1)
1804 nid = env.src_nid;
1805 else
1806 nid = env.dst_nid;
1807
Rik van Riel4142c3e2016-01-25 17:07:39 -05001808 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001809 sched_setnuma(p, env.dst_nid);
1810 }
1811
1812 /* No better CPU than the current one was found. */
1813 if (env.best_cpu == -1)
1814 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001815
Rik van Riel04bb2f92013-10-07 11:29:36 +01001816 /*
1817 * Reset the scan period if the task is being rescheduled on an
1818 * alternative node to recheck if the tasks is now properly placed.
1819 */
1820 p->numa_scan_period = task_scan_min(p);
1821
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001822 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001823 ret = migrate_task_to(p, env.best_cpu);
1824 if (ret != 0)
1825 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001826 return ret;
1827 }
1828
1829 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001830 if (ret != 0)
1831 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001832 put_task_struct(env.best_task);
1833 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001834}
1835
Mel Gorman6b9a7462013-10-07 11:29:11 +01001836/* Attempt to migrate a task to a CPU on the preferred node. */
1837static void numa_migrate_preferred(struct task_struct *p)
1838{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001839 unsigned long interval = HZ;
1840
Rik van Riel2739d3e2013-10-07 11:29:41 +01001841 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001842 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001843 return;
1844
1845 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001846 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1847 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001848
Mel Gorman6b9a7462013-10-07 11:29:11 +01001849 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001850 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001851 return;
1852
Mel Gorman6b9a7462013-10-07 11:29:11 +01001853 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001854 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001855}
1856
Rik van Riel04bb2f92013-10-07 11:29:36 +01001857/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001858 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001859 * tracking the nodes from which NUMA hinting faults are triggered. This can
1860 * be different from the set of nodes where the workload's memory is currently
1861 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001862 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001863static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001864{
1865 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001866 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001867
1868 for_each_online_node(nid) {
1869 faults = group_faults_cpu(numa_group, nid);
1870 if (faults > max_faults)
1871 max_faults = faults;
1872 }
1873
1874 for_each_online_node(nid) {
1875 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001876 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1877 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001878 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001879
1880 numa_group->max_faults_cpu = max_faults;
1881 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001882}
1883
1884/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001885 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1886 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001887 * period will be for the next scan window. If local/(local+remote) ratio is
1888 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1889 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001890 */
1891#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001892#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001893
1894/*
1895 * Increase the scan period (slow down scanning) if the majority of
1896 * our memory is already on our local node, or if the majority of
1897 * the page accesses are shared with other processes.
1898 * Otherwise, decrease the scan period.
1899 */
1900static void update_task_scan_period(struct task_struct *p,
1901 unsigned long shared, unsigned long private)
1902{
1903 unsigned int period_slot;
1904 int ratio;
1905 int diff;
1906
1907 unsigned long remote = p->numa_faults_locality[0];
1908 unsigned long local = p->numa_faults_locality[1];
1909
1910 /*
1911 * If there were no record hinting faults then either the task is
1912 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001913 * to automatic numa balancing. Related to that, if there were failed
1914 * migration then it implies we are migrating too quickly or the local
1915 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001916 */
Mel Gorman074c2382015-03-25 15:55:42 -07001917 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001918 p->numa_scan_period = min(p->numa_scan_period_max,
1919 p->numa_scan_period << 1);
1920
1921 p->mm->numa_next_scan = jiffies +
1922 msecs_to_jiffies(p->numa_scan_period);
1923
1924 return;
1925 }
1926
1927 /*
1928 * Prepare to scale scan period relative to the current period.
1929 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1930 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1931 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1932 */
1933 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1934 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1935 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1936 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1937 if (!slot)
1938 slot = 1;
1939 diff = slot * period_slot;
1940 } else {
1941 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1942
1943 /*
1944 * Scale scan rate increases based on sharing. There is an
1945 * inverse relationship between the degree of sharing and
1946 * the adjustment made to the scanning period. Broadly
1947 * speaking the intent is that there is little point
1948 * scanning faster if shared accesses dominate as it may
1949 * simply bounce migrations uselessly
1950 */
Yasuaki Ishimatsu2847c902014-10-22 16:04:35 +09001951 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1));
Rik van Riel04bb2f92013-10-07 11:29:36 +01001952 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1953 }
1954
1955 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1956 task_scan_min(p), task_scan_max(p));
1957 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1958}
1959
Rik van Riel7e2703e2014-01-27 17:03:45 -05001960/*
1961 * Get the fraction of time the task has been running since the last
1962 * NUMA placement cycle. The scheduler keeps similar statistics, but
1963 * decays those on a 32ms period, which is orders of magnitude off
1964 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
1965 * stats only if the task is so new there are no NUMA statistics yet.
1966 */
1967static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
1968{
1969 u64 runtime, delta, now;
1970 /* Use the start of this time slice to avoid calculations. */
1971 now = p->se.exec_start;
1972 runtime = p->se.sum_exec_runtime;
1973
1974 if (p->last_task_numa_placement) {
1975 delta = runtime - p->last_sum_exec_runtime;
1976 *period = now - p->last_task_numa_placement;
1977 } else {
Yuyang Du9d89c252015-07-15 08:04:37 +08001978 delta = p->se.avg.load_sum / p->se.load.weight;
1979 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001980 }
1981
1982 p->last_sum_exec_runtime = runtime;
1983 p->last_task_numa_placement = now;
1984
1985 return delta;
1986}
1987
Rik van Riel54009412014-10-17 03:29:53 -04001988/*
1989 * Determine the preferred nid for a task in a numa_group. This needs to
1990 * be done in a way that produces consistent results with group_weight,
1991 * otherwise workloads might not converge.
1992 */
1993static int preferred_group_nid(struct task_struct *p, int nid)
1994{
1995 nodemask_t nodes;
1996 int dist;
1997
1998 /* Direct connections between all NUMA nodes. */
1999 if (sched_numa_topology_type == NUMA_DIRECT)
2000 return nid;
2001
2002 /*
2003 * On a system with glueless mesh NUMA topology, group_weight
2004 * scores nodes according to the number of NUMA hinting faults on
2005 * both the node itself, and on nearby nodes.
2006 */
2007 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
2008 unsigned long score, max_score = 0;
2009 int node, max_node = nid;
2010
2011 dist = sched_max_numa_distance;
2012
2013 for_each_online_node(node) {
2014 score = group_weight(p, node, dist);
2015 if (score > max_score) {
2016 max_score = score;
2017 max_node = node;
2018 }
2019 }
2020 return max_node;
2021 }
2022
2023 /*
2024 * Finding the preferred nid in a system with NUMA backplane
2025 * interconnect topology is more involved. The goal is to locate
2026 * tasks from numa_groups near each other in the system, and
2027 * untangle workloads from different sides of the system. This requires
2028 * searching down the hierarchy of node groups, recursively searching
2029 * inside the highest scoring group of nodes. The nodemask tricks
2030 * keep the complexity of the search down.
2031 */
2032 nodes = node_online_map;
2033 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
2034 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00002035 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04002036 int a, b;
2037
2038 /* Are there nodes at this distance from each other? */
2039 if (!find_numa_distance(dist))
2040 continue;
2041
2042 for_each_node_mask(a, nodes) {
2043 unsigned long faults = 0;
2044 nodemask_t this_group;
2045 nodes_clear(this_group);
2046
2047 /* Sum group's NUMA faults; includes a==b case. */
2048 for_each_node_mask(b, nodes) {
2049 if (node_distance(a, b) < dist) {
2050 faults += group_faults(p, b);
2051 node_set(b, this_group);
2052 node_clear(b, nodes);
2053 }
2054 }
2055
2056 /* Remember the top group. */
2057 if (faults > max_faults) {
2058 max_faults = faults;
2059 max_group = this_group;
2060 /*
2061 * subtle: at the smallest distance there is
2062 * just one node left in each "group", the
2063 * winner is the preferred nid.
2064 */
2065 nid = a;
2066 }
2067 }
2068 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002069 if (!max_faults)
2070 break;
Rik van Riel54009412014-10-17 03:29:53 -04002071 nodes = max_group;
2072 }
2073 return nid;
2074}
2075
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002076static void task_numa_placement(struct task_struct *p)
2077{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002078 int seq, nid, max_nid = -1, max_group_nid = -1;
2079 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002080 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002081 unsigned long total_faults;
2082 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002083 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002084
Jason Low7e5a2c12015-04-30 17:28:14 -07002085 /*
2086 * The p->mm->numa_scan_seq field gets updated without
2087 * exclusive access. Use READ_ONCE() here to ensure
2088 * that the field is read in a single access:
2089 */
Jason Low316c1608d2015-04-28 13:00:20 -07002090 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002091 if (p->numa_scan_seq == seq)
2092 return;
2093 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002094 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002095
Rik van Riel7e2703e2014-01-27 17:03:45 -05002096 total_faults = p->numa_faults_locality[0] +
2097 p->numa_faults_locality[1];
2098 runtime = numa_get_avg_runtime(p, &period);
2099
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002100 /* If the task is part of a group prevent parallel updates to group stats */
2101 if (p->numa_group) {
2102 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002103 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002104 }
2105
Mel Gorman688b7582013-10-07 11:28:58 +01002106 /* Find the node with the highest number of faults */
2107 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002108 /* Keep track of the offsets in numa_faults array */
2109 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002110 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002111 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002112
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002113 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002114 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002115
Iulia Manda44dba3d2014-10-31 02:13:31 +02002116 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2117 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2118 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2119 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002120
Mel Gormanac8e8952013-10-07 11:29:03 +01002121 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002122 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2123 fault_types[priv] += p->numa_faults[membuf_idx];
2124 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002125
Rik van Riel7e2703e2014-01-27 17:03:45 -05002126 /*
2127 * Normalize the faults_from, so all tasks in a group
2128 * count according to CPU use, instead of by the raw
2129 * number of faults. Tasks with little runtime have
2130 * little over-all impact on throughput, and thus their
2131 * faults are less important.
2132 */
2133 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002134 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002135 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002136 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2137 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002138
Iulia Manda44dba3d2014-10-31 02:13:31 +02002139 p->numa_faults[mem_idx] += diff;
2140 p->numa_faults[cpu_idx] += f_diff;
2141 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002142 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002143 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002144 /*
2145 * safe because we can only change our own group
2146 *
2147 * mem_idx represents the offset for a given
2148 * nid and priv in a specific region because it
2149 * is at the beginning of the numa_faults array.
2150 */
2151 p->numa_group->faults[mem_idx] += diff;
2152 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002153 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002154 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002155 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002156 }
2157
Mel Gorman688b7582013-10-07 11:28:58 +01002158 if (faults > max_faults) {
2159 max_faults = faults;
2160 max_nid = nid;
2161 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002162
2163 if (group_faults > max_group_faults) {
2164 max_group_faults = group_faults;
2165 max_group_nid = nid;
2166 }
2167 }
2168
Rik van Riel04bb2f92013-10-07 11:29:36 +01002169 update_task_scan_period(p, fault_types[0], fault_types[1]);
2170
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002171 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002172 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002173 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002174 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002175 }
2176
Rik van Rielbb97fc32014-06-04 16:33:15 -04002177 if (max_faults) {
2178 /* Set the new preferred node */
2179 if (max_nid != p->numa_preferred_nid)
2180 sched_setnuma(p, max_nid);
2181
2182 if (task_node(p) != p->numa_preferred_nid)
2183 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002184 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002185}
2186
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002187static inline int get_numa_group(struct numa_group *grp)
2188{
2189 return atomic_inc_not_zero(&grp->refcount);
2190}
2191
2192static inline void put_numa_group(struct numa_group *grp)
2193{
2194 if (atomic_dec_and_test(&grp->refcount))
2195 kfree_rcu(grp, rcu);
2196}
2197
Mel Gorman3e6a9412013-10-07 11:29:35 +01002198static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2199 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002200{
2201 struct numa_group *grp, *my_grp;
2202 struct task_struct *tsk;
2203 bool join = false;
2204 int cpu = cpupid_to_cpu(cpupid);
2205 int i;
2206
2207 if (unlikely(!p->numa_group)) {
2208 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002209 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002210
2211 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2212 if (!grp)
2213 return;
2214
2215 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002216 grp->active_nodes = 1;
2217 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002218 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002219 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002220 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002221 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2222 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002223
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002224 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002225 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002226
Mel Gorman989348b2013-10-07 11:29:40 +01002227 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002228
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002229 grp->nr_tasks++;
2230 rcu_assign_pointer(p->numa_group, grp);
2231 }
2232
2233 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002234 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002235
2236 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002237 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002238
2239 grp = rcu_dereference(tsk->numa_group);
2240 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002241 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002242
2243 my_grp = p->numa_group;
2244 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002245 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002246
2247 /*
2248 * Only join the other group if its bigger; if we're the bigger group,
2249 * the other task will join us.
2250 */
2251 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002252 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002253
2254 /*
2255 * Tie-break on the grp address.
2256 */
2257 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002258 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002259
Rik van Rieldabe1d92013-10-07 11:29:34 +01002260 /* Always join threads in the same process. */
2261 if (tsk->mm == current->mm)
2262 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002263
Rik van Rieldabe1d92013-10-07 11:29:34 +01002264 /* Simple filter to avoid false positives due to PID collisions */
2265 if (flags & TNF_SHARED)
2266 join = true;
2267
Mel Gorman3e6a9412013-10-07 11:29:35 +01002268 /* Update priv based on whether false sharing was detected */
2269 *priv = !join;
2270
Rik van Rieldabe1d92013-10-07 11:29:34 +01002271 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002272 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002273
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002274 rcu_read_unlock();
2275
2276 if (!join)
2277 return;
2278
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002279 BUG_ON(irqs_disabled());
2280 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002281
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002282 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002283 my_grp->faults[i] -= p->numa_faults[i];
2284 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002285 }
2286 my_grp->total_faults -= p->total_numa_faults;
2287 grp->total_faults += p->total_numa_faults;
2288
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002289 my_grp->nr_tasks--;
2290 grp->nr_tasks++;
2291
2292 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002293 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002294
2295 rcu_assign_pointer(p->numa_group, grp);
2296
2297 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002298 return;
2299
2300no_join:
2301 rcu_read_unlock();
2302 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002303}
2304
2305void task_numa_free(struct task_struct *p)
2306{
2307 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002308 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002309 unsigned long flags;
2310 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002311
2312 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002313 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002314 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002315 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002316 grp->total_faults -= p->total_numa_faults;
2317
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002318 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002319 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002320 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002321 put_numa_group(grp);
2322 }
2323
Iulia Manda44dba3d2014-10-31 02:13:31 +02002324 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002325 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002326}
2327
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002328/*
2329 * Got a PROT_NONE fault for a page on @node.
2330 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002331void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002332{
2333 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002334 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002335 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002336 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002337 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002338 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002339
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302340 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002341 return;
2342
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002343 /* for example, ksmd faulting in a user's mm */
2344 if (!p->mm)
2345 return;
2346
Mel Gormanf809ca92013-10-07 11:28:57 +01002347 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002348 if (unlikely(!p->numa_faults)) {
2349 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002350 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002351
Iulia Manda44dba3d2014-10-31 02:13:31 +02002352 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2353 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002354 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002355
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002356 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002357 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002358 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002359
Mel Gormanfb003b82012-11-15 09:01:14 +00002360 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002361 * First accesses are treated as private, otherwise consider accesses
2362 * to be private if the accessing pid has not changed
2363 */
2364 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2365 priv = 1;
2366 } else {
2367 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002368 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002369 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002370 }
2371
Rik van Riel792568e2014-04-11 13:00:27 -04002372 /*
2373 * If a workload spans multiple NUMA nodes, a shared fault that
2374 * occurs wholly within the set of nodes that the workload is
2375 * actively using should be counted as local. This allows the
2376 * scan rate to slow down when a workload has settled down.
2377 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002378 ng = p->numa_group;
2379 if (!priv && !local && ng && ng->active_nodes > 1 &&
2380 numa_is_active_node(cpu_node, ng) &&
2381 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002382 local = 1;
2383
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002384 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002385
Rik van Riel2739d3e2013-10-07 11:29:41 +01002386 /*
2387 * Retry task to preferred node migration periodically, in case it
2388 * case it previously failed, or the scheduler moved us.
2389 */
2390 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002391 numa_migrate_preferred(p);
2392
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002393 if (migrated)
2394 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002395 if (flags & TNF_MIGRATE_FAIL)
2396 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002397
Iulia Manda44dba3d2014-10-31 02:13:31 +02002398 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2399 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002400 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002401}
2402
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002403static void reset_ptenuma_scan(struct task_struct *p)
2404{
Jason Low7e5a2c12015-04-30 17:28:14 -07002405 /*
2406 * We only did a read acquisition of the mmap sem, so
2407 * p->mm->numa_scan_seq is written to without exclusive access
2408 * and the update is not guaranteed to be atomic. That's not
2409 * much of an issue though, since this is just used for
2410 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2411 * expensive, to avoid any form of compiler optimizations:
2412 */
Jason Low316c1608d2015-04-28 13:00:20 -07002413 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002414 p->mm->numa_scan_offset = 0;
2415}
2416
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002417/*
2418 * The expensive part of numa migration is done from task_work context.
2419 * Triggered from task_tick_numa().
2420 */
2421void task_numa_work(struct callback_head *work)
2422{
2423 unsigned long migrate, next_scan, now = jiffies;
2424 struct task_struct *p = current;
2425 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002426 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002427 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002428 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002429 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002430 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002431
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002432 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002433
2434 work->next = work; /* protect against double add */
2435 /*
2436 * Who cares about NUMA placement when they're dying.
2437 *
2438 * NOTE: make sure not to dereference p->mm before this check,
2439 * exit_task_work() happens _after_ exit_mm() so we could be called
2440 * without p->mm even though we still had it when we enqueued this
2441 * work.
2442 */
2443 if (p->flags & PF_EXITING)
2444 return;
2445
Mel Gorman930aa172013-10-07 11:29:37 +01002446 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002447 mm->numa_next_scan = now +
2448 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002449 }
2450
2451 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002452 * Enforce maximal scan/migration frequency..
2453 */
2454 migrate = mm->numa_next_scan;
2455 if (time_before(now, migrate))
2456 return;
2457
Mel Gorman598f0ec2013-10-07 11:28:55 +01002458 if (p->numa_scan_period == 0) {
2459 p->numa_scan_period_max = task_scan_max(p);
2460 p->numa_scan_period = task_scan_min(p);
2461 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002462
Mel Gormanfb003b82012-11-15 09:01:14 +00002463 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002464 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2465 return;
2466
Mel Gormane14808b2012-11-19 10:59:15 +00002467 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002468 * Delay this task enough that another task of this mm will likely win
2469 * the next time around.
2470 */
2471 p->node_stamp += 2 * TICK_NSEC;
2472
Mel Gorman9f406042012-11-14 18:34:32 +00002473 start = mm->numa_scan_offset;
2474 pages = sysctl_numa_balancing_scan_size;
2475 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002476 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002477 if (!pages)
2478 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002479
Rik van Riel4620f8c2015-09-11 09:00:27 -04002480
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002481 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00002482 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002483 if (!vma) {
2484 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002485 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002486 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002487 }
Mel Gorman9f406042012-11-14 18:34:32 +00002488 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002489 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002490 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002491 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002492 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002493
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002494 /*
2495 * Shared library pages mapped by multiple processes are not
2496 * migrated as it is expected they are cache replicated. Avoid
2497 * hinting faults in read-only file-backed mappings or the vdso
2498 * as migrating the pages will be of marginal benefit.
2499 */
2500 if (!vma->vm_mm ||
2501 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2502 continue;
2503
Mel Gorman3c67f472013-12-18 17:08:40 -08002504 /*
2505 * Skip inaccessible VMAs to avoid any confusion between
2506 * PROT_NONE and NUMA hinting ptes
2507 */
2508 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2509 continue;
2510
Mel Gorman9f406042012-11-14 18:34:32 +00002511 do {
2512 start = max(start, vma->vm_start);
2513 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2514 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002515 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002516
2517 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002518 * Try to scan sysctl_numa_balancing_size worth of
2519 * hpages that have at least one present PTE that
2520 * is not already pte-numa. If the VMA contains
2521 * areas that are unused or already full of prot_numa
2522 * PTEs, scan up to virtpages, to skip through those
2523 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002524 */
2525 if (nr_pte_updates)
2526 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002527 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002528
Mel Gorman9f406042012-11-14 18:34:32 +00002529 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002530 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002531 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002532
2533 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002534 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002535 }
2536
Mel Gorman9f406042012-11-14 18:34:32 +00002537out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002538 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002539 * It is possible to reach the end of the VMA list but the last few
2540 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2541 * would find the !migratable VMA on the next scan but not reset the
2542 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002543 */
2544 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002545 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002546 else
2547 reset_ptenuma_scan(p);
2548 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002549
2550 /*
2551 * Make sure tasks use at least 32x as much time to run other code
2552 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2553 * Usually update_task_scan_period slows down scanning enough; on an
2554 * overloaded system we need to limit overhead on a per task basis.
2555 */
2556 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2557 u64 diff = p->se.sum_exec_runtime - runtime;
2558 p->node_stamp += 32 * diff;
2559 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002560}
2561
2562/*
2563 * Drive the periodic memory faults..
2564 */
2565void task_tick_numa(struct rq *rq, struct task_struct *curr)
2566{
2567 struct callback_head *work = &curr->numa_work;
2568 u64 period, now;
2569
2570 /*
2571 * We don't care about NUMA placement if we don't have memory.
2572 */
2573 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2574 return;
2575
2576 /*
2577 * Using runtime rather than walltime has the dual advantage that
2578 * we (mostly) drive the selection from busy threads and that the
2579 * task needs to have done some actual work before we bother with
2580 * NUMA placement.
2581 */
2582 now = curr->se.sum_exec_runtime;
2583 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2584
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002585 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02002586 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01002587 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002588 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002589
2590 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2591 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2592 task_work_add(curr, work, true);
2593 }
2594 }
2595}
2596#else
2597static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2598{
2599}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002600
2601static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2602{
2603}
2604
2605static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2606{
2607}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002608#endif /* CONFIG_NUMA_BALANCING */
2609
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002610static void
2611account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2612{
2613 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002614 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002615 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002616#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002617 if (entity_is_task(se)) {
2618 struct rq *rq = rq_of(cfs_rq);
2619
2620 account_numa_enqueue(rq, task_of(se));
2621 list_add(&se->group_node, &rq->cfs_tasks);
2622 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002623#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002624 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002625}
2626
2627static void
2628account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2629{
2630 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002631 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002632 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002633#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002634 if (entity_is_task(se)) {
2635 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302636 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002637 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002638#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002639 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002640}
2641
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002642#ifdef CONFIG_FAIR_GROUP_SCHED
2643# ifdef CONFIG_SMP
Paul Turner6d5ab292011-01-21 20:45:01 -08002644static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002645{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002646 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002647
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002648 /*
2649 * This really should be: cfs_rq->avg.load_avg, but instead we use
2650 * cfs_rq->load.weight, which is its upper bound. This helps ramp up
2651 * the shares for small weight interactive tasks.
2652 */
2653 load = scale_load_down(cfs_rq->load.weight);
2654
2655 tg_weight = atomic_long_read(&tg->load_avg);
2656
2657 /* Ensure tg_weight >= load */
2658 tg_weight -= cfs_rq->tg_load_avg_contrib;
2659 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002660
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002661 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002662 if (tg_weight)
2663 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002664
2665 if (shares < MIN_SHARES)
2666 shares = MIN_SHARES;
2667 if (shares > tg->shares)
2668 shares = tg->shares;
2669
2670 return shares;
2671}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002672# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002673static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002674{
2675 return tg->shares;
2676}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002677# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002678
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002679static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2680 unsigned long weight)
2681{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002682 if (se->on_rq) {
2683 /* commit outstanding execution time */
2684 if (cfs_rq->curr == se)
2685 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002686 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002687 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002688
2689 update_load_set(&se->load, weight);
2690
2691 if (se->on_rq)
2692 account_entity_enqueue(cfs_rq, se);
2693}
2694
Paul Turner82958362012-10-04 13:18:31 +02002695static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2696
Vincent Guittot6960f772016-12-21 16:50:26 +01002697static void update_cfs_shares(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002698{
Vincent Guittot6960f772016-12-21 16:50:26 +01002699 struct cfs_rq *cfs_rq = group_cfs_rq(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002700 struct task_group *tg;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002701 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002702
Vincent Guittot6960f772016-12-21 16:50:26 +01002703 if (!cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002704 return;
Vincent Guittot6960f772016-12-21 16:50:26 +01002705
2706 if (throttled_hierarchy(cfs_rq))
2707 return;
2708
2709 tg = cfs_rq->tg;
2710
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002711#ifndef CONFIG_SMP
2712 if (likely(se->load.weight == tg->shares))
2713 return;
2714#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002715 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002716
2717 reweight_entity(cfs_rq_of(se), se, shares);
2718}
Vincent Guittot6960f772016-12-21 16:50:26 +01002719
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002720#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot6960f772016-12-21 16:50:26 +01002721static inline void update_cfs_shares(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002722{
2723}
2724#endif /* CONFIG_FAIR_GROUP_SCHED */
2725
Alex Shi141965c2013-06-26 13:05:39 +08002726#ifdef CONFIG_SMP
Paul Turner5b51f2f2012-10-04 13:18:32 +02002727/* Precomputed fixed inverse multiplies for multiplication by y^n */
2728static const u32 runnable_avg_yN_inv[] = {
2729 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
2730 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
2731 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
2732 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
2733 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
2734 0x85aac367, 0x82cd8698,
2735};
2736
2737/*
2738 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
2739 * over-estimates when re-combining.
2740 */
2741static const u32 runnable_avg_yN_sum[] = {
2742 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
2743 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
2744 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
2745};
2746
2747/*
Yuyang Du7b20b912016-05-03 05:54:27 +08002748 * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to
2749 * lower integers. See Documentation/scheduler/sched-avg.txt how these
2750 * were generated:
2751 */
2752static const u32 __accumulated_sum_N32[] = {
2753 0, 23371, 35056, 40899, 43820, 45281,
2754 46011, 46376, 46559, 46650, 46696, 46719,
2755};
2756
2757/*
Paul Turner9d85f212012-10-04 13:18:29 +02002758 * Approximate:
2759 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2760 */
2761static __always_inline u64 decay_load(u64 val, u64 n)
2762{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002763 unsigned int local_n;
2764
2765 if (!n)
2766 return val;
2767 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
2768 return 0;
2769
2770 /* after bounds checking we can collapse to 32-bit */
2771 local_n = n;
2772
2773 /*
2774 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002775 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2776 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002777 *
2778 * To achieve constant time decay_load.
2779 */
2780 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2781 val >>= local_n / LOAD_AVG_PERIOD;
2782 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002783 }
2784
Yuyang Du9d89c252015-07-15 08:04:37 +08002785 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
2786 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002787}
2788
2789/*
2790 * For updates fully spanning n periods, the contribution to runnable
2791 * average will be: \Sum 1024*y^n
2792 *
2793 * We can compute this reasonably efficiently by combining:
2794 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2795 */
2796static u32 __compute_runnable_contrib(u64 n)
2797{
2798 u32 contrib = 0;
2799
2800 if (likely(n <= LOAD_AVG_PERIOD))
2801 return runnable_avg_yN_sum[n];
2802 else if (unlikely(n >= LOAD_AVG_MAX_N))
2803 return LOAD_AVG_MAX;
2804
Yuyang Du7b20b912016-05-03 05:54:27 +08002805 /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */
2806 contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD];
2807 n %= LOAD_AVG_PERIOD;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002808 contrib = decay_load(contrib, n);
2809 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002810}
2811
Peter Zijlstra54a21382015-09-07 15:05:42 +02002812#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002813
Paul Turner9d85f212012-10-04 13:18:29 +02002814/*
2815 * We can represent the historical contribution to runnable average as the
2816 * coefficients of a geometric series. To do this we sub-divide our runnable
2817 * history into segments of approximately 1ms (1024us); label the segment that
2818 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2819 *
2820 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2821 * p0 p1 p2
2822 * (now) (~1ms ago) (~2ms ago)
2823 *
2824 * Let u_i denote the fraction of p_i that the entity was runnable.
2825 *
2826 * We then designate the fractions u_i as our co-efficients, yielding the
2827 * following representation of historical load:
2828 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2829 *
2830 * We choose y based on the with of a reasonably scheduling period, fixing:
2831 * y^32 = 0.5
2832 *
2833 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2834 * approximately half as much as the contribution to load within the last ms
2835 * (u_0).
2836 *
2837 * When a period "rolls over" and we have new u_0`, multiplying the previous
2838 * sum again by y is sufficient to update:
2839 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2840 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2841 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002842static __always_inline int
2843__update_load_avg(u64 now, int cpu, struct sched_avg *sa,
Yuyang Du13962232015-07-15 08:04:41 +08002844 unsigned long weight, int running, struct cfs_rq *cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002845{
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002846 u64 delta, scaled_delta, periods;
Yuyang Du9d89c252015-07-15 08:04:37 +08002847 u32 contrib;
Peter Zijlstra6115c792015-09-07 15:09:15 +02002848 unsigned int delta_w, scaled_delta_w, decayed = 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002849 unsigned long scale_freq, scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002850
Yuyang Du9d89c252015-07-15 08:04:37 +08002851 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02002852 /*
2853 * This should only happen when time goes backwards, which it
2854 * unfortunately does during sched clock init when we swap over to TSC.
2855 */
2856 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002857 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002858 return 0;
2859 }
2860
2861 /*
2862 * Use 1024ns as the unit of measurement since it's a reasonable
2863 * approximation of 1us and fast to compute.
2864 */
2865 delta >>= 10;
2866 if (!delta)
2867 return 0;
Yuyang Du9d89c252015-07-15 08:04:37 +08002868 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002869
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002870 scale_freq = arch_scale_freq_capacity(NULL, cpu);
2871 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
Juri Lelli0a942002015-11-09 12:06:24 +00002872 trace_sched_contrib_scale_f(cpu, scale_freq, scale_cpu);
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002873
Paul Turner9d85f212012-10-04 13:18:29 +02002874 /* delta_w is the amount already accumulated against our next period */
Yuyang Du9d89c252015-07-15 08:04:37 +08002875 delta_w = sa->period_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002876 if (delta + delta_w >= 1024) {
Paul Turner9d85f212012-10-04 13:18:29 +02002877 decayed = 1;
2878
Yuyang Du9d89c252015-07-15 08:04:37 +08002879 /* how much left for next period will start over, we don't know yet */
2880 sa->period_contrib = 0;
2881
Paul Turner9d85f212012-10-04 13:18:29 +02002882 /*
2883 * Now that we know we're crossing a period boundary, figure
2884 * out how much from delta we need to complete the current
2885 * period and accrue it.
2886 */
2887 delta_w = 1024 - delta_w;
Peter Zijlstra54a21382015-09-07 15:05:42 +02002888 scaled_delta_w = cap_scale(delta_w, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002889 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002890 sa->load_sum += weight * scaled_delta_w;
2891 if (cfs_rq) {
2892 cfs_rq->runnable_load_sum +=
2893 weight * scaled_delta_w;
2894 }
Yuyang Du13962232015-07-15 08:04:41 +08002895 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002896 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002897 sa->util_sum += scaled_delta_w * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002898
Paul Turner5b51f2f2012-10-04 13:18:32 +02002899 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002900
Paul Turner5b51f2f2012-10-04 13:18:32 +02002901 /* Figure out how many additional periods this update spans */
2902 periods = delta / 1024;
2903 delta %= 1024;
2904
Yuyang Du9d89c252015-07-15 08:04:37 +08002905 sa->load_sum = decay_load(sa->load_sum, periods + 1);
Yuyang Du13962232015-07-15 08:04:41 +08002906 if (cfs_rq) {
2907 cfs_rq->runnable_load_sum =
2908 decay_load(cfs_rq->runnable_load_sum, periods + 1);
2909 }
Yuyang Du9d89c252015-07-15 08:04:37 +08002910 sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1);
Paul Turner5b51f2f2012-10-04 13:18:32 +02002911
2912 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
Yuyang Du9d89c252015-07-15 08:04:37 +08002913 contrib = __compute_runnable_contrib(periods);
Peter Zijlstra54a21382015-09-07 15:05:42 +02002914 contrib = cap_scale(contrib, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002915 if (weight) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002916 sa->load_sum += weight * contrib;
Yuyang Du13962232015-07-15 08:04:41 +08002917 if (cfs_rq)
2918 cfs_rq->runnable_load_sum += weight * contrib;
2919 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002920 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002921 sa->util_sum += contrib * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002922 }
2923
2924 /* Remainder of delta accrued against u_0` */
Peter Zijlstra54a21382015-09-07 15:05:42 +02002925 scaled_delta = cap_scale(delta, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002926 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002927 sa->load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002928 if (cfs_rq)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002929 cfs_rq->runnable_load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002930 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002931 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002932 sa->util_sum += scaled_delta * scale_cpu;
Yuyang Du9d89c252015-07-15 08:04:37 +08002933
2934 sa->period_contrib += delta;
2935
2936 if (decayed) {
2937 sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX);
Yuyang Du13962232015-07-15 08:04:41 +08002938 if (cfs_rq) {
2939 cfs_rq->runnable_load_avg =
2940 div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX);
2941 }
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002942 sa->util_avg = sa->util_sum / LOAD_AVG_MAX;
Yuyang Du9d89c252015-07-15 08:04:37 +08002943 }
Paul Turner9d85f212012-10-04 13:18:29 +02002944
2945 return decayed;
2946}
2947
Vincent Guittot96956e22016-11-08 10:53:44 +01002948/*
2949 * Signed add and clamp on underflow.
2950 *
2951 * Explicitly do a load-store to ensure the intermediate value never hits
2952 * memory. This allows lockless observations without ever seeing the negative
2953 * values.
2954 */
2955#define add_positive(_ptr, _val) do { \
2956 typeof(_ptr) ptr = (_ptr); \
2957 typeof(_val) val = (_val); \
2958 typeof(*ptr) res, var = READ_ONCE(*ptr); \
2959 \
2960 res = var + val; \
2961 \
2962 if (val < 0 && res > var) \
2963 res = 0; \
2964 \
2965 WRITE_ONCE(*ptr, res); \
2966} while (0)
2967
Paul Turnerc566e8e2012-10-04 13:18:30 +02002968#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02002969/**
2970 * update_tg_load_avg - update the tg's load avg
2971 * @cfs_rq: the cfs_rq whose avg changed
2972 * @force: update regardless of how small the difference
2973 *
2974 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
2975 * However, because tg->load_avg is a global value there are performance
2976 * considerations.
2977 *
2978 * In order to avoid having to look at the other cfs_rq's, we use a
2979 * differential update where we store the last value we propagated. This in
2980 * turn allows skipping updates if the differential is 'small'.
2981 *
2982 * Updating tg's load_avg is necessary before update_cfs_share() (which is
2983 * done) and effective_load() (which is not done because it is too costly).
Paul Turnerbb17f652012-10-04 13:18:31 +02002984 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002985static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02002986{
Yuyang Du9d89c252015-07-15 08:04:37 +08002987 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02002988
Waiman Longaa0b7ae2015-12-02 13:41:50 -05002989 /*
2990 * No need to update load_avg for root_task_group as it is not used.
2991 */
2992 if (cfs_rq->tg == &root_task_group)
2993 return;
2994
Yuyang Du9d89c252015-07-15 08:04:37 +08002995 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
2996 atomic_long_add(delta, &cfs_rq->tg->load_avg);
2997 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02002998 }
Paul Turner8165e142012-10-04 13:18:31 +02002999}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00003000
Byungchul Parkad936d82015-10-24 01:16:19 +09003001/*
3002 * Called within set_task_rq() right before setting a task's cpu. The
3003 * caller only guarantees p->pi_lock is held; no other assumptions,
3004 * including the state of rq->lock, should be made.
3005 */
3006void set_task_rq_fair(struct sched_entity *se,
3007 struct cfs_rq *prev, struct cfs_rq *next)
3008{
3009 if (!sched_feat(ATTACH_AGE_LOAD))
3010 return;
3011
3012 /*
3013 * We are supposed to update the task to "current" time, then its up to
3014 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
3015 * getting what current time is, so simply throw away the out-of-date
3016 * time. This will result in the wakee task is less decayed, but giving
3017 * the wakee more load sounds not bad.
3018 */
3019 if (se->avg.last_update_time && prev) {
3020 u64 p_last_update_time;
3021 u64 n_last_update_time;
3022
3023#ifndef CONFIG_64BIT
3024 u64 p_last_update_time_copy;
3025 u64 n_last_update_time_copy;
3026
3027 do {
3028 p_last_update_time_copy = prev->load_last_update_time_copy;
3029 n_last_update_time_copy = next->load_last_update_time_copy;
3030
3031 smp_rmb();
3032
3033 p_last_update_time = prev->avg.last_update_time;
3034 n_last_update_time = next->avg.last_update_time;
3035
3036 } while (p_last_update_time != p_last_update_time_copy ||
3037 n_last_update_time != n_last_update_time_copy);
3038#else
3039 p_last_update_time = prev->avg.last_update_time;
3040 n_last_update_time = next->avg.last_update_time;
3041#endif
3042 __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)),
3043 &se->avg, 0, 0, NULL);
3044 se->avg.last_update_time = n_last_update_time;
3045 }
3046}
Vincent Guittot96956e22016-11-08 10:53:44 +01003047
3048/* Take into account change of utilization of a child task group */
3049static inline void
3050update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se)
3051{
3052 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3053 long delta = gcfs_rq->avg.util_avg - se->avg.util_avg;
3054
3055 /* Nothing to update */
3056 if (!delta)
3057 return;
3058
3059 /* Set new sched_entity's utilization */
3060 se->avg.util_avg = gcfs_rq->avg.util_avg;
3061 se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX;
3062
3063 /* Update parent cfs_rq utilization */
3064 add_positive(&cfs_rq->avg.util_avg, delta);
3065 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX;
3066}
3067
3068/* Take into account change of load of a child task group */
3069static inline void
3070update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se)
3071{
3072 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3073 long delta, load = gcfs_rq->avg.load_avg;
3074
3075 /*
3076 * If the load of group cfs_rq is null, the load of the
3077 * sched_entity will also be null so we can skip the formula
3078 */
3079 if (load) {
3080 long tg_load;
3081
3082 /* Get tg's load and ensure tg_load > 0 */
3083 tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1;
3084
3085 /* Ensure tg_load >= load and updated with current load*/
3086 tg_load -= gcfs_rq->tg_load_avg_contrib;
3087 tg_load += load;
3088
3089 /*
3090 * We need to compute a correction term in the case that the
3091 * task group is consuming more CPU than a task of equal
3092 * weight. A task with a weight equals to tg->shares will have
3093 * a load less or equal to scale_load_down(tg->shares).
3094 * Similarly, the sched_entities that represent the task group
3095 * at parent level, can't have a load higher than
3096 * scale_load_down(tg->shares). And the Sum of sched_entities'
3097 * load must be <= scale_load_down(tg->shares).
3098 */
3099 if (tg_load > scale_load_down(gcfs_rq->tg->shares)) {
3100 /* scale gcfs_rq's load into tg's shares*/
3101 load *= scale_load_down(gcfs_rq->tg->shares);
3102 load /= tg_load;
3103 }
3104 }
3105
3106 delta = load - se->avg.load_avg;
3107
3108 /* Nothing to update */
3109 if (!delta)
3110 return;
3111
3112 /* Set new sched_entity's load */
3113 se->avg.load_avg = load;
3114 se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX;
3115
3116 /* Update parent cfs_rq load */
3117 add_positive(&cfs_rq->avg.load_avg, delta);
3118 cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX;
3119
3120 /*
3121 * If the sched_entity is already enqueued, we also have to update the
3122 * runnable load avg.
3123 */
3124 if (se->on_rq) {
3125 /* Update parent cfs_rq runnable_load_avg */
3126 add_positive(&cfs_rq->runnable_load_avg, delta);
3127 cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX;
3128 }
3129}
3130
3131static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq)
3132{
3133 cfs_rq->propagate_avg = 1;
3134}
3135
3136static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se)
3137{
3138 struct cfs_rq *cfs_rq = group_cfs_rq(se);
3139
3140 if (!cfs_rq->propagate_avg)
3141 return 0;
3142
3143 cfs_rq->propagate_avg = 0;
3144 return 1;
3145}
3146
3147/* Update task and its cfs_rq load average */
3148static inline int propagate_entity_load_avg(struct sched_entity *se)
3149{
3150 struct cfs_rq *cfs_rq;
3151
3152 if (entity_is_task(se))
3153 return 0;
3154
3155 if (!test_and_clear_tg_cfs_propagate(se))
3156 return 0;
3157
3158 cfs_rq = cfs_rq_of(se);
3159
3160 set_tg_cfs_propagate(cfs_rq);
3161
3162 update_tg_cfs_util(cfs_rq, se);
3163 update_tg_cfs_load(cfs_rq, se);
3164
3165 return 1;
3166}
3167
Vincent Guittot0b4a2f12017-03-17 14:47:22 +01003168/*
3169 * Check if we need to update the load and the utilization of a blocked
3170 * group_entity:
3171 */
3172static inline bool skip_blocked_update(struct sched_entity *se)
3173{
3174 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3175
3176 /*
3177 * If sched_entity still have not zero load or utilization, we have to
3178 * decay it:
3179 */
3180 if (se->avg.load_avg || se->avg.util_avg)
3181 return false;
3182
3183 /*
3184 * If there is a pending propagation, we have to update the load and
3185 * the utilization of the sched_entity:
3186 */
3187 if (gcfs_rq->propagate_avg)
3188 return false;
3189
3190 /*
3191 * Otherwise, the load and the utilization of the sched_entity is
3192 * already zero and there is no pending propagation, so it will be a
3193 * waste of time to try to decay it:
3194 */
3195 return true;
3196}
3197
Peter Zijlstra6e831252014-02-11 16:11:48 +01003198#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot96956e22016-11-08 10:53:44 +01003199
Yuyang Du9d89c252015-07-15 08:04:37 +08003200static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Vincent Guittot96956e22016-11-08 10:53:44 +01003201
3202static inline int propagate_entity_load_avg(struct sched_entity *se)
3203{
3204 return 0;
3205}
3206
3207static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
3208
Peter Zijlstra6e831252014-02-11 16:11:48 +01003209#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02003210
Steve Mucklea2c6c912016-03-24 15:26:07 -07003211static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
Yuyang Du9d89c252015-07-15 08:04:37 +08003212{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003213 if (&this_rq()->cfs == cfs_rq) {
Steve Muckle21e96f82016-03-21 17:21:07 -07003214 /*
3215 * There are a few boundary cases this might miss but it should
3216 * get called often enough that that should (hopefully) not be
3217 * a real problem -- added to that it only calls on the local
3218 * CPU, so if we enqueue remotely we'll miss an update, but
3219 * the next tick/schedule should update.
3220 *
3221 * It will not get called when we go idle, because the idle
3222 * thread is a different class (!fair), nor will the utilization
3223 * number include things like RT tasks.
3224 *
3225 * As is, the util number is not freq-invariant (we'd have to
3226 * implement arch_scale_freq_capacity() for that).
3227 *
3228 * See cpu_util().
3229 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003230 cpufreq_update_util(rq_of(cfs_rq), 0);
Steve Muckle21e96f82016-03-21 17:21:07 -07003231 }
Steve Mucklea2c6c912016-03-24 15:26:07 -07003232}
3233
Peter Zijlstra89741892016-06-16 10:50:40 +02003234/*
3235 * Unsigned subtract and clamp on underflow.
3236 *
3237 * Explicitly do a load-store to ensure the intermediate value never hits
3238 * memory. This allows lockless observations without ever seeing the negative
3239 * values.
3240 */
3241#define sub_positive(_ptr, _val) do { \
3242 typeof(_ptr) ptr = (_ptr); \
3243 typeof(*ptr) val = (_val); \
3244 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3245 res = var - val; \
3246 if (res > var) \
3247 res = 0; \
3248 WRITE_ONCE(*ptr, res); \
3249} while (0)
3250
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003251/**
3252 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3253 * @now: current time, as per cfs_rq_clock_task()
3254 * @cfs_rq: cfs_rq to update
3255 * @update_freq: should we call cfs_rq_util_change() or will the call do so
3256 *
3257 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3258 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3259 * post_init_entity_util_avg().
3260 *
3261 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3262 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003263 * Returns true if the load decayed or we removed load.
3264 *
3265 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3266 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003267 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003268static inline int
3269update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3270{
3271 struct sched_avg *sa = &cfs_rq->avg;
3272 int decayed, removed_load = 0, removed_util = 0;
3273
3274 if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3275 s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003276 sub_positive(&sa->load_avg, r);
3277 sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003278 removed_load = 1;
Vincent Guittot3a34bf52016-11-08 10:53:46 +01003279 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003280 }
3281
3282 if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3283 long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003284 sub_positive(&sa->util_avg, r);
3285 sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003286 removed_util = 1;
Vincent Guittot3a34bf52016-11-08 10:53:46 +01003287 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003288 }
3289
3290 decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
3291 scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq);
3292
3293#ifndef CONFIG_64BIT
3294 smp_wmb();
3295 cfs_rq->load_last_update_time_copy = sa->last_update_time;
3296#endif
3297
3298 if (update_freq && (decayed || removed_util))
3299 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07003300
Brendan Jackmanb2246472017-01-10 11:31:01 +00003301 /* Trace CPU load, unless cfs_rq belongs to a non-root task_group */
3302 if (cfs_rq == &rq_of(cfs_rq)->cfs)
3303 trace_sched_load_avg_cpu(cpu_of(rq_of(cfs_rq)), cfs_rq);
Brendan Jackman0f493a72017-01-09 17:20:11 +00003304
Steve Muckle41e0d372016-03-21 17:21:08 -07003305 return decayed || removed_load;
Yuyang Du9d89c252015-07-15 08:04:37 +08003306}
3307
Vincent Guittot96956e22016-11-08 10:53:44 +01003308/*
3309 * Optional action to be done while updating the load average
3310 */
3311#define UPDATE_TG 0x1
3312#define SKIP_AGE_LOAD 0x2
3313
Yuyang Du9d89c252015-07-15 08:04:37 +08003314/* Update task and its cfs_rq load average */
Vincent Guittot96956e22016-11-08 10:53:44 +01003315static inline void update_load_avg(struct sched_entity *se, int flags)
Paul Turner9d85f212012-10-04 13:18:29 +02003316{
Paul Turner2dac7542012-10-04 13:18:30 +02003317 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003318 u64 now = cfs_rq_clock_task(cfs_rq);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +01003319 struct rq *rq = rq_of(cfs_rq);
3320 int cpu = cpu_of(rq);
Vincent Guittot96956e22016-11-08 10:53:44 +01003321 int decayed;
Chris Redpath8d40f582017-02-08 14:25:35 +00003322 void *ptr = NULL;
Paul Turner2dac7542012-10-04 13:18:30 +02003323
Paul Turnerf1b17282012-10-04 13:18:31 +02003324 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08003325 * Track task load average for carrying it to new CPU after migrated, and
3326 * track group sched_entity load average for task_h_load calc in migration
Paul Turnerf1b17282012-10-04 13:18:31 +02003327 */
Vincent Guittot96956e22016-11-08 10:53:44 +01003328 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) {
3329 __update_load_avg(now, cpu, &se->avg,
Byungchul Parka05e8c52015-08-20 20:21:56 +09003330 se->on_rq * scale_load_down(se->load.weight),
3331 cfs_rq->curr == se, NULL);
Vincent Guittot96956e22016-11-08 10:53:44 +01003332 }
Paul Turnerf1b17282012-10-04 13:18:31 +02003333
Vincent Guittot96956e22016-11-08 10:53:44 +01003334 decayed = update_cfs_rq_load_avg(now, cfs_rq, true);
3335 decayed |= propagate_entity_load_avg(se);
3336
3337 if (decayed && (flags & UPDATE_TG))
Yuyang Du9d89c252015-07-15 08:04:37 +08003338 update_tg_load_avg(cfs_rq, 0);
Juri Lellia4b0c3a2015-11-09 12:07:27 +00003339
Chris Redpath8d40f582017-02-08 14:25:35 +00003340 if (entity_is_task(se)) {
3341#ifdef CONFIG_SCHED_WALT
3342 ptr = (void *)&(task_of(se)->ravg);
3343#endif
3344 trace_sched_load_avg_task(task_of(se), &se->avg, ptr);
3345 }
Yuyang Du9d89c252015-07-15 08:04:37 +08003346}
Paul Turner2dac7542012-10-04 13:18:30 +02003347
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003348/**
3349 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3350 * @cfs_rq: cfs_rq to attach to
3351 * @se: sched_entity to attach
3352 *
3353 * Must call update_cfs_rq_load_avg() before this, since we rely on
3354 * cfs_rq->avg.last_update_time being current.
3355 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003356static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3357{
3358 se->avg.last_update_time = cfs_rq->avg.last_update_time;
3359 cfs_rq->avg.load_avg += se->avg.load_avg;
3360 cfs_rq->avg.load_sum += se->avg.load_sum;
3361 cfs_rq->avg.util_avg += se->avg.util_avg;
3362 cfs_rq->avg.util_sum += se->avg.util_sum;
Vincent Guittot96956e22016-11-08 10:53:44 +01003363 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003364
3365 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003366}
3367
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003368/**
3369 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3370 * @cfs_rq: cfs_rq to detach from
3371 * @se: sched_entity to detach
3372 *
3373 * Must call update_cfs_rq_load_avg() before this, since we rely on
3374 * cfs_rq->avg.last_update_time being current.
3375 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003376static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3377{
Byungchul Parka05e8c52015-08-20 20:21:56 +09003378
Peter Zijlstra89741892016-06-16 10:50:40 +02003379 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
3380 sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum);
3381 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
3382 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Vincent Guittot96956e22016-11-08 10:53:44 +01003383 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003384
3385 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003386}
3387
Yuyang Du9d89c252015-07-15 08:04:37 +08003388/* Add the load generated by se into cfs_rq's load average */
3389static inline void
3390enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3391{
3392 struct sched_avg *sa = &se->avg;
Yuyang Du9d89c252015-07-15 08:04:37 +08003393
Yuyang Du13962232015-07-15 08:04:41 +08003394 cfs_rq->runnable_load_avg += sa->load_avg;
3395 cfs_rq->runnable_load_sum += sa->load_sum;
3396
Vincent Guittot96956e22016-11-08 10:53:44 +01003397 if (!sa->last_update_time) {
Byungchul Parka05e8c52015-08-20 20:21:56 +09003398 attach_entity_load_avg(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003399 update_tg_load_avg(cfs_rq, 0);
Vincent Guittot96956e22016-11-08 10:53:44 +01003400 }
Paul Turner9ee474f2012-10-04 13:18:30 +02003401}
3402
Yuyang Du13962232015-07-15 08:04:41 +08003403/* Remove the runnable load generated by se from cfs_rq's runnable load average */
3404static inline void
3405dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3406{
Yuyang Du13962232015-07-15 08:04:41 +08003407 cfs_rq->runnable_load_avg =
3408 max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0);
3409 cfs_rq->runnable_load_sum =
Byungchul Parka05e8c52015-08-20 20:21:56 +09003410 max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0);
Yuyang Du13962232015-07-15 08:04:41 +08003411}
3412
Yuyang Du0905f042015-12-17 07:34:27 +08003413#ifndef CONFIG_64BIT
3414static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3415{
3416 u64 last_update_time_copy;
3417 u64 last_update_time;
3418
3419 do {
3420 last_update_time_copy = cfs_rq->load_last_update_time_copy;
3421 smp_rmb();
3422 last_update_time = cfs_rq->avg.last_update_time;
3423 } while (last_update_time != last_update_time_copy);
3424
3425 return last_update_time;
3426}
3427#else
3428static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3429{
3430 return cfs_rq->avg.last_update_time;
3431}
3432#endif
3433
Paul Turner9ee474f2012-10-04 13:18:30 +02003434/*
Morten Rasmussen355772432016-10-14 14:41:07 +01003435 * Synchronize entity load avg of dequeued entity without locking
3436 * the previous rq.
3437 */
3438void sync_entity_load_avg(struct sched_entity *se)
3439{
3440 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3441 u64 last_update_time;
3442
3443 last_update_time = cfs_rq_last_update_time(cfs_rq);
3444 __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL);
3445}
3446
3447/*
Yuyang Du9d89c252015-07-15 08:04:37 +08003448 * Task first catches up with cfs_rq, and then subtract
3449 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02003450 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003451void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02003452{
Yuyang Du9d89c252015-07-15 08:04:37 +08003453 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02003454
Yuyang Du0905f042015-12-17 07:34:27 +08003455 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003456 * tasks cannot exit without having gone through wake_up_new_task() ->
3457 * post_init_entity_util_avg() which will have added things to the
3458 * cfs_rq, so we can remove unconditionally.
3459 *
3460 * Similarly for groups, they will have passed through
3461 * post_init_entity_util_avg() before unregister_sched_fair_group()
3462 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08003463 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003464
Morten Rasmussen355772432016-10-14 14:41:07 +01003465 sync_entity_load_avg(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003466 atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
3467 atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
Paul Turner2dac7542012-10-04 13:18:30 +02003468}
Vincent Guittot642dbc32013-04-18 18:34:26 +02003469
Yuyang Du7ea241a2015-07-15 08:04:42 +08003470static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
3471{
3472 return cfs_rq->runnable_load_avg;
3473}
3474
3475static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
3476{
3477 return cfs_rq->avg.load_avg;
3478}
3479
Peter Zijlstra6e831252014-02-11 16:11:48 +01003480static int idle_balance(struct rq *this_rq);
3481
Peter Zijlstra38033c32014-01-23 20:32:21 +01003482#else /* CONFIG_SMP */
3483
Peter Zijlstra01011472016-06-17 11:20:46 +02003484static inline int
3485update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3486{
3487 return 0;
3488}
3489
Vincent Guittot96956e22016-11-08 10:53:44 +01003490#define UPDATE_TG 0x0
3491#define SKIP_AGE_LOAD 0x0
3492
3493static inline void update_load_avg(struct sched_entity *se, int not_used1)
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003494{
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003495 cpufreq_update_util(rq_of(cfs_rq_of(se)), 0);
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003496}
3497
Yuyang Du9d89c252015-07-15 08:04:37 +08003498static inline void
3499enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du13962232015-07-15 08:04:41 +08003500static inline void
3501dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du9d89c252015-07-15 08:04:37 +08003502static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003503
Byungchul Parka05e8c52015-08-20 20:21:56 +09003504static inline void
3505attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3506static inline void
3507detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3508
Peter Zijlstra6e831252014-02-11 16:11:48 +01003509static inline int idle_balance(struct rq *rq)
3510{
3511 return 0;
3512}
3513
Peter Zijlstra38033c32014-01-23 20:32:21 +01003514#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02003515
Peter Zijlstraddc97292007-10-15 17:00:10 +02003516static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
3517{
3518#ifdef CONFIG_SCHED_DEBUG
3519 s64 d = se->vruntime - cfs_rq->min_vruntime;
3520
3521 if (d < 0)
3522 d = -d;
3523
3524 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05003525 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02003526#endif
3527}
3528
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003529static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003530place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
3531{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02003532 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003533
Peter Zijlstra2cb86002007-11-09 22:39:37 +01003534 /*
3535 * The 'current' period is already promised to the current tasks,
3536 * however the extra weight of the new task will slow them down a
3537 * little, place the new task so that it fits in the slot that
3538 * stays open at the end.
3539 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003540 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02003541 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003542
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003543 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01003544 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003545 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02003546
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003547 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003548 * Halve their sleep time's effect, to allow
3549 * for a gentler effect of sleepers:
3550 */
3551 if (sched_feat(GENTLE_FAIR_SLEEPERS))
3552 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02003553
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003554 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003555 }
3556
Mike Galbraithb5d9d732009-09-08 11:12:28 +02003557 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05303558 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003559}
3560
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003561static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
3562
Mel Gormancb251762016-02-05 09:08:36 +00003563static inline void check_schedstat_required(void)
3564{
3565#ifdef CONFIG_SCHEDSTATS
3566 if (schedstat_enabled())
3567 return;
3568
3569 /* Force schedstat enabled if a dependent tracepoint is active */
3570 if (trace_sched_stat_wait_enabled() ||
3571 trace_sched_stat_sleep_enabled() ||
3572 trace_sched_stat_iowait_enabled() ||
3573 trace_sched_stat_blocked_enabled() ||
3574 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05003575 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00003576 "stat_blocked and stat_runtime require the "
3577 "kernel parameter schedstats=enabled or "
3578 "kernel.sched_schedstats=1\n");
3579 }
3580#endif
3581}
3582
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003583
3584/*
3585 * MIGRATION
3586 *
3587 * dequeue
3588 * update_curr()
3589 * update_min_vruntime()
3590 * vruntime -= min_vruntime
3591 *
3592 * enqueue
3593 * update_curr()
3594 * update_min_vruntime()
3595 * vruntime += min_vruntime
3596 *
3597 * this way the vruntime transition between RQs is done when both
3598 * min_vruntime are up-to-date.
3599 *
3600 * WAKEUP (remote)
3601 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02003602 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003603 * vruntime -= min_vruntime
3604 *
3605 * enqueue
3606 * update_curr()
3607 * update_min_vruntime()
3608 * vruntime += min_vruntime
3609 *
3610 * this way we don't have the most up-to-date min_vruntime on the originating
3611 * CPU and an up-to-date min_vruntime on the destination CPU.
3612 */
3613
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003614static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003615enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003616{
Peter Zijlstra2f950352016-05-11 19:27:56 +02003617 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
3618 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01003619
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003620 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02003621 * If we're the current task, we must renormalise before calling
3622 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003623 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02003624 if (renorm && curr)
3625 se->vruntime += cfs_rq->min_vruntime;
3626
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003627 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003628
3629 /*
3630 * Otherwise, renormalise after, such that we're placed at the current
3631 * moment in time, instead of some random moment in the past. Being
3632 * placed in the past could significantly boost this task to the
3633 * fairness detriment of existing tasks.
3634 */
3635 if (renorm && !curr)
3636 se->vruntime += cfs_rq->min_vruntime;
3637
Vincent Guittot6960f772016-12-21 16:50:26 +01003638 /*
3639 * When enqueuing a sched_entity, we must:
3640 * - Update loads to have both entity and cfs_rq synced with now.
3641 * - Add its load to cfs_rq->runnable_avg
3642 * - For group_entity, update its weight to reflect the new share of
3643 * its group cfs_rq
3644 * - Add its new weight to cfs_rq->load.weight
3645 */
Vincent Guittot96956e22016-11-08 10:53:44 +01003646 update_load_avg(se, UPDATE_TG);
Yuyang Du9d89c252015-07-15 08:04:37 +08003647 enqueue_entity_load_avg(cfs_rq, se);
Vincent Guittot6960f772016-12-21 16:50:26 +01003648 update_cfs_shares(se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003649 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003650
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05003651 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003652 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003653
Mel Gormancb251762016-02-05 09:08:36 +00003654 check_schedstat_required();
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003655 update_stats_enqueue(cfs_rq, se, flags);
3656 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003657 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003658 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003659 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003660
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003661 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003662 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003663 check_enqueue_throttle(cfs_rq);
3664 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003665}
3666
Rik van Riel2c13c9192011-02-01 09:48:37 -05003667static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01003668{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003669 for_each_sched_entity(se) {
3670 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003671 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003672 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003673
3674 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003675 }
3676}
Peter Zijlstra2002c692008-11-11 11:52:33 +01003677
Rik van Riel2c13c9192011-02-01 09:48:37 -05003678static void __clear_buddies_next(struct sched_entity *se)
3679{
3680 for_each_sched_entity(se) {
3681 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003682 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003683 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003684
3685 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003686 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01003687}
3688
Rik van Rielac53db52011-02-01 09:51:03 -05003689static void __clear_buddies_skip(struct sched_entity *se)
3690{
3691 for_each_sched_entity(se) {
3692 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003693 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05003694 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003695
3696 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05003697 }
3698}
3699
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003700static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
3701{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003702 if (cfs_rq->last == se)
3703 __clear_buddies_last(se);
3704
3705 if (cfs_rq->next == se)
3706 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05003707
3708 if (cfs_rq->skip == se)
3709 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003710}
3711
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003712static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07003713
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003714static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003715dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003716{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003717 /*
3718 * Update run-time statistics of the 'current'.
3719 */
3720 update_curr(cfs_rq);
Vincent Guittot6960f772016-12-21 16:50:26 +01003721
3722 /*
3723 * When dequeuing a sched_entity, we must:
3724 * - Update loads to have both entity and cfs_rq synced with now.
3725 * - Substract its load from the cfs_rq->runnable_avg.
3726 * - Substract its previous weight from cfs_rq->load.weight.
3727 * - For group entity, update its weight to reflect the new share
3728 * of its group cfs_rq.
3729 */
Vincent Guittot96956e22016-11-08 10:53:44 +01003730 update_load_avg(se, UPDATE_TG);
Yuyang Du13962232015-07-15 08:04:41 +08003731 dequeue_entity_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003732
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003733 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02003734
Peter Zijlstra2002c692008-11-11 11:52:33 +01003735 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003736
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003737 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003738 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003739 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003740 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003741
3742 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003743 * Normalize after update_curr(); which will also have moved
3744 * min_vruntime if @se is the one holding it back. But before doing
3745 * update_min_vruntime() again, which will discount @se's position and
3746 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003747 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003748 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003749 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07003750
Paul Turnerd8b49862011-07-21 09:43:41 -07003751 /* return excess runtime on last dequeue */
3752 return_cfs_rq_runtime(cfs_rq);
3753
Vincent Guittot6960f772016-12-21 16:50:26 +01003754 update_cfs_shares(se);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003755
3756 /*
3757 * Now advance min_vruntime if @se was the entity holding it back,
3758 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
3759 * put back on, and if we advance min_vruntime, we'll be placed back
3760 * further than we started -- ie. we'll be penalized.
3761 */
3762 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
3763 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003764}
3765
3766/*
3767 * Preempt the current task with a newly woken task if needed:
3768 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02003769static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003770check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003771{
Peter Zijlstra11697832007-09-05 14:32:49 +02003772 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003773 struct sched_entity *se;
3774 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02003775
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02003776 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02003777 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003778 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003779 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003780 /*
3781 * The current task ran long enough, ensure it doesn't get
3782 * re-elected due to buddy favours.
3783 */
3784 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003785 return;
3786 }
3787
3788 /*
3789 * Ensure that a task that missed wakeup preemption by a
3790 * narrow margin doesn't have to wait for a full slice.
3791 * This also mitigates buddy induced latencies under load.
3792 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02003793 if (delta_exec < sysctl_sched_min_granularity)
3794 return;
3795
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003796 se = __pick_first_entity(cfs_rq);
3797 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02003798
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003799 if (delta < 0)
3800 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01003801
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003802 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04003803 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003804}
3805
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003806static void
Ingo Molnar8494f412007-08-09 11:16:48 +02003807set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003808{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003809 /* 'current' is not kept within the tree. */
3810 if (se->on_rq) {
3811 /*
3812 * Any task has to be enqueued before it get to execute on
3813 * a CPU. So account for the time it spent waiting on the
3814 * runqueue.
3815 */
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003816 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003817 __dequeue_entity(cfs_rq, se);
Vincent Guittot96956e22016-11-08 10:53:44 +01003818 update_load_avg(se, UPDATE_TG);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003819 }
3820
Ingo Molnar79303e92007-08-09 11:16:47 +02003821 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02003822 cfs_rq->curr = se;
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003823
Ingo Molnareba1ed42007-10-15 17:00:02 +02003824 /*
3825 * Track our maximum slice length, if the CPU's load is at
3826 * least twice that of our own weight (i.e. dont track it
3827 * when there are only lesser-weight tasks around):
3828 */
Mel Gormancb251762016-02-05 09:08:36 +00003829 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003830 schedstat_set(se->statistics.slice_max,
3831 max((u64)schedstat_val(se->statistics.slice_max),
3832 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02003833 }
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003834
Peter Zijlstra4a55b452007-09-05 14:32:49 +02003835 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003836}
3837
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02003838static int
3839wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
3840
Rik van Rielac53db52011-02-01 09:51:03 -05003841/*
3842 * Pick the next process, keeping these things in mind, in this order:
3843 * 1) keep things fair between processes/task groups
3844 * 2) pick the "next" process, since someone really wants that to run
3845 * 3) pick the "last" process, for cache locality
3846 * 4) do not run the "skip" process, if something else is available
3847 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01003848static struct sched_entity *
3849pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003850{
Peter Zijlstra678d5712012-02-11 06:05:00 +01003851 struct sched_entity *left = __pick_first_entity(cfs_rq);
3852 struct sched_entity *se;
3853
3854 /*
3855 * If curr is set we have to see if its left of the leftmost entity
3856 * still in the tree, provided there was anything in the tree at all.
3857 */
3858 if (!left || (curr && entity_before(curr, left)))
3859 left = curr;
3860
3861 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003862
Rik van Rielac53db52011-02-01 09:51:03 -05003863 /*
3864 * Avoid running the skip buddy, if running something else can
3865 * be done without getting too unfair.
3866 */
3867 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01003868 struct sched_entity *second;
3869
3870 if (se == curr) {
3871 second = __pick_first_entity(cfs_rq);
3872 } else {
3873 second = __pick_next_entity(se);
3874 if (!second || (curr && entity_before(curr, second)))
3875 second = curr;
3876 }
3877
Rik van Rielac53db52011-02-01 09:51:03 -05003878 if (second && wakeup_preempt_entity(second, left) < 1)
3879 se = second;
3880 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003881
Mike Galbraithf685cea2009-10-23 23:09:22 +02003882 /*
3883 * Prefer last buddy, try to return the CPU to a preempted task.
3884 */
3885 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
3886 se = cfs_rq->last;
3887
Rik van Rielac53db52011-02-01 09:51:03 -05003888 /*
3889 * Someone really wants this to run. If it's not unfair, run it.
3890 */
3891 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
3892 se = cfs_rq->next;
3893
Mike Galbraithf685cea2009-10-23 23:09:22 +02003894 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003895
3896 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003897}
3898
Peter Zijlstra678d5712012-02-11 06:05:00 +01003899static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003900
Ingo Molnarab6cde22007-08-09 11:16:48 +02003901static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003902{
3903 /*
3904 * If still on the runqueue then deactivate_task()
3905 * was not called and update_curr() has to be done:
3906 */
3907 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003908 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003909
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003910 /* throttle cfs_rqs exceeding runtime */
3911 check_cfs_rq_runtime(cfs_rq);
3912
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003913 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00003914
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003915 if (prev->on_rq) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05003916 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003917 /* Put 'current' back into the tree. */
3918 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02003919 /* in !on_rq case, update occurred at dequeue */
Yuyang Du9d89c252015-07-15 08:04:37 +08003920 update_load_avg(prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003921 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02003922 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003923}
3924
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003925static void
3926entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003927{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003928 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003929 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003930 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003931 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003932
Paul Turner43365bd2010-12-15 19:10:17 -08003933 /*
Paul Turner9d85f212012-10-04 13:18:29 +02003934 * Ensure that runnable average is periodically updated.
3935 */
Vincent Guittot96956e22016-11-08 10:53:44 +01003936 update_load_avg(curr, UPDATE_TG);
Vincent Guittot6960f772016-12-21 16:50:26 +01003937 update_cfs_shares(curr);
Paul Turner9d85f212012-10-04 13:18:29 +02003938
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003939#ifdef CONFIG_SCHED_HRTICK
3940 /*
3941 * queued ticks are scheduled to match the slice, so don't bother
3942 * validating it and just reschedule.
3943 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003944 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003945 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003946 return;
3947 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003948 /*
3949 * don't let the period tick interfere with the hrtick preemption
3950 */
3951 if (!sched_feat(DOUBLE_TICK) &&
3952 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
3953 return;
3954#endif
3955
Yong Zhang2c2efae2011-07-29 16:20:33 +08003956 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003957 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003958}
3959
Paul Turnerab84d312011-07-21 09:43:28 -07003960
3961/**************************************************
3962 * CFS bandwidth control machinery
3963 */
3964
3965#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02003966
3967#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01003968static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003969
3970static inline bool cfs_bandwidth_used(void)
3971{
Ingo Molnarc5905af2012-02-24 08:31:31 +01003972 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003973}
3974
Ben Segall1ee14e62013-10-16 11:16:12 -07003975void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003976{
Ben Segall1ee14e62013-10-16 11:16:12 -07003977 static_key_slow_inc(&__cfs_bandwidth_used);
3978}
3979
3980void cfs_bandwidth_usage_dec(void)
3981{
3982 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003983}
3984#else /* HAVE_JUMP_LABEL */
3985static bool cfs_bandwidth_used(void)
3986{
3987 return true;
3988}
3989
Ben Segall1ee14e62013-10-16 11:16:12 -07003990void cfs_bandwidth_usage_inc(void) {}
3991void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003992#endif /* HAVE_JUMP_LABEL */
3993
Paul Turnerab84d312011-07-21 09:43:28 -07003994/*
3995 * default period for cfs group bandwidth.
3996 * default: 0.1s, units: nanoseconds
3997 */
3998static inline u64 default_cfs_period(void)
3999{
4000 return 100000000ULL;
4001}
Paul Turnerec12cb72011-07-21 09:43:30 -07004002
4003static inline u64 sched_cfs_bandwidth_slice(void)
4004{
4005 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
4006}
4007
Paul Turnera9cf55b2011-07-21 09:43:32 -07004008/*
4009 * Replenish runtime according to assigned quota and update expiration time.
4010 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
4011 * additional synchronization around rq->lock.
4012 *
4013 * requires cfs_b->lock
4014 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004015void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07004016{
4017 u64 now;
4018
4019 if (cfs_b->quota == RUNTIME_INF)
4020 return;
4021
4022 now = sched_clock_cpu(smp_processor_id());
4023 cfs_b->runtime = cfs_b->quota;
4024 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
4025}
4026
Peter Zijlstra029632f2011-10-25 10:00:11 +02004027static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4028{
4029 return &tg->cfs_bandwidth;
4030}
4031
Paul Turnerf1b17282012-10-04 13:18:31 +02004032/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
4033static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4034{
4035 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08004036 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004037
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004038 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004039}
4040
Paul Turner85dac902011-07-21 09:43:33 -07004041/* returns 0 on failure to allocate runtime */
4042static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07004043{
4044 struct task_group *tg = cfs_rq->tg;
4045 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004046 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004047
4048 /* note: this is a positive sum as runtime_remaining <= 0 */
4049 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
4050
4051 raw_spin_lock(&cfs_b->lock);
4052 if (cfs_b->quota == RUNTIME_INF)
4053 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07004054 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004055 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07004056
4057 if (cfs_b->runtime > 0) {
4058 amount = min(cfs_b->runtime, min_amount);
4059 cfs_b->runtime -= amount;
4060 cfs_b->idle = 0;
4061 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004062 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07004063 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004064 raw_spin_unlock(&cfs_b->lock);
4065
4066 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004067 /*
4068 * we may have advanced our local expiration to account for allowed
4069 * spread between our sched_clock and the one on which runtime was
4070 * issued.
4071 */
4072 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
4073 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07004074
4075 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004076}
4077
4078/*
4079 * Note: This depends on the synchronization provided by sched_clock and the
4080 * fact that rq->clock snapshots this value.
4081 */
4082static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4083{
4084 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004085
4086 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004087 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07004088 return;
4089
4090 if (cfs_rq->runtime_remaining < 0)
4091 return;
4092
4093 /*
4094 * If the local deadline has passed we have to consider the
4095 * possibility that our sched_clock is 'fast' and the global deadline
4096 * has not truly expired.
4097 *
4098 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07004099 * whether the global deadline has advanced. It is valid to compare
4100 * cfs_b->runtime_expires without any locks since we only care about
4101 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07004102 */
4103
Ben Segall51f21762014-05-19 15:49:45 -07004104 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07004105 /* extend local deadline, drift is bounded above by 2 ticks */
4106 cfs_rq->runtime_expires += TICK_NSEC;
4107 } else {
4108 /* global deadline is ahead, expiration has passed */
4109 cfs_rq->runtime_remaining = 0;
4110 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004111}
4112
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004113static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004114{
Paul Turnera9cf55b2011-07-21 09:43:32 -07004115 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07004116 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004117 expire_cfs_rq_runtime(cfs_rq);
4118
4119 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07004120 return;
4121
Paul Turner85dac902011-07-21 09:43:33 -07004122 /*
4123 * if we're unable to extend our runtime we resched so that the active
4124 * hierarchy can be throttled
4125 */
4126 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04004127 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07004128}
4129
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004130static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004131void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004132{
Paul Turner56f570e2011-11-07 20:26:33 -08004133 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07004134 return;
4135
4136 __account_cfs_rq_runtime(cfs_rq, delta_exec);
4137}
4138
Paul Turner85dac902011-07-21 09:43:33 -07004139static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4140{
Paul Turner56f570e2011-11-07 20:26:33 -08004141 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07004142}
4143
Paul Turner64660c82011-07-21 09:43:36 -07004144/* check whether cfs_rq, or any parent, is throttled */
4145static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4146{
Paul Turner56f570e2011-11-07 20:26:33 -08004147 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07004148}
4149
4150/*
4151 * Ensure that neither of the group entities corresponding to src_cpu or
4152 * dest_cpu are members of a throttled hierarchy when performing group
4153 * load-balance operations.
4154 */
4155static inline int throttled_lb_pair(struct task_group *tg,
4156 int src_cpu, int dest_cpu)
4157{
4158 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
4159
4160 src_cfs_rq = tg->cfs_rq[src_cpu];
4161 dest_cfs_rq = tg->cfs_rq[dest_cpu];
4162
4163 return throttled_hierarchy(src_cfs_rq) ||
4164 throttled_hierarchy(dest_cfs_rq);
4165}
4166
4167/* updated child weight may affect parent so we have to do this bottom up */
4168static int tg_unthrottle_up(struct task_group *tg, void *data)
4169{
4170 struct rq *rq = data;
4171 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4172
4173 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07004174 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02004175 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004176 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02004177 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07004178 }
Paul Turner64660c82011-07-21 09:43:36 -07004179
4180 return 0;
4181}
4182
4183static int tg_throttle_down(struct task_group *tg, void *data)
4184{
4185 struct rq *rq = data;
4186 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4187
Paul Turner82958362012-10-04 13:18:31 +02004188 /* group is entering throttled state, stop time */
4189 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004190 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07004191 cfs_rq->throttle_count++;
4192
4193 return 0;
4194}
4195
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004196static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07004197{
4198 struct rq *rq = rq_of(cfs_rq);
4199 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4200 struct sched_entity *se;
4201 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004202 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07004203
4204 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
4205
Paul Turnerf1b17282012-10-04 13:18:31 +02004206 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07004207 rcu_read_lock();
4208 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
4209 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07004210
4211 task_delta = cfs_rq->h_nr_running;
4212 for_each_sched_entity(se) {
4213 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
4214 /* throttled entity or throttle-on-deactivate */
4215 if (!se->on_rq)
4216 break;
4217
4218 if (dequeue)
4219 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
4220 qcfs_rq->h_nr_running -= task_delta;
4221
4222 if (qcfs_rq->load.weight)
4223 dequeue = 0;
4224 }
4225
4226 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004227 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07004228
4229 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004230 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07004231 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07004232 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004233
Ben Segallc06f04c2014-06-20 15:21:20 -07004234 /*
4235 * Add to the _head_ of the list, so that an already-started
4236 * distribute_cfs_runtime will not see us
4237 */
4238 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004239
4240 /*
4241 * If we're the first throttled task, make sure the bandwidth
4242 * timer is running.
4243 */
4244 if (empty)
4245 start_cfs_bandwidth(cfs_b);
4246
Paul Turner85dac902011-07-21 09:43:33 -07004247 raw_spin_unlock(&cfs_b->lock);
4248}
4249
Peter Zijlstra029632f2011-10-25 10:00:11 +02004250void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004251{
4252 struct rq *rq = rq_of(cfs_rq);
4253 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4254 struct sched_entity *se;
4255 int enqueue = 1;
4256 long task_delta;
4257
Michael Wang22b958d2013-06-04 14:23:39 +08004258 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004259
4260 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004261
4262 update_rq_clock(rq);
4263
Paul Turner671fd9d2011-07-21 09:43:34 -07004264 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004265 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004266 list_del_rcu(&cfs_rq->throttled_list);
4267 raw_spin_unlock(&cfs_b->lock);
4268
Paul Turner64660c82011-07-21 09:43:36 -07004269 /* update hierarchical throttle state */
4270 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
4271
Paul Turner671fd9d2011-07-21 09:43:34 -07004272 if (!cfs_rq->load.weight)
4273 return;
4274
4275 task_delta = cfs_rq->h_nr_running;
4276 for_each_sched_entity(se) {
4277 if (se->on_rq)
4278 enqueue = 0;
4279
4280 cfs_rq = cfs_rq_of(se);
4281 if (enqueue)
4282 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
4283 cfs_rq->h_nr_running += task_delta;
4284
4285 if (cfs_rq_throttled(cfs_rq))
4286 break;
4287 }
4288
4289 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004290 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07004291
4292 /* determine whether we need to wake up potentially idle cpu */
4293 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04004294 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07004295}
4296
4297static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
4298 u64 remaining, u64 expires)
4299{
4300 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07004301 u64 runtime;
4302 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004303
4304 rcu_read_lock();
4305 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
4306 throttled_list) {
4307 struct rq *rq = rq_of(cfs_rq);
4308
4309 raw_spin_lock(&rq->lock);
4310 if (!cfs_rq_throttled(cfs_rq))
4311 goto next;
4312
4313 runtime = -cfs_rq->runtime_remaining + 1;
4314 if (runtime > remaining)
4315 runtime = remaining;
4316 remaining -= runtime;
4317
4318 cfs_rq->runtime_remaining += runtime;
4319 cfs_rq->runtime_expires = expires;
4320
4321 /* we check whether we're throttled above */
4322 if (cfs_rq->runtime_remaining > 0)
4323 unthrottle_cfs_rq(cfs_rq);
4324
4325next:
4326 raw_spin_unlock(&rq->lock);
4327
4328 if (!remaining)
4329 break;
4330 }
4331 rcu_read_unlock();
4332
Ben Segallc06f04c2014-06-20 15:21:20 -07004333 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004334}
4335
Paul Turner58088ad2011-07-21 09:43:31 -07004336/*
4337 * Responsible for refilling a task_group's bandwidth and unthrottling its
4338 * cfs_rqs as appropriate. If there has been no activity within the last
4339 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
4340 * used to track this state.
4341 */
4342static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
4343{
Paul Turner671fd9d2011-07-21 09:43:34 -07004344 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07004345 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07004346
Paul Turner58088ad2011-07-21 09:43:31 -07004347 /* no need to continue the timer with no bandwidth constraint */
4348 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07004349 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07004350
Paul Turner671fd9d2011-07-21 09:43:34 -07004351 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004352 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07004353
Ben Segall51f21762014-05-19 15:49:45 -07004354 /*
4355 * idle depends on !throttled (for the case of a large deficit), and if
4356 * we're going inactive then everything else can be deferred
4357 */
4358 if (cfs_b->idle && !throttled)
4359 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004360
4361 __refill_cfs_bandwidth_runtime(cfs_b);
4362
Paul Turner671fd9d2011-07-21 09:43:34 -07004363 if (!throttled) {
4364 /* mark as potentially idle for the upcoming period */
4365 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07004366 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07004367 }
Paul Turner58088ad2011-07-21 09:43:31 -07004368
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004369 /* account preceding periods in which throttling occurred */
4370 cfs_b->nr_throttled += overrun;
4371
Paul Turner671fd9d2011-07-21 09:43:34 -07004372 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07004373
4374 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07004375 * This check is repeated as we are holding onto the new bandwidth while
4376 * we unthrottle. This can potentially race with an unthrottled group
4377 * trying to acquire new bandwidth from the global pool. This can result
4378 * in us over-using our runtime if it is all used during this loop, but
4379 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07004380 */
Ben Segallc06f04c2014-06-20 15:21:20 -07004381 while (throttled && cfs_b->runtime > 0) {
4382 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07004383 raw_spin_unlock(&cfs_b->lock);
4384 /* we can't nest cfs_b->lock while distributing bandwidth */
4385 runtime = distribute_cfs_runtime(cfs_b, runtime,
4386 runtime_expires);
4387 raw_spin_lock(&cfs_b->lock);
4388
4389 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07004390
4391 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07004392 }
4393
Paul Turner671fd9d2011-07-21 09:43:34 -07004394 /*
4395 * While we are ensured activity in the period following an
4396 * unthrottle, this also covers the case in which the new bandwidth is
4397 * insufficient to cover the existing bandwidth deficit. (Forcing the
4398 * timer to remain active while there are any throttled entities.)
4399 */
4400 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07004401
Ben Segall51f21762014-05-19 15:49:45 -07004402 return 0;
4403
4404out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07004405 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07004406}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004407
Paul Turnerd8b49862011-07-21 09:43:41 -07004408/* a cfs_rq won't donate quota below this amount */
4409static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
4410/* minimum remaining period time to redistribute slack quota */
4411static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
4412/* how long we wait to gather additional slack before distributing */
4413static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
4414
Ben Segalldb06e782013-10-16 11:16:17 -07004415/*
4416 * Are we near the end of the current quota period?
4417 *
4418 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00004419 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07004420 * migrate_hrtimers, base is never cleared, so we are fine.
4421 */
Paul Turnerd8b49862011-07-21 09:43:41 -07004422static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
4423{
4424 struct hrtimer *refresh_timer = &cfs_b->period_timer;
4425 u64 remaining;
4426
4427 /* if the call-back is running a quota refresh is already occurring */
4428 if (hrtimer_callback_running(refresh_timer))
4429 return 1;
4430
4431 /* is a quota refresh about to occur? */
4432 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
4433 if (remaining < min_expire)
4434 return 1;
4435
4436 return 0;
4437}
4438
4439static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
4440{
4441 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
4442
4443 /* if there's a quota refresh soon don't bother with slack */
4444 if (runtime_refresh_within(cfs_b, min_left))
4445 return;
4446
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004447 hrtimer_start(&cfs_b->slack_timer,
4448 ns_to_ktime(cfs_bandwidth_slack_period),
4449 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07004450}
4451
4452/* we know any runtime found here is valid as update_curr() precedes return */
4453static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4454{
4455 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4456 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
4457
4458 if (slack_runtime <= 0)
4459 return;
4460
4461 raw_spin_lock(&cfs_b->lock);
4462 if (cfs_b->quota != RUNTIME_INF &&
4463 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
4464 cfs_b->runtime += slack_runtime;
4465
4466 /* we are under rq->lock, defer unthrottling using a timer */
4467 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
4468 !list_empty(&cfs_b->throttled_cfs_rq))
4469 start_cfs_slack_bandwidth(cfs_b);
4470 }
4471 raw_spin_unlock(&cfs_b->lock);
4472
4473 /* even if it's not valid for return we don't want to try again */
4474 cfs_rq->runtime_remaining -= slack_runtime;
4475}
4476
4477static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4478{
Paul Turner56f570e2011-11-07 20:26:33 -08004479 if (!cfs_bandwidth_used())
4480 return;
4481
Paul Turnerfccfdc62011-11-07 20:26:34 -08004482 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07004483 return;
4484
4485 __return_cfs_rq_runtime(cfs_rq);
4486}
4487
4488/*
4489 * This is done with a timer (instead of inline with bandwidth return) since
4490 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
4491 */
4492static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
4493{
4494 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
4495 u64 expires;
4496
4497 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07004498 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07004499 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
4500 raw_spin_unlock(&cfs_b->lock);
4501 return;
4502 }
4503
Ben Segallc06f04c2014-06-20 15:21:20 -07004504 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07004505 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07004506
Paul Turnerd8b49862011-07-21 09:43:41 -07004507 expires = cfs_b->runtime_expires;
4508 raw_spin_unlock(&cfs_b->lock);
4509
4510 if (!runtime)
4511 return;
4512
4513 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
4514
4515 raw_spin_lock(&cfs_b->lock);
4516 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07004517 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07004518 raw_spin_unlock(&cfs_b->lock);
4519}
4520
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004521/*
4522 * When a group wakes up we want to make sure that its quota is not already
4523 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
4524 * runtime as update_curr() throttling can not not trigger until it's on-rq.
4525 */
4526static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
4527{
Paul Turner56f570e2011-11-07 20:26:33 -08004528 if (!cfs_bandwidth_used())
4529 return;
4530
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004531 /* an active group must be handled by the update_curr()->put() path */
4532 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
4533 return;
4534
4535 /* ensure the group is not already throttled */
4536 if (cfs_rq_throttled(cfs_rq))
4537 return;
4538
4539 /* update runtime allocation */
4540 account_cfs_rq_runtime(cfs_rq, 0);
4541 if (cfs_rq->runtime_remaining <= 0)
4542 throttle_cfs_rq(cfs_rq);
4543}
4544
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004545static void sync_throttle(struct task_group *tg, int cpu)
4546{
4547 struct cfs_rq *pcfs_rq, *cfs_rq;
4548
4549 if (!cfs_bandwidth_used())
4550 return;
4551
4552 if (!tg->parent)
4553 return;
4554
4555 cfs_rq = tg->cfs_rq[cpu];
4556 pcfs_rq = tg->parent->cfs_rq[cpu];
4557
4558 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08004559 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004560}
4561
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004562/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004563static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004564{
Paul Turner56f570e2011-11-07 20:26:33 -08004565 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01004566 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08004567
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004568 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004569 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004570
4571 /*
4572 * it's possible for a throttled entity to be forced into a running
4573 * state (e.g. set_curr_task), in this case we're finished.
4574 */
4575 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004576 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004577
4578 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004579 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004580}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004581
Peter Zijlstra029632f2011-10-25 10:00:11 +02004582static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
4583{
4584 struct cfs_bandwidth *cfs_b =
4585 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004586
Peter Zijlstra029632f2011-10-25 10:00:11 +02004587 do_sched_cfs_slack_timer(cfs_b);
4588
4589 return HRTIMER_NORESTART;
4590}
4591
4592static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
4593{
4594 struct cfs_bandwidth *cfs_b =
4595 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004596 int overrun;
4597 int idle = 0;
4598
Ben Segall51f21762014-05-19 15:49:45 -07004599 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004600 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004601 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004602 if (!overrun)
4603 break;
4604
4605 idle = do_sched_cfs_period_timer(cfs_b, overrun);
4606 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004607 if (idle)
4608 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07004609 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004610
4611 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
4612}
4613
4614void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4615{
4616 raw_spin_lock_init(&cfs_b->lock);
4617 cfs_b->runtime = 0;
4618 cfs_b->quota = RUNTIME_INF;
4619 cfs_b->period = ns_to_ktime(default_cfs_period());
4620
4621 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004622 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004623 cfs_b->period_timer.function = sched_cfs_period_timer;
4624 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4625 cfs_b->slack_timer.function = sched_cfs_slack_timer;
4626}
4627
4628static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4629{
4630 cfs_rq->runtime_enabled = 0;
4631 INIT_LIST_HEAD(&cfs_rq->throttled_list);
4632}
4633
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004634void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004635{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004636 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004637
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004638 if (!cfs_b->period_active) {
4639 cfs_b->period_active = 1;
4640 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
4641 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
4642 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02004643}
4644
4645static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4646{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09004647 /* init_cfs_bandwidth() was not called */
4648 if (!cfs_b->throttled_cfs_rq.next)
4649 return;
4650
Peter Zijlstra029632f2011-10-25 10:00:11 +02004651 hrtimer_cancel(&cfs_b->period_timer);
4652 hrtimer_cancel(&cfs_b->slack_timer);
4653}
4654
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004655static void __maybe_unused update_runtime_enabled(struct rq *rq)
4656{
4657 struct cfs_rq *cfs_rq;
4658
4659 for_each_leaf_cfs_rq(rq, cfs_rq) {
4660 struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
4661
4662 raw_spin_lock(&cfs_b->lock);
4663 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
4664 raw_spin_unlock(&cfs_b->lock);
4665 }
4666}
4667
Arnd Bergmann38dc3342013-01-25 14:14:22 +00004668static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004669{
4670 struct cfs_rq *cfs_rq;
4671
4672 for_each_leaf_cfs_rq(rq, cfs_rq) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02004673 if (!cfs_rq->runtime_enabled)
4674 continue;
4675
4676 /*
4677 * clock_task is not advancing so we just need to make sure
4678 * there's some valid quota amount
4679 */
Ben Segall51f21762014-05-19 15:49:45 -07004680 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004681 /*
4682 * Offline rq is schedulable till cpu is completely disabled
4683 * in take_cpu_down(), so we prevent new cfs throttling here.
4684 */
4685 cfs_rq->runtime_enabled = 0;
4686
Peter Zijlstra029632f2011-10-25 10:00:11 +02004687 if (cfs_rq_throttled(cfs_rq))
4688 unthrottle_cfs_rq(cfs_rq);
4689 }
4690}
4691
4692#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02004693static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4694{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004695 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02004696}
4697
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004698static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01004699static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004700static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004701static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004702static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07004703
4704static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4705{
4706 return 0;
4707}
Paul Turner64660c82011-07-21 09:43:36 -07004708
4709static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4710{
4711 return 0;
4712}
4713
4714static inline int throttled_lb_pair(struct task_group *tg,
4715 int src_cpu, int dest_cpu)
4716{
4717 return 0;
4718}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004719
4720void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
4721
4722#ifdef CONFIG_FAIR_GROUP_SCHED
4723static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07004724#endif
4725
Peter Zijlstra029632f2011-10-25 10:00:11 +02004726static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4727{
4728 return NULL;
4729}
4730static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004731static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07004732static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004733
4734#endif /* CONFIG_CFS_BANDWIDTH */
4735
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004736/**************************************************
4737 * CFS operations on tasks:
4738 */
4739
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004740#ifdef CONFIG_SCHED_HRTICK
4741static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
4742{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004743 struct sched_entity *se = &p->se;
4744 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4745
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02004746 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004747
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07004748 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004749 u64 slice = sched_slice(cfs_rq, se);
4750 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
4751 s64 delta = slice - ran;
4752
4753 if (delta < 0) {
4754 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04004755 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004756 return;
4757 }
Peter Zijlstra31656512008-07-18 18:01:23 +02004758 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004759 }
4760}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004761
4762/*
4763 * called from enqueue/dequeue and updates the hrtick when the
4764 * current task is from our class and nr_running is low enough
4765 * to matter.
4766 */
4767static void hrtick_update(struct rq *rq)
4768{
4769 struct task_struct *curr = rq->curr;
4770
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004771 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004772 return;
4773
4774 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
4775 hrtick_start_fair(rq, curr);
4776}
Dhaval Giani55e12e52008-06-24 23:39:43 +05304777#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004778static inline void
4779hrtick_start_fair(struct rq *rq, struct task_struct *p)
4780{
4781}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004782
4783static inline void hrtick_update(struct rq *rq)
4784{
4785}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004786#endif
4787
Patrick Bellasi2178e842016-07-22 11:35:59 +01004788#ifdef CONFIG_SMP
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08004789static bool __cpu_overutilized(int cpu, int delta);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004790static bool cpu_overutilized(int cpu);
Juri Lellic6e94382016-12-14 16:10:10 +00004791unsigned long boosted_cpu_util(int cpu);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004792#else
Joonwoo Park8b34bba2016-12-08 16:12:12 -08004793#define boosted_cpu_util(cpu) cpu_util_freq(cpu)
Patrick Bellasi2178e842016-07-22 11:35:59 +01004794#endif
Juri Lelli4585a262015-08-19 19:47:12 +01004795
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004796/*
4797 * The enqueue_task method is called before nr_running is
4798 * increased. Here we update the fair scheduling stats and
4799 * then put the task into the rbtree:
4800 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00004801static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004802enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004803{
4804 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004805 struct sched_entity *se = &p->se;
Patrick Bellasi2178e842016-07-22 11:35:59 +01004806#ifdef CONFIG_SMP
Juri Lelli43aac892015-06-26 12:14:23 +01004807 int task_new = flags & ENQUEUE_WAKEUP_NEW;
Patrick Bellasi2178e842016-07-22 11:35:59 +01004808#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004809
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02004810 /*
4811 * If in_iowait is set, the code below may not trigger any cpufreq
4812 * utilization updates, so do it here explicitly with the IOWAIT flag
4813 * passed.
4814 */
4815 if (p->in_iowait)
4816 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT);
4817
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004818 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004819 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004820 break;
4821 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004822 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004823
4824 /*
4825 * end evaluation on encountering a throttled cfs_rq
4826 *
4827 * note: in the case of encountering a throttled cfs_rq we will
4828 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02004829 */
Paul Turner85dac902011-07-21 09:43:33 -07004830 if (cfs_rq_throttled(cfs_rq))
4831 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004832 cfs_rq->h_nr_running++;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004833 walt_inc_cfs_cumulative_runnable_avg(cfs_rq, p);
Paul Turner85dac902011-07-21 09:43:33 -07004834
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004835 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004836 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004837
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004838 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004839 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004840 cfs_rq->h_nr_running++;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004841 walt_inc_cfs_cumulative_runnable_avg(cfs_rq, p);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004842
Paul Turner85dac902011-07-21 09:43:33 -07004843 if (cfs_rq_throttled(cfs_rq))
4844 break;
4845
Vincent Guittot96956e22016-11-08 10:53:44 +01004846 update_load_avg(se, UPDATE_TG);
Vincent Guittot6960f772016-12-21 16:50:26 +01004847 update_cfs_shares(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004848 }
4849
Patrick Bellasi2178e842016-07-22 11:35:59 +01004850 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004851 add_nr_running(rq, 1);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004852
4853#ifdef CONFIG_SMP
4854
Patrick Bellasie72491c2016-08-24 11:27:27 +01004855 /*
4856 * Update SchedTune accounting.
4857 *
4858 * We do it before updating the CPU capacity to ensure the
4859 * boost value of the current task is accounted for in the
4860 * selection of the OPP.
4861 *
4862 * We do it also in the case where we enqueue a throttled task;
4863 * we could argue that a throttled task should not boost a CPU,
4864 * however:
4865 * a) properly implementing CPU boosting considering throttled
4866 * tasks will increase a lot the complexity of the solution
4867 * b) it's not easy to quantify the benefits introduced by
4868 * such a more complex solution.
4869 * Thus, for the time being we go for the simple solution and boost
4870 * also for throttled RQs.
4871 */
4872 schedtune_enqueue_task(p, cpu_of(rq));
4873
Patrick Bellasi2178e842016-07-22 11:35:59 +01004874 if (!se) {
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004875 walt_inc_cumulative_runnable_avg(rq, p);
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004876 if (!task_new && !rq->rd->overutilized &&
Patrick Bellasi8e45d942016-02-10 09:24:36 +00004877 cpu_overutilized(rq->cpu)) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004878 rq->rd->overutilized = true;
Patrick Bellasi8e45d942016-02-10 09:24:36 +00004879 trace_sched_overutilized(true);
4880 }
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004881 }
Patrick Bellasid2489002016-07-28 18:44:40 +01004882
Patrick Bellasi2178e842016-07-22 11:35:59 +01004883#endif /* CONFIG_SMP */
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004884 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004885}
4886
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004887static void set_next_buddy(struct sched_entity *se);
4888
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004889/*
4890 * The dequeue_task method is called before nr_running is
4891 * decreased. We remove the task from the rbtree and
4892 * update the fair scheduling stats:
4893 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004894static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004895{
4896 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004897 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004898 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004899
4900 for_each_sched_entity(se) {
4901 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004902 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004903
4904 /*
4905 * end evaluation on encountering a throttled cfs_rq
4906 *
4907 * note: in the case of encountering a throttled cfs_rq we will
4908 * post the final h_nr_running decrement below.
4909 */
4910 if (cfs_rq_throttled(cfs_rq))
4911 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004912 cfs_rq->h_nr_running--;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004913 walt_dec_cfs_cumulative_runnable_avg(cfs_rq, p);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004914
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004915 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004916 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004917 /* Avoid re-evaluating load for this entity: */
4918 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004919 /*
4920 * Bias pick_next to pick a task from this cfs_rq, as
4921 * p is sleeping when it is within its sched_slice.
4922 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004923 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
4924 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004925 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004926 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004927 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004928 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004929
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004930 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004931 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004932 cfs_rq->h_nr_running--;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004933 walt_dec_cfs_cumulative_runnable_avg(cfs_rq, p);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004934
Paul Turner85dac902011-07-21 09:43:33 -07004935 if (cfs_rq_throttled(cfs_rq))
4936 break;
4937
Vincent Guittot96956e22016-11-08 10:53:44 +01004938 update_load_avg(se, UPDATE_TG);
Vincent Guittot6960f772016-12-21 16:50:26 +01004939 update_cfs_shares(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004940 }
4941
Patrick Bellasi2178e842016-07-22 11:35:59 +01004942 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004943 sub_nr_running(rq, 1);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004944
4945#ifdef CONFIG_SMP
4946
Patrick Bellasie72491c2016-08-24 11:27:27 +01004947 /*
4948 * Update SchedTune accounting
4949 *
4950 * We do it before updating the CPU capacity to ensure the
4951 * boost value of the current task is accounted for in the
4952 * selection of the OPP.
4953 */
4954 schedtune_dequeue_task(p, cpu_of(rq));
4955
Viresh Kumare0907552017-11-02 15:13:26 +05304956 if (!se)
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07004957 walt_dec_cumulative_runnable_avg(rq, p);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004958#endif /* CONFIG_SMP */
4959
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004960 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004961}
4962
Gregory Haskinse7693a32008-01-25 21:08:09 +01004963#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02004964
4965/* Working cpumask for: load_balance, load_balance_newidle. */
4966DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
4967DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
4968
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004969#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004970/*
4971 * per rq 'load' arrray crap; XXX kill this.
4972 */
4973
4974/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004975 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004976 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004977 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
4978 *
4979 * If a cpu misses updates for n ticks (as it was idle) and update gets
4980 * called on the n+1-th tick when cpu may be busy, then we have:
4981 *
4982 * load_n = (1 - 1/2^i)^n * load_0
4983 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004984 *
4985 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004986 *
4987 * load' = (1 - 1/2^i)^n * load
4988 *
4989 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
4990 * This allows us to precompute the above in said factors, thereby allowing the
4991 * reduction of an arbitrary n in O(log_2 n) steps. (See also
4992 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004993 *
4994 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004995 */
4996#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004997
4998static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
4999static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
5000 { 0, 0, 0, 0, 0, 0, 0, 0 },
5001 { 64, 32, 8, 0, 0, 0, 0, 0 },
5002 { 96, 72, 40, 12, 1, 0, 0, 0 },
5003 { 112, 98, 75, 43, 15, 1, 0, 0 },
5004 { 120, 112, 98, 76, 45, 16, 2, 0 }
5005};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005006
5007/*
5008 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
5009 * would be when CPU is idle and so we just decay the old load without
5010 * adding any new load.
5011 */
5012static unsigned long
5013decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
5014{
5015 int j = 0;
5016
5017 if (!missed_updates)
5018 return load;
5019
5020 if (missed_updates >= degrade_zero_ticks[idx])
5021 return 0;
5022
5023 if (idx == 1)
5024 return load >> missed_updates;
5025
5026 while (missed_updates) {
5027 if (missed_updates % 2)
5028 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
5029
5030 missed_updates >>= 1;
5031 j++;
5032 }
5033 return load;
5034}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005035#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005036
Byungchul Park59543272015-10-14 18:47:35 +09005037/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005038 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09005039 * @this_rq: The rq to update statistics for
5040 * @this_load: The current load
5041 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09005042 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005043 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09005044 * scheduler tick (TICK_NSEC).
5045 *
5046 * This function computes a decaying average:
5047 *
5048 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
5049 *
5050 * Because of NOHZ it might not get called on every tick which gives need for
5051 * the @pending_updates argument.
5052 *
5053 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
5054 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
5055 * = A * (A * load[i]_n-2 + B) + B
5056 * = A * (A * (A * load[i]_n-3 + B) + B) + B
5057 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
5058 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
5059 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
5060 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
5061 *
5062 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
5063 * any change in load would have resulted in the tick being turned back on.
5064 *
5065 * For regular NOHZ, this reduces to:
5066 *
5067 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
5068 *
5069 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005070 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005071 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005072static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
5073 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005074{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005075 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005076 int i, scale;
5077
5078 this_rq->nr_load_updates++;
5079
5080 /* Update our load: */
5081 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
5082 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
5083 unsigned long old_load, new_load;
5084
5085 /* scale is effectively 1 << i now, and >> i divides by scale */
5086
Byungchul Park7400d3b2016-01-15 16:07:49 +09005087 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005088#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005089 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09005090 if (tickless_load) {
5091 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
5092 /*
5093 * old_load can never be a negative value because a
5094 * decayed tickless_load cannot be greater than the
5095 * original tickless_load.
5096 */
5097 old_load += tickless_load;
5098 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005099#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005100 new_load = this_load;
5101 /*
5102 * Round up the averaging division if load is increasing. This
5103 * prevents us from getting stuck on 9 if the load is 10, for
5104 * example.
5105 */
5106 if (new_load > old_load)
5107 new_load += scale - 1;
5108
5109 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
5110 }
5111
5112 sched_avg_update(this_rq);
5113}
5114
Yuyang Du7ea241a2015-07-15 08:04:42 +08005115/* Used instead of source_load when we know the type == 0 */
5116static unsigned long weighted_cpuload(const int cpu)
5117{
5118 return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs);
5119}
5120
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005121#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005122/*
5123 * There is no sane way to deal with nohz on smp when using jiffies because the
5124 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
5125 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
5126 *
5127 * Therefore we need to avoid the delta approach from the regular tick when
5128 * possible since that would seriously skew the load calculation. This is why we
5129 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
5130 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
5131 * loop exit, nohz_idle_balance, nohz full exit...)
5132 *
5133 * This means we might still be one tick off for nohz periods.
5134 */
5135
5136static void cpu_load_update_nohz(struct rq *this_rq,
5137 unsigned long curr_jiffies,
5138 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005139{
5140 unsigned long pending_updates;
5141
5142 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
5143 if (pending_updates) {
5144 this_rq->last_load_update_tick = curr_jiffies;
5145 /*
5146 * In the regular NOHZ case, we were idle, this means load 0.
5147 * In the NOHZ_FULL case, we were non-idle, we should consider
5148 * its weighted load.
5149 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005150 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005151 }
5152}
5153
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005154/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005155 * Called from nohz_idle_balance() to update the load ratings before doing the
5156 * idle balance.
5157 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005158static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005159{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005160 /*
5161 * bail if there's load or we're actually up-to-date.
5162 */
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005163 if (weighted_cpuload(cpu_of(this_rq)))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005164 return;
5165
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005166 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005167}
5168
5169/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005170 * Record CPU load on nohz entry so we know the tickless load to account
5171 * on nohz exit. cpu_load[0] happens then to be updated more frequently
5172 * than other cpu_load[idx] but it should be fine as cpu_load readers
5173 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005174 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005175void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005176{
5177 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005178
5179 /*
5180 * This is all lockless but should be fine. If weighted_cpuload changes
5181 * concurrently we'll exit nohz. And cpu_load write can race with
5182 * cpu_load_update_idle() but both updater would be writing the same.
5183 */
5184 this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
5185}
5186
5187/*
5188 * Account the tickless load in the end of a nohz frame.
5189 */
5190void cpu_load_update_nohz_stop(void)
5191{
Jason Low316c1608d2015-04-28 13:00:20 -07005192 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005193 struct rq *this_rq = this_rq();
5194 unsigned long load;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005195
5196 if (curr_jiffies == this_rq->last_load_update_tick)
5197 return;
5198
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005199 load = weighted_cpuload(cpu_of(this_rq));
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005200 raw_spin_lock(&this_rq->lock);
Matt Flemingb52fad22016-05-03 20:46:54 +01005201 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005202 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005203 raw_spin_unlock(&this_rq->lock);
5204}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005205#else /* !CONFIG_NO_HZ_COMMON */
5206static inline void cpu_load_update_nohz(struct rq *this_rq,
5207 unsigned long curr_jiffies,
5208 unsigned long load) { }
5209#endif /* CONFIG_NO_HZ_COMMON */
5210
5211static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
5212{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005213#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005214 /* See the mess around cpu_load_update_nohz(). */
5215 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005216#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005217 cpu_load_update(this_rq, load, 1);
5218}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005219
5220/*
5221 * Called from scheduler_tick()
5222 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005223void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005224{
Yuyang Du7ea241a2015-07-15 08:04:42 +08005225 unsigned long load = weighted_cpuload(cpu_of(this_rq));
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005226
5227 if (tick_nohz_tick_stopped())
5228 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
5229 else
5230 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005231}
5232
Peter Zijlstra029632f2011-10-25 10:00:11 +02005233/*
5234 * Return a low guess at the load of a migration-source cpu weighted
5235 * according to the scheduling class and "nice" value.
5236 *
5237 * We want to under-estimate the load of migration sources, to
5238 * balance conservatively.
5239 */
5240static unsigned long source_load(int cpu, int type)
5241{
5242 struct rq *rq = cpu_rq(cpu);
5243 unsigned long total = weighted_cpuload(cpu);
5244
5245 if (type == 0 || !sched_feat(LB_BIAS))
5246 return total;
5247
5248 return min(rq->cpu_load[type-1], total);
5249}
5250
5251/*
5252 * Return a high guess at the load of a migration-target cpu weighted
5253 * according to the scheduling class and "nice" value.
5254 */
5255static unsigned long target_load(int cpu, int type)
5256{
5257 struct rq *rq = cpu_rq(cpu);
5258 unsigned long total = weighted_cpuload(cpu);
5259
5260 if (type == 0 || !sched_feat(LB_BIAS))
5261 return total;
5262
5263 return max(rq->cpu_load[type-1], total);
5264}
5265
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005266
Peter Zijlstra029632f2011-10-25 10:00:11 +02005267static unsigned long cpu_avg_load_per_task(int cpu)
5268{
5269 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005270 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005271 unsigned long load_avg = weighted_cpuload(cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005272
5273 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005274 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005275
5276 return 0;
5277}
5278
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005279#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005280/*
5281 * effective_load() calculates the load change as seen from the root_task_group
5282 *
5283 * Adding load to a group doesn't make a group heavier, but can cause movement
5284 * of group shares between cpus. Assuming the shares were perfectly aligned one
5285 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005286 *
5287 * Calculate the effective load difference if @wl is added (subtracted) to @tg
5288 * on this @cpu and results in a total addition (subtraction) of @wg to the
5289 * total group weight.
5290 *
5291 * Given a runqueue weight distribution (rw_i) we can compute a shares
5292 * distribution (s_i) using:
5293 *
5294 * s_i = rw_i / \Sum rw_j (1)
5295 *
5296 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
5297 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
5298 * shares distribution (s_i):
5299 *
5300 * rw_i = { 2, 4, 1, 0 }
5301 * s_i = { 2/7, 4/7, 1/7, 0 }
5302 *
5303 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
5304 * task used to run on and the CPU the waker is running on), we need to
5305 * compute the effect of waking a task on either CPU and, in case of a sync
5306 * wakeup, compute the effect of the current task going to sleep.
5307 *
5308 * So for a change of @wl to the local @cpu with an overall group weight change
5309 * of @wl we can compute the new shares distribution (s'_i) using:
5310 *
5311 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
5312 *
5313 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
5314 * differences in waking a task to CPU 0. The additional task changes the
5315 * weight and shares distributions like:
5316 *
5317 * rw'_i = { 3, 4, 1, 0 }
5318 * s'_i = { 3/8, 4/8, 1/8, 0 }
5319 *
5320 * We can then compute the difference in effective weight by using:
5321 *
5322 * dw_i = S * (s'_i - s_i) (3)
5323 *
5324 * Where 'S' is the group weight as seen by its parent.
5325 *
5326 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
5327 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
5328 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005329 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005330static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005331{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005332 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005333
Rik van Riel9722c2d2014-01-06 11:39:12 +00005334 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005335 return wl;
5336
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005337 for_each_sched_entity(se) {
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005338 struct cfs_rq *cfs_rq = se->my_q;
5339 long W, w = cfs_rq_load_avg(cfs_rq);
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005340
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005341 tg = cfs_rq->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005342
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005343 /*
5344 * W = @wg + \Sum rw_j
5345 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005346 W = wg + atomic_long_read(&tg->load_avg);
5347
5348 /* Ensure \Sum rw_j >= rw_i */
5349 W -= cfs_rq->tg_load_avg_contrib;
5350 W += w;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005351
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005352 /*
5353 * w = rw_i + @wl
5354 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005355 w += wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02005356
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005357 /*
5358 * wl = S * s'_i; see (2)
5359 */
5360 if (W > 0 && w < W)
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005361 wl = (w * (long)scale_load_down(tg->shares)) / W;
Paul Turner977dda72011-01-14 17:57:50 -08005362 else
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005363 wl = scale_load_down(tg->shares);
Peter Zijlstra940959e2008-09-23 15:33:42 +02005364
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005365 /*
5366 * Per the above, wl is the new se->load.weight value; since
5367 * those are clipped to [MIN_SHARES, ...) do so now. See
5368 * calc_cfs_shares().
5369 */
Paul Turner977dda72011-01-14 17:57:50 -08005370 if (wl < MIN_SHARES)
5371 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005372
5373 /*
5374 * wl = dw_i = S * (s'_i - s_i); see (3)
5375 */
Yuyang Du9d89c252015-07-15 08:04:37 +08005376 wl -= se->avg.load_avg;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005377
5378 /*
5379 * Recursively apply this logic to all parent groups to compute
5380 * the final effective load change on the root group. Since
5381 * only the @tg group gets extra weight, all parent groups can
5382 * only redistribute existing shares. @wl is the shift in shares
5383 * resulting from this level per the above.
5384 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005385 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005386 }
5387
5388 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005389}
5390#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005391
Mel Gorman58d081b2013-10-07 11:29:10 +01005392static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005393{
Peter Zijlstra83378262008-06-27 13:41:37 +02005394 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005395}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005396
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005397#endif
5398
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005399static void record_wakee(struct task_struct *p)
5400{
5401 /*
5402 * Only decay a single time; tasks that have less then 1 wakeup per
5403 * jiffy will not have built up many flips.
5404 */
5405 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
5406 current->wakee_flips >>= 1;
5407 current->wakee_flip_decay_ts = jiffies;
5408 }
5409
5410 if (current->last_wakee != p) {
5411 current->last_wakee = p;
5412 current->wakee_flips++;
5413 }
5414}
5415
Juri Lelli2f8ed122015-04-30 17:35:23 +01005416/*
5417 * Returns the current capacity of cpu after applying both
5418 * cpu and freq scaling.
5419 */
5420unsigned long capacity_curr_of(int cpu)
5421{
5422 return cpu_rq(cpu)->cpu_capacity_orig *
5423 arch_scale_freq_capacity(NULL, cpu)
5424 >> SCHED_CAPACITY_SHIFT;
5425}
5426
Ionela Voinescu58b761f2017-12-07 19:43:46 +00005427/*
5428 * Returns the current capacity of cpu after applying both
5429 * cpu and min freq scaling.
5430 */
5431unsigned long capacity_min_of(int cpu)
5432{
5433 if (!sched_feat(MIN_CAPACITY_CAPPING))
5434 return 0;
5435 return arch_scale_cpu_capacity(NULL, cpu) *
5436 arch_scale_min_freq_capacity(NULL, cpu)
5437 >> SCHED_CAPACITY_SHIFT;
5438}
5439
5440
Morten Rasmussen94c4cea2015-01-13 13:45:51 +00005441static inline bool energy_aware(void)
5442{
5443 return sched_feat(ENERGY_AWARE);
5444}
5445
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005446/*
5447 * CPU candidates.
5448 *
5449 * These are labels to reference CPU candidates for an energy_diff.
5450 * Currently we support only two possible candidates: the task's previous CPU
5451 * and another candiate CPU.
5452 * More advanced/aggressive EAS selection policies can consider more
5453 * candidates.
5454 */
5455#define EAS_CPU_PRV 0
5456#define EAS_CPU_NXT 1
5457#define EAS_CPU_BKP 2
5458#define EAS_CPU_CNT 3
5459
5460/*
5461 * energy_diff - supports the computation of the estimated energy impact in
5462 * moving a "task"'s "util_delta" between different CPU candidates.
5463 */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005464struct energy_env {
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005465 /* Utilization to move */
5466 struct task_struct *p;
5467 int util_delta;
5468
5469 /* Mask of CPUs candidates to evaluate */
5470 cpumask_t cpus_mask;
5471
5472 /* CPU candidates to evaluate */
5473 struct {
5474
5475 /* CPU ID, must be in cpus_mask */
5476 int cpu_id;
5477
5478 /*
5479 * Index (into sched_group_energy::cap_states) of the OPP the
5480 * CPU needs to run at if the task is placed on it.
5481 * This includes the both active and blocked load, due to
5482 * other tasks on this CPU, as well as the task's own
5483 * utilization.
5484 */
5485 int cap_idx;
5486 int cap;
5487
5488 /* Estimated system energy */
5489 unsigned int energy;
5490
5491 /* Estimated energy variation wrt EAS_CPU_PRV */
5492 int nrg_delta;
5493
5494 } cpu[EAS_CPU_CNT];
5495
5496 /*
5497 * Index (into energy_env::cpu) of the morst energy efficient CPU for
5498 * the specified energy_env::task
5499 */
5500 int next_idx;
5501
5502 /* Support data */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005503 struct sched_group *sg_top;
5504 struct sched_group *sg_cap;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005505 struct sched_group *sg;
Morten Rasmussena455fa72015-01-02 14:21:56 +00005506};
5507
Chris Redpath4530ed92017-09-12 14:44:24 +01005508static int cpu_util_wake(int cpu, struct task_struct *p);
5509
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005510/*
Morten Rasmussena455fa72015-01-02 14:21:56 +00005511 * __cpu_norm_util() returns the cpu util relative to a specific capacity,
Chris Redpath4530ed92017-09-12 14:44:24 +01005512 * i.e. it's busy ratio, in the range [0..SCHED_LOAD_SCALE], which is useful for
5513 * energy calculations.
5514 *
5515 * Since util is a scale-invariant utilization defined as:
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005516 *
5517 * util ~ (curr_freq/max_freq)*1024 * capacity_orig/1024 * running_time/time
5518 *
5519 * the normalized util can be found using the specific capacity.
5520 *
5521 * capacity = capacity_orig * curr_freq/max_freq
5522 *
5523 * norm_util = running_time/time ~ util/capacity
5524 */
Chris Redpath4530ed92017-09-12 14:44:24 +01005525static unsigned long __cpu_norm_util(unsigned long util, unsigned long capacity)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005526{
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005527 if (util >= capacity)
5528 return SCHED_CAPACITY_SCALE;
5529
5530 return (util << SCHED_CAPACITY_SHIFT)/capacity;
5531}
5532
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005533static unsigned long group_max_util(struct energy_env *eenv, int cpu_idx)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005534{
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005535 unsigned long max_util = 0;
Patrick Bellasi06d637c2017-06-01 16:40:22 +01005536 unsigned long util;
5537 int cpu;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005538
Patrick Bellasi06d637c2017-06-01 16:40:22 +01005539 for_each_cpu(cpu, sched_group_cpus(eenv->sg_cap)) {
Chris Redpathe997bf02017-09-12 14:48:29 +01005540 util = cpu_util_wake(cpu, eenv->p);
Patrick Bellasi06d637c2017-06-01 16:40:22 +01005541
5542 /*
5543 * If we are looking at the target CPU specified by the eenv,
5544 * then we should add the (estimated) utilization of the task
5545 * assuming we will wake it up on that CPU.
5546 */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005547 if (unlikely(cpu == eenv->cpu[cpu_idx].cpu_id))
Patrick Bellasi06d637c2017-06-01 16:40:22 +01005548 util += eenv->util_delta;
5549
5550 max_util = max(max_util, util);
Ionela Voinescu9e1c6482017-12-07 19:50:45 +00005551
5552 /*
5553 * Take into account any minimum frequency imposed
5554 * elsewhere which limits the energy states available
5555 * If the MIN_CAPACITY_CAPPING feature is not enabled
5556 * capacity_min_of will return 0 (not capped).
5557 */
5558 max_util = max(max_util, capacity_min_of(cpu));
5559
Morten Rasmussena455fa72015-01-02 14:21:56 +00005560 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005561
5562 return max_util;
5563}
5564
5565/*
5566 * group_norm_util() returns the approximated group util relative to it's
Chris Redpath4530ed92017-09-12 14:44:24 +01005567 * current capacity (busy ratio), in the range [0..SCHED_LOAD_SCALE], for use
5568 * in energy calculations.
5569 *
5570 * Since task executions may or may not overlap in time in the group the true
5571 * normalized util is between MAX(cpu_norm_util(i)) and SUM(cpu_norm_util(i))
5572 * when iterating over all CPUs in the group.
5573 * The latter estimate is used as it leads to a more pessimistic energy
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005574 * estimate (more busy).
5575 */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005576static unsigned
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005577long group_norm_util(struct energy_env *eenv, int cpu_idx)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005578{
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005579 unsigned long capacity = eenv->cpu[cpu_idx].cap;
Chris Redpath4530ed92017-09-12 14:44:24 +01005580 unsigned long util, util_sum = 0;
5581 int cpu;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005582
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005583 for_each_cpu(cpu, sched_group_cpus(eenv->sg)) {
Chris Redpathe997bf02017-09-12 14:48:29 +01005584 util = cpu_util_wake(cpu, eenv->p);
Chris Redpath4530ed92017-09-12 14:44:24 +01005585
5586 /*
5587 * If we are looking at the target CPU specified by the eenv,
5588 * then we should add the (estimated) utilization of the task
5589 * assuming we will wake it up on that CPU.
5590 */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005591 if (unlikely(cpu == eenv->cpu[cpu_idx].cpu_id))
Chris Redpath4530ed92017-09-12 14:44:24 +01005592 util += eenv->util_delta;
5593
5594 util_sum += __cpu_norm_util(util, capacity);
Morten Rasmussena455fa72015-01-02 14:21:56 +00005595 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005596
Chris Redpath4530ed92017-09-12 14:44:24 +01005597 return min_t(unsigned long, util_sum, SCHED_CAPACITY_SCALE);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005598}
5599
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005600static int find_new_capacity(struct energy_env *eenv, int cpu_idx)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005601{
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005602 const struct sched_group_energy *sge = eenv->sg->sge;
Chris Redpath185507e2017-10-25 17:25:20 +01005603 int idx, max_idx = sge->nr_cap_states - 1;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005604 unsigned long util = group_max_util(eenv, cpu_idx);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005605
Chris Redpath185507e2017-10-25 17:25:20 +01005606 /* default is max_cap if we don't find a match */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005607 eenv->cpu[cpu_idx].cap_idx = max_idx;
5608 eenv->cpu[cpu_idx].cap = sge->cap_states[max_idx].cap;
Chris Redpath185507e2017-10-25 17:25:20 +01005609
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005610 for (idx = 0; idx < sge->nr_cap_states; idx++) {
Chris Redpath185507e2017-10-25 17:25:20 +01005611 if (sge->cap_states[idx].cap >= util) {
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005612 /* Keep track of SG's capacity */
5613 eenv->cpu[cpu_idx].cap_idx = idx;
5614 eenv->cpu[cpu_idx].cap = sge->cap_states[idx].cap;
Morten Rasmussena455fa72015-01-02 14:21:56 +00005615 break;
Chris Redpath185507e2017-10-25 17:25:20 +01005616 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005617 }
5618
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005619 return eenv->cpu[cpu_idx].cap_idx;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005620}
5621
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005622static int group_idle_state(struct energy_env *eenv, int cpu_idx)
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005623{
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005624 struct sched_group *sg = eenv->sg;
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005625 int i, state = INT_MAX;
Chris Redpathda03fc12017-07-04 10:23:03 +01005626 int src_in_grp, dst_in_grp;
5627 long grp_util = 0;
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005628
5629 /* Find the shallowest idle state in the sched group. */
5630 for_each_cpu(i, sched_group_cpus(sg))
5631 state = min(state, idle_get_state_idx(cpu_rq(i)));
5632
5633 /* Take non-cpuidle idling into account (active idle/arch_cpu_idle()) */
5634 state++;
5635
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005636 src_in_grp = cpumask_test_cpu(eenv->cpu[EAS_CPU_PRV].cpu_id,
5637 sched_group_cpus(sg));
5638 dst_in_grp = cpumask_test_cpu(eenv->cpu[cpu_idx].cpu_id,
5639 sched_group_cpus(sg));
Chris Redpathda03fc12017-07-04 10:23:03 +01005640 if (src_in_grp == dst_in_grp) {
5641 /* both CPUs under consideration are in the same group or not in
5642 * either group, migration should leave idle state the same.
5643 */
5644 goto end;
5645 }
Ke Wang225006a2017-11-01 16:07:38 +08005646
5647 /*
5648 * Try to estimate if a deeper idle state is
5649 * achievable when we move the task.
Chris Redpathda03fc12017-07-04 10:23:03 +01005650 */
Ke Wang225006a2017-11-01 16:07:38 +08005651 for_each_cpu(i, sched_group_cpus(sg)) {
Chris Redpathe997bf02017-09-12 14:48:29 +01005652 grp_util += cpu_util_wake(i, eenv->p);
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005653 if (unlikely(i == eenv->cpu[cpu_idx].cpu_id))
Ke Wang225006a2017-11-01 16:07:38 +08005654 grp_util += eenv->util_delta;
5655 }
Chris Redpathda03fc12017-07-04 10:23:03 +01005656
5657 if (grp_util <=
5658 ((long)sg->sgc->max_capacity * (int)sg->group_weight)) {
5659 /* after moving, this group is at most partly
5660 * occupied, so it should have some idle time.
5661 */
5662 int max_idle_state_idx = sg->sge->nr_idle_states - 2;
5663 int new_state = grp_util * max_idle_state_idx;
5664 if (grp_util <= 0)
5665 /* group will have no util, use lowest state */
5666 new_state = max_idle_state_idx + 1;
5667 else {
5668 /* for partially idle, linearly map util to idle
5669 * states, excluding the lowest one. This does not
5670 * correspond to the state we expect to enter in
5671 * reality, but an indication of what might happen.
5672 */
5673 new_state = min(max_idle_state_idx, (int)
5674 (new_state / sg->sgc->max_capacity));
5675 new_state = max_idle_state_idx - new_state;
5676 }
5677 state = new_state;
5678 } else {
5679 /* After moving, the group will be fully occupied
5680 * so assume it will not be idle at all.
5681 */
5682 state = 0;
5683 }
5684end:
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005685 return state;
5686}
5687
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005688/*
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005689 * calc_sg_energy: compute energy for the eenv's SG (i.e. eenv->sg).
5690 *
5691 * This works in iterations to compute the SG's energy for each CPU
5692 * candidate defined by the energy_env's cpu array.
Patrick Bellasi3b9305d2017-07-31 11:21:37 +01005693 *
5694 * NOTE: in the following computations for busy_energy and idle_energy we do
5695 * not shift by SCHED_CAPACITY_SHIFT in order to reduce rounding errors.
5696 * The required scaling will be performed just one time, by the calling
5697 * functions, once we accumulated the contributons for all the SGs.
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005698 */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005699static void calc_sg_energy(struct energy_env *eenv)
5700{
5701 struct sched_group *sg = eenv->sg;
5702 int busy_energy, idle_energy;
5703 unsigned int busy_power;
5704 unsigned int idle_power;
5705 unsigned long sg_util;
5706 int cap_idx, idle_idx;
5707 int total_energy = 0;
5708 int cpu_idx;
5709
5710 for (cpu_idx = EAS_CPU_PRV; cpu_idx < EAS_CPU_CNT; ++cpu_idx) {
5711
5712
5713 if (eenv->cpu[cpu_idx].cpu_id == -1)
5714 continue;
5715 /* Compute ACTIVE energy */
5716 cap_idx = find_new_capacity(eenv, cpu_idx);
5717 busy_power = sg->sge->cap_states[cap_idx].power;
5718 /*
5719 * in order to calculate cpu_norm_util, we need to know which
5720 * capacity level the group will be at, so calculate that first
5721 */
5722 sg_util = group_norm_util(eenv, cpu_idx);
5723
5724 busy_energy = sg_util * busy_power;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005725
5726 /* Compute IDLE energy */
5727 idle_idx = group_idle_state(eenv, cpu_idx);
5728 idle_power = sg->sge->idle_states[idle_idx].power;
5729
5730 idle_energy = SCHED_CAPACITY_SCALE - sg_util;
5731 idle_energy *= idle_power;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005732
5733 total_energy = busy_energy + idle_energy;
5734 eenv->cpu[cpu_idx].energy += total_energy;
5735 }
5736}
5737
5738/*
5739 * compute_energy() computes the absolute variation in energy consumption by
5740 * moving eenv.util_delta from EAS_CPU_PRV to EAS_CPU_NXT.
5741 *
5742 * NOTE: compute_energy() may fail when racing with sched_domain updates, in
5743 * which case we abort by returning -EINVAL.
5744 */
5745static int compute_energy(struct energy_env *eenv)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005746{
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005747 struct cpumask visit_cpus;
Chris Redpath8a174b472018-01-24 09:25:24 +00005748 int cpu_count;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005749
Morten Rasmussena455fa72015-01-02 14:21:56 +00005750 WARN_ON(!eenv->sg_top->sge);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005751
Morten Rasmussena455fa72015-01-02 14:21:56 +00005752 cpumask_copy(&visit_cpus, sched_group_cpus(eenv->sg_top));
Chris Redpath8a174b472018-01-24 09:25:24 +00005753 /* If a cpu is hotplugged in while we are in this function,
5754 * it does not appear in the existing visit_cpus mask
5755 * which came from the sched_group pointer of the
5756 * sched_domain pointed at by sd_ea for either the prev
5757 * or next cpu and was dereferenced in __energy_diff.
5758 * Since we will dereference sd_scs later as we iterate
5759 * through the CPUs we expect to visit, new CPUs can
5760 * be present which are not in the visit_cpus mask.
5761 * Guard this with cpu_count.
5762 */
5763 cpu_count = cpumask_weight(&visit_cpus);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005764
5765 while (!cpumask_empty(&visit_cpus)) {
5766 struct sched_group *sg_shared_cap = NULL;
Joonwoo Parkd7a6b8b2017-07-24 13:23:05 +01005767 int cpu = cpumask_first(&visit_cpus);
5768 struct sched_domain *sd;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005769
5770 /*
5771 * Is the group utilization affected by cpus outside this
5772 * sched_group?
Chris Redpath8a174b472018-01-24 09:25:24 +00005773 * This sd may have groups with cpus which were not present
5774 * when we took visit_cpus.
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005775 */
5776 sd = rcu_dereference(per_cpu(sd_scs, cpu));
Morten Rasmussene4188502017-02-06 16:28:53 +00005777 if (sd && sd->parent)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005778 sg_shared_cap = sd->parent->groups;
5779
5780 for_each_domain(cpu, sd) {
Joonwoo Parkd7a6b8b2017-07-24 13:23:05 +01005781 struct sched_group *sg = sd->groups;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005782
5783 /* Has this sched_domain already been visited? */
5784 if (sd->child && group_first_cpu(sg) != cpu)
5785 break;
5786
5787 do {
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005788 eenv->sg_cap = sg;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005789 if (sg_shared_cap && sg_shared_cap->group_weight >= sg->group_weight)
Morten Rasmussena455fa72015-01-02 14:21:56 +00005790 eenv->sg_cap = sg_shared_cap;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005791
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005792 /*
5793 * Compute the energy for all the candidate
5794 * CPUs in the current visited SG.
5795 */
5796 eenv->sg = sg;
5797 calc_sg_energy(eenv);
Chris Redpathda03fc12017-07-04 10:23:03 +01005798
Chris Redpath8a174b472018-01-24 09:25:24 +00005799 if (!sd->child) {
5800 /*
5801 * cpu_count here is the number of
5802 * cpus we expect to visit in this
5803 * calculation. If we race against
5804 * hotplug, we can have extra cpus
5805 * added to the groups we are
5806 * iterating which do not appear in
5807 * the visit_cpus mask. In that case
5808 * we are not able to calculate energy
5809 * without restarting so we will bail
5810 * out and use prev_cpu this time.
5811 */
5812 if (!cpu_count)
5813 return -EINVAL;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005814 cpumask_xor(&visit_cpus, &visit_cpus, sched_group_cpus(sg));
Chris Redpath8a174b472018-01-24 09:25:24 +00005815 cpu_count--;
5816 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005817
Morten Rasmussena455fa72015-01-02 14:21:56 +00005818 if (cpumask_equal(sched_group_cpus(sg), sched_group_cpus(eenv->sg_top)))
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005819 goto next_cpu;
5820
5821 } while (sg = sg->next, sg != sd->groups);
5822 }
Morten Rasmussene4188502017-02-06 16:28:53 +00005823
5824 /*
5825 * If we raced with hotplug and got an sd NULL-pointer;
5826 * returning a wrong energy estimation is better than
5827 * entering an infinite loop.
Chris Redpath8a174b472018-01-24 09:25:24 +00005828 * Specifically: If a cpu is unplugged after we took
5829 * the visit_cpus mask, it no longer has an sd_scs
5830 * pointer, so when we dereference it, we get NULL.
Morten Rasmussene4188502017-02-06 16:28:53 +00005831 */
5832 if (cpumask_test_cpu(cpu, &visit_cpus))
5833 return -EINVAL;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005834next_cpu:
Todd Kjos64f6fd12016-06-16 16:33:54 -07005835 cpumask_clear_cpu(cpu, &visit_cpus);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005836 continue;
5837 }
5838
Morten Rasmussena455fa72015-01-02 14:21:56 +00005839 return 0;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005840}
5841
Morten Rasmussen931bd822015-01-06 17:34:05 +00005842static inline bool cpu_in_sg(struct sched_group *sg, int cpu)
5843{
5844 return cpu != -1 && cpumask_test_cpu(cpu, sched_group_cpus(sg));
5845}
5846
5847/*
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005848 * select_energy_cpu_idx(): estimate the energy impact of changing the
5849 * utilization distribution.
5850 *
5851 * The eenv parameter specifies the changes: utilisation amount and a pair of
5852 * possible CPU candidates (the previous CPU and a different target CPU).
5853 *
5854 * This function returns the index of a CPU candidate specified by the
5855 * energy_env which corresponds to the first CPU saving energy.
5856 * Thus, 0 (EAS_CPU_PRV) means that non of the CPU candidate is more energy
5857 * efficient than running on prev_cpu. This is also the value returned in case
5858 * of abort due to error conditions during the computations.
5859 * A value greater than zero means that the first energy-efficient CPU is the
5860 * one represented by eenv->cpu[eenv->next_idx].cpu_id.
Morten Rasmussen931bd822015-01-06 17:34:05 +00005861 */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005862static inline int select_energy_cpu_idx(struct energy_env *eenv)
Morten Rasmussen931bd822015-01-06 17:34:05 +00005863{
5864 struct sched_domain *sd;
5865 struct sched_group *sg;
Patrick Bellasi7f44e922017-09-12 14:57:51 +01005866 int sd_cpu = -1;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005867 int cpu_idx;
Patrick Bellasi7f44e922017-09-12 14:57:51 +01005868 int margin;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005869
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005870 sd_cpu = eenv->cpu[EAS_CPU_PRV].cpu_id;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005871 sd = rcu_dereference(per_cpu(sd_ea, sd_cpu));
Morten Rasmussen931bd822015-01-06 17:34:05 +00005872 if (!sd)
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005873 return EAS_CPU_PRV;
5874
5875 cpumask_clear(&eenv->cpus_mask);
5876 for (cpu_idx = EAS_CPU_PRV; cpu_idx < EAS_CPU_CNT; ++cpu_idx) {
5877 int cpu = eenv->cpu[cpu_idx].cpu_id;
5878
5879 if (cpu < 0)
5880 continue;
5881 cpumask_set_cpu(cpu, &eenv->cpus_mask);
5882 }
Morten Rasmussen931bd822015-01-06 17:34:05 +00005883
5884 sg = sd->groups;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005885 do {
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005886 /* Skip SGs which do not contains a candidate CPU */
5887 if (!cpumask_intersects(&eenv->cpus_mask, sched_group_cpus(sg)))
5888 continue;
5889
5890 eenv->sg_top = sg;
Patrick Bellasi3b9305d2017-07-31 11:21:37 +01005891 /* energy is unscaled to reduce rounding errors */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005892 if (compute_energy(eenv) == -EINVAL)
5893 return EAS_CPU_PRV;
5894
Morten Rasmussen931bd822015-01-06 17:34:05 +00005895 } while (sg = sg->next, sg != sd->groups);
Patrick Bellasi49059322017-06-28 15:59:46 +01005896
Patrick Bellasi3b9305d2017-07-31 11:21:37 +01005897 /* Scale energy before comparisons */
5898 for (cpu_idx = EAS_CPU_PRV; cpu_idx < EAS_CPU_CNT; ++cpu_idx)
5899 eenv->cpu[cpu_idx].energy >>= SCHED_CAPACITY_SHIFT;
5900
Morten Rasmussen53838922016-03-30 14:20:12 +01005901 /*
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005902 * Compute the dead-zone margin used to prevent too many task
5903 * migrations with negligible energy savings.
5904 * An energy saving is considered meaningful if it reduces the energy
5905 * consumption of EAS_CPU_PRV CPU candidate by at least ~1.56%
Morten Rasmussen53838922016-03-30 14:20:12 +01005906 */
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005907 margin = eenv->cpu[EAS_CPU_PRV].energy >> 6;
Morten Rasmussen53838922016-03-30 14:20:12 +01005908
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005909 /*
5910 * By default the EAS_CPU_PRV CPU is considered the most energy
5911 * efficient, with a 0 energy variation.
5912 */
5913 eenv->next_idx = EAS_CPU_PRV;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005914
5915 /*
5916 * Compare the other CPU candidates to find a CPU which can be
5917 * more energy efficient then EAS_CPU_PRV
5918 */
5919 for (cpu_idx = EAS_CPU_NXT; cpu_idx < EAS_CPU_CNT; ++cpu_idx) {
5920 /* Skip not valid scheduled candidates */
5921 if (eenv->cpu[cpu_idx].cpu_id < 0)
5922 continue;
5923 /* Compute energy delta wrt EAS_CPU_PRV */
5924 eenv->cpu[cpu_idx].nrg_delta =
5925 eenv->cpu[cpu_idx].energy -
5926 eenv->cpu[EAS_CPU_PRV].energy;
5927 /* filter energy variations within the dead-zone margin */
5928 if (abs(eenv->cpu[cpu_idx].nrg_delta) < margin)
5929 eenv->cpu[cpu_idx].nrg_delta = 0;
5930 /* update the schedule candidate with min(nrg_delta) */
5931 if (eenv->cpu[cpu_idx].nrg_delta <
5932 eenv->cpu[eenv->next_idx].nrg_delta) {
5933 eenv->next_idx = cpu_idx;
Quentin Perretb5ba5692017-12-11 14:56:12 +00005934 if (sched_feat(FBT_STRICT_ORDER))
5935 break;
Patrick Bellasicf28cf02017-07-05 10:59:59 +01005936 }
5937 }
5938
5939 return eenv->next_idx;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005940}
5941
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005942/*
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005943 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005944 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005945 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005946 * at a frequency roughly N times higher than one of its wakees.
5947 *
5948 * In order to determine whether we should let the load spread vs consolidating
5949 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
5950 * partner, and a factor of lls_size higher frequency in the other.
5951 *
5952 * With both conditions met, we can be relatively sure that the relationship is
5953 * non-monogamous, with partner count exceeding socket size.
5954 *
5955 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
5956 * whatever is irrelevant, spread criteria is apparent partner count exceeds
5957 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005958 */
Michael Wang62470412013-07-04 12:55:51 +08005959static int wake_wide(struct task_struct *p)
5960{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005961 unsigned int master = current->wakee_flips;
5962 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005963 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08005964
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005965 if (master < slave)
5966 swap(master, slave);
5967 if (slave < factor || master < slave * factor)
5968 return 0;
5969 return 1;
Michael Wang62470412013-07-04 12:55:51 +08005970}
5971
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005972static int wake_affine(struct sched_domain *sd, struct task_struct *p,
5973 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005974{
Paul Turnere37b6a72011-01-21 20:44:59 -08005975 s64 this_load, load;
Vincent Guittotbd61c982014-08-26 13:06:50 +02005976 s64 this_eff_load, prev_eff_load;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005977 int idx, this_cpu;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005978 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02005979 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005980 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005981
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005982 idx = sd->wake_idx;
5983 this_cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005984 load = source_load(prev_cpu, idx);
5985 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005986
5987 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005988 * If sync wakeup then subtract the (maximum possible)
5989 * effect of the currently running task from the load
5990 * of the current CPU:
5991 */
Peter Zijlstra83378262008-06-27 13:41:37 +02005992 if (sync) {
5993 tg = task_group(current);
Yuyang Du9d89c252015-07-15 08:04:37 +08005994 weight = current->se.avg.load_avg;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005995
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005996 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02005997 load += effective_load(tg, prev_cpu, 0, -weight);
5998 }
5999
6000 tg = task_group(p);
Yuyang Du9d89c252015-07-15 08:04:37 +08006001 weight = p->se.avg.load_avg;
Peter Zijlstra83378262008-06-27 13:41:37 +02006002
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02006003 /*
6004 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006005 * due to the sync cause above having dropped this_load to 0, we'll
6006 * always have an imbalance, but there's really nothing you can do
6007 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02006008 *
6009 * Otherwise check if either cpus are near enough in load to allow this
6010 * task to be woken on this_cpu.
6011 */
Vincent Guittotbd61c982014-08-26 13:06:50 +02006012 this_eff_load = 100;
6013 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006014
Vincent Guittotbd61c982014-08-26 13:06:50 +02006015 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
6016 prev_eff_load *= capacity_of(this_cpu);
6017
6018 if (this_load > 0) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006019 this_eff_load *= this_load +
6020 effective_load(tg, this_cpu, weight, weight);
6021
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006022 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
Vincent Guittotbd61c982014-08-26 13:06:50 +02006023 }
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006024
Vincent Guittotbd61c982014-08-26 13:06:50 +02006025 balanced = this_eff_load <= prev_eff_load;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02006026
Josh Poimboeufae928822016-06-17 12:43:24 -05006027 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02006028
Vincent Guittot05bfb652014-08-26 13:06:45 +02006029 if (!balanced)
6030 return 0;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006031
Josh Poimboeufae928822016-06-17 12:43:24 -05006032 schedstat_inc(sd->ttwu_move_affine);
6033 schedstat_inc(p->se.statistics.nr_wakeups_affine);
Vincent Guittot05bfb652014-08-26 13:06:45 +02006034
6035 return 1;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006036}
6037
Quentin Perret7fb3e0c2017-06-07 17:40:30 +01006038static inline unsigned long task_util(struct task_struct *p)
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006039{
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07006040#ifdef CONFIG_SCHED_WALT
6041 if (!walt_disabled && sysctl_sched_use_walt_task_util) {
6042 unsigned long demand = p->ravg.demand;
Quentin Perret904c79c2017-08-30 16:54:44 +01006043 return (demand << SCHED_CAPACITY_SHIFT) / walt_ravg_window;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07006044 }
6045#endif
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006046 return p->se.avg.util_avg;
6047}
6048
Chris Redpathe997bf02017-09-12 14:48:29 +01006049static inline unsigned long boosted_task_util(struct task_struct *p);
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006050
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006051static inline bool __task_fits(struct task_struct *p, int cpu, int util)
6052{
6053 unsigned long capacity = capacity_of(cpu);
6054
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006055 util += boosted_task_util(p);
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006056
6057 return (capacity * 1024) > (util * capacity_margin);
6058}
6059
6060static inline bool task_fits_max(struct task_struct *p, int cpu)
6061{
6062 unsigned long capacity = capacity_of(cpu);
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01006063 unsigned long max_capacity = cpu_rq(cpu)->rd->max_cpu_capacity.val;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006064
6065 if (capacity == max_capacity)
6066 return true;
6067
6068 if (capacity * capacity_margin > max_capacity * 1024)
6069 return true;
6070
6071 return __task_fits(p, cpu, 0);
6072}
6073
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08006074static bool __cpu_overutilized(int cpu, int delta)
6075{
6076 return (capacity_of(cpu) * 1024) < ((cpu_util(cpu) + delta) * capacity_margin);
6077}
6078
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00006079static bool cpu_overutilized(int cpu)
6080{
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08006081 return __cpu_overutilized(cpu, 0);
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00006082}
6083
Patrick Bellasib08685b2015-06-22 18:32:36 +01006084#ifdef CONFIG_SCHED_TUNE
6085
Patrick Bellasid8460c72016-10-13 17:31:24 +01006086struct reciprocal_value schedtune_spc_rdiv;
6087
Srinath Sridharane71c4252016-07-28 17:28:55 +01006088static long
6089schedtune_margin(unsigned long signal, long boost)
Patrick Bellasib08685b2015-06-22 18:32:36 +01006090{
Srinath Sridharane71c4252016-07-28 17:28:55 +01006091 long long margin = 0;
Patrick Bellasib08685b2015-06-22 18:32:36 +01006092
6093 /*
6094 * Signal proportional compensation (SPC)
6095 *
6096 * The Boost (B) value is used to compute a Margin (M) which is
6097 * proportional to the complement of the original Signal (S):
Patrick Bellasid8460c72016-10-13 17:31:24 +01006098 * M = B * (SCHED_CAPACITY_SCALE - S)
Patrick Bellasib08685b2015-06-22 18:32:36 +01006099 * The obtained M could be used by the caller to "boost" S.
6100 */
Srinath Sridharane71c4252016-07-28 17:28:55 +01006101 if (boost >= 0) {
6102 margin = SCHED_CAPACITY_SCALE - signal;
6103 margin *= boost;
6104 } else
6105 margin = -signal * boost;
Patrick Bellasid8460c72016-10-13 17:31:24 +01006106
6107 margin = reciprocal_divide(margin, schedtune_spc_rdiv);
Patrick Bellasib08685b2015-06-22 18:32:36 +01006108
Srinath Sridharane71c4252016-07-28 17:28:55 +01006109 if (boost < 0)
6110 margin *= -1;
Patrick Bellasib08685b2015-06-22 18:32:36 +01006111 return margin;
6112}
6113
Srinath Sridharane71c4252016-07-28 17:28:55 +01006114static inline int
Patrick Bellasiedd28d32015-07-07 15:33:20 +01006115schedtune_cpu_margin(unsigned long util, int cpu)
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006116{
Patrick Bellasia33034d2016-07-28 17:42:36 +01006117 int boost = schedtune_cpu_boost(cpu);
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006118
6119 if (boost == 0)
6120 return 0;
6121
6122 return schedtune_margin(util, boost);
6123}
6124
Srinath Sridharane71c4252016-07-28 17:28:55 +01006125static inline long
Chris Redpathe997bf02017-09-12 14:48:29 +01006126schedtune_task_margin(struct task_struct *p)
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006127{
Chris Redpathe997bf02017-09-12 14:48:29 +01006128 int boost = schedtune_task_boost(p);
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006129 unsigned long util;
Srinath Sridharane71c4252016-07-28 17:28:55 +01006130 long margin;
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006131
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006132 if (boost == 0)
6133 return 0;
6134
Chris Redpathe997bf02017-09-12 14:48:29 +01006135 util = task_util(p);
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006136 margin = schedtune_margin(util, boost);
6137
6138 return margin;
6139}
6140
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006141#else /* CONFIG_SCHED_TUNE */
6142
Srinath Sridharane71c4252016-07-28 17:28:55 +01006143static inline int
Patrick Bellasiedd28d32015-07-07 15:33:20 +01006144schedtune_cpu_margin(unsigned long util, int cpu)
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006145{
6146 return 0;
6147}
6148
Srinath Sridharane71c4252016-07-28 17:28:55 +01006149static inline int
Chris Redpathe997bf02017-09-12 14:48:29 +01006150schedtune_task_margin(struct task_struct *p)
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006151{
6152 return 0;
6153}
6154
Patrick Bellasib08685b2015-06-22 18:32:36 +01006155#endif /* CONFIG_SCHED_TUNE */
6156
Juri Lellic6e94382016-12-14 16:10:10 +00006157unsigned long
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006158boosted_cpu_util(int cpu)
6159{
Joonwoo Park8b34bba2016-12-08 16:12:12 -08006160 unsigned long util = cpu_util_freq(cpu);
Srinath Sridharane71c4252016-07-28 17:28:55 +01006161 long margin = schedtune_cpu_margin(util, cpu);
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006162
Patrick Bellasicccead12015-06-22 13:51:07 +01006163 trace_sched_boost_cpu(cpu, util, margin);
6164
Patrick Bellasicaa24e42015-06-26 09:55:06 +01006165 return util + margin;
6166}
6167
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006168static inline unsigned long
Chris Redpathe997bf02017-09-12 14:48:29 +01006169boosted_task_util(struct task_struct *p)
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006170{
Chris Redpathe997bf02017-09-12 14:48:29 +01006171 unsigned long util = task_util(p);
6172 long margin = schedtune_task_margin(p);
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006173
Chris Redpathe997bf02017-09-12 14:48:29 +01006174 trace_sched_boost_task(p, util, margin);
Patrick Bellasiecccdb72016-01-14 18:43:37 +00006175
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006176 return util + margin;
6177}
6178
Morten Rasmussen54444352016-10-14 14:41:08 +01006179static unsigned long capacity_spare_wake(int cpu, struct task_struct *p)
6180{
6181 return capacity_orig_of(cpu) - cpu_util_wake(cpu, p);
6182}
6183
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006184/*
6185 * find_idlest_group finds and returns the least busy CPU group within the
6186 * domain.
Brendan Jackmana8c3c112017-10-05 12:45:15 +01006187 *
6188 * Assumes p is allowed on at least one CPU in sd.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006189 */
6190static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02006191find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006192 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01006193{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07006194 struct sched_group *idlest = NULL, *group = sd->groups;
Morten Rasmussen54444352016-10-14 14:41:08 +01006195 struct sched_group *most_spare_sg = NULL;
Brendan Jackman13c4d4f2017-10-05 12:45:14 +01006196 unsigned long min_runnable_load = ULONG_MAX;
6197 unsigned long this_runnable_load = ULONG_MAX;
6198 unsigned long min_avg_load = ULONG_MAX, this_avg_load = ULONG_MAX;
Morten Rasmussen54444352016-10-14 14:41:08 +01006199 unsigned long most_spare = 0, this_spare = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006200 int load_idx = sd->forkexec_idx;
Vincent Guittot08c53a62016-12-08 17:56:54 +01006201 int imbalance_scale = 100 + (sd->imbalance_pct-100)/2;
6202 unsigned long imbalance = scale_load_down(NICE_0_LOAD) *
6203 (sd->imbalance_pct-100) / 100;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006204
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006205 if (sd_flag & SD_BALANCE_WAKE)
6206 load_idx = sd->wake_idx;
6207
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006208 do {
Vincent Guittot08c53a62016-12-08 17:56:54 +01006209 unsigned long load, avg_load, runnable_load;
6210 unsigned long spare_cap, max_spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006211 int local_group;
6212 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006213
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006214 /* Skip over this group if it has no CPUs allowed */
6215 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006216 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006217 continue;
6218
6219 local_group = cpumask_test_cpu(this_cpu,
6220 sched_group_cpus(group));
6221
Morten Rasmussen54444352016-10-14 14:41:08 +01006222 /*
6223 * Tally up the load of all CPUs in the group and find
6224 * the group containing the CPU with most spare capacity.
6225 */
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006226 avg_load = 0;
Vincent Guittot08c53a62016-12-08 17:56:54 +01006227 runnable_load = 0;
Morten Rasmussen54444352016-10-14 14:41:08 +01006228 max_spare_cap = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006229
6230 for_each_cpu(i, sched_group_cpus(group)) {
6231 /* Bias balancing toward cpus of our domain */
6232 if (local_group)
6233 load = source_load(i, load_idx);
6234 else
6235 load = target_load(i, load_idx);
6236
Vincent Guittot08c53a62016-12-08 17:56:54 +01006237 runnable_load += load;
6238
6239 avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs);
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01006240
Morten Rasmussen54444352016-10-14 14:41:08 +01006241 spare_cap = capacity_spare_wake(i, p);
6242
6243 if (spare_cap > max_spare_cap)
6244 max_spare_cap = spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006245 }
6246
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006247 /* Adjust by relative CPU capacity of the group */
Vincent Guittot08c53a62016-12-08 17:56:54 +01006248 avg_load = (avg_load * SCHED_CAPACITY_SCALE) /
6249 group->sgc->capacity;
6250 runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) /
6251 group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006252
6253 if (local_group) {
Vincent Guittot08c53a62016-12-08 17:56:54 +01006254 this_runnable_load = runnable_load;
6255 this_avg_load = avg_load;
Morten Rasmussen54444352016-10-14 14:41:08 +01006256 this_spare = max_spare_cap;
6257 } else {
Vincent Guittot08c53a62016-12-08 17:56:54 +01006258 if (min_runnable_load > (runnable_load + imbalance)) {
6259 /*
6260 * The runnable load is significantly smaller
6261 * so we can pick this new cpu
6262 */
6263 min_runnable_load = runnable_load;
6264 min_avg_load = avg_load;
6265 idlest = group;
6266 } else if ((runnable_load < (min_runnable_load + imbalance)) &&
6267 (100*min_avg_load > imbalance_scale*avg_load)) {
6268 /*
6269 * The runnable loads are close so we take
6270 * into account blocked load through avg_load
6271 * which is blocked + runnable load
6272 */
6273 min_avg_load = avg_load;
Morten Rasmussen54444352016-10-14 14:41:08 +01006274 idlest = group;
6275 }
6276
6277 if (most_spare < max_spare_cap) {
6278 most_spare = max_spare_cap;
6279 most_spare_sg = group;
6280 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006281 }
6282 } while (group = group->next, group != sd->groups);
6283
Morten Rasmussen54444352016-10-14 14:41:08 +01006284 /*
6285 * The cross-over point between using spare capacity or least load
6286 * is too conservative for high utilization tasks on partially
6287 * utilized systems if we require spare_capacity > task_util(p),
6288 * so we allow for some task stuffing by using
6289 * spare_capacity > task_util(p)/2.
Vincent Guittotff634002016-12-08 17:56:53 +01006290 * spare capacity can't be used for fork because the utilization has
6291 * not been set yet as it need to get a rq to init the utilization
Morten Rasmussen54444352016-10-14 14:41:08 +01006292 */
Vincent Guittotff634002016-12-08 17:56:53 +01006293 if (sd_flag & SD_BALANCE_FORK)
6294 goto skip_spare;
6295
Morten Rasmussen54444352016-10-14 14:41:08 +01006296 if (this_spare > task_util(p) / 2 &&
Vincent Guittot08c53a62016-12-08 17:56:54 +01006297 imbalance_scale*this_spare > 100*most_spare)
Morten Rasmussen54444352016-10-14 14:41:08 +01006298 return NULL;
6299 else if (most_spare > task_util(p) / 2)
6300 return most_spare_sg;
6301
Vincent Guittotff634002016-12-08 17:56:53 +01006302skip_spare:
Vincent Guittot08c53a62016-12-08 17:56:54 +01006303 if (!idlest ||
6304 (min_runnable_load > (this_runnable_load + imbalance)) ||
6305 ((this_runnable_load < (min_runnable_load + imbalance)) &&
6306 (100*this_avg_load < imbalance_scale*min_avg_load)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006307 return NULL;
6308 return idlest;
6309}
6310
6311/*
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006312 * find_idlest_group_cpu - find the idlest cpu among the cpus in group.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006313 */
6314static int
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006315find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006316{
6317 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006318 unsigned int min_exit_latency = UINT_MAX;
6319 u64 latest_idle_timestamp = 0;
6320 int least_loaded_cpu = this_cpu;
6321 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006322 int i;
6323
Morten Rasmusseneaecf412016-06-22 18:03:14 +01006324 /* Check if we have any choice: */
6325 if (group->group_weight == 1)
6326 return cpumask_first(sched_group_cpus(group));
6327
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006328 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006329 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Quentin Perret7fb3e0c2017-06-07 17:40:30 +01006330 if (idle_cpu(i)) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006331 struct rq *rq = cpu_rq(i);
6332 struct cpuidle_state *idle = idle_get_state(rq);
6333 if (idle && idle->exit_latency < min_exit_latency) {
6334 /*
6335 * We give priority to a CPU whose idle state
6336 * has the smallest exit latency irrespective
6337 * of any idle timestamp.
6338 */
6339 min_exit_latency = idle->exit_latency;
6340 latest_idle_timestamp = rq->idle_stamp;
6341 shallowest_idle_cpu = i;
Quentin Perret7fb3e0c2017-06-07 17:40:30 +01006342 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006343 rq->idle_stamp > latest_idle_timestamp) {
6344 /*
6345 * If equal or no active idle state, then
6346 * the most recently idled CPU might have
6347 * a warmer cache.
6348 */
6349 latest_idle_timestamp = rq->idle_stamp;
6350 shallowest_idle_cpu = i;
6351 }
Yao Dongdong9f967422014-10-28 04:08:06 +00006352 } else if (shallowest_idle_cpu == -1) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006353 load = weighted_cpuload(i);
6354 if (load < min_load || (load == min_load && i == this_cpu)) {
6355 min_load = load;
6356 least_loaded_cpu = i;
6357 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01006358 }
6359 }
6360
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006361 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006362}
Gregory Haskinse7693a32008-01-25 21:08:09 +01006363
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006364static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *p,
6365 int cpu, int prev_cpu, int sd_flag)
6366{
6367 int wu = sd_flag & SD_BALANCE_WAKE;
6368 int cas_cpu = -1;
Brendan Jackman55f81c12017-10-05 12:45:16 +01006369 int new_cpu = cpu;
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006370
6371 if (wu) {
6372 schedstat_inc(p->se.statistics.nr_wakeups_cas_attempts);
6373 schedstat_inc(this_rq()->eas_stats.cas_attempts);
6374 }
6375
Brendan Jackmana8c3c112017-10-05 12:45:15 +01006376 if (!cpumask_intersects(sched_domain_span(sd), &p->cpus_allowed))
6377 return prev_cpu;
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006378
6379 while (sd) {
6380 struct sched_group *group;
6381 struct sched_domain *tmp;
6382 int weight;
6383
6384 if (wu)
6385 schedstat_inc(sd->eas_stats.cas_attempts);
6386
6387 if (!(sd->flags & sd_flag)) {
6388 sd = sd->child;
6389 continue;
6390 }
6391
6392 group = find_idlest_group(sd, p, cpu, sd_flag);
6393 if (!group) {
6394 sd = sd->child;
6395 continue;
6396 }
6397
6398 new_cpu = find_idlest_group_cpu(group, p, cpu);
Brendan Jackmanda6485c2017-10-05 12:45:13 +01006399 if (new_cpu == cpu) {
Brendan Jackman6cc75c92017-10-05 12:45:12 +01006400 /* Now try balancing at a lower domain level of cpu */
6401 sd = sd->child;
6402 continue;
6403 }
6404
6405 /* Now try balancing at a lower domain level of new_cpu */
6406 cpu = cas_cpu = new_cpu;
6407 weight = sd->span_weight;
6408 sd = NULL;
6409 for_each_domain(cpu, tmp) {
6410 if (weight <= tmp->span_weight)
6411 break;
6412 if (tmp->flags & sd_flag)
6413 sd = tmp;
6414 }
6415 /* while loop will break here if sd == NULL */
6416 }
6417
6418 if (wu && (cas_cpu >= 0)) {
6419 schedstat_inc(p->se.statistics.nr_wakeups_cas_count);
6420 schedstat_inc(this_rq()->eas_stats.cas_count);
6421 }
6422
6423 return new_cpu;
6424}
6425
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006426#ifdef CONFIG_SCHED_SMT
6427
6428static inline void set_idle_cores(int cpu, int val)
6429{
6430 struct sched_domain_shared *sds;
6431
6432 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
6433 if (sds)
6434 WRITE_ONCE(sds->has_idle_cores, val);
6435}
6436
6437static inline bool test_idle_cores(int cpu, bool def)
6438{
6439 struct sched_domain_shared *sds;
6440
6441 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
6442 if (sds)
6443 return READ_ONCE(sds->has_idle_cores);
6444
6445 return def;
6446}
6447
6448/*
6449 * Scans the local SMT mask to see if the entire core is idle, and records this
6450 * information in sd_llc_shared->has_idle_cores.
6451 *
6452 * Since SMT siblings share all cache levels, inspecting this limited remote
6453 * state should be fairly cheap.
6454 */
Greg Kroah-Hartmana0a93e32017-07-19 09:58:49 +02006455void update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006456{
6457 int core = cpu_of(rq);
6458 int cpu;
6459
6460 rcu_read_lock();
6461 if (test_idle_cores(core, true))
6462 goto unlock;
6463
6464 for_each_cpu(cpu, cpu_smt_mask(core)) {
6465 if (cpu == core)
6466 continue;
6467
6468 if (!idle_cpu(cpu))
6469 goto unlock;
6470 }
6471
6472 set_idle_cores(core, 1);
6473unlock:
6474 rcu_read_unlock();
6475}
6476
6477/*
6478 * Scan the entire LLC domain for idle cores; this dynamically switches off if
6479 * there are no idle cores left in the system; tracked through
6480 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
6481 */
6482static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6483{
6484 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
Peter Zijlstra542ebc92017-04-14 14:20:05 +02006485 int core, cpu;
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006486
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006487 if (!test_idle_cores(target, false))
6488 return -1;
6489
6490 cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p));
6491
Peter Zijlstra542ebc92017-04-14 14:20:05 +02006492 for_each_cpu_wrap(core, cpus, target) {
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006493 bool idle = true;
6494
6495 for_each_cpu(cpu, cpu_smt_mask(core)) {
6496 cpumask_clear_cpu(cpu, cpus);
6497 if (!idle_cpu(cpu))
6498 idle = false;
6499 }
6500
6501 if (idle)
6502 return core;
6503 }
6504
6505 /*
6506 * Failed to find an idle core; stop looking for one.
6507 */
6508 set_idle_cores(target, 0);
6509
6510 return -1;
6511}
6512
6513/*
6514 * Scan the local SMT mask for idle CPUs.
6515 */
6516static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6517{
6518 int cpu;
6519
6520 for_each_cpu(cpu, cpu_smt_mask(target)) {
6521 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6522 continue;
6523 if (idle_cpu(cpu))
6524 return cpu;
6525 }
6526
6527 return -1;
6528}
6529
6530#else /* CONFIG_SCHED_SMT */
6531
6532static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6533{
6534 return -1;
6535}
6536
6537static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6538{
6539 return -1;
6540}
6541
6542#endif /* CONFIG_SCHED_SMT */
6543
6544/*
6545 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
6546 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6547 * average idle time for this rq (as found in rq->avg_idle).
6548 */
6549static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
6550{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006551 struct sched_domain *this_sd;
6552 u64 avg_cost, avg_idle = this_rq()->avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006553 u64 time, cost;
6554 s64 delta;
Peter Zijlstra542ebc92017-04-14 14:20:05 +02006555 int cpu;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006556
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006557 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
6558 if (!this_sd)
6559 return -1;
6560
6561 avg_cost = this_sd->avg_scan_cost;
6562
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006563 /*
6564 * Due to large variance we need a large fuzz factor; hackbench in
6565 * particularly is sensitive here.
6566 */
Peter Zijlstra4e4a9eb2017-03-01 11:24:35 +01006567 if (sched_feat(SIS_AVG_CPU) && (avg_idle / 512) < avg_cost)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006568 return -1;
6569
6570 time = local_clock();
6571
Peter Zijlstra542ebc92017-04-14 14:20:05 +02006572 for_each_cpu_wrap(cpu, sched_domain_span(sd), target) {
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006573 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6574 continue;
6575 if (idle_cpu(cpu))
6576 break;
6577 }
6578
6579 time = local_clock() - time;
6580 cost = this_sd->avg_scan_cost;
6581 delta = (s64)(time - cost) / 8;
6582 this_sd->avg_scan_cost += delta;
6583
6584 return cpu;
6585}
6586
6587/*
6588 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006589 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006590static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006591{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006592 struct sched_domain *sd;
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006593 struct sched_group *sg;
6594 int i = task_cpu(p);
Dietmar Eggemann56ffdd62017-01-16 12:42:59 +00006595 int best_idle_cpu = -1;
6596 int best_idle_cstate = INT_MAX;
6597 unsigned long best_idle_capacity = ULONG_MAX;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01006598
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006599 schedstat_inc(p->se.statistics.nr_wakeups_sis_attempts);
6600 schedstat_inc(this_rq()->eas_stats.sis_attempts);
6601
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006602 if (!sysctl_sched_cstate_aware) {
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006603 if (idle_cpu(target)) {
6604 schedstat_inc(p->se.statistics.nr_wakeups_sis_idle);
6605 schedstat_inc(this_rq()->eas_stats.sis_idle);
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006606 return target;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006607 }
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006608
6609 /*
6610 * If the prevous cpu is cache affine and idle, don't be stupid.
6611 */
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006612 if (i != target && cpus_share_cache(i, target) && idle_cpu(i)) {
6613 schedstat_inc(p->se.statistics.nr_wakeups_sis_cache_affine);
6614 schedstat_inc(this_rq()->eas_stats.sis_cache_affine);
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006615 return i;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006616 }
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006617
6618 sd = rcu_dereference(per_cpu(sd_llc, target));
6619 if (!sd)
6620 return target;
6621
6622 i = select_idle_core(p, sd, target);
6623 if ((unsigned)i < nr_cpumask_bits)
6624 return i;
6625
6626 i = select_idle_cpu(p, sd, target);
6627 if ((unsigned)i < nr_cpumask_bits)
6628 return i;
6629
6630 i = select_idle_smt(p, sd, target);
6631 if ((unsigned)i < nr_cpumask_bits)
6632 return i;
6633 }
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006634
6635 /*
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006636 * Otherwise, iterate the domains and find an elegible idle cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006637 */
Peter Zijlstra518cd622011-12-07 15:07:31 +01006638 sd = rcu_dereference(per_cpu(sd_llc, target));
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006639 for_each_lower_domain(sd) {
6640 sg = sd->groups;
6641 do {
6642 if (!cpumask_intersects(sched_group_cpus(sg),
6643 tsk_cpus_allowed(p)))
6644 goto next;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006645
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006646
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006647 if (sysctl_sched_cstate_aware) {
6648 for_each_cpu_and(i, tsk_cpus_allowed(p), sched_group_cpus(sg)) {
Dietmar Eggemann56ffdd62017-01-16 12:42:59 +00006649 int idle_idx = idle_get_state_idx(cpu_rq(i));
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006650 unsigned long new_usage = boosted_task_util(p);
6651 unsigned long capacity_orig = capacity_orig_of(i);
Dietmar Eggemann56ffdd62017-01-16 12:42:59 +00006652
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006653 if (new_usage > capacity_orig || !idle_cpu(i))
6654 goto next;
Mike Galbraith970e1782012-06-12 05:18:32 +02006655
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006656 if (i == target && new_usage <= capacity_curr_of(target)) {
6657 schedstat_inc(p->se.statistics.nr_wakeups_sis_suff_cap);
6658 schedstat_inc(this_rq()->eas_stats.sis_suff_cap);
6659 schedstat_inc(sd->eas_stats.sis_suff_cap);
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006660 return target;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006661 }
Linus Torvalds37407ea2012-09-16 12:29:43 -07006662
Dietmar Eggemann56ffdd62017-01-16 12:42:59 +00006663 if (idle_idx < best_idle_cstate &&
6664 capacity_orig <= best_idle_capacity) {
6665 best_idle_cpu = i;
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006666 best_idle_cstate = idle_idx;
6667 best_idle_capacity = capacity_orig;
6668 }
6669 }
6670 } else {
6671 for_each_cpu(i, sched_group_cpus(sg)) {
6672 if (i == target || !idle_cpu(i))
6673 goto next;
6674 }
6675
6676 target = cpumask_first_and(sched_group_cpus(sg),
6677 tsk_cpus_allowed(p));
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006678 schedstat_inc(p->se.statistics.nr_wakeups_sis_idle_cpu);
6679 schedstat_inc(this_rq()->eas_stats.sis_idle_cpu);
6680 schedstat_inc(sd->eas_stats.sis_idle_cpu);
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006681 goto done;
6682 }
6683next:
6684 sg = sg->next;
6685 } while (sg != sd->groups);
6686 }
Dietmar Eggemann56ffdd62017-01-16 12:42:59 +00006687
6688 if (best_idle_cpu >= 0)
6689 target = best_idle_cpu;
Srinath Sridharanbf47bdd2016-07-14 09:57:29 +01006690
6691done:
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006692 schedstat_inc(p->se.statistics.nr_wakeups_sis_count);
6693 schedstat_inc(this_rq()->eas_stats.sis_count);
6694
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006695 return target;
6696}
Chris Redpath7de1b832017-02-28 17:27:28 +00006697
6698/*
6699 * cpu_util_wake: Compute cpu utilization with any contributions from
Joonwoo Park4f0693a2017-05-18 17:43:58 -07006700 * the waking task p removed. check_for_migration() looks for a better CPU of
6701 * rq->curr. For that case we should return cpu util with contributions from
6702 * currently running task p removed.
Chris Redpath7de1b832017-02-28 17:27:28 +00006703 */
6704static int cpu_util_wake(int cpu, struct task_struct *p)
6705{
6706 unsigned long util, capacity;
6707
6708#ifdef CONFIG_SCHED_WALT
6709 /*
6710 * WALT does not decay idle tasks in the same manner
6711 * as PELT, so it makes little sense to subtract task
6712 * utilization from cpu utilization. Instead just use
6713 * cpu_util for this case.
6714 */
Joonwoo Park4f0693a2017-05-18 17:43:58 -07006715 if (!walt_disabled && sysctl_sched_use_walt_cpu_util &&
6716 p->state == TASK_WAKING)
Chris Redpath7de1b832017-02-28 17:27:28 +00006717 return cpu_util(cpu);
6718#endif
6719 /* Task has no contribution or is new */
6720 if (cpu != task_cpu(p) || !p->se.avg.last_update_time)
6721 return cpu_util(cpu);
6722
6723 capacity = capacity_orig_of(cpu);
6724 util = max_t(long, cpu_util(cpu) - task_util(p), 0);
6725
6726 return (util >= capacity) ? capacity : util;
6727}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006728
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006729static int start_cpu(bool boosted)
6730{
6731 struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
6732
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006733 return boosted ? rd->max_cap_orig_cpu : rd->min_cap_orig_cpu;
6734}
6735
Chris Redpath7b2727c2017-07-04 10:19:58 +01006736static inline int find_best_target(struct task_struct *p, int *backup_cpu,
6737 bool boosted, bool prefer_idle)
Juri Lelli1931b932016-07-29 14:04:11 +01006738{
Patrick Bellasi56e27192017-03-29 09:01:06 +01006739 unsigned long best_idle_min_cap_orig = ULONG_MAX;
Dietmar Eggemann2dfb1722017-01-07 14:33:51 +00006740 unsigned long min_util = boosted_task_util(p);
Patrick Bellasi56e27192017-03-29 09:01:06 +01006741 unsigned long target_capacity = ULONG_MAX;
6742 unsigned long min_wake_util = ULONG_MAX;
6743 unsigned long target_max_spare_cap = 0;
6744 unsigned long target_util = ULONG_MAX;
6745 unsigned long best_active_util = ULONG_MAX;
Ionela Voinescu88a968c2017-12-07 20:09:11 +00006746 unsigned long target_idle_max_spare_cap = 0;
Patrick Bellasi56e27192017-03-29 09:01:06 +01006747 int best_idle_cstate = INT_MAX;
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006748 struct sched_domain *sd;
6749 struct sched_group *sg;
Patrick Bellasi56e27192017-03-29 09:01:06 +01006750 int best_active_cpu = -1;
6751 int best_idle_cpu = -1;
6752 int target_cpu = -1;
6753 int cpu, i;
Juri Lelli1931b932016-07-29 14:04:11 +01006754
Chris Redpath7b2727c2017-07-04 10:19:58 +01006755 *backup_cpu = -1;
6756
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006757 schedstat_inc(p->se.statistics.nr_wakeups_fbt_attempts);
6758 schedstat_inc(this_rq()->eas_stats.fbt_attempts);
6759
Patrick Bellasi56e27192017-03-29 09:01:06 +01006760 /* Find start CPU based on boost value */
6761 cpu = start_cpu(boosted);
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006762 if (cpu < 0) {
6763 schedstat_inc(p->se.statistics.nr_wakeups_fbt_no_cpu);
6764 schedstat_inc(this_rq()->eas_stats.fbt_no_cpu);
Patrick Bellasi56e27192017-03-29 09:01:06 +01006765 return -1;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006766 }
Todd Kjos8dce8eb2016-07-29 14:41:25 +01006767
Patrick Bellasi56e27192017-03-29 09:01:06 +01006768 /* Find SD for the start CPU */
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006769 sd = rcu_dereference(per_cpu(sd_ea, cpu));
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006770 if (!sd) {
6771 schedstat_inc(p->se.statistics.nr_wakeups_fbt_no_sd);
6772 schedstat_inc(this_rq()->eas_stats.fbt_no_sd);
Patrick Bellasi56e27192017-03-29 09:01:06 +01006773 return -1;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00006774 }
Juri Lelli1931b932016-07-29 14:04:11 +01006775
Patrick Bellasi56e27192017-03-29 09:01:06 +01006776 /* Scan CPUs in all SDs */
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006777 sg = sd->groups;
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006778 do {
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006779 for_each_cpu_and(i, tsk_cpus_allowed(p), sched_group_cpus(sg)) {
Patrick Bellasi56e27192017-03-29 09:01:06 +01006780 unsigned long capacity_curr = capacity_curr_of(i);
6781 unsigned long capacity_orig = capacity_orig_of(i);
Ionela Voinescu88a968c2017-12-07 20:09:11 +00006782 unsigned long wake_util, new_util, min_capped_util;
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006783
6784 if (!cpu_online(i))
6785 continue;
6786
Patrick Bellasi56e27192017-03-29 09:01:06 +01006787 if (walt_cpu_high_irqload(i))
6788 continue;
6789
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006790 /*
6791 * p's blocked utilization is still accounted for on prev_cpu
6792 * so prev_cpu will receive a negative bias due to the double
6793 * accounting. However, the blocked utilization may be zero.
6794 */
Valentin Schneider352e0962017-03-03 11:43:03 +00006795 wake_util = cpu_util_wake(i, p);
6796 new_util = wake_util + task_util(p);
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006797
6798 /*
6799 * Ensure minimum capacity to grant the required boost.
6800 * The target CPU can be already at a capacity level higher
6801 * than the one required to boost the task.
6802 */
6803 new_util = max(min_util, new_util);
Ionela Voinescu88a968c2017-12-07 20:09:11 +00006804
6805 /*
6806 * Include minimum capacity constraint:
6807 * new_util contains the required utilization including
6808 * boost. min_capped_util also takes into account a
6809 * minimum capacity cap imposed on the CPU by external
6810 * actors.
6811 */
6812 min_capped_util = max(new_util, capacity_min_of(i));
6813
Patrick Bellasi56e27192017-03-29 09:01:06 +01006814 if (new_util > capacity_orig)
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006815 continue;
Juri Lelli1931b932016-07-29 14:04:11 +01006816
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006817 /*
Patrick Bellasi56e27192017-03-29 09:01:06 +01006818 * Case A) Latency sensitive tasks
6819 *
6820 * Unconditionally favoring tasks that prefer idle CPU to
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006821 * improve latency.
Patrick Bellasi56e27192017-03-29 09:01:06 +01006822 *
6823 * Looking for:
6824 * - an idle CPU, whatever its idle_state is, since
6825 * the first CPUs we explore are more likely to be
6826 * reserved for latency sensitive tasks.
6827 * - a non idle CPU where the task fits in its current
6828 * capacity and has the maximum spare capacity.
6829 * - a non idle CPU with lower contention from other
6830 * tasks and running at the lowest possible OPP.
6831 *
6832 * The last two goals tries to favor a non idle CPU
6833 * where the task can run as if it is "almost alone".
6834 * A maximum spare capacity CPU is favoured since
6835 * the task already fits into that CPU's capacity
6836 * without waiting for an OPP chance.
6837 *
6838 * The following code path is the only one in the CPUs
6839 * exploration loop which is always used by
6840 * prefer_idle tasks. It exits the loop with wither a
6841 * best_active_cpu or a target_cpu which should
6842 * represent an optimal choice for latency sensitive
6843 * tasks.
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006844 */
Patrick Bellasi56e27192017-03-29 09:01:06 +01006845 if (prefer_idle) {
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006846
Patrick Bellasi56e27192017-03-29 09:01:06 +01006847 /*
6848 * Case A.1: IDLE CPU
6849 * Return the first IDLE CPU we find.
6850 */
6851 if (idle_cpu(i)) {
6852 schedstat_inc(p->se.statistics.nr_wakeups_fbt_pref_idle);
6853 schedstat_inc(this_rq()->eas_stats.fbt_pref_idle);
Patrick Bellasi11184b62017-06-29 12:24:27 +01006854
6855 trace_sched_find_best_target(p,
6856 prefer_idle, min_util,
6857 cpu, best_idle_cpu,
6858 best_active_cpu, i);
6859
Patrick Bellasi56e27192017-03-29 09:01:06 +01006860 return i;
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006861 }
Patrick Bellasi56e27192017-03-29 09:01:06 +01006862
6863 /*
6864 * Case A.2: Target ACTIVE CPU
6865 * Favor CPUs with max spare capacity.
6866 */
6867 if ((capacity_curr > new_util) &&
6868 (capacity_orig - new_util > target_max_spare_cap)) {
6869 target_max_spare_cap = capacity_orig - new_util;
6870 target_cpu = i;
6871 continue;
6872 }
6873 if (target_cpu != -1)
6874 continue;
6875
6876
6877 /*
6878 * Case A.3: Backup ACTIVE CPU
6879 * Favor CPUs with:
6880 * - lower utilization due to other tasks
6881 * - lower utilization with the task in
6882 */
6883 if (wake_util > min_wake_util)
6884 continue;
6885 if (new_util > best_active_util)
6886 continue;
6887 min_wake_util = wake_util;
6888 best_active_util = new_util;
6889 best_active_cpu = i;
6890 continue;
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006891 }
Patrick Bellasi56e27192017-03-29 09:01:06 +01006892
6893 /*
Patrick Bellasi1e586742017-07-17 15:54:39 +01006894 * Enforce EAS mode
6895 *
6896 * For non latency sensitive tasks, skip CPUs that
6897 * will be overutilized by moving the task there.
6898 *
6899 * The goal here is to remain in EAS mode as long as
6900 * possible at least for !prefer_idle tasks.
6901 */
6902 if ((new_util * capacity_margin) >
6903 (capacity_orig * SCHED_CAPACITY_SCALE))
6904 continue;
6905
6906 /*
Patrick Bellasi56e27192017-03-29 09:01:06 +01006907 * Case B) Non latency sensitive tasks on IDLE CPUs.
6908 *
6909 * Find an optimal backup IDLE CPU for non latency
6910 * sensitive tasks.
6911 *
6912 * Looking for:
6913 * - minimizing the capacity_orig,
6914 * i.e. preferring LITTLE CPUs
6915 * - favoring shallowest idle states
6916 * i.e. avoid to wakeup deep-idle CPUs
6917 *
6918 * The following code path is used by non latency
6919 * sensitive tasks if IDLE CPUs are available. If at
6920 * least one of such CPUs are available it sets the
6921 * best_idle_cpu to the most suitable idle CPU to be
6922 * selected.
6923 *
6924 * If idle CPUs are available, favour these CPUs to
6925 * improve performances by spreading tasks.
6926 * Indeed, the energy_diff() computed by the caller
6927 * will take care to ensure the minimization of energy
6928 * consumptions without affecting performance.
6929 */
6930 if (idle_cpu(i)) {
6931 int idle_idx = idle_get_state_idx(cpu_rq(i));
6932
6933 /* Select idle CPU with lower cap_orig */
6934 if (capacity_orig > best_idle_min_cap_orig)
6935 continue;
Ionela Voinescu88a968c2017-12-07 20:09:11 +00006936 /* Favor CPUs that won't end up running at a
6937 * high OPP.
6938 */
6939 if ((capacity_orig - min_capped_util) <
6940 target_idle_max_spare_cap)
6941 continue;
Patrick Bellasi56e27192017-03-29 09:01:06 +01006942
6943 /*
6944 * Skip CPUs in deeper idle state, but only
6945 * if they are also less energy efficient.
6946 * IOW, prefer a deep IDLE LITTLE CPU vs a
6947 * shallow idle big CPU.
6948 */
6949 if (sysctl_sched_cstate_aware &&
6950 best_idle_cstate <= idle_idx)
6951 continue;
6952
6953 /* Keep track of best idle CPU */
6954 best_idle_min_cap_orig = capacity_orig;
Ionela Voinescu88a968c2017-12-07 20:09:11 +00006955 target_idle_max_spare_cap = capacity_orig -
6956 min_capped_util;
Patrick Bellasi56e27192017-03-29 09:01:06 +01006957 best_idle_cstate = idle_idx;
6958 best_idle_cpu = i;
6959 continue;
6960 }
6961
6962 /*
6963 * Case C) Non latency sensitive tasks on ACTIVE CPUs.
6964 *
6965 * Pack tasks in the most energy efficient capacities.
6966 *
6967 * This task packing strategy prefers more energy
6968 * efficient CPUs (i.e. pack on smaller maximum
6969 * capacity CPUs) while also trying to spread tasks to
6970 * run them all at the lower OPP.
6971 *
6972 * This assumes for example that it's more energy
6973 * efficient to run two tasks on two CPUs at a lower
6974 * OPP than packing both on a single CPU but running
6975 * that CPU at an higher OPP.
6976 *
6977 * Thus, this case keep track of the CPU with the
6978 * smallest maximum capacity and highest spare maximum
6979 * capacity.
6980 */
6981
6982 /* Favor CPUs with smaller capacity */
6983 if (capacity_orig > target_capacity)
6984 continue;
6985
6986 /* Favor CPUs with maximum spare capacity */
Ionela Voinescuf9641202017-12-07 20:09:50 +00006987 if ((capacity_orig - min_capped_util) <
6988 target_max_spare_cap)
Patrick Bellasi56e27192017-03-29 09:01:06 +01006989 continue;
6990
Ionela Voinescuf9641202017-12-07 20:09:50 +00006991 target_max_spare_cap = capacity_orig - min_capped_util;
Patrick Bellasi56e27192017-03-29 09:01:06 +01006992 target_capacity = capacity_orig;
6993 target_util = new_util;
6994 target_cpu = i;
Juri Lelli1931b932016-07-29 14:04:11 +01006995 }
Patrick Bellasi56e27192017-03-29 09:01:06 +01006996
Dietmar Eggemann3bfde3b2017-01-13 17:54:34 +00006997 } while (sg = sg->next, sg != sd->groups);
Juri Lelli1931b932016-07-29 14:04:11 +01006998
Patrick Bellasi56e27192017-03-29 09:01:06 +01006999 /*
7000 * For non latency sensitive tasks, cases B and C in the previous loop,
7001 * we pick the best IDLE CPU only if we was not able to find a target
7002 * ACTIVE CPU.
7003 *
7004 * Policies priorities:
7005 *
7006 * - prefer_idle tasks:
7007 *
7008 * a) IDLE CPU available, we return immediately
7009 * b) ACTIVE CPU where task fits and has the bigger maximum spare
7010 * capacity (i.e. target_cpu)
7011 * c) ACTIVE CPU with less contention due to other tasks
7012 * (i.e. best_active_cpu)
7013 *
7014 * - NON prefer_idle tasks:
7015 *
7016 * a) ACTIVE CPU: target_cpu
7017 * b) IDLE CPU: best_idle_cpu
7018 */
7019 if (target_cpu == -1)
7020 target_cpu = prefer_idle
7021 ? best_active_cpu
7022 : best_idle_cpu;
Chris Redpath7b2727c2017-07-04 10:19:58 +01007023 else
7024 *backup_cpu = prefer_idle
7025 ? best_active_cpu
7026 : best_idle_cpu;
Juri Lelli1931b932016-07-29 14:04:11 +01007027
Patrick Bellasi11184b62017-06-29 12:24:27 +01007028 trace_sched_find_best_target(p, prefer_idle, min_util, cpu,
7029 best_idle_cpu, best_active_cpu,
7030 target_cpu);
7031
Patrick Bellasi56e27192017-03-29 09:01:06 +01007032 schedstat_inc(p->se.statistics.nr_wakeups_fbt_count);
7033 schedstat_inc(this_rq()->eas_stats.fbt_count);
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007034
Juri Lelli1931b932016-07-29 14:04:11 +01007035 return target_cpu;
7036}
7037
Quentin Perret7fb3e0c2017-06-07 17:40:30 +01007038/*
7039 * Disable WAKE_AFFINE in the case where task @p doesn't fit in the
7040 * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu.
7041 *
7042 * In that case WAKE_AFFINE doesn't make sense and we'll let
7043 * BALANCE_WAKE sort things out.
7044 */
7045static int wake_cap(struct task_struct *p, int cpu, int prev_cpu)
7046{
7047 long min_cap, max_cap;
7048 min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu));
7049 max_cap = cpu_rq(cpu)->rd->max_cpu_capacity.val;
7050 /* Minimum capacity is close to max, no need to abort wake_affine */
7051 if (max_cap - min_cap < max_cap >> 3)
7052 return 0;
Morten Rasmussen355772432016-10-14 14:41:07 +01007053
7054 /* Bring task utilization in sync with prev_cpu */
7055 sync_entity_load_avg(&p->se);
7056
Quentin Perret7fb3e0c2017-06-07 17:40:30 +01007057 return min_cap * 1024 < task_util(p) * capacity_margin;
7058}
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01007059
Dietmar Eggemann52b09b12016-12-05 14:15:54 +00007060static int select_energy_cpu_brute(struct task_struct *p, int prev_cpu, int sync)
Morten Rasmussen9e312182016-03-30 14:29:48 +01007061{
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007062 bool boosted, prefer_idle;
Patrick Bellasi142ce322017-09-12 15:01:17 +01007063 struct sched_domain *sd;
7064 int target_cpu;
7065 int backup_cpu;
7066 int next_cpu;
Morten Rasmussen9e312182016-03-30 14:29:48 +01007067
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007068 schedstat_inc(p->se.statistics.nr_wakeups_secb_attempts);
7069 schedstat_inc(this_rq()->eas_stats.secb_attempts);
7070
Dietmar Eggemann52b09b12016-12-05 14:15:54 +00007071 if (sysctl_sched_sync_hint_enable && sync) {
7072 int cpu = smp_processor_id();
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007073
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007074 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
7075 schedstat_inc(p->se.statistics.nr_wakeups_secb_sync);
7076 schedstat_inc(this_rq()->eas_stats.secb_sync);
Dietmar Eggemann52b09b12016-12-05 14:15:54 +00007077 return cpu;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007078 }
Dietmar Eggemann52b09b12016-12-05 14:15:54 +00007079 }
7080
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007081#ifdef CONFIG_CGROUP_SCHEDTUNE
7082 boosted = schedtune_task_boost(p) > 0;
7083 prefer_idle = schedtune_prefer_idle(p) > 0;
7084#else
7085 boosted = get_sysctl_sched_cfs_boost() > 0;
7086 prefer_idle = 0;
7087#endif
Morten Rasmussen9e312182016-03-30 14:29:48 +01007088
Patrick Bellasi142ce322017-09-12 15:01:17 +01007089 rcu_read_lock();
Brendan Jackmanc0a392b2017-06-29 17:29:31 +01007090
Morten Rasmussen9e312182016-03-30 14:29:48 +01007091 sd = rcu_dereference(per_cpu(sd_ea, prev_cpu));
Patrick Bellasi142ce322017-09-12 15:01:17 +01007092 if (!sd) {
7093 target_cpu = prev_cpu;
Morten Rasmussen9e312182016-03-30 14:29:48 +01007094 goto unlock;
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007095 }
Morten Rasmussen9e312182016-03-30 14:29:48 +01007096
Patrick Bellasi142ce322017-09-12 15:01:17 +01007097 sync_entity_load_avg(&p->se);
7098
7099 /* Find a cpu with sufficient capacity */
7100 next_cpu = find_best_target(p, &backup_cpu, boosted, prefer_idle);
7101 if (next_cpu == -1) {
7102 target_cpu = prev_cpu;
7103 goto unlock;
7104 }
7105
7106 /* Unconditionally prefer IDLE CPUs for boosted/prefer_idle tasks */
7107 if ((boosted || prefer_idle) && idle_cpu(next_cpu)) {
7108 schedstat_inc(p->se.statistics.nr_wakeups_secb_idle_bt);
7109 schedstat_inc(this_rq()->eas_stats.secb_idle_bt);
7110 target_cpu = next_cpu;
7111 goto unlock;
7112 }
7113
7114 target_cpu = prev_cpu;
7115 if (next_cpu != prev_cpu) {
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08007116 int delta = 0;
Morten Rasmussen9e312182016-03-30 14:29:48 +01007117 struct energy_env eenv = {
Chris Redpathe997bf02017-09-12 14:48:29 +01007118 .p = p,
Patrick Bellasicf28cf02017-07-05 10:59:59 +01007119 .util_delta = task_util(p),
7120 /* Task's previous CPU candidate */
7121 .cpu[EAS_CPU_PRV] = {
7122 .cpu_id = prev_cpu,
7123 },
7124 /* Main alternative CPU candidate */
7125 .cpu[EAS_CPU_NXT] = {
7126 .cpu_id = next_cpu,
7127 },
7128 /* Backup alternative CPU candidate */
7129 .cpu[EAS_CPU_BKP] = {
7130 .cpu_id = backup_cpu,
7131 },
Morten Rasmussen9e312182016-03-30 14:29:48 +01007132 };
7133
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08007134
7135#ifdef CONFIG_SCHED_WALT
Ke Wangb7634802017-11-09 11:30:56 +08007136 if (!walt_disabled && sysctl_sched_use_walt_cpu_util &&
7137 p->state == TASK_WAKING)
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08007138 delta = task_util(p);
7139#endif
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007140 /* Not enough spare capacity on previous cpu */
Joonwoo Parkb41e1ca2017-01-25 17:45:56 -08007141 if (__cpu_overutilized(prev_cpu, delta)) {
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007142 schedstat_inc(p->se.statistics.nr_wakeups_secb_insuff_cap);
7143 schedstat_inc(this_rq()->eas_stats.secb_insuff_cap);
Patrick Bellasi142ce322017-09-12 15:01:17 +01007144 target_cpu = next_cpu;
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007145 goto unlock;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007146 }
Morten Rasmussen9e312182016-03-30 14:29:48 +01007147
Patrick Bellasicf28cf02017-07-05 10:59:59 +01007148 /* Check if EAS_CPU_NXT is a more energy efficient CPU */
7149 if (select_energy_cpu_idx(&eenv) != EAS_CPU_PRV) {
7150 schedstat_inc(p->se.statistics.nr_wakeups_secb_nrg_sav);
7151 schedstat_inc(this_rq()->eas_stats.secb_nrg_sav);
7152 target_cpu = eenv.cpu[eenv.next_idx].cpu_id;
7153 goto unlock;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007154 }
7155
Patrick Bellasicf28cf02017-07-05 10:59:59 +01007156 schedstat_inc(p->se.statistics.nr_wakeups_secb_no_nrg_sav);
7157 schedstat_inc(this_rq()->eas_stats.secb_no_nrg_sav);
7158 target_cpu = prev_cpu;
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007159 goto unlock;
Morten Rasmussen9e312182016-03-30 14:29:48 +01007160 }
7161
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007162 schedstat_inc(p->se.statistics.nr_wakeups_secb_count);
7163 schedstat_inc(this_rq()->eas_stats.secb_count);
7164
Morten Rasmussen9e312182016-03-30 14:29:48 +01007165unlock:
7166 rcu_read_unlock();
Dietmar Eggemann8c2e3d82017-03-22 18:16:03 +00007167 return target_cpu;
Morten Rasmussen9e312182016-03-30 14:29:48 +01007168}
7169
Vincent Guittot8bb5b002015-03-04 08:48:47 +01007170/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00007171 * select_task_rq_fair: Select target runqueue for the waking task in domains
7172 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
7173 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007174 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00007175 * Balances load by selecting the idlest cpu in the idlest group, or under
7176 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007177 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00007178 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007179 *
7180 * preempt must be disabled.
7181 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01007182static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01007183select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007184{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02007185 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007186 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007187 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07007188 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02007189 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01007190
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02007191 if (sd_flag & SD_BALANCE_WAKE) {
7192 record_wakee(p);
Quentin Perret4ec7d812017-07-21 15:40:36 +01007193 want_affine = (!wake_wide(p) && !wake_cap(p, cpu, prev_cpu) &&
7194 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)));
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02007195 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01007196
Morten Rasmussen9e312182016-03-30 14:29:48 +01007197 if (energy_aware() && !(cpu_rq(prev_cpu)->rd->overutilized))
Dietmar Eggemann52b09b12016-12-05 14:15:54 +00007198 return select_energy_cpu_brute(p, prev_cpu, sync);
Morten Rasmussen9e312182016-03-30 14:29:48 +01007199
Peter Zijlstradce840a2011-04-07 14:09:50 +02007200 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007201 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01007202 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007203 break;
Peter Zijlstrae4f428882009-12-16 18:04:34 +01007204
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007205 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07007206 * If both cpu and prev_cpu are part of this domain,
7207 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01007208 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07007209 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
7210 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
7211 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08007212 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007213 }
7214
Alex Shif03542a2012-07-26 08:55:34 +08007215 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02007216 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007217 else if (!want_affine)
7218 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007219 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02007220
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007221 if (affine_sd) {
7222 sd = NULL; /* Prefer wake_affine over balance flags */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01007223 if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007224 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01007225 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02007226
Brendan Jackman32ea7752017-10-05 11:55:51 +01007227 if (sd && !(sd_flag & SD_BALANCE_FORK)) {
7228 /*
7229 * We're going to need the task's util for capacity_spare_wake
7230 * in find_idlest_group. Sync it up to prev_cpu's
7231 * last_update_time.
7232 */
7233 sync_entity_load_avg(&p->se);
7234 }
7235
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007236 if (!sd) {
Dietmar Eggemann0df28982017-01-26 16:04:34 +00007237 if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01007238 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02007239
Dietmar Eggemannaf88a162017-03-22 18:23:13 +00007240 } else {
Brendan Jackman6cc75c92017-10-05 12:45:12 +01007241 new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag);
Gregory Haskinse7693a32008-01-25 21:08:09 +01007242 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007243 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01007244
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007245 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01007246}
Paul Turner0a74bef2012-10-04 13:18:30 +02007247
7248/*
7249 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
7250 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09007251 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02007252 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08007253static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02007254{
Paul Turneraff3e492012-10-04 13:18:30 +02007255 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02007256 * As blocked tasks retain absolute vruntime the migration needs to
7257 * deal with this by subtracting the old and adding the new
7258 * min_vruntime -- the latter is done by enqueue_entity() when placing
7259 * the task on the new runqueue.
7260 */
7261 if (p->state == TASK_WAKING) {
7262 struct sched_entity *se = &p->se;
7263 struct cfs_rq *cfs_rq = cfs_rq_of(se);
7264 u64 min_vruntime;
7265
7266#ifndef CONFIG_64BIT
7267 u64 min_vruntime_copy;
7268
7269 do {
7270 min_vruntime_copy = cfs_rq->min_vruntime_copy;
7271 smp_rmb();
7272 min_vruntime = cfs_rq->min_vruntime;
7273 } while (min_vruntime != min_vruntime_copy);
7274#else
7275 min_vruntime = cfs_rq->min_vruntime;
7276#endif
7277
7278 se->vruntime -= min_vruntime;
7279 }
7280
7281 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08007282 * We are supposed to update the task to "current" time, then its up to date
7283 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
7284 * what current time is, so simply throw away the out-of-date time. This
7285 * will result in the wakee task is less decayed, but giving the wakee more
7286 * load sounds not bad.
Paul Turneraff3e492012-10-04 13:18:30 +02007287 */
Yuyang Du9d89c252015-07-15 08:04:37 +08007288 remove_entity_load_avg(&p->se);
7289
7290 /* Tell new CPU we are migrated */
7291 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07007292
7293 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08007294 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02007295}
Yuyang Du12695572015-07-15 08:04:40 +08007296
7297static void task_dead_fair(struct task_struct *p)
7298{
7299 remove_entity_load_avg(&p->se);
7300}
Patrick Bellasi2178e842016-07-22 11:35:59 +01007301#else
7302#define task_fits_max(p, cpu) true
Gregory Haskinse7693a32008-01-25 21:08:09 +01007303#endif /* CONFIG_SMP */
7304
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01007305static unsigned long
7306wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02007307{
7308 unsigned long gran = sysctl_sched_wakeup_granularity;
7309
7310 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01007311 * Since its curr running now, convert the gran from real-time
7312 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01007313 *
7314 * By using 'se' instead of 'curr' we penalize light tasks, so
7315 * they get preempted easier. That is, if 'se' < 'curr' then
7316 * the resulting gran will be larger, therefore penalizing the
7317 * lighter, if otoh 'se' > 'curr' then the resulting gran will
7318 * be smaller, again penalizing the lighter task.
7319 *
7320 * This is especially important for buddies when the leftmost
7321 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02007322 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08007323 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02007324}
7325
7326/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02007327 * Should 'se' preempt 'curr'.
7328 *
7329 * |s1
7330 * |s2
7331 * |s3
7332 * g
7333 * |<--->|c
7334 *
7335 * w(c, s1) = -1
7336 * w(c, s2) = 0
7337 * w(c, s3) = 1
7338 *
7339 */
7340static int
7341wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
7342{
7343 s64 gran, vdiff = curr->vruntime - se->vruntime;
7344
7345 if (vdiff <= 0)
7346 return -1;
7347
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01007348 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02007349 if (vdiff > gran)
7350 return 1;
7351
7352 return 0;
7353}
7354
Peter Zijlstra02479092008-11-04 21:25:10 +01007355static void set_last_buddy(struct sched_entity *se)
7356{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07007357 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
7358 return;
7359
7360 for_each_sched_entity(se)
7361 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01007362}
7363
7364static void set_next_buddy(struct sched_entity *se)
7365{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07007366 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
7367 return;
7368
7369 for_each_sched_entity(se)
7370 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01007371}
7372
Rik van Rielac53db52011-02-01 09:51:03 -05007373static void set_skip_buddy(struct sched_entity *se)
7374{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07007375 for_each_sched_entity(se)
7376 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05007377}
7378
Peter Zijlstra464b7522008-10-24 11:06:15 +02007379/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007380 * Preempt the current task with a newly woken task if needed:
7381 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02007382static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007383{
7384 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02007385 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01007386 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02007387 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07007388 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01007389
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01007390 if (unlikely(se == pse))
7391 return;
7392
Paul Turner5238cdd2011-07-21 09:43:37 -07007393 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007394 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07007395 * unconditionally check_prempt_curr() after an enqueue (which may have
7396 * lead to a throttle). This both saves work and prevents false
7397 * next-buddy nomination below.
7398 */
7399 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
7400 return;
7401
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07007402 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02007403 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07007404 next_buddy_marked = 1;
7405 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02007406
Bharata B Raoaec0a512008-08-28 14:42:49 +05307407 /*
7408 * We can come here with TIF_NEED_RESCHED already set from new task
7409 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07007410 *
7411 * Note: this also catches the edge-case of curr being in a throttled
7412 * group (e.g. via set_curr_task), since update_curr() (in the
7413 * enqueue of curr) will have resulted in resched being set. This
7414 * prevents us from potentially nominating it as a false LAST_BUDDY
7415 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05307416 */
7417 if (test_tsk_need_resched(curr))
7418 return;
7419
Darren Harta2f5c9a2011-02-22 13:04:33 -08007420 /* Idle tasks are by definition preempted by non-idle tasks. */
7421 if (unlikely(curr->policy == SCHED_IDLE) &&
7422 likely(p->policy != SCHED_IDLE))
7423 goto preempt;
7424
Ingo Molnar91c234b2007-10-15 17:00:18 +02007425 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08007426 * Batch and idle tasks do not preempt non-idle tasks (their preemption
7427 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02007428 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02007429 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02007430 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007431
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01007432 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07007433 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01007434 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07007435 if (wakeup_preempt_entity(se, pse) == 1) {
7436 /*
7437 * Bias pick_next to pick the sched entity that is
7438 * triggering this preemption.
7439 */
7440 if (!next_buddy_marked)
7441 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01007442 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07007443 }
Jupyung Leea65ac742009-11-17 18:51:40 +09007444
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01007445 return;
7446
7447preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04007448 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01007449 /*
7450 * Only set the backward buddy when the current task is still
7451 * on the rq. This can happen when a wakeup gets interleaved
7452 * with schedule on the ->pre_schedule() or idle_balance()
7453 * point, either of which can * drop the rq lock.
7454 *
7455 * Also, during early boot the idle thread is in the fair class,
7456 * for obvious reasons its a bad idea to schedule back to it.
7457 */
7458 if (unlikely(!se->on_rq || curr == rq->idle))
7459 return;
7460
7461 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
7462 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007463}
7464
Peter Zijlstra606dba22012-02-11 06:05:00 +01007465static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02007466pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007467{
7468 struct cfs_rq *cfs_rq = &rq->cfs;
7469 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01007470 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01007471 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01007472
Peter Zijlstra6e831252014-02-11 16:11:48 +01007473again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01007474#ifdef CONFIG_FAIR_GROUP_SCHED
7475 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01007476 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01007477
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01007478 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01007479 goto simple;
7480
7481 /*
7482 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
7483 * likely that a next task is from the same cgroup as the current.
7484 *
7485 * Therefore attempt to avoid putting and setting the entire cgroup
7486 * hierarchy, only change the part that actually changes.
7487 */
7488
7489 do {
7490 struct sched_entity *curr = cfs_rq->curr;
7491
7492 /*
7493 * Since we got here without doing put_prev_entity() we also
7494 * have to consider cfs_rq->curr. If it is still a runnable
7495 * entity, update_curr() will update its vruntime, otherwise
7496 * forget we've ever seen it.
7497 */
Ben Segall54d27362015-04-06 15:28:10 -07007498 if (curr) {
7499 if (curr->on_rq)
7500 update_curr(cfs_rq);
7501 else
7502 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01007503
Ben Segall54d27362015-04-06 15:28:10 -07007504 /*
7505 * This call to check_cfs_rq_runtime() will do the
7506 * throttle and dequeue its entity in the parent(s).
7507 * Therefore the 'simple' nr_running test will indeed
7508 * be correct.
7509 */
7510 if (unlikely(check_cfs_rq_runtime(cfs_rq)))
7511 goto simple;
7512 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01007513
7514 se = pick_next_entity(cfs_rq, curr);
7515 cfs_rq = group_cfs_rq(se);
7516 } while (cfs_rq);
7517
7518 p = task_of(se);
7519
7520 /*
7521 * Since we haven't yet done put_prev_entity and if the selected task
7522 * is a different task than we started out with, try and touch the
7523 * least amount of cfs_rqs.
7524 */
7525 if (prev != p) {
7526 struct sched_entity *pse = &prev->se;
7527
7528 while (!(cfs_rq = is_same_group(se, pse))) {
7529 int se_depth = se->depth;
7530 int pse_depth = pse->depth;
7531
7532 if (se_depth <= pse_depth) {
7533 put_prev_entity(cfs_rq_of(pse), pse);
7534 pse = parent_entity(pse);
7535 }
7536 if (se_depth >= pse_depth) {
7537 set_next_entity(cfs_rq_of(se), se);
7538 se = parent_entity(se);
7539 }
7540 }
7541
7542 put_prev_entity(cfs_rq, pse);
7543 set_next_entity(cfs_rq, se);
7544 }
7545
7546 if (hrtick_enabled(rq))
7547 hrtick_start_fair(rq, p);
7548
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007549 rq->misfit_task = !task_fits_max(p, rq->cpu);
7550
Peter Zijlstra678d5712012-02-11 06:05:00 +01007551 return p;
7552simple:
7553 cfs_rq = &rq->cfs;
7554#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007555
Tim Blechmann36ace272009-11-24 11:55:45 +01007556 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01007557 goto idle;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007558
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01007559 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01007560
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007561 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01007562 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01007563 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007564 cfs_rq = group_cfs_rq(se);
7565 } while (cfs_rq);
7566
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007567 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01007568
Mike Galbraithb39e66e2011-11-22 15:20:07 +01007569 if (hrtick_enabled(rq))
7570 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007571
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007572 rq->misfit_task = !task_fits_max(p, rq->cpu);
7573
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007574 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01007575
7576idle:
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007577 rq->misfit_task = 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02007578 /*
7579 * This is OK, because current is on_cpu, which avoids it being picked
7580 * for load-balance and preemption/IRQs are still disabled avoiding
7581 * further scheduler activity on it and we're being very careful to
7582 * re-start the picking loop.
7583 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02007584 lockdep_unpin_lock(&rq->lock, cookie);
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007585 new_tasks = idle_balance(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02007586 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01007587 /*
7588 * Because idle_balance() releases (and re-acquires) rq->lock, it is
7589 * possible for any higher priority task to appear. In that case we
7590 * must re-start the pick_next_entity() loop.
7591 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007592 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01007593 return RETRY_TASK;
7594
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007595 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01007596 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01007597
7598 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007599}
7600
7601/*
7602 * Account for a descheduled task:
7603 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02007604static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007605{
7606 struct sched_entity *se = &prev->se;
7607 struct cfs_rq *cfs_rq;
7608
7609 for_each_sched_entity(se) {
7610 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02007611 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007612 }
7613}
7614
Rik van Rielac53db52011-02-01 09:51:03 -05007615/*
7616 * sched_yield() is very simple
7617 *
7618 * The magic of dealing with the ->skip buddy is in pick_next_entity.
7619 */
7620static void yield_task_fair(struct rq *rq)
7621{
7622 struct task_struct *curr = rq->curr;
7623 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
7624 struct sched_entity *se = &curr->se;
7625
7626 /*
7627 * Are we the only task in the tree?
7628 */
7629 if (unlikely(rq->nr_running == 1))
7630 return;
7631
7632 clear_buddies(cfs_rq, se);
7633
7634 if (curr->policy != SCHED_BATCH) {
7635 update_rq_clock(rq);
7636 /*
7637 * Update run-time statistics of the 'current'.
7638 */
7639 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01007640 /*
7641 * Tell update_rq_clock() that we've just updated,
7642 * so we don't do microscopic update in schedule()
7643 * and double the fastpath cost.
7644 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01007645 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05007646 }
7647
7648 set_skip_buddy(se);
7649}
7650
Mike Galbraithd95f4122011-02-01 09:50:51 -05007651static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
7652{
7653 struct sched_entity *se = &p->se;
7654
Paul Turner5238cdd2011-07-21 09:43:37 -07007655 /* throttled hierarchies are not runnable */
7656 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05007657 return false;
7658
7659 /* Tell the scheduler that we'd really like pse to run next. */
7660 set_next_buddy(se);
7661
Mike Galbraithd95f4122011-02-01 09:50:51 -05007662 yield_task_fair(rq);
7663
7664 return true;
7665}
7666
Peter Williams681f3e62007-10-24 18:23:51 +02007667#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007668/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007669 * Fair scheduling class load-balancing methods.
7670 *
7671 * BASICS
7672 *
7673 * The purpose of load-balancing is to achieve the same basic fairness the
7674 * per-cpu scheduler provides, namely provide a proportional amount of compute
7675 * time to each task. This is expressed in the following equation:
7676 *
7677 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
7678 *
7679 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
7680 * W_i,0 is defined as:
7681 *
7682 * W_i,0 = \Sum_j w_i,j (2)
7683 *
7684 * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight
Yuyang Du1c3de5e2016-03-30 07:07:51 +08007685 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007686 *
7687 * The weight average is an exponential decay average of the instantaneous
7688 * weight:
7689 *
7690 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
7691 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007692 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007693 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
7694 * can also include other factors [XXX].
7695 *
7696 * To achieve this balance we define a measure of imbalance which follows
7697 * directly from (1):
7698 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007699 * 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 +02007700 *
7701 * We them move tasks around to minimize the imbalance. In the continuous
7702 * function space it is obvious this converges, in the discrete case we get
7703 * a few fun cases generally called infeasible weight scenarios.
7704 *
7705 * [XXX expand on:
7706 * - infeasible weights;
7707 * - local vs global optima in the discrete case. ]
7708 *
7709 *
7710 * SCHED DOMAINS
7711 *
7712 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
7713 * for all i,j solution, we create a tree of cpus that follows the hardware
7714 * topology where each level pairs two lower groups (or better). This results
7715 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
7716 * tree to only the first of the previous level and we decrease the frequency
7717 * of load-balance at each level inv. proportional to the number of cpus in
7718 * the groups.
7719 *
7720 * This yields:
7721 *
7722 * log_2 n 1 n
7723 * \Sum { --- * --- * 2^i } = O(n) (5)
7724 * i = 0 2^i 2^i
7725 * `- size of each group
7726 * | | `- number of cpus doing load-balance
7727 * | `- freq
7728 * `- sum over all levels
7729 *
7730 * Coupled with a limit on how many tasks we can migrate every balance pass,
7731 * this makes (5) the runtime complexity of the balancer.
7732 *
7733 * An important property here is that each CPU is still (indirectly) connected
7734 * to every other cpu in at most O(log n) steps:
7735 *
7736 * The adjacency matrix of the resulting graph is given by:
7737 *
Byungchul Park97a71422015-07-05 18:33:48 +09007738 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007739 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
7740 * k = 0
7741 *
7742 * And you'll find that:
7743 *
7744 * A^(log_2 n)_i,j != 0 for all i,j (7)
7745 *
7746 * Showing there's indeed a path between every cpu in at most O(log n) steps.
7747 * The task movement gives a factor of O(m), giving a convergence complexity
7748 * of:
7749 *
7750 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
7751 *
7752 *
7753 * WORK CONSERVING
7754 *
7755 * In order to avoid CPUs going idle while there's still work to do, new idle
7756 * balancing is more aggressive and has the newly idle cpu iterate up the domain
7757 * tree itself instead of relying on other CPUs to bring it work.
7758 *
7759 * This adds some complexity to both (5) and (8) but it reduces the total idle
7760 * time.
7761 *
7762 * [XXX more?]
7763 *
7764 *
7765 * CGROUPS
7766 *
7767 * Cgroups make a horror show out of (2), instead of a simple sum we get:
7768 *
7769 * s_k,i
7770 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
7771 * S_k
7772 *
7773 * Where
7774 *
7775 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
7776 *
7777 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
7778 *
7779 * The big problem is S_k, its a global sum needed to compute a local (W_i)
7780 * property.
7781 *
7782 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
7783 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09007784 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007785
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09007786static unsigned long __read_mostly max_load_balance_interval = HZ/10;
7787
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007788enum fbq_type { regular, remote, all };
7789
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007790enum group_type {
7791 group_other = 0,
7792 group_misfit_task,
7793 group_imbalanced,
7794 group_overloaded,
7795};
7796
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007797#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01007798#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02007799#define LBF_DST_PINNED 0x04
7800#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007801
7802struct lb_env {
7803 struct sched_domain *sd;
7804
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007805 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05307806 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007807
7808 int dst_cpu;
7809 struct rq *dst_rq;
7810
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307811 struct cpumask *dst_grpmask;
7812 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007813 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007814 long imbalance;
Morten Rasmussen94beeae2015-07-02 17:16:34 +01007815 unsigned int src_grp_nr_running;
Michael Wangb94031302012-07-12 16:10:13 +08007816 /* The set of CPUs under consideration for load-balancing */
7817 struct cpumask *cpus;
7818
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007819 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007820
7821 unsigned int loop;
7822 unsigned int loop_break;
7823 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007824
7825 enum fbq_type fbq_type;
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00007826 enum group_type busiest_group_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007827 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007828};
7829
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007830/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02007831 * Is this task likely cache-hot:
7832 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007833static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02007834{
7835 s64 delta;
7836
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007837 lockdep_assert_held(&env->src_rq->lock);
7838
Peter Zijlstra029632f2011-10-25 10:00:11 +02007839 if (p->sched_class != &fair_sched_class)
7840 return 0;
7841
7842 if (unlikely(p->policy == SCHED_IDLE))
7843 return 0;
7844
7845 /*
7846 * Buddy candidates are cache hot:
7847 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007848 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02007849 (&p->se == cfs_rq_of(&p->se)->next ||
7850 &p->se == cfs_rq_of(&p->se)->last))
7851 return 1;
7852
7853 if (sysctl_sched_migration_cost == -1)
7854 return 1;
7855 if (sysctl_sched_migration_cost == 0)
7856 return 0;
7857
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007858 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007859
7860 return delta < (s64)sysctl_sched_migration_cost;
7861}
7862
Mel Gorman3a7053b2013-10-07 11:29:00 +01007863#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04007864/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307865 * Returns 1, if task migration degrades locality
7866 * Returns 0, if task migration improves locality i.e migration preferred.
7867 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04007868 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307869static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01007870{
Rik van Rielb1ad0652014-05-15 13:03:06 -04007871 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04007872 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01007873 int src_nid, dst_nid;
7874
Srikar Dronamraju2a595722015-08-11 21:54:21 +05307875 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307876 return -1;
7877
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05307878 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307879 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007880
7881 src_nid = cpu_to_node(env->src_cpu);
7882 dst_nid = cpu_to_node(env->dst_cpu);
7883
Mel Gorman83e1d2c2013-10-07 11:29:27 +01007884 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307885 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007886
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307887 /* Migrating away from the preferred node is always bad. */
7888 if (src_nid == p->numa_preferred_nid) {
7889 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
7890 return 1;
7891 else
7892 return -1;
7893 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01007894
Rik van Rielc1ceac62015-05-14 22:59:36 -04007895 /* Encourage migration to the preferred node. */
7896 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307897 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04007898
7899 if (numa_group) {
7900 src_faults = group_faults(p, src_nid);
7901 dst_faults = group_faults(p, dst_nid);
7902 } else {
7903 src_faults = task_faults(p, src_nid);
7904 dst_faults = task_faults(p, dst_nid);
7905 }
7906
7907 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007908}
7909
Mel Gorman3a7053b2013-10-07 11:29:00 +01007910#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307911static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01007912 struct lb_env *env)
7913{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307914 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007915}
Mel Gorman3a7053b2013-10-07 11:29:00 +01007916#endif
7917
Peter Zijlstra029632f2011-10-25 10:00:11 +02007918/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007919 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
7920 */
7921static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007922int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007923{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307924 int tsk_cache_hot;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007925
7926 lockdep_assert_held(&env->src_rq->lock);
7927
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007928 /*
7929 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09007930 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007931 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09007932 * 3) running (obviously), or
7933 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007934 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09007935 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
7936 return 0;
7937
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007938 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007939 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307940
Josh Poimboeufae928822016-06-17 12:43:24 -05007941 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307942
Peter Zijlstra62633222013-08-19 12:41:09 +02007943 env->flags |= LBF_SOME_PINNED;
7944
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307945 /*
7946 * Remember if this task can be migrated to any other cpu in
7947 * our sched_group. We may want to revisit it if we couldn't
7948 * meet load balance goals by pulling other tasks on src_cpu.
7949 *
7950 * Also avoid computing new_dst_cpu if we have already computed
7951 * one in current iteration.
7952 */
Peter Zijlstra62633222013-08-19 12:41:09 +02007953 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307954 return 0;
7955
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007956 /* Prevent to re-select dst_cpu via env's cpus */
7957 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
7958 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02007959 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007960 env->new_dst_cpu = cpu;
7961 break;
7962 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307963 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007964
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007965 return 0;
7966 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307967
7968 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007969 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007970
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007971 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007972 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007973 return 0;
7974 }
7975
7976 /*
7977 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01007978 * 1) destination numa is preferred
7979 * 2) task is cache cold, or
7980 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007981 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307982 tsk_cache_hot = migrate_degrades_locality(p, env);
7983 if (tsk_cache_hot == -1)
7984 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007985
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307986 if (tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04007987 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307988 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007989 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
7990 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007991 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007992 return 1;
7993 }
7994
Josh Poimboeufae928822016-06-17 12:43:24 -05007995 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08007996 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007997}
7998
Peter Zijlstra897c3952009-12-17 17:45:42 +01007999/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008000 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01008001 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008002static void detach_task(struct task_struct *p, struct lb_env *env)
8003{
8004 lockdep_assert_held(&env->src_rq->lock);
8005
Kirill Tkhai163122b2014-08-20 13:48:29 +04008006 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -08008007 deactivate_task(env->src_rq, p, 0);
Andres Oportusb2eb4312017-03-03 14:20:23 -08008008 double_lock_balance(env->src_rq, env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04008009 set_task_cpu(p, env->dst_cpu);
Andres Oportusb2eb4312017-03-03 14:20:23 -08008010 double_unlock_balance(env->src_rq, env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04008011}
8012
8013/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008014 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01008015 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01008016 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008017 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01008018 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008019static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01008020{
8021 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01008022
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008023 lockdep_assert_held(&env->src_rq->lock);
8024
Peter Zijlstra367456c2012-02-20 21:49:09 +01008025 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01008026 if (!can_migrate_task(p, env))
8027 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01008028
Kirill Tkhai163122b2014-08-20 13:48:29 +04008029 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008030
Peter Zijlstra367456c2012-02-20 21:49:09 +01008031 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008032 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04008033 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008034 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04008035 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01008036 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008037 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008038 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01008039 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008040 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01008041}
8042
Peter Zijlstraeb953082012-04-17 13:38:40 +02008043static const unsigned int sched_nr_migrate_break = 32;
8044
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008045/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008046 * detach_tasks() -- tries to detach up to imbalance weighted load from
8047 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008048 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04008049 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008050 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008051static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008052{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008053 struct list_head *tasks = &env->src_rq->cfs_tasks;
8054 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01008055 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04008056 int detached = 0;
8057
8058 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008059
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008060 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008061 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008062
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008063 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08008064 /*
8065 * We don't want to steal all, otherwise we may be treated likewise,
8066 * which could at worst lead to a livelock crash.
8067 */
8068 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
8069 break;
8070
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008071 p = list_first_entry(tasks, struct task_struct, se.group_node);
8072
Peter Zijlstra367456c2012-02-20 21:49:09 +01008073 env->loop++;
8074 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008075 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01008076 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008077
8078 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01008079 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02008080 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008081 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01008082 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02008083 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008084
Joonsoo Kimd3198082013-04-23 17:27:40 +09008085 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01008086 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008087
Peter Zijlstra367456c2012-02-20 21:49:09 +01008088 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008089
Peter Zijlstraeb953082012-04-17 13:38:40 +02008090 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01008091 goto next;
8092
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008093 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01008094 goto next;
8095
Kirill Tkhai163122b2014-08-20 13:48:29 +04008096 detach_task(p, env);
8097 list_add(&p->se.group_node, &env->tasks);
8098
8099 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008100 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008101
8102#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01008103 /*
8104 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04008105 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01008106 * the critical section.
8107 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008108 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01008109 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008110#endif
8111
Peter Zijlstraee00e662009-12-17 17:25:20 +01008112 /*
8113 * We only want to steal up to the prescribed amount of
8114 * weighted load.
8115 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008116 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01008117 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008118
Peter Zijlstra367456c2012-02-20 21:49:09 +01008119 continue;
8120next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008121 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008122 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008123
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008124 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008125 * Right now, this is one of only two places we collect this stat
8126 * so we can safely collect detach_one_task() stats here rather
8127 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008128 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008129 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008130
Kirill Tkhai163122b2014-08-20 13:48:29 +04008131 return detached;
8132}
8133
8134/*
8135 * attach_task() -- attach the task detached by detach_task() to its new rq.
8136 */
8137static void attach_task(struct rq *rq, struct task_struct *p)
8138{
8139 lockdep_assert_held(&rq->lock);
8140
8141 BUG_ON(task_rq(p) != rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04008142 activate_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08008143 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04008144 check_preempt_curr(rq, p, 0);
8145}
8146
8147/*
8148 * attach_one_task() -- attaches the task returned from detach_one_task() to
8149 * its new rq.
8150 */
8151static void attach_one_task(struct rq *rq, struct task_struct *p)
8152{
8153 raw_spin_lock(&rq->lock);
8154 attach_task(rq, p);
8155 raw_spin_unlock(&rq->lock);
8156}
8157
8158/*
8159 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
8160 * new rq.
8161 */
8162static void attach_tasks(struct lb_env *env)
8163{
8164 struct list_head *tasks = &env->tasks;
8165 struct task_struct *p;
8166
8167 raw_spin_lock(&env->dst_rq->lock);
8168
8169 while (!list_empty(tasks)) {
8170 p = list_first_entry(tasks, struct task_struct, se.group_node);
8171 list_del_init(&p->se.group_node);
8172
8173 attach_task(env->dst_rq, p);
8174 }
8175
8176 raw_spin_unlock(&env->dst_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008177}
8178
Peter Zijlstra230059de2009-12-17 17:47:12 +01008179#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner48a16752012-10-04 13:18:31 +02008180static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008181{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008182 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02008183 struct cfs_rq *cfs_rq;
8184 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008185
Paul Turner48a16752012-10-04 13:18:31 +02008186 raw_spin_lock_irqsave(&rq->lock, flags);
8187 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08008188
Peter Zijlstra9763b672011-07-13 13:09:25 +02008189 /*
8190 * Iterates the task_group tree in a bottom up fashion, see
8191 * list_add_leaf_cfs_rq() for details.
8192 */
Paul Turner64660c82011-07-21 09:43:36 -07008193 for_each_leaf_cfs_rq(rq, cfs_rq) {
Vincent Guittot0b4a2f12017-03-17 14:47:22 +01008194 struct sched_entity *se;
8195
Yuyang Du9d89c252015-07-15 08:04:37 +08008196 /* throttled entities do not contribute to load */
8197 if (throttled_hierarchy(cfs_rq))
8198 continue;
Paul Turner48a16752012-10-04 13:18:31 +02008199
Steve Mucklea2c6c912016-03-24 15:26:07 -07008200 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true))
Yuyang Du9d89c252015-07-15 08:04:37 +08008201 update_tg_load_avg(cfs_rq, 0);
Vincent Guittot3a34bf52016-11-08 10:53:46 +01008202
Vincent Guittot0b4a2f12017-03-17 14:47:22 +01008203 /* Propagate pending load changes to the parent, if any: */
8204 se = cfs_rq->tg->se[cpu];
8205 if (se && !skip_blocked_update(se))
8206 update_load_avg(se, 0);
Yuyang Du9d89c252015-07-15 08:04:37 +08008207 }
Paul Turner48a16752012-10-04 13:18:31 +02008208 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008209}
8210
Peter Zijlstra9763b672011-07-13 13:09:25 +02008211/*
Vladimir Davydov68520792013-07-15 17:49:19 +04008212 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02008213 * This needs to be done in a top-down fashion because the load of a child
8214 * group is a fraction of its parents load.
8215 */
Vladimir Davydov68520792013-07-15 17:49:19 +04008216static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02008217{
Vladimir Davydov68520792013-07-15 17:49:19 +04008218 struct rq *rq = rq_of(cfs_rq);
8219 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02008220 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04008221 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02008222
Vladimir Davydov68520792013-07-15 17:49:19 +04008223 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02008224 return;
8225
Vladimir Davydov68520792013-07-15 17:49:19 +04008226 cfs_rq->h_load_next = NULL;
8227 for_each_sched_entity(se) {
8228 cfs_rq = cfs_rq_of(se);
8229 cfs_rq->h_load_next = se;
8230 if (cfs_rq->last_h_load_update == now)
8231 break;
8232 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02008233
Vladimir Davydov68520792013-07-15 17:49:19 +04008234 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08008235 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04008236 cfs_rq->last_h_load_update = now;
8237 }
8238
8239 while ((se = cfs_rq->h_load_next) != NULL) {
8240 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08008241 load = div64_ul(load * se->avg.load_avg,
8242 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04008243 cfs_rq = group_cfs_rq(se);
8244 cfs_rq->h_load = load;
8245 cfs_rq->last_h_load_update = now;
8246 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02008247}
8248
Peter Zijlstra367456c2012-02-20 21:49:09 +01008249static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01008250{
Peter Zijlstra367456c2012-02-20 21:49:09 +01008251 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01008252
Vladimir Davydov68520792013-07-15 17:49:19 +04008253 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08008254 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08008255 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01008256}
8257#else
Paul Turner48a16752012-10-04 13:18:31 +02008258static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008259{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08008260 struct rq *rq = cpu_rq(cpu);
8261 struct cfs_rq *cfs_rq = &rq->cfs;
8262 unsigned long flags;
8263
8264 raw_spin_lock_irqsave(&rq->lock, flags);
8265 update_rq_clock(rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07008266 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08008267 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08008268}
8269
Peter Zijlstra367456c2012-02-20 21:49:09 +01008270static unsigned long task_h_load(struct task_struct *p)
8271{
Yuyang Du9d89c252015-07-15 08:04:37 +08008272 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01008273}
8274#endif
8275
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008276/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04008277
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008278/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008279 * sg_lb_stats - stats of a sched_group required for load_balancing
8280 */
8281struct sg_lb_stats {
8282 unsigned long avg_load; /*Avg load across the CPUs of the group */
8283 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008284 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008285 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008286 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008287 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008288 unsigned int sum_nr_running; /* Nr tasks running in the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008289 unsigned int idle_cpus;
8290 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04008291 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01008292 int group_no_capacity;
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00008293 int group_misfit_task; /* A cpu has a task too big for its capacity */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008294#ifdef CONFIG_NUMA_BALANCING
8295 unsigned int nr_numa_running;
8296 unsigned int nr_preferred_running;
8297#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008298};
8299
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008300/*
8301 * sd_lb_stats - Structure to store the statistics of a sched_domain
8302 * during load balancing.
8303 */
8304struct sd_lb_stats {
8305 struct sched_group *busiest; /* Busiest group in this sd */
8306 struct sched_group *local; /* Local group in this sd */
8307 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008308 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008309 unsigned long avg_load; /* Average load across all groups in sd */
8310
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008311 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008312 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008313};
8314
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008315static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
8316{
8317 /*
8318 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
8319 * local_stat because update_sg_lb_stats() does a full clear/assignment.
8320 * We must however clear busiest_stat::avg_load because
8321 * update_sd_pick_busiest() reads this before assignment.
8322 */
8323 *sds = (struct sd_lb_stats){
8324 .busiest = NULL,
8325 .local = NULL,
8326 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008327 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008328 .busiest_stat = {
8329 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04008330 .sum_nr_running = 0,
8331 .group_type = group_other,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008332 },
8333 };
8334}
8335
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008336/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008337 * get_sd_load_idx - Obtain the load index for a given sched domain.
8338 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05308339 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02008340 *
8341 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008342 */
8343static inline int get_sd_load_idx(struct sched_domain *sd,
8344 enum cpu_idle_type idle)
8345{
8346 int load_idx;
8347
8348 switch (idle) {
8349 case CPU_NOT_IDLE:
8350 load_idx = sd->busy_idx;
8351 break;
8352
8353 case CPU_NEWLY_IDLE:
8354 load_idx = sd->newidle_idx;
8355 break;
8356 default:
8357 load_idx = sd->idle_idx;
8358 break;
8359 }
8360
8361 return load_idx;
8362}
8363
Nicolas Pitreced549f2014-05-26 18:19:38 -04008364static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008365{
8366 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01008367 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01008368 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008369
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02008370 /*
8371 * Since we're reading these variables without serialization make sure
8372 * we read them once before doing sanity checks on them.
8373 */
Jason Low316c1608d2015-04-28 13:00:20 -07008374 age_stamp = READ_ONCE(rq->age_stamp);
8375 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01008376 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07008377
Peter Zijlstracadefd32014-02-27 10:40:35 +01008378 if (unlikely(delta < 0))
8379 delta = 0;
8380
8381 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02008382
Vincent Guittotb5b48602015-02-27 16:54:08 +01008383 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008384
Vincent Guittotb5b48602015-02-27 16:54:08 +01008385 if (likely(used < SCHED_CAPACITY_SCALE))
8386 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008387
Vincent Guittotb5b48602015-02-27 16:54:08 +01008388 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008389}
8390
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01008391void init_max_cpu_capacity(struct max_cpu_capacity *mcc)
8392{
8393 raw_spin_lock_init(&mcc->lock);
8394 mcc->val = 0;
8395 mcc->cpu = -1;
8396}
8397
Nicolas Pitreced549f2014-05-26 18:19:38 -04008398static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008399{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01008400 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008401 struct sched_group *sdg = sd->groups;
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01008402 struct max_cpu_capacity *mcc;
8403 unsigned long max_capacity;
8404 int max_cap_cpu;
8405 unsigned long flags;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008406
Vincent Guittotca6d75e2015-02-27 16:54:09 +01008407 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008408
Dietmar Eggemannda6833cf2017-07-13 09:48:42 +01008409 capacity *= arch_scale_max_freq_capacity(sd, cpu);
8410 capacity >>= SCHED_CAPACITY_SHIFT;
8411
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01008412 mcc = &cpu_rq(cpu)->rd->max_cpu_capacity;
8413
8414 raw_spin_lock_irqsave(&mcc->lock, flags);
8415 max_capacity = mcc->val;
8416 max_cap_cpu = mcc->cpu;
8417
8418 if ((max_capacity > capacity && max_cap_cpu == cpu) ||
8419 (max_capacity < capacity)) {
8420 mcc->val = capacity;
8421 mcc->cpu = cpu;
8422#ifdef CONFIG_SCHED_DEBUG
8423 raw_spin_unlock_irqrestore(&mcc->lock, flags);
8424 pr_info("CPU%d: update max cpu_capacity %lu\n", cpu, capacity);
8425 goto skip_unlock;
8426#endif
8427 }
8428 raw_spin_unlock_irqrestore(&mcc->lock, flags);
8429
8430skip_unlock: __attribute__ ((unused));
Nicolas Pitreced549f2014-05-26 18:19:38 -04008431 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008432 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008433
Nicolas Pitreced549f2014-05-26 18:19:38 -04008434 if (!capacity)
8435 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008436
Nicolas Pitreced549f2014-05-26 18:19:38 -04008437 cpu_rq(cpu)->cpu_capacity = capacity;
8438 sdg->sgc->capacity = capacity;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008439 sdg->sgc->max_capacity = capacity;
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008440 sdg->sgc->min_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008441}
8442
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008443void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008444{
8445 struct sched_domain *child = sd->child;
8446 struct sched_group *group, *sdg = sd->groups;
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008447 unsigned long capacity, max_capacity, min_capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01008448 unsigned long interval;
8449
8450 interval = msecs_to_jiffies(sd->balance_interval);
8451 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008452 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008453
8454 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04008455 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008456 return;
8457 }
8458
Vincent Guittotdc7ff762015-03-03 11:35:03 +01008459 capacity = 0;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008460 max_capacity = 0;
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008461 min_capacity = ULONG_MAX;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008462
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008463 if (child->flags & SD_OVERLAP) {
8464 /*
8465 * SD_OVERLAP domains cannot assume that child groups
8466 * span the current group.
8467 */
8468
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008469 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008470 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308471 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008472
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308473 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008474 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308475 * gets here before we've attached the domains to the
8476 * runqueues.
8477 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04008478 * Use capacity_of(), which is set irrespective of domains
8479 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308480 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01008481 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308482 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308483 */
8484 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04008485 capacity += capacity_of(cpu);
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008486 } else {
8487 sgc = rq->sd->groups->sgc;
8488 capacity += sgc->capacity;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308489 }
8490
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008491 max_capacity = max(capacity, max_capacity);
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008492 min_capacity = min(capacity, min_capacity);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008493 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008494 } else {
8495 /*
8496 * !SD_OVERLAP domains can assume that child groups
8497 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09008498 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008499
8500 group = child->groups;
8501 do {
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008502 struct sched_group_capacity *sgc = group->sgc;
8503
8504 capacity += sgc->capacity;
8505 max_capacity = max(sgc->max_capacity, max_capacity);
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008506 min_capacity = min(sgc->min_capacity, min_capacity);
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008507 group = group->next;
8508 } while (group != child->groups);
8509 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008510
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008511 sdg->sgc->capacity = capacity;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00008512 sdg->sgc->max_capacity = max_capacity;
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01008513 sdg->sgc->min_capacity = min_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008514}
8515
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008516/*
Vincent Guittotea678212015-02-27 16:54:11 +01008517 * Check whether the capacity of the rq has been noticeably reduced by side
8518 * activity. The imbalance_pct is used for the threshold.
8519 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008520 */
8521static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01008522check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008523{
Vincent Guittotea678212015-02-27 16:54:11 +01008524 return ((rq->cpu_capacity * sd->imbalance_pct) <
8525 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008526}
8527
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008528/*
8529 * Group imbalance indicates (and tries to solve) the problem where balancing
8530 * groups is inadequate due to tsk_cpus_allowed() constraints.
8531 *
8532 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
8533 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
8534 * Something like:
8535 *
8536 * { 0 1 2 3 } { 4 5 6 7 }
8537 * * * * *
8538 *
8539 * If we were to balance group-wise we'd place two tasks in the first group and
8540 * two tasks in the second group. Clearly this is undesired as it will overload
8541 * cpu 3 and leave one of the cpus in the second group unused.
8542 *
8543 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02008544 * by noticing the lower domain failed to reach balance and had difficulty
8545 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008546 *
8547 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05308548 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02008549 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008550 * to create an effective group imbalance.
8551 *
8552 * This is a somewhat tricky proposition since the next run might not find the
8553 * group imbalance and decide the groups need to be balanced again. A most
8554 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008555 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008556
Peter Zijlstra62633222013-08-19 12:41:09 +02008557static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008558{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008559 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008560}
8561
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008562/*
Vincent Guittotea678212015-02-27 16:54:11 +01008563 * group_has_capacity returns true if the group has spare capacity that could
8564 * be used by some tasks.
8565 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008566 * smaller than the number of CPUs or if the utilization is lower than the
8567 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01008568 * For the latter, we use a threshold to stabilize the state, to take into
8569 * account the variance of the tasks' load and to return true if the available
8570 * capacity in meaningful for the load balancer.
8571 * As an example, an available capacity of 1% can appear but it doesn't make
8572 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008573 */
Vincent Guittotea678212015-02-27 16:54:11 +01008574static inline bool
8575group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008576{
Vincent Guittotea678212015-02-27 16:54:11 +01008577 if (sgs->sum_nr_running < sgs->group_weight)
8578 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008579
Vincent Guittotea678212015-02-27 16:54:11 +01008580 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008581 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01008582 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008583
Vincent Guittotea678212015-02-27 16:54:11 +01008584 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008585}
8586
Vincent Guittotea678212015-02-27 16:54:11 +01008587/*
8588 * group_is_overloaded returns true if the group has more tasks than it can
8589 * handle.
8590 * group_is_overloaded is not equals to !group_has_capacity because a group
8591 * with the exact right number of tasks, has no more spare capacity but is not
8592 * overloaded so both group_has_capacity and group_is_overloaded return
8593 * false.
8594 */
8595static inline bool
8596group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04008597{
Vincent Guittotea678212015-02-27 16:54:11 +01008598 if (sgs->sum_nr_running <= sgs->group_weight)
8599 return false;
8600
8601 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008602 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01008603 return true;
8604
8605 return false;
8606}
8607
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00008608/*
8609 * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller
8610 * per-cpu capacity than sched_group ref.
8611 */
8612static inline bool
8613group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref)
8614{
8615 return sg->sgc->max_capacity + capacity_margin - SCHED_CAPACITY_SCALE <
8616 ref->sgc->max_capacity;
8617}
8618
Leo Yan79a89f92015-09-15 18:56:45 +08008619static inline enum
8620group_type group_classify(struct sched_group *group,
8621 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01008622{
8623 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04008624 return group_overloaded;
8625
8626 if (sg_imbalanced(group))
8627 return group_imbalanced;
8628
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00008629 if (sgs->group_misfit_task)
8630 return group_misfit_task;
8631
Rik van Rielcaeb1782014-07-28 14:16:28 -04008632 return group_other;
8633}
8634
Chris Redpath5f97ef92017-04-28 15:23:33 +01008635#ifdef CONFIG_NO_HZ_COMMON
8636/*
8637 * idle load balancing data
8638 * - used by the nohz balance, but we want it available here
8639 * so that we can see which CPUs have no tick.
8640 */
8641static struct {
8642 cpumask_var_t idle_cpus_mask;
8643 atomic_t nr_cpus;
8644 unsigned long next_balance; /* in jiffy units */
8645} nohz ____cacheline_aligned;
8646
8647static inline void update_cpu_stats_if_tickless(struct rq *rq)
8648{
8649 /* only called from update_sg_lb_stats when irqs are disabled */
8650 if (cpumask_test_cpu(rq->cpu, nohz.idle_cpus_mask)) {
8651 /* rate limit updates to once-per-jiffie at most */
8652 if (READ_ONCE(jiffies) <= rq->last_load_update_tick)
8653 return;
8654
8655 raw_spin_lock(&rq->lock);
8656 update_rq_clock(rq);
Leo Yan0db9eac2017-03-27 15:00:14 +01008657 cpu_load_update_idle(rq);
Chris Redpath5f97ef92017-04-28 15:23:33 +01008658 update_cfs_rq_load_avg(rq->clock_task, &rq->cfs, false);
8659 raw_spin_unlock(&rq->lock);
8660 }
8661}
8662
8663#else
8664static inline void update_cpu_stats_if_tickless(struct rq *rq) { }
8665#endif
8666
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008667/**
8668 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
8669 * @env: The load balancing environment.
8670 * @group: sched_group whose statistics are to be updated.
8671 * @load_idx: Load index of sched_domain of this_cpu for load calc.
8672 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008673 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09008674 * @overload: Indicate more than one runnable task for any CPU.
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008675 * @overutilized: Indicate overutilization for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008676 */
8677static inline void update_sg_lb_stats(struct lb_env *env,
8678 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07008679 int local_group, struct sg_lb_stats *sgs,
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008680 bool *overload, bool *overutilized)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008681{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008682 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05008683 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008684
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008685 memset(sgs, 0, sizeof(*sgs));
8686
Michael Wangb94031302012-07-12 16:10:13 +08008687 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008688 struct rq *rq = cpu_rq(i);
8689
Chris Redpath5f97ef92017-04-28 15:23:33 +01008690 /* if we are entering idle and there are CPUs with
8691 * their tick stopped, do an update for them
8692 */
8693 if (env->idle == CPU_NEWLY_IDLE)
8694 update_cpu_stats_if_tickless(rq);
8695
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008696 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02008697 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02008698 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02008699 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008700 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008701
8702 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008703 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02008704 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07008705
Waiman Longa426f992015-11-25 14:09:38 -05008706 nr_running = rq->nr_running;
8707 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07008708 *overload = true;
8709
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008710#ifdef CONFIG_NUMA_BALANCING
8711 sgs->nr_numa_running += rq->nr_numa_running;
8712 sgs->nr_preferred_running += rq->nr_preferred_running;
8713#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008714 sgs->sum_weighted_load += weighted_cpuload(i);
Waiman Longa426f992015-11-25 14:09:38 -05008715 /*
8716 * No need to call idle_cpu() if nr_running is not 0
8717 */
8718 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008719 sgs->idle_cpus++;
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008720
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00008721 if (cpu_overutilized(i)) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008722 *overutilized = true;
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00008723 if (!sgs->group_misfit_task && rq->misfit_task)
8724 sgs->group_misfit_task = capacity_of(i);
8725 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008726 }
8727
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008728 /* Adjust by relative CPU capacity of the group */
8729 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008730 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008731
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008732 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02008733 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008734
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008735 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008736
Vincent Guittotea678212015-02-27 16:54:11 +01008737 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08008738 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008739}
8740
8741/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10008742 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07008743 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008744 * @sds: sched_domain statistics
8745 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10008746 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10008747 *
8748 * Determine if @sg is a busier group than the previously selected
8749 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02008750 *
8751 * Return: %true if @sg is a busier group than the previously selected
8752 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008753 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008754static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10008755 struct sd_lb_stats *sds,
8756 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008757 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008758{
Rik van Rielcaeb1782014-07-28 14:16:28 -04008759 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10008760
Rik van Rielcaeb1782014-07-28 14:16:28 -04008761 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008762 return true;
8763
Rik van Rielcaeb1782014-07-28 14:16:28 -04008764 if (sgs->group_type < busiest->group_type)
8765 return false;
8766
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00008767 /*
8768 * Candidate sg doesn't face any serious load-balance problems
8769 * so don't pick it if the local sg is already filled up.
8770 */
8771 if (sgs->group_type == group_other &&
8772 !group_has_capacity(env, &sds->local_stat))
8773 return false;
8774
Rik van Rielcaeb1782014-07-28 14:16:28 -04008775 if (sgs->avg_load <= busiest->avg_load)
8776 return false;
8777
Morten Rasmussen942295e2016-10-14 14:41:10 +01008778 if (!(env->sd->flags & SD_ASYM_CPUCAPACITY))
8779 goto asym_packing;
8780
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00008781 /*
Morten Rasmussen942295e2016-10-14 14:41:10 +01008782 * Candidate sg has no more than one task per CPU and
8783 * has higher per-CPU capacity. Migrating tasks to less
8784 * capable CPUs may harm throughput. Maximize throughput,
8785 * power/energy consequences are not considered.
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00008786 */
8787 if (sgs->sum_nr_running <= sgs->group_weight &&
8788 group_smaller_cpu_capacity(sds->local, sg))
8789 return false;
8790
Morten Rasmussen942295e2016-10-14 14:41:10 +01008791asym_packing:
Rik van Rielcaeb1782014-07-28 14:16:28 -04008792 /* This is the busiest node in its class. */
8793 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008794 return true;
8795
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308796 /* No ASYM_PACKING if target cpu is already busy */
8797 if (env->idle == CPU_NOT_IDLE)
8798 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10008799 /*
8800 * ASYM_PACKING needs to move all the work to the lowest
8801 * numbered CPUs in the group, therefore mark all groups
8802 * higher than ourself as busy.
8803 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04008804 if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008805 if (!sds->busiest)
8806 return true;
8807
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308808 /* Prefer to move from highest possible cpu's work */
8809 if (group_first_cpu(sds->busiest) < group_first_cpu(sg))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008810 return true;
8811 }
8812
8813 return false;
8814}
8815
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008816#ifdef CONFIG_NUMA_BALANCING
8817static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
8818{
8819 if (sgs->sum_nr_running > sgs->nr_numa_running)
8820 return regular;
8821 if (sgs->sum_nr_running > sgs->nr_preferred_running)
8822 return remote;
8823 return all;
8824}
8825
8826static inline enum fbq_type fbq_classify_rq(struct rq *rq)
8827{
8828 if (rq->nr_running > rq->nr_numa_running)
8829 return regular;
8830 if (rq->nr_running > rq->nr_preferred_running)
8831 return remote;
8832 return all;
8833}
8834#else
8835static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
8836{
8837 return all;
8838}
8839
8840static inline enum fbq_type fbq_classify_rq(struct rq *rq)
8841{
8842 return regular;
8843}
8844#endif /* CONFIG_NUMA_BALANCING */
8845
Dietmar Eggemann06654992015-07-30 16:53:30 +01008846#define lb_sd_parent(sd) \
8847 (sd->parent && sd->parent->groups != sd->parent->groups->next)
8848
Michael Neuling532cb4c2010-06-08 14:57:02 +10008849/**
Hui Kang461819a2011-10-11 23:00:59 -04008850 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07008851 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008852 * @sds: variable to hold the statistics for this sched_domain.
8853 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008854static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008855{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008856 struct sched_domain *child = env->sd->child;
8857 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008858 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008859 int load_idx, prefer_sibling = 0;
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008860 bool overload = false, overutilized = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008861
8862 if (child && child->flags & SD_PREFER_SIBLING)
8863 prefer_sibling = 1;
8864
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008865 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008866
8867 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008868 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008869 int local_group;
8870
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008871 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008872 if (local_group) {
8873 sds->local = sg;
8874 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008875
8876 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008877 time_after_eq(jiffies, sg->sgc->next_update))
8878 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008879 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008880
Tim Chen4486edd2014-06-23 12:16:49 -07008881 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008882 &overload, &overutilized);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008883
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008884 if (local_group)
8885 goto next_group;
8886
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008887 /*
8888 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01008889 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07008890 * and move all the excess tasks away. We lower the capacity
8891 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01008892 * these excess tasks. The extra check prevents the case where
8893 * you always pull from the heaviest group when it is already
8894 * under-utilized (possible with a large weight task outweighs
8895 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008896 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008897 if (prefer_sibling && sds->local &&
Vincent Guittotea678212015-02-27 16:54:11 +01008898 group_has_capacity(env, &sds->local_stat) &&
8899 (sgs->sum_nr_running > 1)) {
8900 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08008901 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08008902 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008903
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00008904 /*
8905 * Ignore task groups with misfit tasks if local group has no
8906 * capacity or if per-cpu capacity isn't higher.
8907 */
8908 if (sgs->group_type == group_misfit_task &&
8909 (!group_has_capacity(env, &sds->local_stat) ||
8910 !group_smaller_cpu_capacity(sg, sds->local)))
8911 sgs->group_type = group_other;
8912
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008913 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008914 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008915 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008916 }
8917
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008918next_group:
8919 /* Now, start updating sd_lb_stats */
8920 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008921 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008922
Michael Neuling532cb4c2010-06-08 14:57:02 +10008923 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008924 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008925
8926 if (env->sd->flags & SD_NUMA)
8927 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07008928
Morten Rasmussen94beeae2015-07-02 17:16:34 +01008929 env->src_grp_nr_running = sds->busiest_stat.sum_nr_running;
8930
Dietmar Eggemann06654992015-07-30 16:53:30 +01008931 if (!lb_sd_parent(env->sd)) {
Tim Chen4486edd2014-06-23 12:16:49 -07008932 /* update overload indicator if we are at root domain */
8933 if (env->dst_rq->rd->overload != overload)
8934 env->dst_rq->rd->overload = overload;
Tim Chen4486edd2014-06-23 12:16:49 -07008935
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008936 /* Update over-utilization (tipping point, U >= 0) indicator */
Patrick Bellasi8e45d942016-02-10 09:24:36 +00008937 if (env->dst_rq->rd->overutilized != overutilized) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008938 env->dst_rq->rd->overutilized = overutilized;
Patrick Bellasi8e45d942016-02-10 09:24:36 +00008939 trace_sched_overutilized(overutilized);
8940 }
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008941 } else {
Patrick Bellasi8e45d942016-02-10 09:24:36 +00008942 if (!env->dst_rq->rd->overutilized && overutilized) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008943 env->dst_rq->rd->overutilized = true;
Patrick Bellasi8e45d942016-02-10 09:24:36 +00008944 trace_sched_overutilized(true);
8945 }
Morten Rasmussena562dfc2015-05-09 16:49:57 +01008946 }
Patrick Bellasi8e45d942016-02-10 09:24:36 +00008947
Michael Neuling532cb4c2010-06-08 14:57:02 +10008948}
8949
Michael Neuling532cb4c2010-06-08 14:57:02 +10008950/**
8951 * check_asym_packing - Check to see if the group is packed into the
8952 * sched doman.
8953 *
8954 * This is primarily intended to used at the sibling level. Some
8955 * cores like POWER7 prefer to use lower numbered SMT threads. In the
8956 * case of POWER7, it can move to lower SMT modes only when higher
8957 * threads are idle. When in lower SMT modes, the threads will
8958 * perform better since they share less core resources. Hence when we
8959 * have idle threads, we want them to be the higher ones.
8960 *
8961 * This packing function is run on idle threads. It checks to see if
8962 * the busiest CPU in this domain (core in the P7 case) has a higher
8963 * CPU number than the packing function is being run on. Here we are
8964 * assuming lower CPU number will be equivalent to lower a SMT thread
8965 * number.
8966 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02008967 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10008968 * this CPU. The amount of the imbalance is returned in *imbalance.
8969 *
Randy Dunlapcd968912012-06-08 13:18:33 -07008970 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008971 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10008972 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008973static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008974{
8975 int busiest_cpu;
8976
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008977 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008978 return 0;
8979
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308980 if (env->idle == CPU_NOT_IDLE)
8981 return 0;
8982
Michael Neuling532cb4c2010-06-08 14:57:02 +10008983 if (!sds->busiest)
8984 return 0;
8985
8986 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008987 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008988 return 0;
8989
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008990 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008991 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008992 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008993
Michael Neuling532cb4c2010-06-08 14:57:02 +10008994 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008995}
8996
8997/**
8998 * fix_small_imbalance - Calculate the minor imbalance that exists
8999 * amongst the groups of a sched_domain, during
9000 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07009001 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009002 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009003 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009004static inline
9005void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009006{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009007 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009008 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009009 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009010 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009011
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009012 local = &sds->local_stat;
9013 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009014
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009015 if (!local->sum_nr_running)
9016 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
9017 else if (busiest->load_per_task > local->load_per_task)
9018 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009019
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009020 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009021 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009022 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009023
Vladimir Davydov3029ede2013-09-15 17:49:14 +04009024 if (busiest->avg_load + scaled_busy_load_per_task >=
9025 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009026 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009027 return;
9028 }
9029
9030 /*
9031 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04009032 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009033 * moving them.
9034 */
9035
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009036 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009037 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009038 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009039 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009040 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009041
9042 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01009043 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009044 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009045 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01009046 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009047 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009048
9049 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009050 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009051 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009052 tmp = (busiest->avg_load * busiest->group_capacity) /
9053 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009054 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009055 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009056 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009057 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009058 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02009059 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009060 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009061
9062 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009063 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009064 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009065}
9066
9067/**
9068 * calculate_imbalance - Calculate the amount of imbalance present within the
9069 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009070 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009071 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009072 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009073static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009074{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009075 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009076 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009077
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009078 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009079 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009080
Rik van Rielcaeb1782014-07-28 14:16:28 -04009081 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02009082 /*
9083 * In the group_imb case we cannot rely on group-wide averages
9084 * to ensure cpu-load equilibrium, look at wider averages. XXX
9085 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009086 busiest->load_per_task =
9087 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009088 }
9089
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009090 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01009091 * Avg load of busiest sg can be less and avg load of local sg can
9092 * be greater than avg load across all sgs of sd because avg load
9093 * factors in sg capacity and sgs with smaller group_type are
9094 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009095 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04009096 if (busiest->avg_load <= sds->avg_load ||
9097 local->avg_load >= sds->avg_load) {
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009098 /* Misfitting tasks should be migrated in any case */
9099 if (busiest->group_type == group_misfit_task) {
9100 env->imbalance = busiest->group_misfit_task;
9101 return;
9102 }
9103
9104 /*
9105 * Busiest group is overloaded, local is not, use the spare
9106 * cycles to maximize throughput
9107 */
9108 if (busiest->group_type == group_overloaded &&
9109 local->group_type <= group_misfit_task) {
9110 env->imbalance = busiest->load_per_task;
9111 return;
9112 }
9113
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009114 env->imbalance = 0;
9115 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009116 }
9117
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02009118 /*
9119 * If there aren't any idle cpus, avoid creating some.
9120 */
9121 if (busiest->group_type == group_overloaded &&
9122 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02009123 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01009124 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01009125 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01009126 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01009127 load_above_capacity /= busiest->group_capacity;
9128 } else
Vincent Guittotea678212015-02-27 16:54:11 +01009129 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009130 }
9131
9132 /*
9133 * We're trying to get all the cpus to the average_load, so we don't
9134 * want to push ourselves above the average load, nor do we wish to
9135 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01009136 * we also don't want to reduce the group load below the group
9137 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08009138 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02009139 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009140
9141 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009142 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009143 max_pull * busiest->group_capacity,
9144 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009145 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009146
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009147 /* Boost imbalance to allow misfit task to be balanced. */
9148 if (busiest->group_type == group_misfit_task)
9149 env->imbalance = max_t(long, env->imbalance,
9150 busiest->group_misfit_task);
9151
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009152 /*
9153 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009154 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009155 * a think about bumping its value to force at least one task to be
9156 * moved
9157 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009158 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009159 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009160}
Nikhil Raofab47622010-10-15 13:12:29 -07009161
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009162/******* find_busiest_group() helpers end here *********************/
9163
9164/**
9165 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01009166 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009167 *
9168 * Also calculates the amount of weighted load which should be moved
9169 * to restore balance.
9170 *
Randy Dunlapcd968912012-06-08 13:18:33 -07009171 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009172 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02009173 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009174 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009175static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009176{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009177 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009178 struct sd_lb_stats sds;
9179
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02009180 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009181
9182 /*
9183 * Compute the various statistics relavent for load balancing at
9184 * this level.
9185 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009186 update_sd_lb_stats(env, &sds);
Dietmar Eggemann53065e82015-05-10 15:17:32 +01009187
9188 if (energy_aware() && !env->dst_rq->rd->overutilized)
9189 goto out_balanced;
9190
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009191 local = &sds.local_stat;
9192 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009193
Vincent Guittotea678212015-02-27 16:54:11 +01009194 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05309195 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10009196 return sds.busiest;
9197
Peter Zijlstracc57aa82011-02-21 18:55:32 +01009198 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009199 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009200 goto out_balanced;
9201
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04009202 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
9203 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07009204
Peter Zijlstra866ab432011-02-21 18:56:47 +01009205 /*
9206 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02009207 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01009208 * isn't true due to cpus_allowed constraints and the like.
9209 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04009210 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01009211 goto force_balance;
9212
Brendan Jackmane019d8f2017-10-05 11:58:54 +01009213 /*
9214 * When dst_cpu is idle, prevent SMP nice and/or asymmetric group
9215 * capacities from resulting in underutilization due to avg_load.
9216 */
9217 if (env->idle != CPU_NOT_IDLE && group_has_capacity(env, local) &&
Vincent Guittotea678212015-02-27 16:54:11 +01009218 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07009219 goto force_balance;
9220
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009221 /* Misfitting tasks should be dealt with regardless of the avg load */
9222 if (busiest->group_type == group_misfit_task) {
9223 goto force_balance;
9224 }
9225
Peter Zijlstracc57aa82011-02-21 18:55:32 +01009226 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04009227 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01009228 * don't try and pull any tasks.
9229 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009230 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009231 goto out_balanced;
9232
Peter Zijlstracc57aa82011-02-21 18:55:32 +01009233 /*
9234 * Don't pull any tasks if this group is already above the domain
9235 * average load.
9236 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009237 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009238 goto out_balanced;
9239
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009240 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07009241 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02009242 * This cpu is idle. If the busiest group is not overloaded
9243 * and there is no imbalance between this and busiest group
9244 * wrt idle cpus, it is balanced. The imbalance becomes
9245 * significant if the diff is greater than 1 otherwise we
9246 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07009247 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02009248 if ((busiest->group_type != group_overloaded) &&
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009249 (local->idle_cpus <= (busiest->idle_cpus + 1)) &&
9250 !group_smaller_cpu_capacity(sds.busiest, sds.local))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07009251 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01009252 } else {
9253 /*
9254 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
9255 * imbalance_pct to be conservative.
9256 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09009257 if (100 * busiest->avg_load <=
9258 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01009259 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07009260 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009261
Nikhil Raofab47622010-10-15 13:12:29 -07009262force_balance:
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009263 env->busiest_group_type = busiest->group_type;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009264 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009265 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009266 return sds.busiest;
9267
9268out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009269 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009270 return NULL;
9271}
9272
9273/*
9274 * find_busiest_queue - find the busiest runqueue among the cpus in group.
9275 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009276static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08009277 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009278{
9279 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04009280 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009281 int i;
9282
Peter Zijlstra6906a402013-08-19 15:20:21 +02009283 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01009284 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01009285 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009286
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01009287 rq = cpu_rq(i);
9288 rt = fbq_classify_rq(rq);
9289
9290 /*
9291 * We classify groups/runqueues into three groups:
9292 * - regular: there are !numa tasks
9293 * - remote: there are numa tasks that run on the 'wrong' node
9294 * - all: there is no distinction
9295 *
9296 * In order to avoid migrating ideally placed numa tasks,
9297 * ignore those when there's better options.
9298 *
9299 * If we ignore the actual busiest queue to migrate another
9300 * task, the next balance pass can still reduce the busiest
9301 * queue by moving tasks around inside the node.
9302 *
9303 * If we cannot move enough load due to this classification
9304 * the next pass will adjust the group classification and
9305 * allow migration of more tasks.
9306 *
9307 * Both cases only affect the total convergence complexity.
9308 */
9309 if (rt > env->fbq_type)
9310 continue;
9311
Nicolas Pitreced549f2014-05-26 18:19:38 -04009312 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10009313
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01009314 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009315
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01009316 /*
9317 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04009318 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01009319 */
Vincent Guittotea678212015-02-27 16:54:11 +01009320
9321 if (rq->nr_running == 1 && wl > env->imbalance &&
Morten Rasmussenb19cdb92016-02-25 12:51:35 +00009322 !check_cpu_capacity(rq, env->sd) &&
9323 env->busiest_group_type != group_misfit_task)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009324 continue;
9325
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01009326 /*
9327 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04009328 * the weighted_cpuload() scaled with the cpu capacity, so
9329 * that the load can be moved away from the cpu that is
9330 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09009331 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04009332 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09009333 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04009334 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
9335 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01009336 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04009337 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09009338 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04009339 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009340 busiest = rq;
9341 }
9342 }
9343
9344 return busiest;
9345}
9346
9347/*
9348 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
9349 * so long as it is large enough.
9350 */
9351#define MAX_PINNED_INTERVAL 512
9352
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009353static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01009354{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009355 struct sched_domain *sd = env->sd;
9356
9357 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10009358
9359 /*
9360 * ASYM_PACKING needs to force migrate tasks from busy but
9361 * higher numbered CPUs in order to pack all tasks in the
9362 * lowest numbered CPUs.
9363 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009364 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10009365 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01009366 }
9367
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009368 /*
9369 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
9370 * It's worth migrating the task if the src_cpu's capacity is reduced
9371 * because of other sched_class or IRQs if more capacity stays
9372 * available on dst_cpu.
9373 */
9374 if ((env->idle != CPU_NOT_IDLE) &&
9375 (env->src_rq->cfs.h_nr_running == 1)) {
9376 if ((check_cpu_capacity(env->src_rq, sd)) &&
9377 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
9378 return 1;
9379 }
9380
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00009381 if ((capacity_of(env->src_cpu) < capacity_of(env->dst_cpu)) &&
Joonwoo Parka8611932016-10-21 13:39:31 -07009382 ((capacity_orig_of(env->src_cpu) < capacity_orig_of(env->dst_cpu))) &&
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00009383 env->src_rq->cfs.h_nr_running == 1 &&
9384 cpu_overutilized(env->src_cpu) &&
9385 !cpu_overutilized(env->dst_cpu)) {
9386 return 1;
9387 }
9388
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01009389 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
9390}
9391
Tejun Heo969c7922010-05-06 18:49:21 +02009392static int active_load_balance_cpu_stop(void *data);
9393
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009394static int should_we_balance(struct lb_env *env)
9395{
9396 struct sched_group *sg = env->sd->groups;
9397 struct cpumask *sg_cpus, *sg_mask;
9398 int cpu, balance_cpu = -1;
9399
9400 /*
9401 * In the newly idle case, we will allow all the cpu's
9402 * to do the newly idle load balance.
9403 */
9404 if (env->idle == CPU_NEWLY_IDLE)
9405 return 1;
9406
9407 sg_cpus = sched_group_cpus(sg);
9408 sg_mask = sched_group_mask(sg);
9409 /* Try to find first idle cpu */
9410 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
9411 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
9412 continue;
9413
9414 balance_cpu = cpu;
9415 break;
9416 }
9417
9418 if (balance_cpu == -1)
9419 balance_cpu = group_balance_cpu(sg);
9420
9421 /*
9422 * First idle cpu or the first cpu(busiest) in this sched group
9423 * is eligible for doing load balancing at this and above domains.
9424 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09009425 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009426}
9427
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009428/*
9429 * Check this_cpu to ensure it is balanced within domain. Attempt to move
9430 * tasks if there is an imbalance.
9431 */
9432static int load_balance(int this_cpu, struct rq *this_rq,
9433 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009434 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009435{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309436 int ld_moved, cur_ld_moved, active_balance = 0;
Dietmar Eggemann06654992015-07-30 16:53:30 +01009437 struct sched_domain *sd_parent = lb_sd_parent(sd) ? sd->parent : NULL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009438 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009439 struct rq *busiest;
9440 unsigned long flags;
Christoph Lameter4ba29682014-08-26 19:12:21 -05009441 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009442
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009443 struct lb_env env = {
9444 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01009445 .dst_cpu = this_cpu,
9446 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309447 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009448 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02009449 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08009450 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01009451 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04009452 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009453 };
9454
Joonsoo Kimcfc03112013-04-23 17:27:39 +09009455 /*
9456 * For NEWLY_IDLE load_balancing, we don't need to consider
9457 * other cpus in our group
9458 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09009459 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09009460 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09009461
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009462 cpumask_copy(cpus, cpu_active_mask);
9463
Josh Poimboeufae928822016-06-17 12:43:24 -05009464 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009465
9466redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009467 if (!should_we_balance(&env)) {
9468 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009469 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009470 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009471
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009472 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009473 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05009474 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009475 goto out_balanced;
9476 }
9477
Michael Wangb94031302012-07-12 16:10:13 +08009478 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009479 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05009480 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009481 goto out_balanced;
9482 }
9483
Michael Wang78feefc2012-08-06 16:41:59 +08009484 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009485
Josh Poimboeufae928822016-06-17 12:43:24 -05009486 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009487
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009488 env.src_cpu = busiest->cpu;
9489 env.src_rq = busiest;
9490
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009491 ld_moved = 0;
9492 if (busiest->nr_running > 1) {
9493 /*
9494 * Attempt to move tasks. If find_busiest_group has found
9495 * an imbalance but busiest->nr_running <= 1, the group is
9496 * still unbalanced. ld_moved simply stays zero, so it is
9497 * correctly treated as an imbalance.
9498 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009499 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02009500 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009501
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01009502more_balance:
Kirill Tkhai163122b2014-08-20 13:48:29 +04009503 raw_spin_lock_irqsave(&busiest->lock, flags);
Peter Zijlstra0aed57e2016-10-03 16:35:32 +02009504 update_rq_clock(busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309505
9506 /*
9507 * cur_ld_moved - load moved in current iteration
9508 * ld_moved - cumulative load moved across iterations
9509 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04009510 cur_ld_moved = detach_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009511
9512 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04009513 * We've detached some tasks from busiest_rq. Every
9514 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
9515 * unlock busiest->lock, and we are able to be sure
9516 * that nobody can manipulate the tasks in parallel.
9517 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009518 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04009519
9520 raw_spin_unlock(&busiest->lock);
9521
9522 if (cur_ld_moved) {
9523 attach_tasks(&env);
9524 ld_moved += cur_ld_moved;
9525 }
9526
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009527 local_irq_restore(flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309528
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09009529 if (env.flags & LBF_NEED_BREAK) {
9530 env.flags &= ~LBF_NEED_BREAK;
9531 goto more_balance;
9532 }
9533
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309534 /*
9535 * Revisit (affine) tasks on src_cpu that couldn't be moved to
9536 * us and move them to an alternate dst_cpu in our sched_group
9537 * where they can run. The upper limit on how many times we
9538 * iterate on same src_cpu is dependent on number of cpus in our
9539 * sched_group.
9540 *
9541 * This changes load balance semantics a bit on who can move
9542 * load to a given_cpu. In addition to the given_cpu itself
9543 * (or a ilb_cpu acting on its behalf where given_cpu is
9544 * nohz-idle), we now have balance_cpu in a position to move
9545 * load to given_cpu. In rare situations, this may cause
9546 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
9547 * _independently_ and at _same_ time to move some load to
9548 * given_cpu) causing exceess load to be moved to given_cpu.
9549 * This however should not happen so much in practice and
9550 * moreover subsequent load balance cycles should correct the
9551 * excess load moved.
9552 */
Peter Zijlstra62633222013-08-19 12:41:09 +02009553 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309554
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04009555 /* Prevent to re-select dst_cpu via env's cpus */
9556 cpumask_clear_cpu(env.dst_cpu, env.cpus);
9557
Michael Wang78feefc2012-08-06 16:41:59 +08009558 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309559 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02009560 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309561 env.loop = 0;
9562 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09009563
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309564 /*
9565 * Go back to "more_balance" rather than "redo" since we
9566 * need to continue with same src_cpu.
9567 */
9568 goto more_balance;
9569 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009570
Peter Zijlstra62633222013-08-19 12:41:09 +02009571 /*
9572 * We failed to reach balance because of affinity.
9573 */
9574 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009575 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02009576
Vincent Guittotafdeee02014-08-26 13:06:44 +02009577 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02009578 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02009579 }
9580
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009581 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009582 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009583 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05309584 if (!cpumask_empty(cpus)) {
9585 env.loop = 0;
9586 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009587 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05309588 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02009589 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009590 }
9591 }
9592
9593 if (!ld_moved) {
Josh Poimboeufae928822016-06-17 12:43:24 -05009594 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07009595 /*
9596 * Increment the failure counter only on periodic balance.
9597 * We do not want newidle balance, which can be very
9598 * frequent, pollute the failure counter causing
9599 * excessive cache_hot migrations and active balances.
9600 */
9601 if (idle != CPU_NEWLY_IDLE)
Morten Rasmussen94beeae2015-07-02 17:16:34 +01009602 if (env.src_grp_nr_running > 1)
9603 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009604
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009605 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009606 raw_spin_lock_irqsave(&busiest->lock, flags);
9607
Tejun Heo969c7922010-05-06 18:49:21 +02009608 /* don't kick the active_load_balance_cpu_stop,
9609 * if the curr task on busiest cpu can't be
9610 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009611 */
9612 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02009613 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009614 raw_spin_unlock_irqrestore(&busiest->lock,
9615 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009616 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009617 goto out_one_pinned;
9618 }
9619
Tejun Heo969c7922010-05-06 18:49:21 +02009620 /*
9621 * ->active_balance synchronizes accesses to
9622 * ->active_balance_work. Once set, it's cleared
9623 * only after active load balance is finished.
9624 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009625 if (!busiest->active_balance) {
9626 busiest->active_balance = 1;
9627 busiest->push_cpu = this_cpu;
9628 active_balance = 1;
9629 }
9630 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02009631
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009632 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02009633 stop_one_cpu_nowait(cpu_of(busiest),
9634 active_load_balance_cpu_stop, busiest,
9635 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009636 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009637
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309638 /* We've kicked active balancing, force task migration. */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009639 sd->nr_balance_failed = sd->cache_nice_tries+1;
9640 }
9641 } else
9642 sd->nr_balance_failed = 0;
9643
9644 if (likely(!active_balance)) {
9645 /* We were unbalanced, so reset the balancing interval */
9646 sd->balance_interval = sd->min_interval;
9647 } else {
9648 /*
9649 * If we've begun active balancing, start to back off. This
9650 * case may not be covered by the all_pinned logic if there
9651 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04009652 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009653 */
9654 if (sd->balance_interval < sd->max_interval)
9655 sd->balance_interval *= 2;
9656 }
9657
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009658 goto out;
9659
9660out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02009661 /*
9662 * We reach balance although we may have faced some affinity
9663 * constraints. Clear the imbalance flag if it was set.
9664 */
9665 if (sd_parent) {
9666 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
9667
9668 if (*group_imbalance)
9669 *group_imbalance = 0;
9670 }
9671
9672out_all_pinned:
9673 /*
9674 * We reach balance because all tasks are pinned at this level so
9675 * we can't migrate them. Let the imbalance flag set so parent level
9676 * can try to migrate them.
9677 */
Josh Poimboeufae928822016-06-17 12:43:24 -05009678 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009679
9680 sd->nr_balance_failed = 0;
9681
9682out_one_pinned:
9683 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009684 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02009685 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009686 (sd->balance_interval < sd->max_interval))
9687 sd->balance_interval *= 2;
9688
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08009689 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009690out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009691 return ld_moved;
9692}
9693
Jason Low52a08ef2014-05-08 17:49:22 -07009694static inline unsigned long
9695get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
9696{
9697 unsigned long interval = sd->balance_interval;
9698
9699 if (cpu_busy)
9700 interval *= sd->busy_factor;
9701
9702 /* scale ms to jiffies */
9703 interval = msecs_to_jiffies(interval);
9704 interval = clamp(interval, 1UL, max_load_balance_interval);
9705
9706 return interval;
9707}
9708
9709static inline void
Leo Yan31851a92016-08-05 14:31:29 +08009710update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07009711{
9712 unsigned long interval, next;
9713
Leo Yan31851a92016-08-05 14:31:29 +08009714 /* used by idle balance, so cpu_busy = 0 */
9715 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07009716 next = sd->last_balance + interval;
9717
9718 if (time_after(*next_balance, next))
9719 *next_balance = next;
9720}
9721
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009722/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009723 * idle_balance is called by schedule() if this_cpu is about to become
9724 * idle. Attempts to pull tasks from other CPUs.
9725 */
Peter Zijlstra6e831252014-02-11 16:11:48 +01009726static int idle_balance(struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009727{
Jason Low52a08ef2014-05-08 17:49:22 -07009728 unsigned long next_balance = jiffies + HZ;
9729 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009730 struct sched_domain *sd;
9731 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07009732 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009733
Peter Zijlstra6e831252014-02-11 16:11:48 +01009734 /*
9735 * We must set idle_stamp _before_ calling idle_balance(), such that we
9736 * measure the duration of idle_balance() as idle time.
9737 */
9738 this_rq->idle_stamp = rq_clock(this_rq);
9739
Dietmar Eggemann785367f2016-01-13 15:49:44 +00009740 if (!energy_aware() &&
9741 (this_rq->avg_idle < sysctl_sched_migration_cost ||
9742 !this_rq->rd->overload)) {
Jason Low52a08ef2014-05-08 17:49:22 -07009743 rcu_read_lock();
9744 sd = rcu_dereference_check_sched_domain(this_rq->sd);
9745 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08009746 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07009747 rcu_read_unlock();
9748
Peter Zijlstra6e831252014-02-11 16:11:48 +01009749 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07009750 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009751
Peter Zijlstraf492e122009-12-23 15:29:42 +01009752 raw_spin_unlock(&this_rq->lock);
9753
Paul Turner48a16752012-10-04 13:18:31 +02009754 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02009755 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009756 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009757 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07009758 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009759
9760 if (!(sd->flags & SD_LOAD_BALANCE))
9761 continue;
9762
Jason Low52a08ef2014-05-08 17:49:22 -07009763 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08009764 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07009765 break;
Jason Low52a08ef2014-05-08 17:49:22 -07009766 }
Jason Low9bd721c2013-09-13 11:26:52 -07009767
Peter Zijlstraf492e122009-12-23 15:29:42 +01009768 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07009769 t0 = sched_clock_cpu(this_cpu);
9770
Peter Zijlstraf492e122009-12-23 15:29:42 +01009771 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009772 sd, CPU_NEWLY_IDLE,
9773 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07009774
9775 domain_cost = sched_clock_cpu(this_cpu) - t0;
9776 if (domain_cost > sd->max_newidle_lb_cost)
9777 sd->max_newidle_lb_cost = domain_cost;
9778
9779 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01009780 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009781
Leo Yan31851a92016-08-05 14:31:29 +08009782 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07009783
9784 /*
9785 * Stop searching for tasks to pull if there are
9786 * now runnable tasks on this rq.
9787 */
9788 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009789 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009790 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009791 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01009792
9793 raw_spin_lock(&this_rq->lock);
9794
Jason Low0e5b5332014-04-28 15:45:54 -07009795 if (curr_cost > this_rq->max_idle_balance_cost)
9796 this_rq->max_idle_balance_cost = curr_cost;
9797
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009798 /*
Jason Low0e5b5332014-04-28 15:45:54 -07009799 * While browsing the domains, we released the rq lock, a task could
9800 * have been enqueued in the meantime. Since we're not going idle,
9801 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009802 */
Jason Low0e5b5332014-04-28 15:45:54 -07009803 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01009804 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009805
Peter Zijlstra6e831252014-02-11 16:11:48 +01009806out:
Jason Low52a08ef2014-05-08 17:49:22 -07009807 /* Move the next balance forward */
9808 if (time_after(this_rq->next_balance, next_balance))
9809 this_rq->next_balance = next_balance;
9810
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04009811 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04009812 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04009813 pulled_task = -1;
9814
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01009815 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01009816 this_rq->idle_stamp = 0;
9817
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01009818 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009819}
9820
9821/*
Tejun Heo969c7922010-05-06 18:49:21 +02009822 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
9823 * running tasks off the busiest CPU onto idle CPUs. It requires at
9824 * least 1 task to be running on each physical CPU where possible, and
9825 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009826 */
Tejun Heo969c7922010-05-06 18:49:21 +02009827static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009828{
Tejun Heo969c7922010-05-06 18:49:21 +02009829 struct rq *busiest_rq = data;
9830 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009831 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02009832 struct rq *target_rq = cpu_rq(target_cpu);
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009833 struct sched_domain *sd = NULL;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009834 struct task_struct *p = NULL;
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009835 struct task_struct *push_task = NULL;
9836 int push_task_detached = 0;
9837 struct lb_env env = {
9838 .sd = sd,
9839 .dst_cpu = target_cpu,
9840 .dst_rq = target_rq,
9841 .src_cpu = busiest_rq->cpu,
9842 .src_rq = busiest_rq,
9843 .idle = CPU_IDLE,
9844 };
Tejun Heo969c7922010-05-06 18:49:21 +02009845
9846 raw_spin_lock_irq(&busiest_rq->lock);
9847
9848 /* make sure the requested cpu hasn't gone down in the meantime */
9849 if (unlikely(busiest_cpu != smp_processor_id() ||
9850 !busiest_rq->active_balance))
9851 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009852
9853 /* Is there any task to move? */
9854 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02009855 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009856
9857 /*
9858 * This condition is "impossible", if it occurs
9859 * we need to fix it. Originally reported by
9860 * Bjorn Helgaas on a 128-cpu setup.
9861 */
9862 BUG_ON(busiest_rq == target_rq);
9863
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009864 push_task = busiest_rq->push_task;
9865 if (push_task) {
9866 if (task_on_rq_queued(push_task) &&
Prasad Sodagudid3453722014-07-05 13:08:54 +05309867 task_cpu(push_task) == busiest_cpu &&
9868 cpu_online(target_cpu)) {
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009869 detach_task(push_task, &env);
9870 push_task_detached = 1;
9871 }
9872 goto out_unlock;
9873 }
9874
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009875 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02009876 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009877 for_each_domain(target_cpu, sd) {
9878 if ((sd->flags & SD_LOAD_BALANCE) &&
9879 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
9880 break;
9881 }
9882
9883 if (likely(sd)) {
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009884 env.sd = sd;
Josh Poimboeufae928822016-06-17 12:43:24 -05009885 schedstat_inc(sd->alb_count);
Peter Zijlstra0aed57e2016-10-03 16:35:32 +02009886 update_rq_clock(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009887
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009888 p = detach_one_task(&env);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309889 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05009890 schedstat_inc(sd->alb_pushed);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309891 /* Active balancing done, reset the failure counter. */
9892 sd->nr_balance_failed = 0;
9893 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05009894 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309895 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009896 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009897 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02009898out_unlock:
9899 busiest_rq->active_balance = 0;
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009900
9901 if (push_task)
9902 busiest_rq->push_task = NULL;
9903
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009904 raw_spin_unlock(&busiest_rq->lock);
9905
Srivatsa Vaddagiri70e14af2014-03-31 10:34:41 -07009906 if (push_task) {
9907 if (push_task_detached)
9908 attach_one_task(target_rq, push_task);
9909 put_task_struct(push_task);
9910 }
9911
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009912 if (p)
9913 attach_one_task(target_rq, p);
9914
9915 local_irq_enable();
9916
Tejun Heo969c7922010-05-06 18:49:21 +02009917 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009918}
9919
Mike Galbraithd987fc72011-12-05 10:01:47 +01009920static inline int on_null_domain(struct rq *rq)
9921{
9922 return unlikely(!rcu_dereference_sched(rq->sd));
9923}
9924
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009925#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009926/*
9927 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009928 * - When one of the busy CPUs notice that there may be an idle rebalancing
9929 * needed, they will kick the idle load balancer, which then does idle
9930 * load balancing for all the idle CPUs.
9931 */
Daniel Lezcano3dd03372014-01-06 12:34:41 +01009932static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009933{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009934 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009935
Suresh Siddha786d6dc72011-12-01 17:07:35 -08009936 if (ilb < nr_cpu_ids && idle_cpu(ilb))
9937 return ilb;
9938
9939 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009940}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009941
9942/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009943 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
9944 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
9945 * CPU (if there is one).
9946 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01009947static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009948{
9949 int ilb_cpu;
9950
9951 nohz.next_balance++;
9952
Daniel Lezcano3dd03372014-01-06 12:34:41 +01009953 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009954
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009955 if (ilb_cpu >= nr_cpu_ids)
9956 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009957
Suresh Siddhacd490c52011-12-06 11:26:34 -08009958 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08009959 return;
9960 /*
9961 * Use smp_send_reschedule() instead of resched_cpu().
9962 * This way we generate a sched IPI on the target cpu which
9963 * is idle. And the softirq performing nohz idle load balance
9964 * will be run before returning from the IPI.
9965 */
9966 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009967 return;
9968}
9969
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01009970void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08009971{
9972 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01009973 /*
9974 * Completely isolated CPUs don't ever set, so we must test.
9975 */
9976 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
9977 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
9978 atomic_dec(&nohz.nr_cpus);
9979 }
Suresh Siddha71325962012-01-19 18:28:57 -08009980 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
9981 }
9982}
9983
Suresh Siddha69e1e812011-12-01 17:07:33 -08009984static inline void set_cpu_sd_state_busy(void)
9985{
9986 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309987 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08009988
Suresh Siddha69e1e812011-12-01 17:07:33 -08009989 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009990 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02009991
9992 if (!sd || !sd->nohz_idle)
9993 goto unlock;
9994 sd->nohz_idle = 0;
9995
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009996 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02009997unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08009998 rcu_read_unlock();
9999}
10000
10001void set_cpu_sd_state_idle(void)
10002{
10003 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +053010004 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -080010005
Suresh Siddha69e1e812011-12-01 17:07:33 -080010006 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +020010007 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +020010008
10009 if (!sd || sd->nohz_idle)
10010 goto unlock;
10011 sd->nohz_idle = 1;
10012
Peter Zijlstra0e369d72016-05-09 10:38:01 +020010013 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +020010014unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -080010015 rcu_read_unlock();
10016}
10017
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010018/*
Alex Shic1cc0172012-09-10 15:10:58 +080010019 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010020 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010021 */
Alex Shic1cc0172012-09-10 15:10:58 +080010022void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010023{
Suresh Siddha71325962012-01-19 18:28:57 -080010024 /*
10025 * If this cpu is going down, then nothing needs to be done.
10026 */
10027 if (!cpu_active(cpu))
10028 return;
10029
Alex Shic1cc0172012-09-10 15:10:58 +080010030 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
10031 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010032
Mike Galbraithd987fc72011-12-05 10:01:47 +010010033 /*
10034 * If we're a completely isolated CPU, we don't play.
10035 */
10036 if (on_null_domain(cpu_rq(cpu)))
10037 return;
10038
Alex Shic1cc0172012-09-10 15:10:58 +080010039 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
10040 atomic_inc(&nohz.nr_cpus);
10041 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010042}
10043#endif
10044
10045static DEFINE_SPINLOCK(balancing);
10046
Peter Zijlstra49c022e2011-04-05 10:14:25 +020010047/*
10048 * Scale the max load_balance interval with the number of CPUs in the system.
10049 * This trades load-balance latency on larger machines for less cross talk.
10050 */
Peter Zijlstra029632f2011-10-25 10:00:11 +020010051void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +020010052{
10053 max_load_balance_interval = HZ*num_online_cpus()/10;
10054}
10055
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010056/*
10057 * It checks each scheduling domain to see if it is due to be balanced,
10058 * and initiates a balancing operation if so.
10059 *
Libinb9b08532013-04-01 19:14:01 +080010060 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010061 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +010010062static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010063{
Joonsoo Kim23f0d202013-08-06 17:36:42 +090010064 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +010010065 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010066 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +020010067 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010068 /* Earliest time when we have to do rebalance again */
10069 unsigned long next_balance = jiffies + 60*HZ;
10070 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -070010071 int need_serialize, need_decay = 0;
10072 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010073
Paul Turner48a16752012-10-04 13:18:31 +020010074 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -080010075
Peter Zijlstradce840a2011-04-07 14:09:50 +020010076 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010077 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -070010078 /*
10079 * Decay the newidle max times here because this is a regular
10080 * visit to all the domains. Decay ~1% per second.
10081 */
10082 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
10083 sd->max_newidle_lb_cost =
10084 (sd->max_newidle_lb_cost * 253) / 256;
10085 sd->next_decay_max_lb_cost = jiffies + HZ;
10086 need_decay = 1;
10087 }
10088 max_cost += sd->max_newidle_lb_cost;
10089
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010090 if (!(sd->flags & SD_LOAD_BALANCE))
10091 continue;
10092
Jason Lowf48627e2013-09-13 11:26:53 -070010093 /*
10094 * Stop the load balance at this level. There is another
10095 * CPU in our sched group which is doing load balancing more
10096 * actively.
10097 */
10098 if (!continue_balancing) {
10099 if (need_decay)
10100 continue;
10101 break;
10102 }
10103
Jason Low52a08ef2014-05-08 17:49:22 -070010104 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010105
10106 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010107 if (need_serialize) {
10108 if (!spin_trylock(&balancing))
10109 goto out;
10110 }
10111
10112 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +090010113 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010114 /*
Peter Zijlstra62633222013-08-19 12:41:09 +020010115 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +090010116 * env->dst_cpu, so we can't know our idle
10117 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010118 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +090010119 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010120 }
10121 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -070010122 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010123 }
10124 if (need_serialize)
10125 spin_unlock(&balancing);
10126out:
10127 if (time_after(next_balance, sd->last_balance + interval)) {
10128 next_balance = sd->last_balance + interval;
10129 update_next_balance = 1;
10130 }
Jason Lowf48627e2013-09-13 11:26:53 -070010131 }
10132 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010133 /*
Jason Lowf48627e2013-09-13 11:26:53 -070010134 * Ensure the rq-wide value also decays but keep it at a
10135 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010136 */
Jason Lowf48627e2013-09-13 11:26:53 -070010137 rq->max_idle_balance_cost =
10138 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010139 }
Peter Zijlstradce840a2011-04-07 14:09:50 +020010140 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010141
10142 /*
10143 * next_balance will be updated only when there is a need.
10144 * When the cpu is attached to null domain for ex, it will not be
10145 * updated.
10146 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +020010147 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010148 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +020010149
10150#ifdef CONFIG_NO_HZ_COMMON
10151 /*
10152 * If this CPU has been elected to perform the nohz idle
10153 * balance. Other idle CPUs have already rebalanced with
10154 * nohz_idle_balance() and nohz.next_balance has been
10155 * updated accordingly. This CPU is now running the idle load
10156 * balance for itself and we need to update the
10157 * nohz.next_balance accordingly.
10158 */
10159 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
10160 nohz.next_balance = rq->next_balance;
10161#endif
10162 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010163}
10164
Frederic Weisbecker3451d022011-08-10 23:21:01 +020010165#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010166/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +020010167 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010168 * rebalancing for all the cpus for whom scheduler ticks are stopped.
10169 */
Daniel Lezcano208cb162014-01-06 12:34:44 +010010170static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010171{
Daniel Lezcano208cb162014-01-06 12:34:44 +010010172 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010173 struct rq *rq;
10174 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +020010175 /* Earliest time when we have to do rebalance again */
10176 unsigned long next_balance = jiffies + 60*HZ;
10177 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010178
Suresh Siddha1c792db2011-12-01 17:07:32 -080010179 if (idle != CPU_IDLE ||
10180 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
10181 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010182
10183 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -080010184 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010185 continue;
10186
10187 /*
10188 * If this cpu gets work to do, stop the load balancing
10189 * work being done for other cpus. Next load
10190 * balancing owner will pick it up.
10191 */
Suresh Siddha1c792db2011-12-01 17:07:32 -080010192 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010193 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010194
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +020010195 rq = cpu_rq(balance_cpu);
10196
Tim Chened61bbc2014-05-20 14:39:27 -070010197 /*
10198 * If time for next balance is due,
10199 * do the balance.
10200 */
10201 if (time_after_eq(jiffies, rq->next_balance)) {
10202 raw_spin_lock_irq(&rq->lock);
10203 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +020010204 cpu_load_update_idle(rq);
Tim Chened61bbc2014-05-20 14:39:27 -070010205 raw_spin_unlock_irq(&rq->lock);
10206 rebalance_domains(rq, CPU_IDLE);
10207 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010208
Vincent Guittotc5afb6a2015-08-03 11:55:50 +020010209 if (time_after(next_balance, rq->next_balance)) {
10210 next_balance = rq->next_balance;
10211 update_next_balance = 1;
10212 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010213 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +020010214
10215 /*
10216 * next_balance will be updated only when there is a need.
10217 * When the CPU is attached to null domain for ex, it will not be
10218 * updated.
10219 */
10220 if (likely(update_next_balance))
10221 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -080010222end:
10223 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010224}
10225
10226/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010227 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010228 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010229 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010230 * - This rq has at least one CFS task and the capacity of the CPU is
10231 * significantly reduced because of RT tasks or IRQs.
10232 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
10233 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010234 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
10235 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010236 */
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010237static inline bool nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010238{
10239 unsigned long now = jiffies;
Peter Zijlstra0e369d72016-05-09 10:38:01 +020010240 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010241 struct sched_domain *sd;
Daniel Lezcano4a725622014-01-06 12:34:39 +010010242 int nr_busy, cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010243 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010244
Daniel Lezcano4a725622014-01-06 12:34:39 +010010245 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010246 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010247
Suresh Siddha1c792db2011-12-01 17:07:32 -080010248 /*
10249 * We may be recently in ticked or tickless idle mode. At the first
10250 * busy tick after returning from idle, we will update the busy stats.
10251 */
Suresh Siddha69e1e812011-12-01 17:07:33 -080010252 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +080010253 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -080010254
10255 /*
10256 * None are in tickless mode and hence no need for NOHZ idle load
10257 * balancing.
10258 */
10259 if (likely(!atomic_read(&nohz.nr_cpus)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010260 return false;
Suresh Siddha1c792db2011-12-01 17:07:32 -080010261
10262 if (time_before(now, nohz.next_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010263 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010264
Morten Rasmussenf69e2dc2015-02-03 13:54:11 +000010265 if (rq->nr_running >= 2 &&
10266 (!energy_aware() || cpu_overutilized(cpu)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010267 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010268
Leo Yan0db9eac2017-03-27 15:00:14 +010010269 /* Do idle load balance if there have misfit task */
Joonwoo Parkcd76b212017-03-07 18:08:24 -080010270 if (energy_aware())
10271 return rq->misfit_task;
Leo Yan0db9eac2017-03-27 15:00:14 +010010272
Peter Zijlstra067491b2011-12-07 14:32:08 +010010273 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +020010274 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
Joonwoo Parkcd76b212017-03-07 18:08:24 -080010275 if (sds) {
Peter Zijlstra0e369d72016-05-09 10:38:01 +020010276 /*
10277 * XXX: write a coherent comment on why we do this.
10278 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
10279 */
10280 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010281 if (nr_busy > 1) {
10282 kick = true;
10283 goto unlock;
10284 }
10285
10286 }
10287
10288 sd = rcu_dereference(rq->sd);
10289 if (sd) {
10290 if ((rq->cfs.h_nr_running >= 1) &&
10291 check_cpu_capacity(rq, sd)) {
10292 kick = true;
10293 goto unlock;
10294 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010295 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +053010296
10297 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Preeti U Murthy37dc6b52013-10-30 08:42:52 +053010298 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010299 sched_domain_span(sd)) < cpu)) {
10300 kick = true;
10301 goto unlock;
10302 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +053010303
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010304unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +010010305 rcu_read_unlock();
Vincent Guittot1aaf90a2015-02-27 16:54:14 +010010306 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010307}
10308#else
Daniel Lezcano208cb162014-01-06 12:34:44 +010010309static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010310#endif
10311
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010312/*
10313 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010314 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010315 */
Emese Revfy0766f782016-06-20 20:42:34 +020010316static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010317{
Daniel Lezcano208cb162014-01-06 12:34:44 +010010318 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -070010319 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010320 CPU_IDLE : CPU_NOT_IDLE;
10321
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010322 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010323 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010324 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +053010325 * stopped. Do nohz_idle_balance *before* rebalance_domains to
10326 * give the idle cpus a chance to load balance. Else we may
10327 * load balance only within the local sched_domain hierarchy
10328 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010329 */
Daniel Lezcano208cb162014-01-06 12:34:44 +010010330 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +053010331 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010332}
10333
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010334/*
10335 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010336 */
Daniel Lezcano7caff662014-01-06 12:34:38 +010010337void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010338{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010339 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +010010340 if (unlikely(on_null_domain(rq)))
10341 return;
10342
10343 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010344 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +020010345#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +010010346 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +010010347 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -070010348#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +010010349}
10350
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010010351static void rq_online_fair(struct rq *rq)
10352{
10353 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +040010354
10355 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010010356}
10357
10358static void rq_offline_fair(struct rq *rq)
10359{
10360 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -070010361
10362 /* Ensure any throttled groups are reachable by pick_next_task */
10363 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010010364}
10365
Joonwoo Park4f0693a2017-05-18 17:43:58 -070010366static inline int
10367kick_active_balance(struct rq *rq, struct task_struct *p, int new_cpu)
10368{
10369 int rc = 0;
10370
10371 /* Invoke active balance to force migrate currently running task */
10372 raw_spin_lock(&rq->lock);
10373 if (!rq->active_balance) {
10374 rq->active_balance = 1;
10375 rq->push_cpu = new_cpu;
10376 get_task_struct(p);
10377 rq->push_task = p;
10378 rc = 1;
10379 }
10380 raw_spin_unlock(&rq->lock);
10381
10382 return rc;
10383}
10384
10385void check_for_migration(struct rq *rq, struct task_struct *p)
10386{
10387 int new_cpu;
10388 int active_balance;
10389 int cpu = task_cpu(p);
10390
Ke Wang7be19852018-01-11 11:16:32 +080010391 if (energy_aware() && rq->misfit_task) {
Joonwoo Park4f0693a2017-05-18 17:43:58 -070010392 if (rq->curr->state != TASK_RUNNING ||
10393 rq->curr->nr_cpus_allowed == 1)
10394 return;
10395
10396 new_cpu = select_energy_cpu_brute(p, cpu, 0);
10397 if (capacity_orig_of(new_cpu) > capacity_orig_of(cpu)) {
10398 active_balance = kick_active_balance(rq, p, new_cpu);
10399 if (active_balance)
10400 stop_one_cpu_nowait(cpu,
10401 active_load_balance_cpu_stop,
10402 rq, &rq->active_balance_work);
10403 }
10404 }
10405}
10406
Dhaval Giani55e12e52008-06-24 23:39:43 +053010407#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +020010408
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010409/*
10410 * scheduler tick hitting a task of our scheduling class:
10411 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010010412static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010413{
10414 struct cfs_rq *cfs_rq;
10415 struct sched_entity *se = &curr->se;
10416
10417 for_each_sched_entity(se) {
10418 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010010419 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010420 }
Ben Segall18bf2802012-10-04 12:51:20 +020010421
Srikar Dronamrajub52da862015-10-02 07:48:25 +053010422 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +020010423 task_tick_numa(rq, curr);
Morten Rasmussena562dfc2015-05-09 16:49:57 +010010424
Patrick Bellasi2178e842016-07-22 11:35:59 +010010425#ifdef CONFIG_SMP
Patrick Bellasi8e45d942016-02-10 09:24:36 +000010426 if (!rq->rd->overutilized && cpu_overutilized(task_cpu(curr))) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +010010427 rq->rd->overutilized = true;
Patrick Bellasi8e45d942016-02-10 09:24:36 +000010428 trace_sched_overutilized(true);
10429 }
Morten Rasmussen4c6a8242016-02-25 12:47:54 +000010430
10431 rq->misfit_task = !task_fits_max(curr, rq->cpu);
Patrick Bellasi2178e842016-07-22 11:35:59 +010010432#endif
10433
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010434}
10435
10436/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010437 * called on fork with the child task as argument from the parent's context
10438 * - child not yet on the tasklist
10439 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010440 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010441static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010442{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +090010443 struct cfs_rq *cfs_rq;
10444 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010445 struct rq *rq = this_rq();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010446
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010447 raw_spin_lock(&rq->lock);
Peter Zijlstra861d0342010-08-19 13:31:43 +020010448 update_rq_clock(rq);
10449
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +090010450 cfs_rq = task_cfs_rq(current);
10451 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010452 if (curr) {
10453 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +020010454 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010455 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +020010456 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010457
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010458 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +020010459 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +020010460 * Upon rescheduling, sched_class::put_prev_task() will place
10461 * 'current' within the tree based on its new key value.
10462 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010463 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +040010464 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010465 }
10466
Peter Zijlstra88ec22d2009-12-16 18:04:41 +010010467 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010468 raw_spin_unlock(&rq->lock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010469}
10470
Steven Rostedtcb469842008-01-25 21:08:22 +010010471/*
10472 * Priority of the task has changed. Check to see if we preempt
10473 * the current task.
10474 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010475static void
10476prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +010010477{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040010478 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010479 return;
10480
Steven Rostedtcb469842008-01-25 21:08:22 +010010481 /*
10482 * Reschedule if we are currently running on this runqueue and
10483 * our priority decreased, or if we are not currently running on
10484 * this runqueue and our priority is higher than the current's
10485 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010486 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +010010487 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +040010488 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +010010489 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +020010490 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +010010491}
10492
Byungchul Parkdaa59402015-08-20 20:22:00 +090010493static inline bool vruntime_normalized(struct task_struct *p)
10494{
10495 struct sched_entity *se = &p->se;
10496
10497 /*
10498 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
10499 * the dequeue_entity(.flags=0) will already have normalized the
10500 * vruntime.
10501 */
10502 if (p->on_rq)
10503 return true;
10504
10505 /*
10506 * When !on_rq, vruntime of the task has usually NOT been normalized.
10507 * But there are some cases where it has already been normalized:
10508 *
10509 * - A forked child which is waiting for being woken up by
10510 * wake_up_new_task().
10511 * - A task which has been woken up by try_to_wake_up() and
10512 * waiting for actually being woken up by sched_ttwu_pending().
10513 */
10514 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
10515 return true;
10516
10517 return false;
10518}
10519
Vincent Guittot96956e22016-11-08 10:53:44 +010010520#ifdef CONFIG_FAIR_GROUP_SCHED
10521/*
10522 * Propagate the changes of the sched_entity across the tg tree to make it
10523 * visible to the root
10524 */
10525static void propagate_entity_cfs_rq(struct sched_entity *se)
10526{
10527 struct cfs_rq *cfs_rq;
10528
10529 /* Start to propagate at parent */
10530 se = se->parent;
10531
10532 for_each_sched_entity(se) {
10533 cfs_rq = cfs_rq_of(se);
10534
10535 if (cfs_rq_throttled(cfs_rq))
10536 break;
10537
10538 update_load_avg(se, UPDATE_TG);
10539 }
10540}
10541#else
10542static void propagate_entity_cfs_rq(struct sched_entity *se) { }
10543#endif
10544
Vincent Guittot793cfff2016-11-08 10:53:42 +010010545static void detach_entity_cfs_rq(struct sched_entity *se)
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010546{
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010547 struct cfs_rq *cfs_rq = cfs_rq_of(se);
10548
Yuyang Du9d89c252015-07-15 08:04:37 +080010549 /* Catch up with the cfs_rq and remove our load when we leave */
Vincent Guittot96956e22016-11-08 10:53:44 +010010550 update_load_avg(se, 0);
Byungchul Parka05e8c52015-08-20 20:21:56 +090010551 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010552 update_tg_load_avg(cfs_rq, false);
Vincent Guittot96956e22016-11-08 10:53:44 +010010553 propagate_entity_cfs_rq(se);
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010554}
10555
Vincent Guittot793cfff2016-11-08 10:53:42 +010010556static void attach_entity_cfs_rq(struct sched_entity *se)
Steven Rostedtcb469842008-01-25 21:08:22 +010010557{
Byungchul Parkdaa59402015-08-20 20:22:00 +090010558 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Byungchul Park7855a352015-08-10 18:02:55 +090010559
10560#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +080010561 /*
10562 * Since the real-depth could have been changed (only FAIR
10563 * class maintain depth value), reset depth properly.
10564 */
10565 se->depth = se->parent ? se->parent->depth + 1 : 0;
10566#endif
Byungchul Park7855a352015-08-10 18:02:55 +090010567
Vincent Guittot793cfff2016-11-08 10:53:42 +010010568 /* Synchronize entity with its cfs_rq */
Vincent Guittot96956e22016-11-08 10:53:44 +010010569 update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD);
Byungchul Parkdaa59402015-08-20 20:22:00 +090010570 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010571 update_tg_load_avg(cfs_rq, false);
Vincent Guittot96956e22016-11-08 10:53:44 +010010572 propagate_entity_cfs_rq(se);
Vincent Guittot793cfff2016-11-08 10:53:42 +010010573}
10574
10575static void detach_task_cfs_rq(struct task_struct *p)
10576{
10577 struct sched_entity *se = &p->se;
10578 struct cfs_rq *cfs_rq = cfs_rq_of(se);
10579
10580 if (!vruntime_normalized(p)) {
10581 /*
10582 * Fix up our vruntime so that the current sleep doesn't
10583 * cause 'unlimited' sleep bonus.
10584 */
10585 place_entity(cfs_rq, se, 0);
10586 se->vruntime -= cfs_rq->min_vruntime;
10587 }
10588
10589 detach_entity_cfs_rq(se);
10590}
10591
10592static void attach_task_cfs_rq(struct task_struct *p)
10593{
10594 struct sched_entity *se = &p->se;
10595 struct cfs_rq *cfs_rq = cfs_rq_of(se);
10596
10597 attach_entity_cfs_rq(se);
Byungchul Park6efdb102015-08-20 20:21:59 +090010598
Byungchul Parkdaa59402015-08-20 20:22:00 +090010599 if (!vruntime_normalized(p))
10600 se->vruntime += cfs_rq->min_vruntime;
10601}
Byungchul Park7855a352015-08-10 18:02:55 +090010602
Byungchul Parkdaa59402015-08-20 20:22:00 +090010603static void switched_from_fair(struct rq *rq, struct task_struct *p)
10604{
10605 detach_task_cfs_rq(p);
10606}
10607
10608static void switched_to_fair(struct rq *rq, struct task_struct *p)
10609{
10610 attach_task_cfs_rq(p);
10611
10612 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +090010613 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +090010614 * We were most likely switched from sched_rt, so
10615 * kick off the schedule if running, otherwise just see
10616 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +090010617 */
Byungchul Parkdaa59402015-08-20 20:22:00 +090010618 if (rq->curr == p)
10619 resched_curr(rq);
10620 else
10621 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +090010622 }
Steven Rostedtcb469842008-01-25 21:08:22 +010010623}
10624
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010625/* Account for a task changing its policy or group.
10626 *
10627 * This routine is mostly called to set cfs_rq->curr field when a task
10628 * migrates between groups/classes.
10629 */
10630static void set_curr_task_fair(struct rq *rq)
10631{
10632 struct sched_entity *se = &rq->curr->se;
10633
Paul Turnerec12cb72011-07-21 09:43:30 -070010634 for_each_sched_entity(se) {
10635 struct cfs_rq *cfs_rq = cfs_rq_of(se);
10636
10637 set_next_entity(cfs_rq, se);
10638 /* ensure bandwidth has been allocated on our new cfs_rq */
10639 account_cfs_rq_runtime(cfs_rq, 0);
10640 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010641}
10642
Peter Zijlstra029632f2011-10-25 10:00:11 +020010643void init_cfs_rq(struct cfs_rq *cfs_rq)
10644{
10645 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010646 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
10647#ifndef CONFIG_64BIT
10648 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
10649#endif
Alex Shi141965c2013-06-26 13:05:39 +080010650#ifdef CONFIG_SMP
Vincent Guittot96956e22016-11-08 10:53:44 +010010651#ifdef CONFIG_FAIR_GROUP_SCHED
10652 cfs_rq->propagate_avg = 0;
10653#endif
Yuyang Du9d89c252015-07-15 08:04:37 +080010654 atomic_long_set(&cfs_rq->removed_load_avg, 0);
10655 atomic_long_set(&cfs_rq->removed_util_avg, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +020010656#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +020010657}
10658
Peter Zijlstra810b3812008-02-29 15:21:01 -050010659#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +020010660static void task_set_group_fair(struct task_struct *p)
10661{
10662 struct sched_entity *se = &p->se;
10663
10664 set_task_rq(p, task_cpu(p));
10665 se->depth = se->parent ? se->parent->depth + 1 : 0;
10666}
10667
Peter Zijlstrabc54da22015-08-31 17:13:55 +020010668static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -050010669{
Byungchul Parkdaa59402015-08-20 20:22:00 +090010670 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +020010671 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +090010672
10673#ifdef CONFIG_SMP
10674 /* Tell se's cfs_rq has been changed -- migrated */
10675 p->se.avg.last_update_time = 0;
10676#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +090010677 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -050010678}
Peter Zijlstra029632f2011-10-25 10:00:11 +020010679
Vincent Guittotea86cb42016-06-17 13:38:55 +020010680static void task_change_group_fair(struct task_struct *p, int type)
10681{
10682 switch (type) {
10683 case TASK_SET_GROUP:
10684 task_set_group_fair(p);
10685 break;
10686
10687 case TASK_MOVE_GROUP:
10688 task_move_group_fair(p);
10689 break;
10690 }
10691}
10692
Peter Zijlstra029632f2011-10-25 10:00:11 +020010693void free_fair_sched_group(struct task_group *tg)
10694{
10695 int i;
10696
10697 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
10698
10699 for_each_possible_cpu(i) {
10700 if (tg->cfs_rq)
10701 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010702 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +020010703 kfree(tg->se[i]);
10704 }
10705
10706 kfree(tg->cfs_rq);
10707 kfree(tg->se);
10708}
10709
10710int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
10711{
Peter Zijlstra029632f2011-10-25 10:00:11 +020010712 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +020010713 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010714 int i;
10715
10716 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
10717 if (!tg->cfs_rq)
10718 goto err;
10719 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
10720 if (!tg->se)
10721 goto err;
10722
10723 tg->shares = NICE_0_LOAD;
10724
10725 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
10726
10727 for_each_possible_cpu(i) {
10728 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
10729 GFP_KERNEL, cpu_to_node(i));
10730 if (!cfs_rq)
10731 goto err;
10732
10733 se = kzalloc_node(sizeof(struct sched_entity),
10734 GFP_KERNEL, cpu_to_node(i));
10735 if (!se)
10736 goto err_free_rq;
10737
10738 init_cfs_rq(cfs_rq);
10739 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +080010740 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010741 }
10742
10743 return 1;
10744
10745err_free_rq:
10746 kfree(cfs_rq);
10747err:
10748 return 0;
10749}
10750
Peter Zijlstra8663e242016-06-22 14:58:02 +020010751void online_fair_sched_group(struct task_group *tg)
10752{
10753 struct sched_entity *se;
10754 struct rq *rq;
10755 int i;
10756
10757 for_each_possible_cpu(i) {
10758 rq = cpu_rq(i);
10759 se = tg->se[i];
10760
10761 raw_spin_lock_irq(&rq->lock);
Peter Zijlstra4b9300b2016-10-03 16:20:59 +020010762 update_rq_clock(rq);
Vincent Guittot46043312016-11-08 10:53:47 +010010763 attach_entity_cfs_rq(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +020010764 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +020010765 raw_spin_unlock_irq(&rq->lock);
10766 }
10767}
10768
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010769void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +020010770{
Peter Zijlstra029632f2011-10-25 10:00:11 +020010771 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010772 struct rq *rq;
10773 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010774
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010775 for_each_possible_cpu(cpu) {
10776 if (tg->se[cpu])
10777 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010778
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010779 /*
10780 * Only empty task groups can be destroyed; so we can speculatively
10781 * check on_list without danger of it being re-added.
10782 */
10783 if (!tg->cfs_rq[cpu]->on_list)
10784 continue;
10785
10786 rq = cpu_rq(cpu);
10787
10788 raw_spin_lock_irqsave(&rq->lock, flags);
10789 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
10790 raw_spin_unlock_irqrestore(&rq->lock, flags);
10791 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010792}
10793
10794void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
10795 struct sched_entity *se, int cpu,
10796 struct sched_entity *parent)
10797{
10798 struct rq *rq = cpu_rq(cpu);
10799
10800 cfs_rq->tg = tg;
10801 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010802 init_cfs_rq_runtime(cfs_rq);
10803
10804 tg->cfs_rq[cpu] = cfs_rq;
10805 tg->se[cpu] = se;
10806
10807 /* se could be NULL for root_task_group */
10808 if (!se)
10809 return;
10810
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010811 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +020010812 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010813 se->depth = 0;
10814 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +020010815 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010816 se->depth = parent->depth + 1;
10817 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010818
10819 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -070010820 /* guarantee group entities always have weight */
10821 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010822 se->parent = parent;
10823}
10824
10825static DEFINE_MUTEX(shares_mutex);
10826
10827int sched_group_set_shares(struct task_group *tg, unsigned long shares)
10828{
10829 int i;
10830 unsigned long flags;
10831
10832 /*
10833 * We can't change the weight of the root cgroup.
10834 */
10835 if (!tg->se[0])
10836 return -EINVAL;
10837
10838 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
10839
10840 mutex_lock(&shares_mutex);
10841 if (tg->shares == shares)
10842 goto done;
10843
10844 tg->shares = shares;
10845 for_each_possible_cpu(i) {
10846 struct rq *rq = cpu_rq(i);
10847 struct sched_entity *se;
10848
10849 se = tg->se[i];
10850 /* Propagate contribution to hierarchy */
10851 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +020010852
10853 /* Possible calls to update_curr() need rq clock */
10854 update_rq_clock(rq);
Vincent Guittot6960f772016-12-21 16:50:26 +010010855 for_each_sched_entity(se) {
10856 update_load_avg(se, UPDATE_TG);
10857 update_cfs_shares(se);
10858 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010859 raw_spin_unlock_irqrestore(&rq->lock, flags);
10860 }
10861
10862done:
10863 mutex_unlock(&shares_mutex);
10864 return 0;
10865}
10866#else /* CONFIG_FAIR_GROUP_SCHED */
10867
10868void free_fair_sched_group(struct task_group *tg) { }
10869
10870int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
10871{
10872 return 1;
10873}
10874
Peter Zijlstra8663e242016-06-22 14:58:02 +020010875void online_fair_sched_group(struct task_group *tg) { }
10876
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010877void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010878
10879#endif /* CONFIG_FAIR_GROUP_SCHED */
10880
Peter Zijlstra810b3812008-02-29 15:21:01 -050010881
H Hartley Sweeten6d686f42010-01-13 20:21:52 -070010882static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +000010883{
10884 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +000010885 unsigned int rr_interval = 0;
10886
10887 /*
10888 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
10889 * idle runqueue:
10890 */
Peter Williams0d721ce2009-09-21 01:31:53 +000010891 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +080010892 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +000010893
10894 return rr_interval;
10895}
10896
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010897/*
10898 * All the scheduling class methods:
10899 */
Peter Zijlstra029632f2011-10-25 10:00:11 +020010900const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +020010901 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010902 .enqueue_task = enqueue_task_fair,
10903 .dequeue_task = dequeue_task_fair,
10904 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -050010905 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010906
Ingo Molnar2e09bf52007-10-15 17:00:05 +020010907 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010908
10909 .pick_next_task = pick_next_task_fair,
10910 .put_prev_task = put_prev_task_fair,
10911
Peter Williams681f3e62007-10-24 18:23:51 +020010912#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +080010913 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +020010914 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +080010915
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010010916 .rq_online = rq_online_fair,
10917 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +010010918
Yuyang Du12695572015-07-15 08:04:40 +080010919 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +020010920 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +020010921#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010922
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010923 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010924 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010925 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +010010926
10927 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010928 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +010010929 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -050010930
Peter Williams0d721ce2009-09-21 01:31:53 +000010931 .get_rr_interval = get_rr_interval_fair,
10932
Stanislaw Gruszka6e998912014-11-12 16:58:44 +010010933 .update_curr = update_curr_fair,
10934
Peter Zijlstra810b3812008-02-29 15:21:01 -050010935#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +020010936 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -050010937#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010938};
10939
10940#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +020010941void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010942{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010943 struct cfs_rq *cfs_rq;
10944
Peter Zijlstra5973e5b2008-01-25 21:08:34 +010010945 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +020010946 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +020010947 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +010010948 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010949}
Srikar Dronamraju397f2372015-06-25 22:51:43 +053010950
10951#ifdef CONFIG_NUMA_BALANCING
10952void show_numa_stats(struct task_struct *p, struct seq_file *m)
10953{
10954 int node;
10955 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
10956
10957 for_each_online_node(node) {
10958 if (p->numa_faults) {
10959 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
10960 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
10961 }
10962 if (p->numa_group) {
10963 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
10964 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
10965 }
10966 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
10967 }
10968}
10969#endif /* CONFIG_NUMA_BALANCING */
10970#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +020010971
10972__init void init_sched_fair_class(void)
10973{
10974#ifdef CONFIG_SMP
10975 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
10976
Frederic Weisbecker3451d022011-08-10 23:21:01 +020010977#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -080010978 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010979 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010980#endif
10981#endif /* SMP */
10982
10983}